From afb1bbab76f1011db2fe9384ecd2438ed421a6e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20Trma=C4=8D?= Date: Sat, 7 Jan 2017 00:18:14 +0100 Subject: [PATCH] WARNING: Dump HTTP requests/responses at logrus Trace level MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit WARNING: This includes credentials, if any. Signed-off-by: Miloslav Trmač --- docker/docker_client.go | 7 ++++--- docker/docker_image_src.go | 3 ++- internal/httpdump/dump.go | 31 +++++++++++++++++++++++++++++++ oci/layout/oci_src.go | 3 ++- openshift/openshift.go | 7 +++---- 5 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 internal/httpdump/dump.go diff --git a/docker/docker_client.go b/docker/docker_client.go index 2f4480fee8..6a99a47941 100644 --- a/docker/docker_client.go +++ b/docker/docker_client.go @@ -18,6 +18,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/useragent" "github.com/containers/image/v5/manifest" @@ -615,7 +616,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 } @@ -726,7 +727,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 } @@ -778,7 +779,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 a115268de3..48962ebb99 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/imagesource/impl" "github.com/containers/image/v5/internal/imagesource/stubs" "github.com/containers/image/v5/internal/iolimits" @@ -495,7 +496,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 } 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 408af20a42..2eaa02b17a 100644 --- a/oci/layout/oci_src.go +++ b/oci/layout/oci_src.go @@ -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/private" @@ -185,7 +186,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 diff --git a/openshift/openshift.go b/openshift/openshift.go index f3d5662e66..37b7c5a878 100644 --- a/openshift/openshift.go +++ b/openshift/openshift.go @@ -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" @@ -91,7 +92,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 } @@ -100,9 +101,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