-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollector.go
59 lines (48 loc) · 1.09 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package cloudsecrets
import (
"reflect"
"slices"
"strings"
)
// Returns de-duplicated secret keys found recursively in given v.
func collectSecretKeys(v reflect.Value) []string {
c := collector{}
c.collectSecretFields(v)
slices.Sort(c)
dedup := slices.Compact(c)
return []string(dedup)
}
type collector []string
// Walk given reflect value recursively and collects any string fields matching $SECRET: prefix.
func (c *collector) collectSecretFields(v reflect.Value) {
switch v.Kind() {
case reflect.Ptr:
if v.IsNil() {
return
}
c.collectSecretFields(v.Elem())
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
c.collectSecretFields(field)
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
item := v.Index(i)
c.collectSecretFields(item)
}
case reflect.Map:
for _, key := range v.MapKeys() {
item := v.MapIndex(key)
c.collectSecretFields(item)
}
case reflect.String:
secretName, found := strings.CutPrefix(v.String(), "$SECRET:")
if !found {
return
}
*c = append(*c, secretName)
default:
return
}
}