Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log warning about short vars once #2454

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}