-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathredact_test.go
More file actions
112 lines (102 loc) · 2.82 KB
/
Copy pathredact_test.go
File metadata and controls
112 lines (102 loc) · 2.82 KB
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
package redact
import (
"testing"
"github.com/buildkite/agent/v3/env"
"github.com/google/go-cmp/cmp"
)
func TestVars(t *testing.T) {
t.Parallel()
tests := []struct {
name string
redactConfig []string
environment []env.Pair
wantMatched []env.Pair
wantShort []string
}{
{
name: "hunter2",
redactConfig: []string{"*_PASSWORD", "*_TOKEN"},
environment: []env.Pair{
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
// These are example values, and are not leaked credentials
{Name: "DATABASE_USERNAME", Value: "AzureDiamond"},
{Name: "DATABASE_PASSWORD", Value: "hunter2"},
},
wantMatched: []env.Pair{{Name: "DATABASE_PASSWORD", Value: "hunter2"}},
wantShort: nil,
},
{
name: "short",
redactConfig: []string{"*_PASSWORD", "*_TOKEN"},
environment: []env.Pair{
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
// These are example values, and are not leaked credentials
{Name: "DATABASE_USERNAME", Value: "AzureDiamond"},
{Name: "DATABASE_PASSWORD", Value: "hunt"},
},
wantMatched: nil,
wantShort: []string{"DATABASE_PASSWORD"},
},
{
name: "empty",
redactConfig: nil,
environment: []env.Pair{
{Name: "FOO", Value: "BAR"},
{Name: "BUILDKITE_PIPELINE", Value: "unit-test"},
},
wantMatched: nil,
wantShort: nil,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
matched, short, err := Vars(test.redactConfig, test.environment)
if err != nil {
t.Fatalf("Vars(%q, %q) error = %v", test.redactConfig, test.environment, err)
}
if diff := cmp.Diff(matched, test.wantMatched); diff != "" {
t.Errorf("Vars(%q, %q) matched diff (-got +want)\n%s", test.redactConfig, test.environment, diff)
}
if diff := cmp.Diff(short, test.wantShort); diff != "" {
t.Errorf("Vars(%q, %q) short diff (-got +want)\n%s", test.redactConfig, test.environment, diff)
}
})
}
}
func TestRedactString(t *testing.T) {
t.Parallel()
tests := []struct {
name string
needles []string
input string
want string
}{
{
name: "no needles",
needles: nil,
input: "secret 1 secret 2 secret 3 s",
want: "secret 1 secret 2 secret 3 s",
},
{
name: "one needle",
needles: []string{"secret 2"},
input: "secret 1 secret 2 secret 3 s",
want: "secret 1 [REDACTED] secret 3 s",
},
{
name: "three needles",
needles: []string{"secret 1", "secret 2", "secret 3"},
input: "secret 1 secret 2 secret 3 s",
want: "[REDACTED] [REDACTED] [REDACTED] s",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
if got := String(test.input, test.needles); got != test.want {
t.Errorf("String(%q, %q) = %q, want %q", test.input, test.needles, got, test.want)
}
})
}
}