-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19340 from droslean/git-clonerefs-censor
censor the potential token in git outputs and messages
- Loading branch information
Showing
2 changed files
with
25 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -669,31 +669,45 @@ func makeFakeGitRepo(fakeTimestamp int) (string, error) { | |
return fakeGitDir, nil | ||
} | ||
|
||
func TestCensorGitCommand(t *testing.T) { | ||
func TestCensorToken(t *testing.T) { | ||
testCases := []struct { | ||
id string | ||
token string | ||
command string | ||
msg string | ||
expected string | ||
}{ | ||
{ | ||
id: "no token", | ||
command: "git fetch https://github.com/kubernetes/test-infra.git", | ||
msg: "git fetch https://github.com/kubernetes/test-infra.git", | ||
expected: "git fetch https://github.com/kubernetes/test-infra.git", | ||
}, | ||
{ | ||
id: "with token", | ||
token: "123456789", | ||
command: "git fetch 123456789:x-oauth-basic@https://github.com/kubernetes/test-infra.git", | ||
msg: "git fetch 123456789:x-oauth-basic@https://github.com/kubernetes/test-infra.git", | ||
expected: "git fetch CENSORED:x-oauth-basic@https://github.com/kubernetes/test-infra.git", | ||
}, | ||
{ | ||
id: "git output with token", | ||
token: "123456789", | ||
msg: ` | ||
Cloning into 'test-infa'... | ||
remote: Invalid username or password. | ||
fatal: Authentication failed for 'https://[email protected]/kubernetes/test-infa/' | ||
`, | ||
expected: ` | ||
Cloning into 'test-infa'... | ||
remote: Invalid username or password. | ||
fatal: Authentication failed for 'https://[email protected]/kubernetes/test-infa/' | ||
`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.id, func(t *testing.T) { | ||
censoredCommand := censorGitCommand(tc.command, tc.token) | ||
if !reflect.DeepEqual(censoredCommand, tc.expected) { | ||
t.Fatalf("expected: %s got %s", tc.expected, censoredCommand) | ||
censoredMsg := censorToken(tc.msg, tc.token) | ||
if !reflect.DeepEqual(censoredMsg, tc.expected) { | ||
t.Fatalf("expected: %s got %s", tc.expected, censoredMsg) | ||
} | ||
}) | ||
} | ||
|