From 8303a2c103335f9f82650fcd44713e7163537fb4 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Wed, 12 May 2021 22:14:30 +0200 Subject: [PATCH] logs: redact in URL passwords So if we have URL like this one http://user:password@127.0.0.1/ the password is not logged --- client.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index adbdd92..97f887e 100644 --- a/client.go +++ b/client.go @@ -546,9 +546,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()) } } @@ -599,9 +599,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, @@ -636,7 +636,7 @@ func (c *Client) Do(req *Request) (*http.Response, error) { } wait := c.Backoff(c.RetryWaitMin, c.RetryWaitMax, i, resp) - desc := fmt.Sprintf("%s %s", req.Method, req.URL) + desc := fmt.Sprintf("%s %s", req.Method, req.URL.Redacted()) if code > 0 { desc = fmt.Sprintf("%s (status: %d)", desc, code) } @@ -687,11 +687,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.