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 a redacted form of environment at startup #596

Merged
merged 2 commits into from
Jul 27, 2022
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
40 changes: 39 additions & 1 deletion cmd/git-sync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ func main() {
"uid", os.Getuid(),
"gid", os.Getgid(),
"home", os.Getenv("HOME"),
"args", os.Args)
"args", logSafeArgs(os.Args),
"env", logSafeEnv(os.Environ()))

if _, err := exec.LookPath(*flGitCmd); err != nil {
log.Error(err, "ERROR: git executable not found", "git", *flGitCmd)
Expand Down Expand Up @@ -717,6 +718,43 @@ func main() {
}
}

const redactedString = "<REDACTED>"

// logSafeArgs makes sure any sensitive args (e.g. passwords) are redacted
// before logging.
func logSafeArgs(args []string) []string {
ret := make([]string, len(args))
redact := false
for i, arg := range args {
if redact {
ret[i] = redactedString
redact = false
continue
}
if arg == "--password" {
redact = true
}
if strings.HasPrefix(arg, "--password=") {
arg = "--password=" + redactedString
}
ret[i] = arg
}
return ret
}

// logSafeEnv makes sure any sensitive env vars (e.g. passwords) are redacted
// before logging.
func logSafeEnv(env []string) []string {
ret := make([]string, len(env))
for i, ev := range env {
if strings.HasPrefix(ev, "GIT_SYNC_PASSWORD=") {
ev = "GIT_SYNC_PASSWORD=" + redactedString
}
ret[i] = ev
}
return ret
}

func normalizePath(path string) (string, error) {
delinked, err := filepath.EvalSymlinks(path)
if err != nil {
Expand Down