-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.go
252 lines (230 loc) · 7.46 KB
/
main.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"runtime"
"strings"
"unicode/utf8"
)
type match struct {
leadingWhitespace string
property string // something like `stage.0.action.0.configuration.OAuthToken`
trailingWhitespace string
firstQuote string // < or "
oldValue string
secondQuote string // > or "
thirdQuote string // < or " or (
newValue string
fourthQuote string // > or " or )
postfix string
}
type keyValueMatch struct {
leadingWhitespace string
property string
assignmentOperator string
trailingWhitespaceAfter string
oldValue string
}
type expression struct {
planStatusRegex *regexp.Regexp
reTfPlanLine *regexp.Regexp
reTfPlanCurrentResource *regexp.Regexp
reMapKeyPair *regexp.Regexp
resourceIndex int
assign string
operator string
}
func init() {
// make sure we only have one process and that it runs on the main thread
// (so that ideally, when we Exec, we keep our user switches and stuff)
runtime.GOMAXPROCS(1)
runtime.LockOSThread()
}
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
var versionedExpressions = map[string]expression{
"0.11": expression{
planStatusRegex: regexp.MustCompile(
"^(.*?): (.*?) +\\(ID: (.*?)\\)$",
),
reTfPlanLine: regexp.MustCompile(
"^( +)([a-zA-Z0-9%._-]+):( +)([\"<])(.*?)([>\"]) +=> +([\"<])(.*)([>\"])(.*)$",
),
reTfPlanCurrentResource: regexp.MustCompile(
"^([~/+-]+) (.*?) +(.*)$",
),
reMapKeyPair: regexp.MustCompile(
"(?i)^(\\s+(?:[~+-] )?)(.*)(\\s?[=:])(\\s+)\"(.*)\"$",
),
resourceIndex: 2,
assign: ":",
operator: "=>",
},
"0.12": expression{
planStatusRegex: regexp.MustCompile(
"^(.*?): (.*?) +\\[id=(.*?)\\]$",
),
reTfPlanLine: regexp.MustCompile(
"^( +)([ ~a-zA-Z0-9%._-]+)=( +)([\"<])(.*?)([>\"]) +-> +(\\()(.*)(\\))(.*)$",
),
reTfPlanCurrentResource: regexp.MustCompile(
"^([~/+-]+) (.*?) +(.*) (.*) (.*)$",
),
reMapKeyPair: regexp.MustCompile(
"(?i)^(\\s+(?:[~+-] )?)(.*)(\\s=)(\\s+)\"(.*)\"$",
),
resourceIndex: 3,
assign: "=",
operator: "->",
},
}
func main() {
log.SetFlags(0) // no timestamps on our logs
// Character used to mask sensitive output
var tfmaskChar = getEnv("TFMASK_CHAR", "*")
// Pattern representing sensitive output
var tfmaskValuesRegex = getEnv("TFMASK_VALUES_REGEX",
"(?i)^.*[^a-zA-Z](oauth|secret|token|password|key|result|id).*$")
// Pattern representing sensitive resource
var tfmaskResourceRegex = getEnv("TFMASK_RESOURCES_REGEX",
"(?i)^(random_id|random_string).*$")
// Default to tf 0.12, but users can override
var tfenv = getEnv("TFENV", "0.12")
reTfValues := regexp.MustCompile(tfmaskValuesRegex)
reTfResource := regexp.MustCompile(tfmaskResourceRegex)
scanner := bufio.NewScanner(os.Stdin)
versionedExpressions := versionedExpressions[tfenv]
// initialize currentResource once before scanning
currentResource := ""
for scanner.Scan() {
line := scanner.Text()
currentResource = getCurrentResource(versionedExpressions,
currentResource, line)
fmt.Println(processLine(versionedExpressions, reTfResource, reTfValues,
tfmaskChar, currentResource, line))
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}
func getCurrentResource(expression expression, currentResource, line string) string {
reTfApplyCurrentResource := regexp.MustCompile("^([a-z].*?): (.*?)$")
if expression.reTfPlanCurrentResource.MatchString(line) {
match := expression.reTfPlanCurrentResource.FindStringSubmatch(line)
// for tf 0.12 the resource is wrapped in quotes, so remove them
strippedResource := strings.Replace(match[expression.resourceIndex],
"\"", "", -1)
currentResource = strippedResource
} else if reTfApplyCurrentResource.MatchString(line) {
match := reTfApplyCurrentResource.FindStringSubmatch(line)
currentResource = match[1]
}
return currentResource
}
func processLine(expression expression, reTfResource,
reTfValues *regexp.Regexp, tfmaskChar, currentResource,
line string) string {
if expression.planStatusRegex.MatchString(line) {
line = planStatus(expression.planStatusRegex, reTfResource, tfmaskChar,
line)
} else if expression.reTfPlanLine.MatchString(line) {
line = planLine(expression.reTfPlanLine, reTfResource, reTfValues,
currentResource, tfmaskChar, expression.assign,
expression.operator, line)
} else if expression.reMapKeyPair.MatchString(line) {
line = assignmentLine(expression.reMapKeyPair, reTfValues,
tfmaskChar, line)
}
return line
}
func planStatus(planStatusRegex, reTfResource *regexp.Regexp, tfmaskChar,
line string) string {
match := planStatusRegex.FindStringSubmatch(line)
resource := match[1]
id := match[3]
if reTfResource.MatchString(resource) {
line = strings.Replace(line, id, strings.Repeat(tfmaskChar,
utf8.RuneCountInString(id)), 1)
}
return line
}
func matchFromLine(reTfPlanLine *regexp.Regexp, line string) match {
subMatch := reTfPlanLine.FindStringSubmatch(line)
return match{
leadingWhitespace: subMatch[1],
property: subMatch[2], // something like `stage.0.action.0.configuration.OAuthToken`
trailingWhitespace: subMatch[3],
firstQuote: subMatch[4],
oldValue: subMatch[5],
secondQuote: subMatch[6], // > or "
thirdQuote: subMatch[7], // < or " or (
newValue: subMatch[8],
fourthQuote: subMatch[9], // > or " or )
postfix: subMatch[10],
}
}
func matchFromAssignment(reMapKeyPair *regexp.Regexp, line string) keyValueMatch {
subMatch := reMapKeyPair.FindStringSubmatch(line)
return keyValueMatch{
leadingWhitespace: subMatch[1],
property: subMatch[2],
assignmentOperator: subMatch[3],
trailingWhitespaceAfter: subMatch[4],
oldValue: subMatch[5],
}
}
func planLine(reTfPlanLine, reTfResource, reTfValues *regexp.Regexp,
currentResource, tfmaskChar, assign, operator, line string) string {
match := matchFromLine(reTfPlanLine, line)
if reTfValues.MatchString(match.property) ||
reTfResource.MatchString(currentResource) {
// The value inside the "...", <...> or (...)
oldValue := maskValue(match.oldValue, tfmaskChar)
// The value inside the "...", <...> or (...)
newValue := maskValue(match.newValue, tfmaskChar)
line = fmt.Sprintf("%v%v%v%v%v%v%v %v %v%v%v%v",
match.leadingWhitespace, match.property, assign,
match.trailingWhitespace, match.firstQuote, oldValue,
match.secondQuote, operator, match.thirdQuote,
newValue, match.fourthQuote, match.postfix)
}
return line
}
func assignmentLine(reMapKeyPair, reTfValues *regexp.Regexp, tfmaskChar, line string) string {
match := matchFromAssignment(reMapKeyPair, line)
if reTfValues.MatchString(match.property) {
maskedValue := maskValue(match.oldValue, tfmaskChar)
line = fmt.Sprintf("%v%v%v%v\"%v\"",
match.leadingWhitespace,
match.property,
match.assignmentOperator,
match.trailingWhitespaceAfter,
maskedValue)
}
return line
}
func maskValue(value, tfmaskChar string) string {
exclusions := []string{"sensitive", "computed", "<computed",
"known after apply"}
if !contains(exclusions, value) {
return strings.Repeat(tfmaskChar,
utf8.RuneCountInString(value))
}
return value
}
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}