From 78383ad1cd9f4e9ac07b81fd2c679c6ec593aff9 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 5de0767408..4ceb967969 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/manifest" "github.com/containers/image/v5/pkg/docker/config" @@ -561,7 +562,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 } @@ -661,7 +662,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 } @@ -713,7 +714,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 b0e8779710..c5a4769718 100644 --- a/docker/docker_image_src.go +++ b/docker/docker_image_src.go @@ -15,6 +15,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" @@ -490,7 +491,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 b2d963b019..e26d0b9112 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" @@ -174,7 +175,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 b2e4dfd9e8..42e51a8783 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, url.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