Skip to content

Commit

Permalink
WARNING: Dump HTTP requests/responses at logrus Trace level
Browse files Browse the repository at this point in the history
WARNING: This includes credentials, if any.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Apr 5, 2024
1 parent f418820 commit cd9692d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 9 deletions.
7 changes: 4 additions & 3 deletions docker/docker_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/internal/httpdump"
"github.com/containers/image/v5/internal/iolimits"
"github.com/containers/image/v5/internal/multierr"
"github.com/containers/image/v5/internal/set"
Expand Down Expand Up @@ -630,7 +631,7 @@ func (c *dockerClient) makeRequestToResolvedURLOnce(ctx context.Context, method
}
}
logrus.Debugf("%s %s", method, resolvedURL.Redacted())
res, err := c.client.Do(req)
res, err := httpdump.DoRequest(c.client, req)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -812,7 +813,7 @@ func (c *dockerClient) getBearerTokenOAuth2(ctx context.Context, challenge chall
authReq.Header.Add("User-Agent", c.userAgent)
authReq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
logrus.Debugf("%s %s", authReq.Method, authReq.URL.Redacted())
res, err := c.client.Do(authReq)
res, err := httpdump.DoRequest(c.client, authReq)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -864,7 +865,7 @@ func (c *dockerClient) getBearerToken(ctx context.Context, challenge challenge,
authReq.Header.Add("User-Agent", c.userAgent)

logrus.Debugf("%s %s", authReq.Method, authReq.URL.Redacted())
res, err := c.client.Do(authReq)
res, err := httpdump.DoRequest(c.client, authReq)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion docker/docker_image_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"sync"

"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/internal/httpdump"
"github.com/containers/image/v5/internal/imagesource/impl"
"github.com/containers/image/v5/internal/imagesource/stubs"
"github.com/containers/image/v5/internal/iolimits"
Expand Down Expand Up @@ -501,7 +502,7 @@ func (s *dockerImageSource) getOneSignature(ctx context.Context, sigURL *url.URL
if err != nil {
return nil, false, err
}
res, err := s.c.client.Do(req)
res, err := httpdump.DoRequest(s.c.client, req)
if err != nil {
return nil, false, err
}
Expand Down
31 changes: 31 additions & 0 deletions internal/httpdump/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package httpdump

import (
"net/http"
"net/http/httputil"

"github.com/sirupsen/logrus"
)

// DoRequest (c, req) is the same as c.Do(req), but it may log the
// request and response at logrus trace level.
func DoRequest(c *http.Client, req *http.Request) (*http.Response, error) {
if logrus.IsLevelEnabled(logrus.TraceLevel) {
if dro, err := httputil.DumpRequestOut(req, false); err != nil {
logrus.Tracef("===Can not log HTTP request: %v", err)
} else {
logrus.Tracef("===REQ===\n%s\n===REQ===\n", dro)
}
}
res, err := c.Do(req)
if logrus.IsLevelEnabled(logrus.TraceLevel) {
if err != nil {
logrus.Tracef("===RES error: %v", err)
} else if dr, err := httputil.DumpResponse(res, false); err != nil {
logrus.Tracef("===Can not log HTTP response: %v", err)
} else {
logrus.Tracef("===RES===\n%s\n===RES===\n", dr)
}
}
return res, err
}
3 changes: 2 additions & 1 deletion oci/layout/oci_src.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"strconv"

"github.com/containers/image/v5/internal/httpdump"
"github.com/containers/image/v5/internal/imagesource/impl"
"github.com/containers/image/v5/internal/imagesource/stubs"
"github.com/containers/image/v5/internal/manifest"
Expand Down Expand Up @@ -186,7 +187,7 @@ func (s *ociImageSource) getExternalBlob(ctx context.Context, urls []string) (io
continue
}

resp, err := s.client.Do(req)
resp, err := httpdump.DoRequest(s.client, req)
if err != nil {
errWrap = fmt.Errorf("fetching %s failed %s: %w", u, err.Error(), errWrap)
continue
Expand Down
7 changes: 3 additions & 4 deletions openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"

"github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/internal/httpdump"
"github.com/containers/image/v5/internal/iolimits"
"github.com/containers/image/v5/version"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -95,7 +96,7 @@ func (c *openshiftClient) doRequest(ctx context.Context, method, path string, re
}

logrus.Debugf("%s %s", method, requestURL.Redacted())
res, err := c.httpClient.Do(req)
res, err := httpdump.DoRequest(c.httpClient, req)
if err != nil {
return nil, err
}
Expand All @@ -104,9 +105,7 @@ func (c *openshiftClient) doRequest(ctx context.Context, method, path string, re
if err != nil {
return nil, err
}
logrus.Debugf("Got body: %s", body)
// FIXME: Just throwing this useful information away only to try to guess later...
logrus.Debugf("Got content-type: %s", res.Header.Get("Content-Type"))
// FIXME: Just throwing Content-Type away only to try to guess later...

var status status
statusValid := false
Expand Down

0 comments on commit cd9692d

Please sign in to comment.