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
Show file tree
Hide file tree
Changes from 2 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
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.14.2"]
go-version: ["1.15.15"]
dany74q marked this conversation as resolved.
Show resolved Hide resolved
name: test-go-<< matrix.go-version >>
14 changes: 7 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)
v.Debug("performing request", "method", req.Method, "url", req.URL.Redacted())
case Logger:
v.Printf("[DEBUG] %s %s", req.Method, req.URL)
v.Printf("[DEBUG] %s %s", req.Method, req.URL.Redacted())
}
}

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)
v.Error("request failed", "error", doErr, "method", req.Method, "url", req.URL.Redacted())
case Logger:
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, doErr)
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL.Redacted(), 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)
desc := fmt.Sprintf("%s %s", req.Method, req.URL.Redacted())
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, attempt)
req.Method, req.URL.Redacted(), attempt)
}

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

// Try to read the response body so we can reuse this connection.
Expand Down
19 changes: 17 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,32 @@ func TestClient_Do_fails(t *testing.T) {
}))
defer ts.Close()

serverUrlWithBasicAuth, err := url.Parse(ts.URL)
if err != nil {
t.Fatalf("failed parsing test server url: %s", ts.URL)
}
serverUrlWithBasicAuth.User = url.UserPassword("user", "pasten")

tests := []struct {
url string
name string
cr CheckRetry
err string
}{
{
url: ts.URL,
name: "default_retry_policy",
cr: DefaultRetryPolicy,
err: "giving up after 3 attempt(s)",
},
{
url: serverUrlWithBasicAuth.String(),
name: "default_retry_policy_url_with_basic_auth",
cr: DefaultRetryPolicy,
err: serverUrlWithBasicAuth.Redacted() + " giving up after 3 attempt(s)",
},
{
url: ts.URL,
name: "error_propagated_retry_policy",
cr: ErrorPropagatedRetryPolicy,
err: "giving up after 3 attempt(s): unexpected HTTP status 500 Internal Server Error",
Expand All @@ -283,15 +298,15 @@ func TestClient_Do_fails(t *testing.T) {
client.RetryMax = 2

// Create the request
req, err := NewRequest("POST", ts.URL, nil)
req, err := NewRequest("POST", tt.url, nil)
if err != nil {
t.Fatalf("err: %v", err)
}

// Send the request.
_, err = client.Do(req)
if err == nil || !strings.HasSuffix(err.Error(), tt.err) {
t.Fatalf("expected giving up error, got: %#v", err)
t.Fatalf("expected %#v, got: %#v", tt.err, err)
}
})
}
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.13
go 1.15