Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit b56f371

Browse files
committed
Enable migrating templates without whitespaces
Now it is no longer enforced to have template formats with whitespaces between the path and the curly brackets, such as `{{ path/to/secrets }}`
1 parent 0cb4c70 commit b56f371

3 files changed

Lines changed: 180 additions & 7 deletions

File tree

internals/secrethub/migrate_config_envfile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func (cmd *MigrateConfigEnvfileCommand) Run() error {
3939
return err
4040
}
4141

42-
replaceCount, err := migrateTemplateTags(bytes.NewBuffer(inFileContents), ".env", refMapping, "%s")
42+
replaceCount, err := migrateTemplateTags(bytes.NewBuffer(inFileContents), ".env", refMapping, "{{ %s }}")
4343
if err != nil {
4444
return err
4545
}

internals/secrethub/migrate_config_templates.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
)
1414

1515
var regexpSecretTemplatePath = regexp.MustCompile(`[A-Za-z0-9_\.\-\$\{\}]{2,}\/[A-Za-z0-9_\.\-\$\{\}]{2,}\/[A-Za-z0-9_\.\-\$\{\}\/]{2,}`)
16-
var regexpSecretTemplateTags = regexp.MustCompile(`{{(\s)*?(` + regexpSecretTemplatePath.String() + `)(\s)*?}}`)
16+
var regexpSecretTemplateTags = regexp.MustCompile(`{{\s*?(` + regexpSecretTemplatePath.String() + `)\s*?}}`)
1717

