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

Add port of url.Redact to satisfy go1.14 compatibility #336

Merged
merged 5 commits into from
Sep 7, 2021
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
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ workflows:
context: go-getter
matrix:
parameters:
go-version: ["1.15.13"]
go-version: ["1.15.13", "1.14.15"]
name: linux-test-go-<< matrix.go-version >>
- windows-tests:
context: go-getter
matrix:
parameters:
go-version: ["1.15.13"]
go-version: ["1.15.13", "1.14.15"]
gotestsum-version: ["0.4.1"]
name: win-test-go-<< matrix.go-version >>
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func (c *Client) Get() error {
// if we're specifying a subdir.
err := g.Get(dst, u)
if err != nil {
err = fmt.Errorf("error downloading '%s': %s", u.Redacted(), err)
err = fmt.Errorf("error downloading '%s': %s", RedactURL(u), err)
return err
}
}
Expand Down
19 changes: 19 additions & 0 deletions url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package getter

import "net/url"

// RedactURL is a port of url.Redacted from the standard library,
// which is like url.String but replaces any password with "xxxxx".
// Only the password in u.URL is redacted. This allows the library
// to maintain compatibility with go1.14.
func RedactURL(u *url.URL) string {
if u == nil {
return ""
}

ru := *u
if _, has := ru.User.Password(); has {
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
}
return ru.String()
}
73 changes: 73 additions & 0 deletions url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package getter

import (
"net/url"
"testing"
)

func TestRedactURL(t *testing.T) {
cases := []struct {
name string
url *url.URL
want string
}{
{
name: "non-blank Password",
url: &url.URL{
Scheme: "http",
Host: "host.tld",
Path: "this:that",
User: url.UserPassword("user", "password"),
},
want: "http://user:xxxxx@host.tld/this:that",
},
{
name: "blank Password",
url: &url.URL{
Scheme: "http",
Host: "host.tld",
Path: "this:that",
User: url.User("user"),
},
want: "http://user@host.tld/this:that",
},
{
name: "nil User",
url: &url.URL{
Scheme: "http",
Host: "host.tld",
Path: "this:that",
User: url.UserPassword("", "password"),
},
want: "http://:xxxxx@host.tld/this:that",
},
{
name: "blank Username, blank Password",
url: &url.URL{
Scheme: "http",
Host: "host.tld",
Path: "this:that",
},
want: "http://host.tld/this:that",
},
{
name: "empty URL",
url: &url.URL{},
want: "",
},
{
name: "nil URL",
url: nil,
want: "",
},
}

for _, tt := range cases {
t := t
t.Run(tt.name, func(t *testing.T) {
if g, w := RedactURL(tt.url), tt.want; g != w {
t.Fatalf("got: %q\nwant: %q", g, w)
}
})
}
}