Skip to content

Commit

Permalink
Merge pull request #2454 from buildkite/combine-redactor-warning
Browse files Browse the repository at this point in the history
Log warning about short vars once
  • Loading branch information
DrJosh9000 authored Oct 25, 2023
2 parents b462eb0 + 3c37060 commit fc11f57
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/redact/redact.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package redact

import (
"path"
"sort"
"strings"

"github.com/buildkite/agent/v3/internal/job/shell"
"golang.org/x/exp/maps"
)

// LengthMin is the shortest string length that will be considered a
Expand Down Expand Up @@ -42,6 +45,7 @@ func Values(logger shell.Logger, patterns []string, environment map[string]strin
func Vars(logger shell.Logger, patterns []string, environment map[string]string) map[string]string {
// Lifted out of Bootstrap.setupRedactors to facilitate testing
vars := make(map[string]string)
shortVars := make(map[string]struct{})

for name, val := range environment {
for _, pattern := range patterns {
Expand All @@ -57,7 +61,7 @@ func Vars(logger shell.Logger, patterns []string, environment map[string]string)
}
if len(val) < LengthMin {
if len(val) > 0 {
logger.Warningf("Value of %s below minimum length (%d bytes) and will not be redacted", name, LengthMin)
shortVars[name] = struct{}{}
}
continue
}
Expand All @@ -67,5 +71,11 @@ func Vars(logger shell.Logger, patterns []string, environment map[string]string)
}
}

if len(shortVars) > 0 {
// TODO: Use stdlib maps when it gets a Keys function (Go 1.22?)
vars := maps.Keys(shortVars)
sort.Strings(vars)
logger.Warningf("Some variables have values below minimum length (%d bytes) and will not be redacted: %s", LengthMin, strings.Join(vars, ", "))
}
return vars
}

0 comments on commit fc11f57

Please sign in to comment.