This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
/
escape.go
88 lines (71 loc) · 1.84 KB
/
escape.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
package processors
import (
"os"
"strings"
"github.com/golangci/golangci-api/pkg/goenvbuild/result"
)
func escapeText(text string, secretableCtx secretable) string {
secrets := buildSecrets(secretableCtx.secrets()...)
ret := text
for secret, replacement := range secrets {
ret = strings.Replace(ret, secret, replacement, -1)
}
return ret
}
type secretable interface {
secrets() []string
}
//nolint:gocyclo
func buildSecrets(vars ...string) map[string]string {
const minSecretValueLen = 6
const hidden = "{hidden}"
ret := map[string]string{}
for _, v := range vars {
if len(v) >= minSecretValueLen {
ret[v] = hidden
}
}
exclude := map[string]bool{
"APP_NAME": true,
"ADMIN_GITHUB_LOGIN": true,
"GITHUB_REVIEWER_LOGIN": true,
"WEB_ROOT": true,
"GOROOT": true,
"GOPATH": true,
"FARGATE_CONTAINER": true,
"PATCH_STORE_DIR": true,
}
for _, kv := range os.Environ() {
parts := strings.Split(kv, "=")
if len(parts) != 2 {
continue
}
k := parts[0]
if exclude[k] {
continue
}
if strings.HasSuffix(k, "_OWNERS") || strings.HasSuffix(k, "_PERCENT") || strings.HasSuffix(k, "_REPOS") {
continue // experiment vars
}
v := parts[1]
if strings.EqualFold(v, "golangci") || strings.EqualFold(v, "golangci-lint") {
continue // just extra check because these are critical words
}
if len(v) >= minSecretValueLen {
ret[v] = hidden
}
}
return ret
}
func escapeBuildLog(buildLog *result.Log, s secretable) {
for _, group := range buildLog.Groups {
group.Name = escapeText(group.Name, s)
for _, step := range group.Steps {
step.Description = escapeText(step.Description, s)
step.Error = escapeText(step.Error, s)
for i := range step.OutputLines {
step.OutputLines[i] = escapeText(step.OutputLines[i], s)
}
}
}
}