Skip to content

Commit ad8efc2

Browse files
authored
fix(config): add redaction for sensitive fields (#185)
1 parent a2cbf1f commit ad8efc2

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

pkg/testcoverage/check.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/logger"
1313
)
1414

15-
//nolint:maintidx // relax
16-
func Check(wout io.Writer, cfg Config) (bool, error) {
15+
//nolint:maintidx,nonamedreturns // relax
16+
func Check(wout io.Writer, cfg Config) (pass bool, err error) {
1717
buffer := &bytes.Buffer{}
1818
w := bufio.NewWriter(buffer)
1919
//nolint:errcheck // relax
2020
defer func() {
21-
if cfg.Debug {
21+
if cfg.Debug || err != nil {
2222
wout.Write(logger.Bytes())
2323
wout.Write([]byte("-------------------------\n\n"))
2424
}
@@ -33,7 +33,7 @@ func Check(wout io.Writer, cfg Config) (bool, error) {
3333
}
3434

3535
logger.L.Info().Msg("running check...")
36-
logger.L.Info().Any("config", cfg).Msg("using configuration")
36+
logger.L.Info().Any("config", cfg.Redacted()).Msg("using configuration")
3737

3838
currentStats, err := GenerateCoverageStats(cfg)
3939
if err != nil {

pkg/testcoverage/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/vladopajic/go-test-coverage/v2/pkg/testcoverage/badgestorer"
1414
)
1515

16+
const HiddenValue = "***"
17+
1618
var (
1719
ErrThresholdNotInRange = errors.New("threshold must be in range [0 - 100]")
1820
ErrCoverageProfileNotSpecified = errors.New("coverage profile file not specified")
@@ -60,6 +62,23 @@ type Badge struct {
6062
Git badgestorer.Git
6163
}
6264

65+
//nolint:wsl,mnd // relax
66+
func (c Config) Redacted() Config {
67+
r := c
68+
69+
if r.Badge.CDN.Key != "" {
70+
r.Badge.CDN.Key = r.Badge.CDN.Key[0:min(len(r.Badge.CDN.Key), 5)] + HiddenValue
71+
}
72+
if r.Badge.CDN.Secret != "" {
73+
r.Badge.CDN.Secret = HiddenValue
74+
}
75+
if r.Badge.Git.Token != "" {
76+
r.Badge.Git.Token = HiddenValue
77+
}
78+
79+
return r
80+
}
81+
6382
func (c Config) Validate() error {
6483
validateRegexp := func(s string) error {
6584
_, err := regexp.Compile("(?i)" + s)

pkg/testcoverage/config_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,36 @@ import (
1212

1313
const nonEmptyStr = "any"
1414

15+
func Test_Config_Redacted(t *testing.T) {
16+
t.Parallel()
17+
18+
cfg := newValidCfg()
19+
cfg.Badge.Git.Token = nonEmptyStr
20+
cfg.Badge.CDN.Secret = nonEmptyStr
21+
cfg.Badge.CDN.Key = nonEmptyStr
22+
23+
r := cfg.Redacted()
24+
25+
// redacted should not be equal to original
26+
assert.NotEqual(t, cfg, r)
27+
28+
// original should not change
29+
assert.Equal(t, nonEmptyStr, cfg.Badge.Git.Token)
30+
assert.Equal(t, nonEmptyStr, cfg.Badge.CDN.Secret)
31+
assert.Equal(t, nonEmptyStr, cfg.Badge.CDN.Key)
32+
33+
// redacted should have hidden values
34+
assert.Equal(t, HiddenValue, r.Badge.Git.Token)
35+
assert.Equal(t, HiddenValue, r.Badge.CDN.Secret)
36+
assert.Equal(t, nonEmptyStr+HiddenValue, r.Badge.CDN.Key)
37+
38+
// redacted config of empty field should not do anything
39+
r = Config{}.Redacted()
40+
assert.Empty(t, r.Badge.Git.Token)
41+
assert.Empty(t, r.Badge.CDN.Secret)
42+
assert.Empty(t, r.Badge.CDN.Key)
43+
}
44+
1545
func Test_Config_Validate(t *testing.T) {
1646
t.Parallel()
1747

0 commit comments

Comments
 (0)