Skip to content

Commit b2aee50

Browse files
committed
Copied Redacted() implementation from go 1.15
1 parent 65e8765 commit b2aee50

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ workflows:
5151
- run-tests:
5252
matrix:
5353
parameters:
54-
go-version: ["1.15.15"]
54+
go-version: ["1.14.2"]
5555
name: test-go-<< matrix.go-version >>

client.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -556,9 +556,9 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
556556
if logger != nil {
557557
switch v := logger.(type) {
558558
case LeveledLogger:
559-
v.Debug("performing request", "method", req.Method, "url", req.URL.Redacted())
559+
v.Debug("performing request", "method", req.Method, "url", redactURL(req.URL))
560560
case Logger:
561-
v.Printf("[DEBUG] %s %s", req.Method, req.URL.Redacted())
561+
v.Printf("[DEBUG] %s %s", req.Method, redactURL(req.URL))
562562
}
563563
}
564564

@@ -604,9 +604,9 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
604604
if doErr != nil {
605605
switch v := logger.(type) {
606606
case LeveledLogger:
607-
v.Error("request failed", "error", doErr, "method", req.Method, "url", req.URL.Redacted())
607+
v.Error("request failed", "error", doErr, "method", req.Method, "url", redactURL(req.URL))
608608
case Logger:
609-
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL.Redacted(), doErr)
609+
v.Printf("[ERR] %s %s request failed: %v", req.Method, redactURL(req.URL), doErr)
610610
}
611611
} else {
612612
// Call this here to maintain the behavior of logging all requests,
@@ -642,7 +642,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
642642

643643
wait := c.Backoff(c.RetryWaitMin, c.RetryWaitMax, i, resp)
644644
if logger != nil {
645-
desc := fmt.Sprintf("%s %s", req.Method, req.URL.Redacted())
645+
desc := fmt.Sprintf("%s %s", req.Method, redactURL(req.URL))
646646
if resp != nil {
647647
desc = fmt.Sprintf("%s (status: %d)", desc, resp.StatusCode)
648648
}
@@ -694,11 +694,11 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
694694
// communicate why
695695
if err == nil {
696696
return nil, fmt.Errorf("%s %s giving up after %d attempt(s)",
697-
req.Method, req.URL.Redacted(), attempt)
697+
req.Method, redactURL(req.URL), attempt)
698698
}
699699

700700
return nil, fmt.Errorf("%s %s giving up after %d attempt(s): %w",
701-
req.Method, req.URL.Redacted(), attempt, err)
701+
req.Method, redactURL(req.URL), attempt, err)
702702
}
703703

704704
// Try to read the response body so we can reuse this connection.
@@ -779,3 +779,17 @@ func (c *Client) StandardClient() *http.Client {
779779
Transport: &RoundTripper{Client: c},
780780
}
781781
}
782+
783+
// Taken from url.URL#Redacted() which was introduced in go 1.15.
784+
// We can switch to using it directly if we'll bump the minimum required go version.
785+
func redactURL(u *url.URL) string {
786+
if u == nil {
787+
return ""
788+
}
789+
790+
ru := *u
791+
if _, has := ru.User.Password(); has {
792+
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
793+
}
794+
return ru.String()
795+
}

client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestClient_Do_fails(t *testing.T) {
278278
url: serverUrlWithBasicAuth.String(),
279279
name: "default_retry_policy_url_with_basic_auth",
280280
cr: DefaultRetryPolicy,
281-
err: serverUrlWithBasicAuth.Redacted() + " giving up after 3 attempt(s)",
281+
err: redactURL(serverUrlWithBasicAuth) + " giving up after 3 attempt(s)",
282282
},
283283
{
284284
url: ts.URL,

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ require (
55
github.com/hashicorp/go-hclog v0.9.2
66
)
77

8-
go 1.15
8+
go 1.13

0 commit comments

Comments
 (0)