Skip to content

Commit

Permalink
Redact url (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
tothszabi authored Jul 28, 2023
1 parent 3f9f8cc commit cb07443
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
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)
}
}
}

0 comments on commit cb07443

Please sign in to comment.