-
Notifications
You must be signed in to change notification settings - Fork 27
/
main_test.go
236 lines (222 loc) · 8.47 KB
/
main_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
package main
import (
"regexp"
"strings"
"testing"
)
var lineTests = []struct {
currentResource string
line string
expectedResult string
minorVersion string
}{
// tf 0.11 ------------------------------------
{"random_id.some_id", "not_secret", "not_secret", "0.11"},
{"random_id.some_id", " stage.0.action.0.configuration.OAuthToken: <tf.0.11> => <tf.0.11> ",
" stage.0.action.0.configuration.OAuthToken: <*******> => <*******> ", "0.11"},
{"random_id.some_id", " stage.0.action.0.configuration.OAuthToken: \"tf.0.11\" => \"tf.0.11\" ",
" stage.0.action.0.configuration.OAuthToken: \"*******\" => \"*******\" ", "0.11"},
{"random_id.some_id", " stage.0.action.0.configuration.DontObfuscate: <tf.0.11> => <tf.0.11> ",
" stage.0.action.0.configuration.DontObfuscate: <*******> => <*******> ", "0.11"},
{"random_id.some_id", "random_id.some_id: Refreshing state... (ID: itILf4x5lqleQV9ZwT2gH-Zg3yuXM8pdUu6VFTX...P5vqUmggDweOoxFMPY5t9thA0SJE2EZIhcHbsQ)",
"random_id.some_id: Refreshing state... (ID: ********************************************************************************)",
"0.11"},
{"random_string.some_password", "random_string.some_password: Refreshing state... (ID: 2iB@@h22@12kA2qE)",
"random_string.some_password: Refreshing state... (ID: ****************)",
"0.11"},
{"random_id.some_id", " id: \"VIxvs2TloohI2XtAsHyu68wQvFQQCTOGgsglqC7zKjsnOmUMIMrZ1y5J6ieOIzl-YXiS1_XmVc8J8gb9fIcwIA\" => <computed> (forces new resource)",
" id: \"**************************************************************************************\" => <computed> (forces new resource)",
"0.11"},
// tf 0.12 ------------------------------------
{"random_id.some_id", "not_secret", "not_secret", "0.12"},
{"random_id.some_id", " ~ result = \"pkwemfpwmfwf\" -> (known after apply) ",
" ~ result = \"************\" -> (known after apply) ", "0.12"},
{"random_id.some_id", "random_id.some_id: Refreshing state... [id=itILf4x5lqleQV9ZwT2gH-Zg3yuXM8pdUu6VFTX...P5vqUmggDweOoxFMPY5t9thA0SJE2EZIhcHbsQ]",
"random_id.some_id: Refreshing state... [id=********************************************************************************]",
"0.12"},
{"", "random_id.some_id: Creation complete after 0s [id=YfK9aF]",
"random_id.some_id: Creation complete after 0s [id=******]",
"0.12"},
{"random_string.some_password", "random_string.some_password: Refreshing state... [id=2iB@@h22@12kA2qE]",
"random_string.some_password: Refreshing state... [id=****************]",
"0.12"},
{"random_id.some_id", " ~ id = \"VIxvs2TloohI2XtAsHyu68wQvFQQCTOGgsglqC7zKjsnOmUMIMrZ1y5J6ieOIzl-YXiS1_XmVc8J8gb9fIcwIA\" -> (known after apply)",
" ~ id = \"**************************************************************************************\" -> (known after apply)",
"0.12"},
{"", "random_string.some_password: Creation complete after 0s [id=5s80SMs@JJpA8e/h]",
"random_string.some_password: Creation complete after 0s [id=****************]",
"0.12"},
}
func TestProcessLine(t *testing.T) {
for _, lineTest := range lineTests {
line := lineTest.line
// Character used to mask sensitive output
var tfmaskChar = "*"
// Pattern representing sensitive output
var tfmaskValuesRegex = "(?i)^.*(oauth|secret|token|password|key|result|id).*$"
// Pattern representing sensitive resource
var tfmaskResourceRegex = "(?i)^(random_id|random_string).*$"
versionedExpressions := versionedExpressions[lineTest.minorVersion]
currentResource := lineTest.currentResource
reTfValues := regexp.MustCompile(tfmaskValuesRegex)
reTfResource := regexp.MustCompile(tfmaskResourceRegex)
result := processLine(versionedExpressions,
reTfResource, reTfValues, tfmaskChar, currentResource, line)
result = strings.TrimSuffix(result, "\n")
expectedResult := lineTest.expectedResult
if result != expectedResult {
t.Errorf("Got %s, want %s", result, expectedResult)
}
}
}
var currentResourceTests = []struct {
currentResource string
line string
expectedResult string
}{
{"", "-/+ random_string.postgres_admin_password (tainted) (new resource required)",
"random_string.postgres_admin_password",
},
// existing currentResource should persist:
{
"random_string.postgres_admin_password",
" id: \"VIxvs2TloohI2XtAsHyu68wQvFQQCTOGgsglqC7zKjsnOmUMIMrZ1y5J6ieOIzl-YXiS1_XmVc8J8gb9fIcwIA\" => <computed> (forces new resource)",
"random_string.postgres_admin_password",
},
}
func TestGetCurrentResource(t *testing.T) {
for _, currentResourceTest := range currentResourceTests {
result := getCurrentResource(versionedExpressions["0.11"], currentResourceTest.currentResource,
currentResourceTest.line)
expectedResult := currentResourceTest.expectedResult
if result != expectedResult {
t.Errorf("Got %s, want %s", result, expectedResult)
}
}
}
var planStatusTests = []struct {
line string
expectedResult string
minorVersion string
}{
// tf 0.11 ------------------------------------
{
"random_id.some_id: Refreshing state... (ID: itILf4x5lqleQV9ZwT2gH-Zg3yuXM8pdUu6VFTX...P5vqUmggDweOoxFMPY5t9thA0SJE2EZIhcHbsQ)",
"random_id.some_id: Refreshing state... (ID: ********************************************************************************)",
"0.11",
},
// the id value isn't sensitive with random_string.some_password:
{
"random_string.some_password: Refreshing state... (ID: 2iB@@h22@12kA2qE)",
"random_string.some_password: Refreshing state... (ID: 2iB@@h22@12kA2qE)",
"0.11",
},
// tf 0.12 ------------------------------------
{
"random_id.some_id: Refreshing state... [id=itILf4x5lqleQV9ZwT2gH-Zg3yuXM8pdUu6VFTX...P5vqUmggDweOoxFMPY5t9thA0SJE2EZIhcHbsQ]",
"random_id.some_id: Refreshing state... [id=********************************************************************************]",
"0.12",
},
// the id value isn't sensitive with random_string.some_password:
{
"random_string.some_password: Refreshing state... [id=2iB@@h22@12kA2qE]",
"random_string.some_password: Refreshing state... [id=2iB@@h22@12kA2qE]",
"0.12",
},
}
func TestPlanStatus(t *testing.T) {
var tfmaskResourceRegex = regexp.MustCompile("(?i)^(random_id).*$")
for _, planStatusTest := range planStatusTests {
result := planStatus(
versionedExpressions[planStatusTest.minorVersion].planStatusRegex,
tfmaskResourceRegex, "*",
planStatusTest.line)
if result != planStatusTest.expectedResult {
t.Errorf("Got %s, want %s", result, planStatusTest.expectedResult)
}
}
}
var maskValueTests = []struct {
value string
tfmaskChar string
expectedResult string
}{
{"password", "*", "********"},
{"password", "@", "@@@@@@@@"},
{"sensitive", "*", "sensitive"},
{"computed", "*", "computed"},
{"<computed", "*", "<computed"},
{"known after apply", "*", "known after apply"},
}
func TestMaskValue(t *testing.T) {
for _, maskValueTest := range maskValueTests {
result := maskValue(maskValueTest.value, maskValueTest.tfmaskChar)
if result != maskValueTest.expectedResult {
t.Errorf("Got %s, want %s", result, maskValueTest.expectedResult)
}
}
}
var assignmentTests = []struct {
line string
expectedResult string
minorVersion string
}{
// tf 0.11 ------------------------------------
{
" + client_secret: \"123456\"",
" + client_secret: \"******\"",
"0.11",
},
{
" + client_secret = \"123456\"",
" + client_secret = \"******\"",
"0.11",
},
// tf 0.12 ------------------------------------
{
" + \"foo_secret\" = \"123456\"",
" + \"foo_secret\" = \"******\"",
"0.12",
},
{
" + foo_secret = \"123456\"",
" + foo_secret = \"******\"",
"0.12",
},
{
" - \"foo_secret\" = \"123456\"",
" - \"foo_secret\" = \"******\"",
"0.12",
},
{
" ~ \"foo_secret\" = \"123456\"",
" ~ \"foo_secret\" = \"******\"",
"0.12",
},
{
" ~ \"foo\" = \"123456\"",
" ~ \"foo\" = \"123456\"",
"0.12",
},
{
" \"foo_secret\" = \"123456\"",
" \"foo_secret\" = \"******\"",
"0.12",
},
}
func TestAssignmentLine(t *testing.T) {
// Character used to mask sensitive output
var tfmaskChar = "*"
// Pattern representing sensitive output
var tfmaskValuesRegex = "(?i)^.*[^a-zA-Z](oauth|secret|token|password|key|result|id).*$"
reTfValues := regexp.MustCompile(tfmaskValuesRegex)
for _, assignmentTest := range assignmentTests {
result := assignmentLine(
versionedExpressions[assignmentTest.minorVersion].reMapKeyPair,
reTfValues, tfmaskChar,
assignmentTest.line)
if result != assignmentTest.expectedResult {
t.Errorf("Got %s, want %s", result, assignmentTest.expectedResult)
}
}
}