From 66c7ed42edcb41e5bac5202db776c825ab6c2ff8 Mon Sep 17 00:00:00 2001 From: Aleksei Sviridkin Date: Wed, 25 Nov 2026 12:20:03 +0300 Subject: [PATCH] test(kafka): check tenant-visible Strimzi Secrets in the e2e suite Which Secrets of a Kafka cluster a tenant can read depends on the labels the Strimzi operators write onto them at run time, matched against the kafka ApplicationDefinition's selectors. A unit test in the shared lineage webhook package read the kafka-rd YAML and fed it hand-typed Strimzi labels, so it pinned one application's selectors against a guess of what the operator writes, from a package that should know neither. The kafka suite now creates one SCRAM user and asserts on the deployed Secrets: the user's password Secret and both CA certificates carry tenantresource=true, and no tenant-visible Secret of the cluster holds a private key. The second check goes by content rather than by name, so it covers the CA keys and the per-node key Secrets, whose names follow the node pools, and does not break when Strimzi labels another certificate Secret. The generic selector matching stays covered by the webhook package's matcher tests. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin --- hack/e2e-chainsaw/kafka/chainsaw-test.yaml | 76 +++++++++++++++++- hack/e2e-chainsaw/kafka/kafka.yaml | 2 + .../kafka_rd_secrets_test.go | 77 ------------------- 3 files changed, 77 insertions(+), 78 deletions(-) delete mode 100644 internal/lineagecontrollerwebhook/kafka_rd_secrets_test.go diff --git a/hack/e2e-chainsaw/kafka/chainsaw-test.yaml b/hack/e2e-chainsaw/kafka/chainsaw-test.yaml index 9f0edbbe7..4b9bc6072 100644 --- a/hack/e2e-chainsaw/kafka/chainsaw-test.yaml +++ b/hack/e2e-chainsaw/kafka/chainsaw-test.yaml @@ -3,7 +3,8 @@ # The chart deploys Kafka in KRaft mode (no ZooKeeper): a combined-free layout # of separate broker + controller KafkaNodePools. This test asserts the KRaft # topology renders and converges (both node pools present, the Strimzi Kafka CR -# is Ready) and that no ZooKeeper resources remain. +# is Ready) and that no ZooKeeper resources remain, and that a tenant sees a +# user's password and the two CA certificates but no Strimzi private key. # The last step deletes the app itself and checks that no node pool or Strimzi # pod of this cluster survives the uninstall. apiVersion: chainsaw.kyverno.io/v1alpha1 @@ -136,6 +137,79 @@ spec: name: kafka-test-kafka-bootstrap (subsets[0].addresses[0].ip != null): true + # A tenant reads the Secrets the lineage webhook stamped tenantresource=true + # from the kafka ApplicationDefinition's selectors. Strimzi writes every + # Secret of this cluster, so what it labels them with only exists here. + - name: verify-tenant-secrets + try: + # Positive controls first: the private-key check below is also satisfied + # by a webhook that stamped nothing at all. + - assert: + timeout: 2m + resource: + apiVersion: kafka.strimzi.io/v1 + kind: KafkaUser + metadata: + name: kafka-test.e2e + status: + (conditions[?type == 'Ready']): + - status: "True" + - assert: + timeout: 2m + resource: + apiVersion: v1 + kind: Secret + metadata: + name: kafka-test.e2e + labels: + internal.cozystack.io/tenantresource: "true" + - assert: + timeout: 2m + resource: + apiVersion: v1 + kind: Secret + metadata: + name: kafka-test-clients-ca-cert + labels: + internal.cozystack.io/tenantresource: "true" + - assert: + timeout: 2m + resource: + apiVersion: v1 + kind: Secret + metadata: + name: kafka-test-cluster-ca-cert + labels: + internal.cozystack.io/tenantresource: "true" + # Strimzi writes every private key under a data key ending in .key: the + # CA keys and one key-bearing Secret per node, named after the node pools. + # No Secret of this cluster holding one may be tenant-visible, whatever + # Strimzi names it. Which certificate Secrets are labelled is left open. + - script: + timeout: 1m + content: | + set -eu + data_keys() { + kubectl -n "$NAMESPACE" --request-timeout=15s get secret -l "$1" \ + -o go-template='{{range .items}}{{$n := .metadata.name}}{{range $k, $v := .data}}{{$n}} {{$k}}{{"\n"}}{{end}}{{end}}' + } + all=$(data_keys strimzi.io/cluster=kafka-test) + # Positive control for the predicate: without it, a Strimzi that + # stopped naming keys *.key would pass the check below vacuously. + if ! printf '%s\n' "$all" | grep -q '\.key$'; then + echo "no Secret of kafka-test holds a *.key entry; the private-key predicate no longer matches what Strimzi writes" >&2 + exit 1 + fi + visible=$(data_keys strimzi.io/cluster=kafka-test,internal.cozystack.io/tenantresource=true) + leaked=$(printf '%s\n' "$visible" | grep '\.key$' || true) + if [ -n "$leaked" ]; then + echo "tenant-visible Secrets of kafka-test hold private keys:" >&2 + printf '%s\n' "$leaked" >&2 + exit 1 + fi + check: + ($error == null): true + # The node pools are created by the migration hook, outside the release # manifest, and each owns the StrimziPodSet running its brokers, so only the # chart's pre-delete hook removes them. Deleting the app explicitly here, diff --git a/hack/e2e-chainsaw/kafka/kafka.yaml b/hack/e2e-chainsaw/kafka/kafka.yaml index 3c107f27b..dcbfe4757 100644 --- a/hack/e2e-chainsaw/kafka/kafka.yaml +++ b/hack/e2e-chainsaw/kafka/kafka.yaml @@ -11,6 +11,8 @@ spec: storageClass: "" resources: {} resourcesPreset: "c1.small" + users: + e2e: {} topics: - name: testResults partitions: 1 diff --git a/internal/lineagecontrollerwebhook/kafka_rd_secrets_test.go b/internal/lineagecontrollerwebhook/kafka_rd_secrets_test.go deleted file mode 100644 index 9029a64db..000000000 --- a/internal/lineagecontrollerwebhook/kafka_rd_secrets_test.go +++ /dev/null @@ -1,77 +0,0 @@ -package lineagecontrollerwebhook - -import ( - "context" - "os" - "testing" - - cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" - "sigs.k8s.io/yaml" -) - -// TestKafkaRDSecrets_KafkaUserCredentialsAreTenantResources runs the Secrets -// the Strimzi User Operator writes for a Kafka instance through the Kafka -// ApplicationDefinition's selectors, as computeLabels does. A user's password -// Secret must be a tenant resource, or the tenant-secret API hides the -// credentials external clients need. A selector whose label value carried a -// template, such as strimzi.io/cluster: kafka-{{ .name }}, never matched, -// since only resourceNames are rendered. -func TestKafkaRDSecrets_KafkaUserCredentialsAreTenantResources(t *testing.T) { - raw, err := os.ReadFile("../../packages/system/kafka-rd/cozyrds/kafka.yaml") - if err != nil { - t.Fatalf("read kafka ApplicationDefinition: %v", err) - } - var appDef cozyv1alpha1.ApplicationDefinition - if err := yaml.Unmarshal(raw, &appDef); err != nil { - t.Fatalf("parse kafka ApplicationDefinition: %v", err) - } - - // The context computeLabels builds for a Secret owned by the Kafka - // application "events" in tenant-test. - templateContext := map[string]string{"kind": "kafka", "name": "events", "namespace": "tenant-test"} - - cases := []struct { - name string - objName string - labels map[string]string - want bool - }{ - { - name: "a KafkaUser's generated credentials", - objName: "kafka-events.app", - labels: map[string]string{ - "strimzi.io/cluster": "kafka-events", - "strimzi.io/kind": "KafkaUser", - "app.kubernetes.io/instance": "app", - "app.kubernetes.io/managed-by": "strimzi-user-operator", - }, - want: true, - }, - { - name: "the clients CA certificate", - objName: "kafka-events-clients-ca-cert", - labels: map[string]string{"strimzi.io/cluster": "kafka-events", "strimzi.io/kind": "Kafka"}, - want: true, - }, - { - name: "the clients CA private key", - objName: "kafka-events-clients-ca", - labels: map[string]string{"strimzi.io/cluster": "kafka-events", "strimzi.io/kind": "Kafka"}, - want: false, - }, - { - name: "the broker certificates", - objName: "kafka-events-kafka-brokers", - labels: map[string]string{"strimzi.io/cluster": "kafka-events", "strimzi.io/kind": "Kafka"}, - want: false, - }, - } - for _, tc := range cases { - t.Run(tc.name, func(t *testing.T) { - got := matchResourceToExcludeInclude(context.Background(), tc.objName, templateContext, tc.labels, &appDef.Spec.Secrets) - if got != tc.want { - t.Errorf("tenant resource = %v, want %v", got, tc.want) - } - }) - } -}