-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): log http requests for OCI and docker based on trace level …
…by injecting a logger (#1118) <!-- markdownlint-disable MD041 --> #### What this PR does / why we need it it is now possible to inject a trace attribute to OCMs logging architecture to allow tracing back HTTP calls for oci registries and the docker client: ``` ocm --logkeys /+ocm/oci=trace [YOUR COMMAND INTERACTING WITH OCI HERE] ``` Note that this does not take care of all access types yet because we dont have a unified http.Client. Note that it is now also possible to pass `/+ocm/docker=trace` to enable logging for the docker client infrastructure, or set `--loglevel=trace` to get a full tracelog with HTTP statements. Authorization Headers are redacted #### Which issue(s) this PR fixes <!-- Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`. --> This allows introspecting HTTP calls for debugging purposes, e.g. ``` bin/ocm --logkeys /+ocm/oci=trace transfer artifact CommonTransportFormat::XXX/ocm/gen/ctf//component-descriptors/ocm.software/ocmcli ghcr.io/jakobmoellerdev/ocm:latest copying CommonTransportFormat::XXX/ocm/gen/ctf//component-descriptors/ocm.software/ocmcli:0.17.0-dev to ghcr.io/jakobmoellerdev/ocm:latest... 2024-11-20T19:09:14+01:00 trace [ocm/oci/ocireg] roundtrip header="{\"Accept\":[\"application/vnd.ocm.software.component.config.v1+json, */*\"],\"User-Agent\":[\"containerd/1.7.23+unknown\"]}" host=ghcr.io method=HEAD namespace=jakobmoellerdev/ocm url=https://ghcr.io/v2/jakobmoellerdev/ocm/blobs/sha256:0351859d79ce5900e610226ed03ab0fb5586f4f19b26693487bf13fac1ce6923 2024-11-20T19:09:14+01:00 trace [ocm/oci/ocireg] "query credentials" host=ghcr.io namespace=jakobmoellerdev/ocm pass="***" user=jakobmoellerdev ```
- Loading branch information
1 parent
1d117ff
commit 10f26eb
Showing
9 changed files
with
140 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package docker | ||
|
||
import ocmlog "ocm.software/ocm/api/utils/logging" | ||
|
||
var REALM = ocmlog.DefineSubRealm("Docker repository handling", "oci", "docker") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package ocireg | ||
|
||
import ( | ||
ocmlog "ocm.software/ocm/api/utils/logging" | ||
) | ||
import ocmlog "ocm.software/ocm/api/utils/logging" | ||
|
||
var REALM = ocmlog.DefineSubRealm("OCI repository handling", "oci", "ocireg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package logging | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/mandelsoft/logging" | ||
) | ||
|
||
func NewRoundTripper(rt http.RoundTripper, logger logging.Logger) *RoundTripper { | ||
return &RoundTripper{ | ||
logger: logger, | ||
RoundTripper: rt, | ||
} | ||
} | ||
|
||
// RoundTripper is a http.RoundTripper that logs requests. | ||
type RoundTripper struct { | ||
logger logging.Logger | ||
http.RoundTripper | ||
} | ||
|
||
func (t *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
// Redact the Authorization header to make sure it doesn't get logged at any point. | ||
header := req.Header | ||
if key := "Authorization"; req.Header.Get(key) != "" { | ||
header = header.Clone() | ||
header.Set(key, "***") | ||
} | ||
|
||
t.logger.Trace("roundtrip", | ||
"url", req.URL, | ||
"method", req.Method, | ||
"header", header, | ||
) | ||
return t.RoundTripper.RoundTrip(req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package logging_test | ||
|
||
import ( | ||
"bytes" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
logcfg "github.com/mandelsoft/logging/config" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/tonglil/buflogr" | ||
|
||
"github.com/mandelsoft/logging" | ||
|
||
local "ocm.software/ocm/api/utils/logging" | ||
) | ||
|
||
var _ = Describe("RoundTripper", func() { | ||
var buf bytes.Buffer | ||
var ctx *local.StaticContext | ||
var roundTripper http.RoundTripper | ||
var server *httptest.Server | ||
|
||
BeforeEach(func() { | ||
buf.Reset() | ||
local.SetContext(logging.NewDefault()) | ||
ctx = local.Context() | ||
ctx.SetBaseLogger(buflogr.NewWithBuffer(&buf)) | ||
}) | ||
|
||
AfterEach(func() { | ||
if server != nil { | ||
server.Close() | ||
} | ||
}) | ||
|
||
It("redacts Authorization header", func() { | ||
r := logcfg.ConditionalRule("trace") | ||
cfg := &logcfg.Config{ | ||
Rules: []logcfg.Rule{r}, | ||
} | ||
Expect(ctx.Configure(cfg)).To(Succeed()) | ||
|
||
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})) | ||
|
||
roundTripper = local.NewRoundTripper(http.DefaultTransport, ctx.Logger()) | ||
client := &http.Client{Transport: roundTripper} | ||
|
||
req, err := http.NewRequest("GET", server.URL, nil) | ||
Expect(err).NotTo(HaveOccurred()) | ||
req.Header.Set("Authorization", "this should be redacted") | ||
|
||
_, err = client.Do(req) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
Expect(buf.String()).To(ContainSubstring("roundtrip")) | ||
Expect(buf.String()).To(ContainSubstring("url")) | ||
Expect(buf.String()).To(ContainSubstring("method")) | ||
Expect(buf.String()).To(ContainSubstring("header")) | ||
Expect(buf.String()).To(ContainSubstring("***")) | ||
Expect(buf.String()).NotTo(ContainSubstring("this should be redacted")) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters