Skip to content

Commit a99f07b

Browse files
authored
Merge pull request #158 from dany74q/danny/redacted-url-in-logs
Redacted URL in logs / errors
2 parents 86e852d + 8a28c57 commit a99f07b

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

client.go

+21-7
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,9 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
658658
if logger != nil {
659659
switch v := logger.(type) {
660660
case LeveledLogger:
661-
v.Debug("performing request", "method", req.Method, "url", req.URL)
661+
v.Debug("performing request", "method", req.Method, "url", redactURL(req.URL))
662662
case Logger:
663-
v.Printf("[DEBUG] %s %s", req.Method, req.URL)
663+
v.Printf("[DEBUG] %s %s", req.Method, redactURL(req.URL))
664664
}
665665
}
666666

@@ -715,9 +715,9 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
715715
if err != nil {
716716
switch v := logger.(type) {
717717
case LeveledLogger:
718-
v.Error("request failed", "error", err, "method", req.Method, "url", req.URL)
718+
v.Error("request failed", "error", err, "method", req.Method, "url", redactURL(req.URL))
719719
case Logger:
720-
v.Printf("[ERR] %s %s request failed: %v", req.Method, req.URL, err)
720+
v.Printf("[ERR] %s %s request failed: %v", req.Method, redactURL(req.URL), err)
721721
}
722722
} else {
723723
// Call this here to maintain the behavior of logging all requests,
@@ -753,7 +753,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
753753

754754
wait := c.Backoff(c.RetryWaitMin, c.RetryWaitMax, i, resp)
755755
if logger != nil {
756-
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
756+
desc := fmt.Sprintf("%s %s", req.Method, redactURL(req.URL))
757757
if resp != nil {
758758
desc = fmt.Sprintf("%s (status: %d)", desc, resp.StatusCode)
759759
}
@@ -818,11 +818,11 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
818818
// communicate why
819819
if err == nil {
820820
return nil, fmt.Errorf("%s %s giving up after %d attempt(s)",
821-
req.Method, req.URL, attempt)
821+
req.Method, redactURL(req.URL), attempt)
822822
}
823823

824824
return nil, fmt.Errorf("%s %s giving up after %d attempt(s): %w",
825-
req.Method, req.URL, attempt, err)
825+
req.Method, redactURL(req.URL), attempt, err)
826826
}
827827

828828
// Try to read the response body so we can reuse this connection.
@@ -903,3 +903,17 @@ func (c *Client) StandardClient() *http.Client {
903903
Transport: &RoundTripper{Client: c},
904904
}
905905
}
906+
907+
// Taken from url.URL#Redacted() which was introduced in go 1.15.
908+
// We can switch to using it directly if we'll bump the minimum required go version.
909+
func redactURL(u *url.URL) string {
910+
if u == nil {
911+
return ""
912+
}
913+
914+
ru := *u
915+
if _, has := ru.User.Password(); has {
916+
ru.User = url.UserPassword(ru.User.Username(), "xxxxx")
917+
}
918+
return ru.String()
919+
}

client_test.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -481,17 +481,32 @@ func TestClient_Do_fails(t *testing.T) {
481481
}))
482482
defer ts.Close()
483483

484+
serverUrlWithBasicAuth, err := url.Parse(ts.URL)
485+
if err != nil {
486+
t.Fatalf("failed parsing test server url: %s", ts.URL)
487+
}
488+
serverUrlWithBasicAuth.User = url.UserPassword("user", "pasten")
489+
484490
tests := []struct {
491+
url string
485492
name string
486493
cr CheckRetry
487494
err string
488495
}{
489496
{
497+
url: ts.URL,
490498
name: "default_retry_policy",
491499
cr: DefaultRetryPolicy,
492500
err: "giving up after 3 attempt(s)",
493501
},
494502
{
503+
url: serverUrlWithBasicAuth.String(),
504+
name: "default_retry_policy_url_with_basic_auth",
505+
cr: DefaultRetryPolicy,
506+
err: redactURL(serverUrlWithBasicAuth) + " giving up after 3 attempt(s)",
507+
},
508+
{
509+
url: ts.URL,
495510
name: "error_propagated_retry_policy",
496511
cr: ErrorPropagatedRetryPolicy,
497512
err: "giving up after 3 attempt(s): unexpected HTTP status 500 Internal Server Error",
@@ -508,15 +523,15 @@ func TestClient_Do_fails(t *testing.T) {
508523
client.RetryMax = 2
509524

510525
// Create the request
511-
req, err := NewRequest("POST", ts.URL, nil)
526+
req, err := NewRequest("POST", tt.url, nil)
512527
if err != nil {
513528
t.Fatalf("err: %v", err)
514529
}
515530

516531
// Send the request.
517532
_, err = client.Do(req)
518533
if err == nil || !strings.HasSuffix(err.Error(), tt.err) {
519-
t.Fatalf("expected giving up error, got: %#v", err)
534+
t.Fatalf("expected %#v, got: %#v", tt.err, err)
520535
}
521536
})
522537
}

0 commit comments

Comments
 (0)