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

Redacted URL in logs / errors #158

Merged
merged 4 commits into from
May 30, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Copied Redacted() implementation from go 1.15
dany74q committed Mar 16, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit b2aee5005449da59dc00efd9c30d91556e739c79
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
@@ -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))
}
}

@@ -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,
@@ -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)
}
@@ -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.
@@ -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
@@ -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,
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -5,4 +5,4 @@ require (
github.com/hashicorp/go-hclog v0.9.2
)

go 1.15
go 1.13