Wire logging: drop auth token-exchange records, elide huge payload fields
The option.WithLogger logger also reaches cloud.google.com/go/auth, which logged its token exchange at Debug — JWT assertion and bearer token included. wireLogger now allowlists only the compute client's api request/response records at Debug (fail-closed for future SDK additions); Warn/Error pass through. String fields over 1KiB (e.g. Shielded-VM UEFI dbx blobs) are elided recursively by default; the new --gcp-wire-log-full-payloads flag restores verbatim payloads. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -9,7 +9,6 @@ package gcp
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -85,28 +84,27 @@ type Provider struct {
|
||||
api instancesAPI
|
||||
}
|
||||
|
||||
// wireLogger returns the slog logger handed to the SDK: its Debug-level
|
||||
// "api request"/"api response" records (slog Debug = +4 on the logr
|
||||
// scale) land at V(5) on top of the base's V(1) shift.
|
||||
func wireLogger(base logr.Logger) *slog.Logger {
|
||||
return slog.New(logr.ToSlogHandler(base.V(1)))
|
||||
}
|
||||
|
||||
// New builds a Provider using Application Default Credentials (workload
|
||||
// identity in-cluster, gcloud ADC locally — no key-file plumbing).
|
||||
// Deliberately untested: it dials real Google endpoints; everything below
|
||||
// it is exercised through newWithAPI.
|
||||
//
|
||||
// The injected wire logger surfaces the SDK's raw HTTP request/response
|
||||
// records at V(5); note option.WithLogger overrides the SDK's own
|
||||
// GOOGLE_SDK_GO_LOGGING_LEVEL env var, so --zap-log-level is the only knob.
|
||||
func New(ctx context.Context, pc provider.ProviderConfig) (provider.Provider, error) {
|
||||
return NewWithWireOptions(ctx, pc, WireLogOptions{})
|
||||
}
|
||||
|
||||
// NewWithWireOptions is New with explicit control over the V(5) wire
|
||||
// logging; the injected wire logger surfaces the SDK's HTTP
|
||||
// request/response records at V(5). Note option.WithLogger overrides the
|
||||
// SDK's own GOOGLE_SDK_GO_LOGGING_LEVEL env var, so --zap-log-level is
|
||||
// the only knob.
|
||||
func NewWithWireOptions(ctx context.Context, pc provider.ProviderConfig, opts WireLogOptions) (provider.Provider, error) {
|
||||
base := logf.Log.WithName("gcp").WithName("http")
|
||||
if base.V(5).Enabled() {
|
||||
logf.Log.WithName("gcp").Info(
|
||||
"GCP HTTP wire logging active — request payloads include cloud-init user-data")
|
||||
"GCP HTTP wire logging active — request payloads include cloud-init user-data",
|
||||
"fullPayloads", opts.FullPayloads)
|
||||
}
|
||||
client, err := compute.NewInstancesRESTClient(ctx, option.WithLogger(wireLogger(base)))
|
||||
client, err := compute.NewInstancesRESTClient(ctx, option.WithLogger(wireLogger(base, opts)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("creating GCP instances client: %w", err)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package gcp
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -418,7 +419,7 @@ func TestWireLogger_gatesAtV5(t *testing.T) {
|
||||
*lines = append(*lines, prefix+" "+args)
|
||||
}, funcr.Options{Verbosity: tc.verbosity})
|
||||
|
||||
slogger := wireLogger(base)
|
||||
slogger := wireLogger(base, WireLogOptions{})
|
||||
slogger.Debug("api request", "rpcName", "Insert")
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
@@ -439,13 +440,84 @@ func TestWireLogger_infoLandsAtV1(t *testing.T) {
|
||||
*lines = append(*lines, prefix+" "+args)
|
||||
}, funcr.Options{Verbosity: 1})
|
||||
|
||||
wireLogger(base).Info("hello")
|
||||
wireLogger(base, WireLogOptions{}).Info("hello")
|
||||
|
||||
if joined := strings.Join(*lines, "\n"); !strings.Contains(joined, "hello") {
|
||||
t.Errorf("slog Info should land at V(1) and be visible at verbosity 1; output:\n%s", joined)
|
||||
}
|
||||
}
|
||||
|
||||
func captureWireLogger(verbosity int, opts WireLogOptions) (*slog.Logger, *[]string) {
|
||||
lines := &[]string{}
|
||||
base := funcr.New(func(prefix, args string) {
|
||||
*lines = append(*lines, prefix+" "+args)
|
||||
}, funcr.Options{Verbosity: verbosity})
|
||||
return wireLogger(base, opts), lines
|
||||
}
|
||||
|
||||
func TestWireLogger_dropsNonAPIDebugRecords(t *testing.T) {
|
||||
t.Parallel()
|
||||
slogger, lines := captureWireLogger(9, WireLogOptions{})
|
||||
|
||||
const secret = "assertion=eyJhbGciOiJSUzI1NiJ9.SECRET"
|
||||
slogger.Debug("2LO token request", "request", map[string]any{"payload": secret})
|
||||
slogger.Debug("2LO token response", "response", map[string]any{"payload": "ya29.SECRET-TOKEN"})
|
||||
|
||||
if len(*lines) != 0 {
|
||||
t.Errorf("auth token-exchange records must be dropped; got:\n%s", strings.Join(*lines, "\n"))
|
||||
}
|
||||
|
||||
slogger.Warn("credential refresh failed")
|
||||
if joined := strings.Join(*lines, "\n"); !strings.Contains(joined, "credential refresh failed") {
|
||||
t.Errorf("non-debug SDK records should pass through; output:\n%s", joined)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWireLogger_elidesLargeFields(t *testing.T) {
|
||||
t.Parallel()
|
||||
slogger, lines := captureWireLogger(9, WireLogOptions{})
|
||||
|
||||
huge := strings.Repeat("x", 4096)
|
||||
slogger.Debug("api response", "response", map[string]any{
|
||||
"status": "200",
|
||||
"payload": map[string]any{
|
||||
"name": "proxy-abc",
|
||||
"disks": []any{map[string]any{"content": huge}},
|
||||
},
|
||||
})
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
if strings.Contains(joined, huge[:64]) {
|
||||
t.Errorf("large field not elided:\n%.500s", joined)
|
||||
}
|
||||
if !strings.Contains(joined, "[elided 4096 bytes]") {
|
||||
t.Errorf("elision marker missing:\n%s", joined)
|
||||
}
|
||||
for _, keep := range []string{"proxy-abc", "200", "api response"} {
|
||||
if !strings.Contains(joined, keep) {
|
||||
t.Errorf("small field %q lost during elision:\n%s", keep, joined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWireLogger_fullPayloadsDisablesElision(t *testing.T) {
|
||||
t.Parallel()
|
||||
slogger, lines := captureWireLogger(9, WireLogOptions{FullPayloads: true})
|
||||
|
||||
huge := strings.Repeat("y", 4096)
|
||||
slogger.Debug("api response", "response", map[string]any{"payload": huge})
|
||||
|
||||
joined := strings.Join(*lines, "\n")
|
||||
if !strings.Contains(joined, huge) {
|
||||
t.Errorf("FullPayloads should keep fields verbatim:\n%.200s", joined)
|
||||
}
|
||||
|
||||
slogger.Debug("2LO token response", "response", "ya29.SECRET")
|
||||
if joined := strings.Join(*lines, "\n"); strings.Contains(joined, "ya29.SECRET") {
|
||||
t.Error("auth records must be dropped even with FullPayloads")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogging_apiErrorKeepsHTTPDetail(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctx, lines := captureContext(1)
|
||||
|
||||
117
internal/provider/gcp/wirelog.go
Normal file
117
internal/provider/gcp/wirelog.go
Normal file
@@ -0,0 +1,117 @@
|
||||
package gcp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/go-logr/logr"
|
||||
)
|
||||
|
||||
// wireLogMaxFieldBytes is the elision threshold for string fields in wire
|
||||
// payloads: GCP responses embed multi-KB blobs (Shielded-VM UEFI dbx
|
||||
// databases, licenses) that swamp the log line without diagnostic value.
|
||||
const wireLogMaxFieldBytes = 1024
|
||||
|
||||
// WireLogOptions controls the V(5) HTTP wire logging of the GCP SDK.
|
||||
type WireLogOptions struct {
|
||||
// FullPayloads disables field elision and logs payloads verbatim.
|
||||
FullPayloads bool
|
||||
}
|
||||
|
||||
// wireLogger returns the slog logger handed to the SDK: its Debug-level
|
||||
// "api request"/"api response" records (slog Debug = +4 on the logr
|
||||
// scale) land at V(5) on top of the base's V(1) shift.
|
||||
//
|
||||
// Debug records other than the compute client's api request/response are
|
||||
// dropped entirely: the same logger propagates into the auth library,
|
||||
// whose token-exchange records contain the signed JWT assertion and the
|
||||
// bearer access token. Warnings and errors pass through.
|
||||
func wireLogger(base logr.Logger, opts WireLogOptions) *slog.Logger {
|
||||
return slog.New(&wireFilterHandler{
|
||||
inner: logr.ToSlogHandler(base.V(1)),
|
||||
fullPayloads: opts.FullPayloads,
|
||||
})
|
||||
}
|
||||
|
||||
type wireFilterHandler struct {
|
||||
inner slog.Handler
|
||||
fullPayloads bool
|
||||
}
|
||||
|
||||
func (h *wireFilterHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
return h.inner.Enabled(ctx, level)
|
||||
}
|
||||
|
||||
func (h *wireFilterHandler) Handle(ctx context.Context, rec slog.Record) error {
|
||||
if rec.Level <= slog.LevelDebug && rec.Message != "api request" && rec.Message != "api response" {
|
||||
return nil
|
||||
}
|
||||
if h.fullPayloads {
|
||||
return h.inner.Handle(ctx, rec)
|
||||
}
|
||||
elided := slog.NewRecord(rec.Time, rec.Level, rec.Message, rec.PC)
|
||||
rec.Attrs(func(a slog.Attr) bool {
|
||||
elided.AddAttrs(slog.Attr{Key: a.Key, Value: elideValue(a.Value)})
|
||||
return true
|
||||
})
|
||||
return h.inner.Handle(ctx, elided)
|
||||
}
|
||||
|
||||
func (h *wireFilterHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
return &wireFilterHandler{inner: h.inner.WithAttrs(attrs), fullPayloads: h.fullPayloads}
|
||||
}
|
||||
|
||||
func (h *wireFilterHandler) WithGroup(name string) slog.Handler {
|
||||
return &wireFilterHandler{inner: h.inner.WithGroup(name), fullPayloads: h.fullPayloads}
|
||||
}
|
||||
|
||||
func elideValue(v slog.Value) slog.Value {
|
||||
v = v.Resolve()
|
||||
switch v.Kind() {
|
||||
case slog.KindString:
|
||||
if s := v.String(); len(s) > wireLogMaxFieldBytes {
|
||||
return slog.StringValue(elisionMarker(len(s)))
|
||||
}
|
||||
return v
|
||||
case slog.KindGroup:
|
||||
attrs := v.Group()
|
||||
out := make([]slog.Attr, 0, len(attrs))
|
||||
for _, a := range attrs {
|
||||
out = append(out, slog.Attr{Key: a.Key, Value: elideValue(a.Value)})
|
||||
}
|
||||
return slog.GroupValue(out...)
|
||||
case slog.KindAny:
|
||||
return slog.AnyValue(elideAny(v.Any()))
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func elideAny(v any) any {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
if len(t) > wireLogMaxFieldBytes {
|
||||
return elisionMarker(len(t))
|
||||
}
|
||||
return t
|
||||
case map[string]any:
|
||||
out := make(map[string]any, len(t))
|
||||
for k, val := range t {
|
||||
out[k] = elideAny(val)
|
||||
}
|
||||
return out
|
||||
case []any:
|
||||
out := make([]any, len(t))
|
||||
for i, val := range t {
|
||||
out[i] = elideAny(val)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func elisionMarker(size int) string {
|
||||
return fmt.Sprintf("[elided %d bytes]", size)
|
||||
}
|
||||
Reference in New Issue
Block a user