Skip to content

Find, verify, and analyze leaked credentials

Recover logger if wrapped by a non-logging context implementation (#4406)

Miccah51d620af2c79
diff --git a/pkg/context/context.go b/pkg/context/context.go
index 62950685e..f761b2539 100644
--- a/pkg/context/context.go
+++ b/pkg/context/context.go
@@ -16,6 +16,13 @@ var (
defaultLogger logr.Logger
)
+// logEntryKey is used to store a reference to the logger in the
+// context.Context key/value bag. This is used for regaining the logger in case
+// the context is converted into a different type.
+const logEntryKey logEntryKeyT = 0
+
+type logEntryKeyT int
+
func init() {
defaultLogger, _ = log.New("context", log.WithConsoleSink(os.Stderr))
}
@@ -132,12 +139,14 @@ func Cause(ctx context.Context) error {
// the value added to the structured log values (if the key is a string).
func WithValue(parent Context, key, val any) Context {
logger := parent.Logger()
+ parentCtx := context.WithValue(parent, key, val)
if k, ok := key.(string); ok {
logger = logger.WithValues(k, val)
+ parentCtx = context.WithValue(parentCtx, logEntryKey, logger)
}
return logCtx{
log: logger,
- Context: context.WithValue(parent, key, val),
+ Context: parentCtx,
}
}
@@ -155,7 +164,7 @@ func WithValues(parent Context, keyAndVals ...any) Context {
func WithLogger(parent context.Context, logger logr.Logger) Context {
return logCtx{
log: logger,
- Context: parent,
+ Context: context.WithValue(parent, logEntryKey, logger),
}
}
@@ -163,9 +172,18 @@ func WithLogger(parent context.Context, logger logr.Logger) Context {
// is already a Context, that will be returned, otherwise a default logger will
// be added.
func AddLogger(parent context.Context) Context {
+ // If the context.Context is already a Context, return that.
if loggerCtx, ok := parent.(Context); ok {
return loggerCtx
}
+ // If the logger exists in the grab bag (and is the correct type),
+ // return that.
+ if logEntryVal := parent.Value(logEntryKey); logEntryVal != nil {
+ if logger, ok := logEntryVal.(logr.Logger); ok {
+ return WithLogger(parent, logger)
+ }
+ }
+ // Otherwise, add the default logger.
return WithLogger(parent, defaultLogger)
}
diff --git a/pkg/context/context_test.go b/pkg/context/context_test.go
index d735727e5..d4430dcba 100644
--- a/pkg/context/context_test.go
+++ b/pkg/context/context_test.go
@@ -185,3 +185,34 @@ func TestCause(t *testing.T) {
cancel(err)
assert.Equal(t, err, Cause(ctx))
}
+
+// TestBuriedLogger tests when a logging context is wrapped by a non-logging
+// implementation that we can still regain the original logger.
+func TestBuriedLogger(t *testing.T) {
+ var buffer bytes.Buffer
+ logger, sync := log.New("test",
+ log.WithConsoleSink(&buffer),
+ )
+ defer func(prevLogger logr.Logger) {
+ defaultLogger = prevLogger
+ }(defaultLogger)
+ SetDefaultLogger(logger)
+
+ // Create a context with a key/value log entry.
+ ctx := WithValue(Background(), "log", "entry")
+ // Convert it to a stdlib context.
+ stdCtx := context.WithValue(ctx, "std", "entry") //nolint:staticcheck
+ // Try to get the logger back again.
+ ctx = AddLogger(stdCtx)
+ ctx.Logger().Info("test")
+
+ assert.Nil(t, sync())
+ logs := strings.Split(strings.TrimSpace(buffer.String()), "\n")
+
+ // Logger has the key/value log entry.
+ assert.Equal(t, 1, len(logs))
+ assert.Contains(t, logs[0], `{"log": "entry"}`)
+ // Grab bag still has both values.
+ assert.Equal(t, "entry", ctx.Value("log"))
+ assert.Equal(t, "entry", ctx.Value("std"))
+}