Skip to content

Commit cd0247a

Browse files
jentingroboquat
authored andcommitted
Redact the rest values once the previous one matches redact fields
Signed-off-by: JenTing Hsiao <[email protected]>
1 parent 2066001 commit cd0247a

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

components/common-go/log/redact.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package log
77
import (
88
"encoding/json"
99
"fmt"
10+
"reflect"
1011
"strings"
1112
)
1213

@@ -16,6 +17,9 @@ var (
1617
"auth_",
1718
"password",
1819
"token",
20+
"key",
21+
"jwt",
22+
"secret",
1923
}
2024
)
2125

@@ -59,6 +63,7 @@ func redactArray(data *[]interface{}) {
5963
}
6064

6165
func redactObject(data *map[string]interface{}) {
66+
var forceRedact bool
6267
for k, v := range *data {
6368
for _, prohibited := range redactedFields {
6469
if strings.Contains(strings.ToLower(fmt.Sprintf("%v", k)), prohibited) {
@@ -67,10 +72,18 @@ func redactObject(data *map[string]interface{}) {
6772
}
6873
}
6974

75+
if forceRedact {
76+
(*data)[k] = redactedValue
77+
}
7078
if (*data)[k] != redactedValue {
7179
//TODO: refactor
7280
//nolint:gosec
81+
was := (*data)[k]
7382
(*data)[k] = redactValue(&v)
83+
if !reflect.DeepEqual(was, (*data)[k]) {
84+
// force the rest values to redact
85+
forceRedact = true
86+
}
7487
}
7588
}
7689
}

components/common-go/log/redact_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func TestRedactJSON(t *testing.T) {
1414
Expectation string
1515
}{
1616
{
17-
`{"auth":{"total":{}},"source":{"file":{"contextPath":".","dockerfilePath":".gitpod.dockerfile","dockerfileVersion":"82561e7f6455e3c0e6ee98be03c4d9aab4d459f8","source":{"git":{"checkoutLocation":"test.repo","cloneTaget":"good-workspace-image","config":{"authPassword":"super-secret-password","authUser":"oauth2","authentication":"BASIC_AUTH"},"remoteUri":"https://github.com/AlexTugarev/test.repo.git","targetMode":"REMOTE_BRANCH"}}}}}`,
18-
`{"auth":{"total":{}},"source":{"file":{"contextPath":".","dockerfilePath":".gitpod.dockerfile","dockerfileVersion":"82561e7f6455e3c0e6ee98be03c4d9aab4d459f8","source":{"git":{"checkoutLocation":"test.repo","cloneTaget":"good-workspace-image","config":{"authPassword":"[redacted]","authUser":"oauth2","authentication":"BASIC_AUTH"},"remoteUri":"https://github.com/AlexTugarev/test.repo.git","targetMode":"REMOTE_BRANCH"}}}}}`,
17+
`{"auth":{"total":{}},"env":[{"name":"SECRET_PASSWORD","value":"i-am-leaked-in-the-logs-yikes"},{"name":"GITHUB_TOKEN","value":"thisismyGitHubTokenDontStealIt"},{"name":"SUPER_SEKRET","value":"you.cant.see.me.or.can.you"},{"name":"GITHUB_SSH_PRIVATE_KEY","value":"super-secret-private-ssh-key-from-github"},{"name":"SHELL","value":"zsh"},{"name":"GITLAB_TOKEN","value":"abcsecrettokendef"}],"source":{"file":{"contextPath":".","dockerfilePath":".gitpod.dockerfile","dockerfileVersion":"82561e7f6455e3c0e6ee98be03c4d9aab4d459f8","source":{"git":{"checkoutLocation":"test.repo","cloneTaget":"good-workspace-image","config":{"authPassword":"super-secret-password","authUser":"oauth2","authentication":"BASIC_AUTH"},"remoteUri":"https://github.com/AlexTugarev/test.repo.git","targetMode":"REMOTE_BRANCH"}}}}}`,
18+
`{"auth":{"total":{}},"env":[{"name":"[redacted]","value":"[redacted]"},{"name":"[redacted]","value":"[redacted]"},{"name":"SUPER_SEKRET","value":"you.cant.see.me.or.can.you"},{"name":"[redacted]","value":"[redacted]"},{"name":"SHELL","value":"zsh"},{"name":"[redacted]","value":"[redacted]"}],"source":{"file":{"contextPath":".","dockerfilePath":".gitpod.dockerfile","dockerfileVersion":"82561e7f6455e3c0e6ee98be03c4d9aab4d459f8","source":{"git":{"checkoutLocation":"test.repo","cloneTaget":"good-workspace-image","config":{"authPassword":"[redacted]","authUser":"oauth2","authentication":"BASIC_AUTH"},"remoteUri":"https://github.com/AlexTugarev/test.repo.git","targetMode":"REMOTE_BRANCH"}}}}}`,
1919
},
2020
}
2121

0 commit comments

Comments
 (0)