-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
) close #36374
- Loading branch information
Showing
6 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
var ( | ||
passwordPatterns = `(password[\s]*=[\s]*(\\")?)(.*?)((\\")?\\n)` | ||
|
||
passwordRegexp *regexp.Regexp | ||
) | ||
|
||
func init() { | ||
passwordRegexp = regexp.MustCompile(passwordPatterns) | ||
} | ||
|
||
// HideSensitive replace password with ******. | ||
func HideSensitive(input string) string { | ||
output := passwordRegexp.ReplaceAllString(input, "$1******$4") | ||
return output | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHideSensitive(t *testing.T) { | ||
strs := []struct { | ||
old string | ||
new string | ||
}{ | ||
{ | ||
`host = "127.0.0.1"\n user = "root"\n password = "/Q7B9DizNLLTTfiZHv9WoEAKamfpIUs="\n port = 3306\n`, | ||
`host = "127.0.0.1"\n user = "root"\n password = ******\n port = 3306\n`, | ||
}, | ||
{ | ||
`host = "127.0.0.1"\n user = "root"\n password = ""\n port = 3306\n`, | ||
`host = "127.0.0.1"\n user = "root"\n password = ******\n port = 3306\n`, | ||
}, | ||
{ | ||
`host = "127.0.0.1"\n user = "root"\n password= "/Q7B9DizNLLTTfiZHv9WoEAKamfpIUs="\n port = 3306\n`, | ||
`host = "127.0.0.1"\n user = "root"\n password= ******\n port = 3306\n`, | ||
}, | ||
{ | ||
`host = "127.0.0.1"\n user = "root"\n password =""\n port = 3306\n`, | ||
`host = "127.0.0.1"\n user = "root"\n password =******\n port = 3306\n`, | ||
}, | ||
{ | ||
`host = "127.0.0.1"\n user = "root"\n password=""\n port = 3306\n`, | ||
`host = "127.0.0.1"\n user = "root"\n password=******\n port = 3306\n`, | ||
}, | ||
} | ||
for i, str := range strs { | ||
t.Logf("case #%d\n", i) | ||
require.Equal(t, str.new, HideSensitive(str.old)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters