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 14, 2022
1 parent d2d961d commit cd8696c
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/pkg/docker/config"
"github.com/containers/image/v5/pkg/sysregistriesv2"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
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/iolimits"
"github.com/containers/image/v5/internal/private"
"github.com/containers/image/v5/manifest"
Expand Down Expand Up @@ -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
}
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 @@ -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"
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions openshift/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit cd8696c

Please sign in to comment.