-
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.
chore: log http requests on trace level by injecting a logger
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
- Loading branch information
1 parent
4b7d6e8
commit e26b479
Showing
9 changed files
with
154 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