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) - } - }) - } -}