Skip to content

Commit a9480c5

Browse files
authored
feat: refactor KsDecode to handle stringData and data nodes (#543)
* feat: refactor KsDecode to handle stringData and data nodes Signed-off-by: yxxhero <[email protected]> * feat(secret): remove sensitive data from secret file Signed-off-by: yxxhero <[email protected]> --------- Signed-off-by: yxxhero <[email protected]>
1 parent 9f44c62 commit a9480c5

File tree

5 files changed

+67
-16
lines changed

5 files changed

+67
-16
lines changed

cmd/vals/main.go

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ the vals-eval outputs onto the disk, for security reasons.`)
227227
if *export {
228228
l = "export " + l
229229
}
230-
fmt.Fprintln(os.Stdout, l)
230+
_, _ = fmt.Fprintln(os.Stdout, l)
231231
}
232232
case CmdKsDecode:
233233
evalCmd := flag.NewFlagSet(CmdKsDecode, flag.ExitOnError)
@@ -269,9 +269,15 @@ func KsDecode(node yaml.Node) (*yaml.Node, error) {
269269

270270
var res yaml.Node = node
271271

272-
var kk yaml.Node
273-
var vv yaml.Node
274-
var ii int
272+
// record the original data node
273+
var datakk yaml.Node
274+
var datavv yaml.Node
275+
var dataii int
276+
277+
// record the original stringData node
278+
var stringDatakk yaml.Node
279+
var stringDatavv yaml.Node
280+
var stringDataii int
275281

276282
isSecret := false
277283
mappings := node.Content[0].Content
@@ -285,16 +291,32 @@ func KsDecode(node yaml.Node) (*yaml.Node, error) {
285291
}
286292

287293
if k.Value == "data" {
288-
ii = i
289-
kk = *k
290-
vv = *v
294+
dataii = i
295+
datakk = *k
296+
datavv = *v
297+
}
298+
if k.Value == "stringData" {
299+
stringDataii = i
300+
stringDatakk = *k
301+
stringDatavv = *v
291302
}
292303
}
293304

294-
if isSecret && !kk.IsZero() {
295-
kk.Value = "stringData"
305+
// if not a secret, just return the node
306+
if !isSecret {
307+
return &res, nil
308+
}
296309

297-
v := vv
310+
// if data node not exists, just return the node
311+
if datakk.IsZero() {
312+
return &res, nil
313+
}
314+
315+
// stringData node not exists
316+
if stringDatakk.IsZero() {
317+
datakk.Value = "stringData"
318+
319+
v := datavv
298320
nestedMappings := v.Content
299321
v.Content = make([]*yaml.Node, len(v.Content))
300322
for i := 0; i < len(nestedMappings); i += 2 {
@@ -309,10 +331,35 @@ func KsDecode(node yaml.Node) (*yaml.Node, error) {
309331
v.Content[i+1] = nestedMappings[i+1]
310332
}
311333

312-
res.Content[0].Content[ii] = &kk
313-
res.Content[0].Content[ii+1] = &v
334+
res.Content[0].Content[dataii] = &datakk
335+
res.Content[0].Content[dataii+1] = &v
336+
return &res, nil
337+
}
338+
339+
// stringData and data node exist in the mean time
340+
dv := datavv
341+
sv := stringDatavv
342+
dNestedMappings := dv.Content
343+
for i := 0; i < len(dNestedMappings); i += 2 {
344+
b64 := dNestedMappings[i+1].Value
345+
decoded, err := base64.StdEncoding.DecodeString(b64)
346+
if err != nil {
347+
return nil, err
348+
}
349+
// replace the value of the nested mapping
350+
dNestedMappings[i+1].Value = string(decoded)
351+
352+
sv.Content = append(sv.Content, dNestedMappings[i])
353+
sv.Content = append(sv.Content, dNestedMappings[i+1])
314354
}
315355

356+
// replace the stringData node
357+
res.Content[0].Content[stringDataii] = &stringDatakk
358+
res.Content[0].Content[stringDataii+1] = &sv
359+
360+
// remove the data node
361+
res.Content[0].Content = append(res.Content[0].Content[:dataii], res.Content[0].Content[dataii+2:]...)
362+
316363
return &res, nil
317364
}
318365

cmd/vals/main_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import (
1010
func TestKsDecode(t *testing.T) {
1111
in := `data:
1212
foo: Rk9P
13+
stringData:
14+
bar: BAR
1315
kind: Secret
1416
`
1517
outExpected := `stringData:
18+
bar: BAR
1619
foo: FOO
1720
kind: Secret
1821
`

io.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func Output(output io.Writer, format string, nodes []yaml.Node) error {
8787
if err != nil {
8888
return err
8989
}
90-
fmt.Fprintln(output, string(bs))
90+
_, _ = fmt.Fprintln(output, string(bs))
9191
} else {
9292
encoder := yaml.NewEncoder(output)
9393
encoder.SetIndent(2)
@@ -97,7 +97,7 @@ func Output(output io.Writer, format string, nodes []yaml.Node) error {
9797
}
9898
}
9999
if i != len(nodes)-1 {
100-
fmt.Fprintln(output, "---")
100+
_, _ = fmt.Fprintln(output, "---")
101101
}
102102
}
103103
return nil

pkg/log/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ func New(c Config) *Logger {
2525
}
2626

2727
func (l *Logger) Debugf(msg string, args ...interface{}) {
28-
fmt.Fprintf(l.output, msg+"\n", args...)
28+
_, _ = fmt.Fprintf(l.output, msg+"\n", args...)
2929
}

pkg/providers/vault/kv_helper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package vault
22

33
import (
44
"errors"
5+
"net/http"
56
"path"
67
"strings"
78

@@ -30,7 +31,7 @@ func kvPreflightVersionRequest(client *api.Client, path string) (string, int, er
3031
if err != nil {
3132
// If we get a 404 we are using an older version of vault, default to
3233
// version 1
33-
if resp != nil && resp.StatusCode == 404 {
34+
if resp != nil && resp.StatusCode == http.StatusNotFound {
3435
return "", 1, nil
3536
}
3637

0 commit comments

Comments
 (0)