-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscrub_test.go
167 lines (145 loc) · 3.72 KB
/
scrub_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
package scrub
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
// Structure definitions to test scrubbing functionalities.
// Simple struct
type User struct {
Username string
Password string
DbSecrets []string
}
// Nested struct
type Users struct {
Secret string
Keys []string
UserInfo []User
}
// TestScrubSimple tests scrubbing on a simple struct with default
// sensitive fields.
func TestScrubSimple(t *testing.T) {
user := &User{
Username: "Shyam Rathi",
Password: "nutanix/4u",
DbSecrets: []string{"db_secret_1", "db_secret_2"},
}
userScrubbed := &User{
Username: "Shyam Rathi",
Password: "********",
DbSecrets: []string{"db_secret_1", "db_secret_2"},
}
validateScrub(t, user, userScrubbed, nil)
}
// TestScrubNested tests scrubbing on a nested complex struct with
// specified sensitive fields.
func TestScrubNested(t *testing.T) {
users := &Users{
Secret: "secret_sshhh",
Keys: []string{"key_1", "key_2", "key_3"},
UserInfo: []User{
{
Username: "John Doe",
Password: "John_Doe's_Password",
DbSecrets: []string{"John's_db_secret_1", "John's_db_secret_2"},
},
{
Username: "Jane Doe",
Password: "Jane_Doe's_Password",
DbSecrets: []string{"Jane's_db_secret_1", "Jane's_db_secret_2"},
},
},
}
userScrubbed := &Users{
Secret: "********",
Keys: []string{"********", "********", "********"},
UserInfo: []User{
{
Username: "John Doe",
Password: "********",
DbSecrets: []string{"********", "********"},
},
{
Username: "Jane Doe",
Password: "********",
DbSecrets: []string{"********", "********"},
},
},
}
secretFields := map[string]bool{
"password": true, "keys": true, "secret": true, "dbsecrets": true}
validateScrub(t, users, userScrubbed, secretFields)
}
// TestScrubNil tests scrubbing on a empty or nil input.
func TestScrubNil(t *testing.T) {
user := &User{
Username: "",
Password: "nutanix/4u",
DbSecrets: []string{},
}
userScrubbed := &User{
Username: "",
Password: "********",
DbSecrets: []string{},
}
// Validate input with empty fields
validateScrub(t, user, userScrubbed, nil)
// Validate empty pointer input
var userEmpty *User
validateScrub(t, userEmpty, userEmpty, nil)
// Validate nil input
validateScrub(t, nil, nil, nil)
}
// TestScrubNestedNil tests scrubbing on a nested complex struct with
// some nil, empty and specified sensitive fields.
func TestScrubNestedNil(t *testing.T) {
users := &Users{
Secret: "",
Keys: nil,
UserInfo: []User{
{
Username: "John Doe",
Password: "",
DbSecrets: []string{"John's_db_secret_1", "John's_db_secret_2"},
},
{
Username: "Jane Doe",
Password: "Jane_Doe's_Password",
DbSecrets: []string{},
},
},
}
userScrubbed := &Users{
Secret: "",
Keys: nil,
UserInfo: []User{
{
Username: "John Doe",
Password: "",
DbSecrets: []string{"********", "********"},
},
{
Username: "Jane Doe",
Password: "********",
DbSecrets: []string{},
},
},
}
// Test a nested struct with some empty and nil fields.
secretFields := map[string]bool{
"password": true, "keys": true, "secret": true, "dbsecrets": true}
validateScrub(t, users, userScrubbed, secretFields)
}
// validateScrub is a helper function to validate scrubbing functionality on a struct.
func validateScrub(t *testing.T, msg, scrubbedMsg interface{}, secretFields map[string]bool) {
t.Helper()
// Get the scrubbed string from util API.
got := Scrub(msg, secretFields)
// Compare it against the given scrubbed representaation.
var b []byte
b, _ = json.Marshal(scrubbedMsg)
want := string(b)
assert.Equal(t, want, got,
"JSON representation mismatch after scrubbing sensitive fields")
}