-
Notifications
You must be signed in to change notification settings - Fork 1
/
collector_test.go
163 lines (153 loc) · 3.79 KB
/
collector_test.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cloudsecrets
import (
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCollectSecretKeys(t *testing.T) {
tt := []struct {
Name string
Input any
Out []string // collected secret keys
Error bool
}{
{
Name: "DB_config_with_no_creds",
Input: &cfg{
DB: dbConfig{
User: "db-user",
Password: "db-password",
},
DBPtr: &dbConfig{
User: "db-user",
Password: "db-password",
},
DBDoublePtr: ptr(&dbConfig{
User: "db-user",
Password: "db-password",
}),
},
Out: []string{},
},
{
Name: "DB_config_with_creds",
Input: &cfg{
DB: dbConfig{
User: "db-user",
Password: "$SECRET:db-password",
},
},
Out: []string{"db-password"},
},
{
Name: "DB config ptr with creds",
Input: &cfg{
DBPtr: &dbConfig{
User: "db-user",
Password: "$SECRET:db-password",
},
},
Out: []string{"db-password"},
},
{
Name: "DB_config_double_ptr_with_creds",
Input: &cfg{
DBDoublePtr: ptr(&dbConfig{
User: "db-user",
Password: "$SECRET:db-password",
}),
},
Out: []string{"db-password"},
},
{
Name: "Slice_of_secret_values",
Input: &cfg{
DB: dbConfig{
User: "db-user",
Password: "$SECRET:secretName",
},
JWTSecrets: []jwtSecret{"$SECRET:jwtSecret1", "$SECRET:jwtSecret2", "nope"},
},
Out: []string{"jwtSecret1", "jwtSecret2", "secretName"},
},
{
Name: "Slice_of_secret_pointer_values",
Input: &cfg{
DB: dbConfig{
User: "db-user",
Password: "$SECRET:secretName",
},
JWTSecretsPtr: []*jwtSecret{ptr(jwtSecret("$SECRET:jwtSecret1")), ptr(jwtSecret("$SECRET:jwtSecret2")), ptr(jwtSecret("nope"))},
},
Out: []string{"jwtSecret1", "jwtSecret2", "secretName"},
},
{
Name: "Map_with_values",
Input: &cfg{
Providers: map[string]providerConfig{
"provider1": {Name: "provider1", Secret: "$SECRET:secretProvider1"},
"provider2": {Name: "provider2", Secret: "$SECRET:secretProvider2"},
"provider3": {Name: "provider3", Secret: "$SECRET:secretProvider3"},
},
},
Out: []string{"secretProvider1", "secretProvider2", "secretProvider3"},
},
{
Name: "Map_with_ptr_values",
Input: &cfg{
ProvidersPtr: map[string]*providerConfig{
"provider1": {Name: "provider1", Secret: "$SECRET:secretProvider1"},
"provider2": {Name: "provider2", Secret: "$SECRET:secretProvider2"},
"provider3": {Name: "provider3", Secret: "$SECRET:secretProvider3"},
},
},
Out: []string{"secretProvider1", "secretProvider2", "secretProvider3"},
},
{
Name: "Duplicated_secret",
Input: &cfg{
DB: dbConfig{
User: "db-user",
Password: "$SECRET:duplicatedKey",
},
JWTSecrets: []jwtSecret{"$SECRET:duplicatedKey", "$SECRET:duplicatedKey"},
ProvidersPtr: map[string]*providerConfig{
"provider1": {Name: "provider1", Secret: "$SECRET:duplicatedKey"},
"provider2": {Name: "provider2", Secret: "$SECRET:duplicatedKey"},
"provider3": {Name: "provider3", Secret: "$SECRET:duplicatedKey"},
},
},
Out: []string{"duplicatedKey"},
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
v := reflect.ValueOf(tc.Input)
secretFields := collectSecretKeys(v)
if !cmp.Equal(secretFields, tc.Out) {
t.Errorf(cmp.Diff(tc.Out, secretFields))
}
})
}
}
type cfg struct {
DB dbConfig
DBPtr *dbConfig
DBDoublePtr **dbConfig
JWTSecrets []jwtSecret
JWTSecretsPtr []*jwtSecret
Providers map[string]providerConfig
ProvidersPtr map[string]*providerConfig
unexported dbConfig
}
type dbConfig struct {
User string
Password string
}
type providerConfig struct {
Name string
Secret string
}
type jwtSecret string
func ptr[T any](v T) *T { return &v }