@@ -24,10 +24,19 @@ type match struct {
24
24
postfix string
25
25
}
26
26
27
+ type keyValueMatch struct {
28
+ leadingWhitespace string
29
+ property string
30
+ trailingWhitespaceBefore string
31
+ trailingWhitespaceAfter string
32
+ oldValue string
33
+ }
34
+
27
35
type expression struct {
28
36
planStatusRegex * regexp.Regexp
29
37
reTfPlanLine * regexp.Regexp
30
38
reTfPlanCurrentResource * regexp.Regexp
39
+ reMapKeyPair * regexp.Regexp
31
40
resourceIndex int
32
41
assign string
33
42
operator string
@@ -58,6 +67,9 @@ var versionedExpressions = map[string]expression{
58
67
reTfPlanCurrentResource : regexp .MustCompile (
59
68
"^([~/+-]+) (.*?) +(.*)$" ,
60
69
),
70
+ reMapKeyPair : regexp .MustCompile (
71
+ "(?i)^(\\ s+(?:[~+-] )?)\" (.*)\" (\\ s+)=(\\ s+)\" (.*)\" $" ,
72
+ ),
61
73
resourceIndex : 2 ,
62
74
assign : ":" ,
63
75
operator : "=>" ,
@@ -72,6 +84,9 @@ var versionedExpressions = map[string]expression{
72
84
reTfPlanCurrentResource : regexp .MustCompile (
73
85
"^([~/+-]+) (.*?) +(.*) (.*) (.*)$" ,
74
86
),
87
+ reMapKeyPair : regexp .MustCompile (
88
+ "(?i)^(\\ s+(?:[~+-] )?)\" (.*)\" (\\ s+)=(\\ s+)\" (.*)\" $" ,
89
+ ),
75
90
resourceIndex : 3 ,
76
91
assign : "=" ,
77
92
operator : "->" ,
@@ -90,14 +105,14 @@ func main() {
90
105
var tfmaskResourceRegex = getEnv ("TFMASK_RESOURCES_REGEX" ,
91
106
"(?i)^(random_id|random_string).*$" )
92
107
93
- // Default to tf 0.11 , but users can override
108
+ // Default to tf 0.12 , but users can override
94
109
var tfenv = getEnv ("TFENV" , "0.12" )
95
110
96
111
reTfValues := regexp .MustCompile (tfmaskValuesRegex )
97
112
reTfResource := regexp .MustCompile (tfmaskResourceRegex )
98
113
scanner := bufio .NewScanner (os .Stdin )
99
114
versionedExpressions := versionedExpressions [tfenv ]
100
- // initialise currentResource once before scanning
115
+ // initialize currentResource once before scanning
101
116
currentResource := ""
102
117
for scanner .Scan () {
103
118
line := scanner .Text ()
@@ -125,6 +140,7 @@ func getCurrentResource(expression expression, currentResource, line string) str
125
140
match := reTfApplyCurrentResource .FindStringSubmatch (line )
126
141
currentResource = match [1 ]
127
142
}
143
+
128
144
return currentResource
129
145
}
130
146
@@ -138,6 +154,9 @@ func processLine(expression expression, reTfResource,
138
154
line = planLine (expression .reTfPlanLine , reTfResource , reTfValues ,
139
155
currentResource , tfmaskChar , expression .assign ,
140
156
expression .operator , line )
157
+ } else if expression .reMapKeyPair .MatchString (line ) {
158
+ line = assignmentLine (expression .reMapKeyPair , reTfValues ,
159
+ tfmaskChar , line )
141
160
}
142
161
return line
143
162
}
@@ -170,6 +189,17 @@ func matchFromLine(reTfPlanLine *regexp.Regexp, line string) match {
170
189
}
171
190
}
172
191
192
+ func matchFromAssignment (reMapKeyPair * regexp.Regexp , line string ) keyValueMatch {
193
+ subMatch := reMapKeyPair .FindStringSubmatch (line )
194
+ return keyValueMatch {
195
+ leadingWhitespace : subMatch [1 ],
196
+ property : subMatch [2 ],
197
+ trailingWhitespaceBefore : subMatch [3 ],
198
+ trailingWhitespaceAfter : subMatch [4 ],
199
+ oldValue : subMatch [5 ],
200
+ }
201
+ }
202
+
173
203
func planLine (reTfPlanLine , reTfResource , reTfValues * regexp.Regexp ,
174
204
currentResource , tfmaskChar , assign , operator , line string ) string {
175
205
match := matchFromLine (reTfPlanLine , line )
@@ -188,6 +218,20 @@ func planLine(reTfPlanLine, reTfResource, reTfValues *regexp.Regexp,
188
218
return line
189
219
}
190
220
221
+ func assignmentLine (reMapKeyPair , reTfValues * regexp.Regexp , tfmaskChar , line string ) string {
222
+ match := matchFromAssignment (reMapKeyPair , line )
223
+ if reTfValues .MatchString (match .property ) {
224
+ maskedValue := maskValue (match .oldValue , tfmaskChar )
225
+ line = fmt .Sprintf ("%v\" %v\" %v=%v\" %v\" " ,
226
+ match .leadingWhitespace ,
227
+ match .property ,
228
+ match .trailingWhitespaceBefore ,
229
+ match .trailingWhitespaceAfter ,
230
+ maskedValue )
231
+ }
232
+ return line
233
+ }
234
+
191
235
func maskValue (value , tfmaskChar string ) string {
192
236
exclusions := []string {"sensitive" , "computed" , "<computed" ,
193
237
"known after apply" }
0 commit comments