-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgosecretive_test.go
342 lines (312 loc) · 9.94 KB
/
gosecretive_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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package gosecretive
import (
"encoding/json"
"reflect"
"testing"
)
type SomeType string
const SomeTypeName SomeType = "SomeTypeName"
type A struct {
String string
Int int
MapStrToInterface map[string]interface{}
}
type B struct {
A
StringOther string
TypeString SomeType
}
type C struct {
String string
MapStrToStr map[string]string
Interface interface{}
OtherStruct A
OtherStructPtr *A
}
type D struct {
List []string
InitializedListEmpty []string
UnInitializedList []interface{}
InitializedMapEmpty map[string]string
UnInitializedMap map[string]string
InitializedStructEmpty A
InitializedStructPtrEmpty *A
UnInitializedStructPtr *A
UnInitializedStruct A
}
var TestCases = []struct {
name string
Raw interface{}
Scrubbed interface{}
Secretes map[string]string
ScrubFuncHandler OnValueFuncHandler
}{
{
name: "Struct",
Raw: &A{String: "hideMe", Int: 1, MapStrToInterface: map[string]interface{}{"password": "123456"}},
Scrubbed: &A{
String: "scrubbed/String",
Int: 1,
MapStrToInterface: map[string]interface{}{"password": "scrubbed/MapStrToInterface/password"},
},
Secretes: map[string]string{
"scrubbed/String": "hideMe",
"scrubbed/MapStrToInterface/password": "123456",
},
ScrubFuncHandler: scrubbingFunction([]string{
"/String",
"/MapStrToInterface/password",
}),
},
{
name: "Struct with embedded struct",
Raw: &B{
A: A{
String: "Expose Me", Int: 1, MapStrToInterface: map[string]interface{}{"name": "B/A"},
},
StringOther: "hideMe",
TypeString: SomeTypeName,
},
Scrubbed: &B{
A: A{
String: "Expose Me", Int: 1, MapStrToInterface: map[string]interface{}{
"name": "scrubbed/A/MapStrToInterface/name",
},
},
StringOther: "scrubbed/StringOther",
TypeString: SomeTypeName,
},
Secretes: map[string]string{
"scrubbed/A/MapStrToInterface/name": "B/A",
"scrubbed/StringOther": "hideMe",
},
ScrubFuncHandler: scrubbingFunction([]string{
"/A/MapStrToInterface/name",
"/StringOther",
}),
},
{
name: "Struct containing a reference to another struct",
Raw: &C{
String: "C",
MapStrToStr: map[string]string{"content": "private!!", "name": "pemfile"},
Interface: map[string]interface{}{"content": "dontell", "name": "secret"},
OtherStruct: A{String: "A", Int: 1, MapStrToInterface: map[string]interface{}{
"name": "C/OtherStruct",
}},
OtherStructPtr: &A{String: "&A", Int: 1, MapStrToInterface: map[string]interface{}{
"name": "C/OtherStructPtr",
}},
},
Scrubbed: &C{
String: "scrubbed/String",
MapStrToStr: map[string]string{"content": "scrubbed/MapStrToStr/content", "name": "pemfile"},
Interface: map[string]interface{}{"content": "scrubbed/Interface/content", "name": "secret"},
OtherStruct: A{String: "A", Int: 1, MapStrToInterface: map[string]interface{}{
"name": "scrubbed/OtherStruct/MapStrToInterface/name",
}},
OtherStructPtr: &A{String: "&A", Int: 1, MapStrToInterface: map[string]interface{}{
"name": "scrubbed/OtherStructPtr/MapStrToInterface/name",
}},
},
Secretes: map[string]string{
"scrubbed/String": "C",
"scrubbed/MapStrToStr/content": "private!!",
"scrubbed/Interface/content": "dontell",
"scrubbed/OtherStruct/MapStrToInterface/name": "C/OtherStruct",
"scrubbed/OtherStructPtr/MapStrToInterface/name": "C/OtherStructPtr",
},
ScrubFuncHandler: scrubbingFunction([]string{
"/String",
"/MapStrToStr/content",
"/Interface/content",
"/OtherStruct/MapStrToInterface/name",
"/OtherStructPtr/MapStrToInterface/name",
}),
},
{
name: "Struct containing a list",
Raw: &D{
List: []string{"a", "b", "c"},
InitializedListEmpty: []string{},
InitializedMapEmpty: map[string]string{},
InitializedStructEmpty: A{},
InitializedStructPtrEmpty: &A{},
},
Scrubbed: &D{
List: []string{"scrubbed/List[0]", "b", "scrubbed/List[2]"},
InitializedListEmpty: []string{},
InitializedMapEmpty: map[string]string{},
InitializedStructEmpty: A{},
InitializedStructPtrEmpty: &A{},
},
Secretes: map[string]string{
"scrubbed/List[0]": "a",
"scrubbed/List[2]": "c",
},
ScrubFuncHandler: scrubbingFunction([]string{
"/List[0]",
"/List[2]",
}),
},
}
func TestRestore(t *testing.T) {
type args struct {
objectToRestore interface{}
secrets map[string]string
}
type testCase struct {
name string
args args
expectedRestoredObject interface{}
}
var testCases []testCase
for _, tc := range TestCases {
testCases = append(testCases, testCase{
name: tc.name,
args: args{
objectToRestore: tc.Scrubbed,
secrets: tc.Secretes,
},
expectedRestoredObject: tc.Raw,
})
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Restore should be idempotent, run it few times
var restoredObject interface{}
for i := 0; i < 3; i++ {
t.Log("Iteration", i)
// marshal testCase.args.objectToRestore to JSON
preRestoreMarshaledObject, err := json.Marshal(tc.args.objectToRestore)
if err != nil {
t.Fatal(err)
}
restoredObject = Restore(tc.args.objectToRestore, tc.args.secrets)
if !reflect.DeepEqual(restoredObject, tc.expectedRestoredObject) {
t.Errorf("Restore() restoredObject = %v, expectedRestoredObject %v", restoredObject, tc.expectedRestoredObject)
}
postRestoreMarshaledObject, err := json.Marshal(tc.args.objectToRestore)
if err != nil {
t.Fatal(err)
}
// verify that the original object was not modified
if string(preRestoreMarshaledObject) != string(postRestoreMarshaledObject) {
t.Errorf("Restore() should not modify the original object, preScrubMarshaledObjectToScrub = %v, postScrubMarshaledObjectToScrub %v", preRestoreMarshaledObject, postRestoreMarshaledObject)
}
// after first iteration, we want to make sure that the object to restore did not change
tc.args.objectToRestore = restoredObject
}
})
}
}
func TestScrub(t *testing.T) {
type args struct {
objectToScrub interface{}
scrubFuncHandler OnValueFuncHandler
}
type testCase struct {
name string
args args
expectedScrubbedObject interface{}
secrets map[string]string
}
var testCases []testCase
for _, tc := range TestCases {
testCases = append(testCases, testCase{
name: tc.name,
args: args{
objectToScrub: tc.Raw,
scrubFuncHandler: tc.ScrubFuncHandler,
},
expectedScrubbedObject: tc.Scrubbed,
secrets: tc.Secretes,
})
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Scrub should be idempotent, run it few times
var scrubbedObject interface{}
var secrets map[string]string
for i := 0; i < 3; i++ {
t.Log("Scrubbing iteration", i)
// marshal tc.args.objectToRestore to JSON
preScrubMarshaledObjectToScrub, err := json.Marshal(tc.args.objectToScrub)
if err != nil {
t.Fatal(err)
}
scrubbedObject, secrets = Scrub(tc.args.objectToScrub, tc.args.scrubFuncHandler)
if !reflect.DeepEqual(scrubbedObject, tc.expectedScrubbedObject) {
t.Errorf("Scrub() scrubbedObject = %v, expectedRestoredObject %v", scrubbedObject, tc.expectedScrubbedObject)
}
postScrubMarshaledObjectToScrub, err := json.Marshal(tc.args.objectToScrub)
if err != nil {
t.Fatal(err)
}
// verify that the original object was not modified
if string(preScrubMarshaledObjectToScrub) != string(postScrubMarshaledObjectToScrub) {
t.Errorf("Scrub() should not modify the original object, preScrubMarshaledObjectToScrub = %v, postScrubMarshaledObjectToScrub %v", preScrubMarshaledObjectToScrub, postScrubMarshaledObjectToScrub)
}
if !reflect.DeepEqual(secrets, tc.secrets) {
t.Errorf("Scrub() secrets = %v, expectedRestoredObject %v", secrets, tc.secrets)
}
// after first iteration, we want to make sure that the object to scrub did not change
tc.args.objectToScrub = scrubbedObject
// after first iteration, we want to make sure that the secrets generated is empty (aka nothing was scrubbed)
tc.secrets = map[string]string{}
}
})
}
}
func TestRoundTrip(t *testing.T) {
for _, tc := range TestCases {
t.Run(tc.name, func(t *testing.T) {
raw := tc.Raw
for i := 0; i < 3; i++ {
restoredObject := Restore(Scrub(raw, tc.ScrubFuncHandler))
if !reflect.DeepEqual(restoredObject, tc.Raw) {
t.Errorf("RoundTrip() restoredObject = %v, expectedRestoredObject %v", restoredObject, tc.Raw)
}
raw = restoredObject
}
})
}
}
func TestSkipUnxportedFields(t *testing.T) {
t.Run("SkipUnexportedFields", func(t *testing.T) {
type structWithUnexported struct {
Exported string
unexported string
}
s := structWithUnexported{
Exported: "exported",
unexported: "unexported",
}
scrubbedObject, _ := Scrub(s, scrubbingFunction([]string{
"/unexported",
}))
if scrubbedObject.(structWithUnexported).unexported == s.unexported || scrubbedObject.(structWithUnexported).unexported != "" {
t.Errorf("Unexported field wasn't skipped. scrubbedObject = %v, expectedRestoredObject %v", scrubbedObject, s)
}
if scrubbedObject.(structWithUnexported).Exported != s.Exported {
t.Errorf("Exported field should not be modified. scrubbedObject = %v, expectedRestoredObject %v", scrubbedObject, s)
}
})
}
func scrubbingFunction(allowedFieldPaths []string) OnValueFuncHandler {
return func(fieldPath string, valueToScrub interface{}) *string {
if stringInSlice(fieldPath, allowedFieldPaths) {
s := "scrubbed" + fieldPath
return &s
}
return nil
}
}
func stringInSlice(s string, ss []string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}