From 342c7673021f6449a23bb86b9334831b58b8aec1 Mon Sep 17 00:00:00 2001 From: xhe Date: Sun, 14 Jul 2024 22:37:27 +0800 Subject: [PATCH] hide other password args (#2436) Signed-off-by: xhe --- pkg/environment/history.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/environment/history.go b/pkg/environment/history.go index 629ced78c7..36bfbc4a3b 100644 --- a/pkg/environment/history.go +++ b/pkg/environment/history.go @@ -245,17 +245,33 @@ func getLatestHistoryFile(dir string) (item historyItem) { // HidePassword replace password with ****** func HidePassword(args []string) []string { - var record []string + redactArgs := []string{ + // general + "-p", + // dumpling + "--password", + // lightning + "--tidb-password", + } + var r []string for i := 0; i < len(args); i++ { arg := args[i] - if strings.HasPrefix(arg, "-p") && len(arg) > 2 { - record = append(record, "-p******") - } else if arg == "-p" && i+1 < len(args) { - record = append(record, "-p", "******") - i++ // skip next word that may be password - } else { - record = append(record, arg) + redacted := false + for _, ra := range redactArgs { + if strings.HasPrefix(arg, ra) && len(arg) > len(ra) { + r = append(r, ra+"******") + redacted = true + break + } else if arg == ra && i+1 < len(args) { + r = append(r, ra, "******") + i++ // skip next word that may be password + redacted = true + break + } + } + if !redacted { + r = append(r, arg) } } - return record + return r }