From 4cbef205698b532791ccf881bc100f7c57f1d9cf Mon Sep 17 00:00:00 2001 From: Irbe Krumina Date: Tue, 26 Mar 2024 17:20:32 +0100 Subject: [PATCH] cmd/k8s-operator: redact auth key from debug logs (#11523) Updates#cleanup Signed-off-by: Irbe Krumina --- cmd/k8s-operator/sts.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/cmd/k8s-operator/sts.go b/cmd/k8s-operator/sts.go index 8534ef7d2..67cae4393 100644 --- a/cmd/k8s-operator/sts.go +++ b/cmd/k8s-operator/sts.go @@ -34,6 +34,7 @@ import ( "tailscale.com/net/netutil" "tailscale.com/tailcfg" "tailscale.com/types/opt" + "tailscale.com/types/ptr" "tailscale.com/util/dnsname" "tailscale.com/util/mak" ) @@ -352,12 +353,12 @@ func (a *tailscaleSTSReconciler) createOrGetSecret(ctx context.Context, logger * } if orig != nil { - logger.Debugf("patching existing state Secret with values %s", secret.Data[tailscaledConfigKey]) + logger.Debugf("patching the existing proxy Secret with tailscaled config %s", sanitizeConfigBytes(secret.Data[tailscaledConfigKey])) if err := a.Patch(ctx, secret, client.MergeFrom(orig)); err != nil { return "", "", err } } else { - logger.Debugf("creating new state Secret with authkey %s", secret.Data[tailscaledConfigKey]) + logger.Debugf("creating a new Secret for the proxy with tailscaled config %s", sanitizeConfigBytes([]byte(secret.StringData[tailscaledConfigKey]))) if err := a.Create(ctx, secret); err != nil { return "", "", err } @@ -365,6 +366,23 @@ func (a *tailscaleSTSReconciler) createOrGetSecret(ctx context.Context, logger * return secret.Name, hash, nil } +// sanitizeConfigBytes returns ipn.ConfigVAlpha in string form with redacted +// auth key. +func sanitizeConfigBytes(bs []byte) string { + c := &ipn.ConfigVAlpha{} + if err := json.Unmarshal(bs, c); err != nil { + return "invalid config" + } + if c.AuthKey != nil { + c.AuthKey = ptr.To("**redacted**") + } + sanitizedBytes, err := json.Marshal(c) + if err != nil { + return "invalid config" + } + return string(sanitizedBytes) +} + // DeviceInfo returns the device ID and hostname for the Tailscale device // associated with the given labels. func (a *tailscaleSTSReconciler) DeviceInfo(ctx context.Context, childLabels map[string]string) (id tailcfg.StableNodeID, hostname string, ips []string, err error) {