Skip to content

Commit 95deff2

Browse files
authored
Merge pull request #336 from hashicorp/go1.14-compat
Add port of url.Redact to satisfy go1.14 compatibility
2 parents 790e6b3 + e31b11f commit 95deff2

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ workflows:
165165
context: go-getter
166166
matrix:
167167
parameters:
168-
go-version: ["1.15.13"]
168+
go-version: ["1.15.13", "1.14.15"]
169169
name: linux-test-go-<< matrix.go-version >>
170170
- windows-tests:
171171
context: go-getter
172172
matrix:
173173
parameters:
174-
go-version: ["1.15.13"]
174+
go-version: ["1.15.13", "1.14.15"]
175175
gotestsum-version: ["0.4.1"]
176176
name: win-test-go-<< matrix.go-version >>

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (c *Client) Get() error {
298298
// if we're specifying a subdir.
299299
err := g.Get(dst, u)
300300
if err != nil {
301-
err = fmt.Errorf("error downloading '%s': %s", u.Redacted(), err)
301+
err = fmt.Errorf("error downloading '%s': %s", RedactURL(u), err)
302302
return err
303303
}
304304
}

url.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package getter
2+
3+
import "net/url"
4+
5+
// RedactURL is a port of url.Redacted from the standard library,
6+
// which is like url.String but replaces any password with "xxxxx".
7+
// Only the password in u.URL is redacted. This allows the library
8+
// to maintain compatibility with go1.14.
9+
func RedactURL(u *url.URL) string {
10+
if u == nil {
11+
return ""
12+
}
13+
14+
ru := *u
15+
if _, has := ru.User.Password(); has {
16+
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
17+
}
18+
return ru.String()
19+
}

url_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package getter
2+
3+
import (
4+
"net/url"
5+
"testing"
6+
)
7+
8+
func TestRedactURL(t *testing.T) {
9+
cases := []struct {
10+
name string
11+
url *url.URL
12+
want string
13+
}{
14+
{
15+
name: "non-blank Password",
16+
url: &url.URL{
17+
Scheme: "http",
18+
Host: "host.tld",
19+
Path: "this:that",
20+
User: url.UserPassword("user", "password"),
21+
},
22+
want: "http://user:[email protected]/this:that",
23+
},
24+
{
25+
name: "blank Password",
26+
url: &url.URL{
27+
Scheme: "http",
28+
Host: "host.tld",
29+
Path: "this:that",
30+
User: url.User("user"),
31+
},
32+
want: "http://[email protected]/this:that",
33+
},
34+
{
35+
name: "nil User",
36+
url: &url.URL{
37+
Scheme: "http",
38+
Host: "host.tld",
39+
Path: "this:that",
40+
User: url.UserPassword("", "password"),
41+
},
42+
want: "http://:[email protected]/this:that",
43+
},
44+
{
45+
name: "blank Username, blank Password",
46+
url: &url.URL{
47+
Scheme: "http",
48+
Host: "host.tld",
49+
Path: "this:that",
50+
},
51+
want: "http://host.tld/this:that",
52+
},
53+
{
54+
name: "empty URL",
55+
url: &url.URL{},
56+
want: "",
57+
},
58+
{
59+
name: "nil URL",
60+
url: nil,
61+
want: "",
62+
},
63+
}
64+
65+
for _, tt := range cases {
66+
t := t
67+
t.Run(tt.name, func(t *testing.T) {
68+
if g, w := RedactURL(tt.url), tt.want; g != w {
69+
t.Fatalf("got: %q\nwant: %q", g, w)
70+
}
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)