Skip to content

Commit

Permalink
Copied Redacted() implementation from go 1.15
Browse files Browse the repository at this point in the history
  • Loading branch information
dany74q committed Mar 16, 2022
1 parent 65e8765 commit b2aee50
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ workflows:
- run-tests:
matrix:
parameters:
go-version: ["1.15.15"]
go-version: ["1.14.2"]
name: test-go-<< matrix.go-version >>
28 changes: 21 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,9 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
if logger != nil {
switch v := logger.(type) {
case LeveledLogger:
v.Debug("performing request", "method", req.Method, "url", req.URL.Redacted())
v.Debug("performing request", "method", req.Method, "url", redactURL(req.URL))
case Logger:
v.Printf("[DEBUG] %s %s", req.Method, req.URL.Redacted())
v.Printf("[DEBUG] %s %s", req.Method, redactURL(req.URL))
}
}

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

wait := c.Backoff(c.RetryWaitMin, c.RetryWaitMax, i, resp)
if logger != nil {
desc := fmt.Sprintf("%s %s", req.Method, req.URL.Redacted())
desc := fmt.Sprintf("%s %s", req.Method, redactURL(req.URL))
if resp != nil {
desc = fmt.Sprintf("%s (status: %d)", desc, resp.StatusCode)
}
Expand Down Expand Up @@ -694,11 +694,11 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
// communicate why
if err == nil {
return nil, fmt.Errorf("%s %s giving up after %d attempt(s)",
req.Method, req.URL.Redacted(), attempt)
req.Method, redactURL(req.URL), attempt)
}

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

// Try to read the response body so we can reuse this connection.
Expand Down Expand Up @@ -779,3 +779,17 @@ func (c *Client) StandardClient() *http.Client {
Transport: &RoundTripper{Client: c},
}
}

// Taken from url.URL#Redacted() which was introduced in go 1.15.
// We can switch to using it directly if we'll bump the minimum required go version.
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()
}
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func TestClient_Do_fails(t *testing.T) {
url: serverUrlWithBasicAuth.String(),
name: "default_retry_policy_url_with_basic_auth",
cr: DefaultRetryPolicy,
err: serverUrlWithBasicAuth.Redacted() + " giving up after 3 attempt(s)",
err: redactURL(serverUrlWithBasicAuth) + " giving up after 3 attempt(s)",
},
{
url: ts.URL,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ require (
github.com/hashicorp/go-hclog v0.9.2
)

go 1.15
go 1.13

0 comments on commit b2aee50

Please sign in to comment.