diff --git a/docker/docker_client.go b/docker/docker_client.go index d984db718e..fc5446e671 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -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/pkg/docker/config" "github.com/containers/image/v5/pkg/sysregistriesv2" @@ -557,7 +558,7 @@ func (c *dockerClient) makeRequestToResolvedURLOnce(ctx context.Context, method } } logrus.Debugf("%s %s", method, url.Redacted()) - res, err := c.client.Do(req) + res, err := httpdump.DoRequest(c.client, req) if err != nil { return nil, err } @@ -657,7 +658,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 } @@ -709,7 +710,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 } diff --git a/docker/docker_image_src.go b/docker/docker_image_src.go index c8e176f904..c3805f8b9b 100644 --- a/docker/docker_image_src.go +++ b/docker/docker_image_src.go @@ -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/iolimits" "github.com/containers/image/v5/internal/private" "github.com/containers/image/v5/manifest" @@ -529,7 +530,7 @@ func (s *dockerImageSource) getOneSignature(ctx context.Context, url *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 } diff --git a/internal/httpdump/dump.go b/internal/httpdump/dump.go new file mode 100644 index 0000000000..d1e2c3a4ac --- /dev/null +++ b/internal/httpdump/dump.go @@ -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 +} diff --git a/oci/layout/oci_src.go b/oci/layout/oci_src.go index 8973f461c9..9c451ce50a 100644 --- a/oci/layout/oci_src.go +++ b/oci/layout/oci_src.go @@ -8,6 +8,7 @@ import ( "os" "strconv" + "github.com/containers/image/v5/internal/httpdump" "github.com/containers/image/v5/manifest" "github.com/containers/image/v5/pkg/tlsclientconfig" "github.com/containers/image/v5/types" @@ -166,7 +167,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 = errors.Wrapf(errWrap, "fetching %s failed %s", u, err.Error()) continue diff --git a/openshift/openshift.go b/openshift/openshift.go index 67612d800c..b52e20abcf 100644 --- a/openshift/openshift.go +++ b/openshift/openshift.go @@ -13,6 +13,7 @@ import ( "github.com/containers/image/v5/docker" "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/manifest" "github.com/containers/image/v5/types" @@ -96,7 +97,7 @@ func (c *openshiftClient) doRequest(ctx context.Context, method, path string, re } logrus.Debugf("%s %s", method, url.Redacted()) - res, err := c.httpClient.Do(req) + res, err := httpdump.DoRequest(c.httpClient, req) if err != nil { return nil, err } @@ -105,9 +106,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