1818
func (cmd *MigrateConfigTemplatesCommand) Run() error {
1919
plan, err := getPlan(cmd.planFile)
@@ -55,16 +55,17 @@ func migrateTemplateTags(inFile io.Reader, outFile string, mapping referenceMapp
5555

5656
var hits, misses []string
5757
output := regexpSecretTemplateTags.ReplaceAllStringFunc(string(raw), func(templateTag string) string {
58-
path := regexpSecretTemplatePath.FindString(templateTag)
59-
if path == "" {
58+
path := regexpSecretTemplateTags.FindStringSubmatch(templateTag)
59+
fmt.Println(path)
60+
if path[1] == "" {
6061
misses = append(misses, templateTag)
6162
return ""
6263
}
6364

64-
opRef, ok := mapping[path]
65+
opRef, ok := mapping[path[1]]
6566
if !ok {
66-
misses = append(misses, path)
67-
return path
67+
misses = append(misses, path[1])
68+
return path[1]
6869
}
6970

7071
hits = append(hits, opRef)

internals/secrethub/migrate_config_test.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,50 @@ func TestMigrateTemplates(t *testing.T) {
3838
"secrethub://org/repo/dir/password": "op://vault/item/password",
3939
},
4040
},
41+
"json no whitespaces": {
42+
in: `
43+
{
44+
"db_host": "db.internal",
45+
"db_user": "{{org/repo/dir/user}}",
46+
"db_password": "{{org/repo/dir/password}}",
47+
"db_port": 5432
48+
}
49+
`,
50+
expected: `
51+
{
52+
"db_host": "db.internal",
53+
"db_user": "{{ op://vault/item/user }}",
54+
"db_password": "{{ op://vault/item/password }}",
55+
"db_port": 5432
56+
}
57+
`,
58+
mapping: map[string]string{
59+
"secrethub://org/repo/dir/user": "op://vault/item/user",
60+
"secrethub://org/repo/dir/password": "op://vault/item/password",
61+
},
62+
},
63+
"json one whitespaces": {
64+
in: `
65+
{
66+
"db_host": "db.internal",
67+
"db_user": "{{org/repo/dir/user }}",
68+
"db_password": "{{ org/repo/dir/password}}",
69+
"db_port": 5432
70+
}
71+
`,
72+
expected: `
73+
{
74+
"db_host": "db.internal",
75+
"db_user": "{{ op://vault/item/user }}",
76+
"db_password": "{{ op://vault/item/password }}",
77+
"db_port": 5432
78+
}
79+
`,
80+
mapping: map[string]string{
81+
"secrethub://org/repo/dir/user": "op://vault/item/user",
82+
"secrethub://org/repo/dir/password": "op://vault/item/password",
83+
},
84+
},
4185
"yaml": {
4286
in: `
4387
db_host: db.internal
@@ -91,6 +135,52 @@ func TestMigrateTemplates(t *testing.T) {
91135
"env": {"dev", "prod"},
92136
},
93137
},
138+
"with vars no whitespaces": {
139+
in: `
140+
db_host: db.internal
141+
db_user: "{{org/repo/$env/dir/user}}"
142+
db_password: {{org/repo/$env/dir/password}}
143+
db_port: 5432
144+
`,
145+
expected: `
146+
db_host: db.internal
147+
db_user: "{{ op://vault-$ENV/item/user }}"
148+
db_password: {{ op://vault-$ENV/item/password }}
149+
db_port: 5432
150+
`,
151+
mapping: map[string]string{
152+
"secrethub://org/repo/prod/dir/user": "op://vault-prod/item/user",
153+
"secrethub://org/repo/prod/dir/password": "op://vault-prod/item/password",
154+
"secrethub://org/repo/dev/dir/user": "op://vault-dev/item/user",
155+
"secrethub://org/repo/dev/dir/password": "op://vault-dev/item/password",
156+
},
157+
vars: map[string][]string{
158+
"env": {"dev", "prod"},
159+
},
160+
},
161+
"with vars one whitespaces": {
162+
in: `
163+
db_host: db.internal
164+
db_user: "{{ org/repo/$env/dir/user}}"
165+
db_password: {{org/repo/$env/dir/password }}
166+
db_port: 5432
167+
`,
168+
expected: `
169+
db_host: db.internal
170+
db_user: "{{ op://vault-$ENV/item/user }}"
171+
db_password: {{ op://vault-$ENV/item/password }}
172+
db_port: 5432
173+
`,
174+
mapping: map[string]string{
175+
"secrethub://org/repo/prod/dir/user": "op://vault-prod/item/user",
176+
"secrethub://org/repo/prod/dir/password": "op://vault-prod/item/password",
177+
"secrethub://org/repo/dev/dir/user": "op://vault-dev/item/user",
178+
"secrethub://org/repo/dev/dir/password": "op://vault-dev/item/password",
179+
},
180+
vars: map[string][]string{
181+
"env": {"dev", "prod"},
182+
},
183+
},
94184
"no op": {
95185
in: `
96186
db_user: "db-user"
@@ -149,6 +239,42 @@ func TestMigrateEnvfile(t *testing.T) {
149239
"secrethub://org/repo/dir/password": "op://vault/item/password",
150240
},
151241
},
242+
"envfile no whitespaces": {
243+
in: `
244+
DB_HOST=db.internal
245+
DB_USER={{org/repo/dir/user}}
246+
DB_PASSWORD={{org/repo/dir/password}}
247+
DB_PORT=5432
248+
`,
249+
expected: `
250+
DB_HOST=db.internal
251+
DB_USER=op://vault/item/user
252+
DB_PASSWORD=op://vault/item/password
253+
DB_PORT=5432
254+
`,
255+
mapping: map[string]string{
256+
"secrethub://org/repo/dir/user": "op://vault/item/user",
257+
"secrethub://org/repo/dir/password": "op://vault/item/password",
258+
},
259+
},
260+
"envfile one whitespace": {
261+
in: `
262+
DB_HOST=db.internal
263+
DB_USER={{org/repo/dir/user }}
264+
DB_PASSWORD={{ org/repo/dir/password}}
265+
DB_PORT=5432
266+
`,
267+
expected: `
268+
DB_HOST=db.internal
269+
DB_USER=op://vault/item/user
270+
DB_PASSWORD=op://vault/item/password
271+
DB_PORT=5432
272+
`,
273+
mapping: map[string]string{
274+
"secrethub://org/repo/dir/user": "op://vault/item/user",
275+
"secrethub://org/repo/dir/password": "op://vault/item/password",
276+
},
277+
},
152278
"with comments": {
153279
in: `
154280
# Database config
@@ -205,6 +331,52 @@ func TestMigrateEnvfile(t *testing.T) {
205331
"env": {"dev", "prod"},
206332
},
207333
},
334+
"with vars no whitespaces": {
335+
in: `
336+
DB_HOST=db.internal
337+
DB_USER={{org/repo/$env/dir/user}}
338+
DB_PASSWORD={{org/repo/$env/dir/password}}
339+
DB_PORT=5432
340+
`,
341+
expected: `
342+
DB_HOST=db.internal
343+
DB_USER=op://vault-$ENV/item/user
344+
DB_PASSWORD=op://vault-$ENV/item/password
345+
DB_PORT=5432
346+
`,
347+
mapping: map[string]string{
348+
"secrethub://org/repo/prod/dir/user": "op://vault-prod/item/user",
349+
"secrethub://org/repo/prod/dir/password": "op://vault-prod/item/password",
350+
"secrethub://org/repo/dev/dir/user": "op://vault-dev/item/user",
351+
"secrethub://org/repo/dev/dir/password": "op://vault-dev/item/password",
352+
},
353+
vars: map[string][]string{
354+
"env": {"dev", "prod"},
355+
},
356+
},
357+
"with vars one whitespace": {
358+
in: `
359+
DB_HOST=db.internal
360+
DB_USER={{org/repo/$env/dir/user }}
361+
DB_PASSWORD={{ org/repo/$env/dir/password}}
362+
DB_PORT=5432
363+
`,
364+
expected: `
365+
DB_HOST=db.internal
366+
DB_USER=op://vault-$ENV/item/user
367+
DB_PASSWORD=op://vault-$ENV/item/password
368+
DB_PORT=5432
369+
`,
370+
mapping: map[string]string{
371+
"secrethub://org/repo/prod/dir/user": "op://vault-prod/item/user",
372+
"secrethub://org/repo/prod/dir/password": "op://vault-prod/item/password",
373+
"secrethub://org/repo/dev/dir/user": "op://vault-dev/item/user",
374+
"secrethub://org/repo/dev/dir/password": "op://vault-dev/item/password",
375+
},
376+
vars: map[string][]string{
377+
"env": {"dev", "prod"},
378+
},
379+
},
208380
"composite secrets": {
209381
in: `
210382
DB_ADDRESS={{ org/repo/dir/host }}:5432

0 commit comments

Comments
 (0)