From 3492c13a596919b672d08cb96c0a179aaa802e03 Mon Sep 17 00:00:00 2001 From: MuhammadFaizan-prog <130775084+MuhammadFaizan-prog@users.noreply.github.com> Date: Sat, 14 Nov 2026 19:43:20 +0000 Subject: [PATCH] [MeshSync] Redact Secret values in the last-applied-configuration annotation With MESHSYNC_REDACT_SECRETS set, ParseList redacted a Secret's data, stringData and binaryData but published the kubectl last-applied-configuration annotation unchanged, and that annotation holds a copy of the same values. Redact them there too, keeping keys and the rest of the manifest; a value that is not a JSON object is replaced whole. Fixes #618 Signed-off-by: MuhammadFaizan-prog <130775084+MuhammadFaizan-prog@users.noreply.github.com> --- pkg/model/model_converter.go | 72 +++++++++++++++++---- pkg/model/model_converter_test.go | 103 ++++++++++++++++++++++++++++++ 2 files changed, 162 insertions(+), 13 deletions(-) diff --git a/pkg/model/model_converter.go b/pkg/model/model_converter.go index f02d187f..7bada4bb 100644 --- a/pkg/model/model_converter.go +++ b/pkg/model/model_converter.go @@ -31,6 +31,11 @@ const redactedSecretPlaceholder = "[REDACTED]" // secretKind is the Kubernetes Kind for which Secret redaction applies. const secretKind = "Secret" +// lastAppliedConfigAnnotation is the annotation kubectl apply writes with the +// full manifest it applied. For a Secret that manifest includes data, +// stringData and binaryData, so it has to be redacted along with them. +const lastAppliedConfigAnnotation = "kubectl.kubernetes.io/last-applied-configuration" + // redactSecretsEnabled reports whether Secret redaction has been opted into via // the MESHSYNC_REDACT_SECRETS environment variable. It mirrors the truthy // parsing used elsewhere in MeshSync (see DEBUG handling in main.go). @@ -67,6 +72,45 @@ func redactSecretData(raw []byte) string { return string(out) } +// annotationValue returns an annotation value as it should be published. When +// Secret redaction is on, the kubectl last-applied-configuration annotation is +// redacted, since it holds a copy of the Secret's data, stringData and +// binaryData. Every other annotation is returned unchanged. +func annotationValue(key, value string, redactSecrets bool) string { + if redactSecrets && key == lastAppliedConfigAnnotation { + return redactLastAppliedConfiguration(value) + } + return value +} + +// redactLastAppliedConfiguration redacts the Secret values inside a stored +// last-applied-configuration manifest with redactSecretData, keeping the keys +// and the rest of the manifest. A value that is not a JSON object cannot be +// checked, so it is replaced whole with redactedSecretPlaceholder. +func redactLastAppliedConfiguration(value string) string { + var manifest map[string]json.RawMessage + if err := json.Unmarshal([]byte(value), &manifest); err != nil || manifest == nil { + return redactedSecretPlaceholder + } + for _, field := range []string{"data", "stringData", "binaryData"} { + raw, ok := manifest[field] + if !ok { + continue + } + var kv map[string]json.RawMessage + if err := json.Unmarshal(raw, &kv); err != nil || kv == nil { + manifest[field] = json.RawMessage(`"` + redactedSecretPlaceholder + `"`) + continue + } + manifest[field] = json.RawMessage(redactSecretData(raw)) + } + out, err := json.Marshal(manifest) + if err != nil { + return redactedSecretPlaceholder + } + return string(out) +} + // jsonStringValue returns the decoded form of a value returned by jsonparser. // jsonparser returns string values without the surrounding quotes but with // JSON escape sequences (\", \n, \\, \uXXXX) still in place, so they are @@ -97,6 +141,20 @@ func ParseList( _ = json.Unmarshal(data, &result) processorInstance := GetProcessorInstance(result.Kind) + + // Secret contents (data/stringData/binaryData) are captured and shipped + // over the broker. Meshery Server may consume this data, so the default + // behavior is unchanged (values pass through as-is). When an operator opts + // in via MESHSYNC_REDACT_SECRETS, Secret values are redacted here while the + // keys are preserved, keeping plaintext/base64 secrets from leaving the + // cluster. Redaction applies to Secret resources only; ConfigMap data is + // left untouched. The kubectl last-applied-configuration annotation, which + // holds a copy of the applied manifest, is redacted the same way. + // result.Kind is checked first so redactSecretsEnabled() (which reads the + // environment) is only evaluated for Secret resources, not on the hot path + // for every non-Secret resource. + redactSecrets := result.Kind == secretKind && redactSecretsEnabled() + // ObjectMeta internal models labels := make([]*KubernetesKeyValue, 0) _ = jsonparser.ObjectEach(data, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error { @@ -120,7 +178,7 @@ func ParseList( annotations = append(annotations, &KubernetesKeyValue{ Kind: KindAnnotation, Key: string(key), - Value: jsonStringValue(value, dataType), + Value: annotationValue(string(key), jsonStringValue(value, dataType), redactSecrets), }) return nil }, "metadata", "annotations") @@ -156,18 +214,6 @@ func ParseList( result.Immutable = string(immutable) } - // Secret contents (data/stringData/binaryData) are captured and shipped - // over the broker. Meshery Server may consume this data, so the default - // behavior is unchanged (values pass through as-is). When an operator opts - // in via MESHSYNC_REDACT_SECRETS, Secret values are redacted here while the - // keys are preserved, keeping plaintext/base64 secrets from leaving the - // cluster. Redaction applies to Secret resources only; ConfigMap data is - // left untouched. - // result.Kind is checked first so redactSecretsEnabled() (which reads the - // environment) is only evaluated for Secret resources, not on the hot path - // for every non-Secret resource. - redactSecrets := result.Kind == secretKind && redactSecretsEnabled() - if objData, _, _, err := jsonparser.Get(data, "data"); err == nil { if redactSecrets { result.Data = redactSecretData(objData) diff --git a/pkg/model/model_converter_test.go b/pkg/model/model_converter_test.go index ce1b599f..3d963766 100644 --- a/pkg/model/model_converter_test.go +++ b/pkg/model/model_converter_test.go @@ -205,6 +205,109 @@ func TestParseList_RedactionEnabledLeavesConfigMapUntouched(t *testing.T) { }) } +const testSecretLastApplied = `{"apiVersion":"v1","kind":"Secret","metadata":{"name":"db-creds","namespace":"default"},` + + `"type":"Opaque","data":{"password":"cGFzc3dvcmQ="},"stringData":{"token":"plaintext-token"},"binaryData":{"cert":"YmluYXJ5"}}` + +// newSecretWithLastApplied returns a Secret as kubectl apply leaves it, with +// the applied manifest stored in the last-applied-configuration annotation. +func newSecretWithLastApplied(annotation string) unstructured.Unstructured { + return newUnstructured(map[string]interface{}{ + "apiVersion": "v1", + "kind": "Secret", + "metadata": map[string]interface{}{ + "name": "db-creds", + "namespace": "default", + "annotations": map[string]interface{}{ + lastAppliedConfigAnnotation: annotation, + "team": "payments", + }, + }, + "type": "Opaque", + "data": map[string]interface{}{ + "password": "cGFzc3dvcmQ=", + }, + }) +} + +func annotationValues(result KubernetesResource) map[string]string { + values := map[string]string{} + for _, a := range result.KubernetesResourceMeta.Annotations { + values[a.Key] = a.Value + } + return values +} + +// TestParseList_SecretRedactionCoversLastAppliedConfiguration asserts that +// redaction also reaches the kubectl.kubernetes.io/last-applied-configuration +// annotation. kubectl apply stores the whole applied manifest there, so without +// this the Secret's data, and its stringData in plain text, would still be +// published in the annotation after the data fields themselves are redacted. +func TestParseList_SecretRedactionCoversLastAppliedConfiguration(t *testing.T) { + t.Setenv(redactSecretsEnvKey, "true") + + annotations := annotationValues(ParseList(newSecretWithLastApplied(testSecretLastApplied), broker.Add, "cluster-1")) + got := annotations[lastAppliedConfigAnnotation] + + for _, secret := range []string{"cGFzc3dvcmQ=", "plaintext-token", "YmluYXJ5"} { + if strings.Contains(got, secret) { + t.Errorf("last-applied-configuration still contains Secret value %q: %s", secret, got) + } + } + + var manifest map[string]json.RawMessage + if err := json.Unmarshal([]byte(got), &manifest); err != nil { + t.Fatalf("redacted annotation is not valid JSON: %v (%q)", err, got) + } + assertJSONEqual(t, string(manifest["data"]), map[string]string{"password": redactedSecretPlaceholder}) + assertJSONEqual(t, string(manifest["stringData"]), map[string]string{"token": redactedSecretPlaceholder}) + assertJSONEqual(t, string(manifest["binaryData"]), map[string]string{"cert": redactedSecretPlaceholder}) + if string(manifest["kind"]) != `"Secret"` || string(manifest["type"]) != `"Opaque"` { + t.Errorf("non-secret fields of the manifest should be kept, got %s", got) + } + if annotations["team"] != "payments" { + t.Errorf("other annotations should be untouched, got %q", annotations["team"]) + } +} + +func TestParseList_SecretRedactionReplacesUnparseableLastApplied(t *testing.T) { + t.Setenv(redactSecretsEnvKey, "true") + + annotations := annotationValues(ParseList(newSecretWithLastApplied(`not json, password=hunter2`), broker.Add, "cluster-1")) + if got := annotations[lastAppliedConfigAnnotation]; got != redactedSecretPlaceholder { + t.Errorf("got %q, want %q", got, redactedSecretPlaceholder) + } +} + +func TestParseList_LastAppliedUnchangedWithoutRedaction(t *testing.T) { + t.Setenv(redactSecretsEnvKey, "") + + annotations := annotationValues(ParseList(newSecretWithLastApplied(testSecretLastApplied), broker.Add, "cluster-1")) + if got := annotations[lastAppliedConfigAnnotation]; got != testSecretLastApplied { + t.Errorf("annotation should pass through unchanged, got %q", got) + } +} + +func TestParseList_RedactionLeavesConfigMapLastAppliedUntouched(t *testing.T) { + t.Setenv(redactSecretsEnvKey, "true") + + const cmLastApplied = `{"apiVersion":"v1","kind":"ConfigMap","data":{"log_level":"debug"}}` + obj := newUnstructured(map[string]interface{}{ + "apiVersion": "v1", + "kind": "ConfigMap", + "metadata": map[string]interface{}{ + "name": "app-config", + "namespace": "default", + "annotations": map[string]interface{}{lastAppliedConfigAnnotation: cmLastApplied}, + }, + "data": map[string]interface{}{"log_level": "debug"}, + }) + + annotations := annotationValues(ParseList(obj, broker.Add, "cluster-1")) + if got := annotations[lastAppliedConfigAnnotation]; got != cmLastApplied { + t.Errorf("ConfigMap annotation should pass through unchanged, got %q", got) + } +} + func TestRedactSecretsEnabled(t *testing.T) { cases := []struct { value string