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

Redact url #143

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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),
)
}
}
Expand All @@ -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),
)
}

Expand Down Expand Up @@ -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),
)
}

Expand Down
15 changes: 15 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -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
}
33 changes: 33 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"reflect"
"testing"
)

func TestURLRedacting(t *testing.T) {
tests := []struct {
url string
want string
}{
{
url: "https://user:[email protected]/org/project.git",
want: "https://[email protected]/org/project.git",
},
{
url: "https://github.com/org/project.git",
want: "https://github.com/org/project.git",
},
{
url: "[email protected]:Org/Applications.git",
want: "[email protected]: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)
}
}
}