diff --git a/main.go b/main.go index 08a7d841..98155195 100644 --- a/main.go +++ b/main.go @@ -94,6 +94,8 @@ func cloneRepo(cfg repoConfig) error { ) } + redactedURL := redactURL(cfg.RepositoryURL) + // Activate SSH key is optional if cfg.SSHRsaPrivateKey != "" { if err := activatesshkey.Execute(activatesshkey.Config{ @@ -104,7 +106,7 @@ func cloneRepo(cfg repoConfig) error { return newStepError( "activate_ssh_key_failed", err, - fmt.Sprintf("Activating SSH key for %s failed", cfg.RepositoryURL), + fmt.Sprintf("Activating SSH key for %s failed", redactedURL), ) } } @@ -118,7 +120,7 @@ func cloneRepo(cfg repoConfig) error { return newStepError( "activate_git_http_credentials_failed", err, - fmt.Sprintf("Activating Git HTTP credentials for %s failed", cfg.RepositoryURL), + fmt.Sprintf("Activating Git HTTP credentials for %s failed", redactedURL), ) } @@ -151,7 +153,7 @@ func cloneRepo(cfg repoConfig) error { return newStepError( "git_clone_failed", err, - fmt.Sprintf("Git clone for %s - %s branch failed (ssh: %t, user: %t, pass: %t)", cfg.RepositoryURL, cfg.Branch, hasSSH, hasUser, hasPass), + fmt.Sprintf("Git clone for %s - %s branch failed (ssh: %t, user: %t, pass: %t)", redactedURL, cfg.Branch, hasSSH, hasUser, hasPass), ) } diff --git a/url.go b/url.go new file mode 100644 index 00000000..803ad578 --- /dev/null +++ b/url.go @@ -0,0 +1,15 @@ +package main + +import "strings" + +func redactURL(url string) string { + firstSeparator := "://" + firstIndex := strings.Index(url, firstSeparator) + secondIndex := strings.Index(url, "@") + + if 0 < firstIndex && 0 < secondIndex && firstIndex < secondIndex { + url = url[:firstIndex+len(firstSeparator)] + "..." + url[secondIndex:] + } + + return url +} diff --git a/url_test.go b/url_test.go new file mode 100644 index 00000000..b7287d62 --- /dev/null +++ b/url_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "reflect" + "testing" +) + +func TestURLRedacting(t *testing.T) { + tests := []struct { + url string + want string + }{ + { + url: "https://user:pass@repo.com/org/project.git", + want: "https://...@repo.com/org/project.git", + }, + { + url: "https://github.com/org/project.git", + want: "https://github.com/org/project.git", + }, + { + url: "git@github.com:Org/Applications.git", + want: "git@github.com:Org/Applications.git", + }, + } + + for _, tt := range tests { + got := redactURL(tt.url) + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("redactURL() = %v, want %v", got, tt.want) + } + } +}