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 <f@lex.la>
This commit is contained in:
Aleksei Sviridkin committed 2026-11-25 12:20:03 +03:00
1 parent ba0ff0f8e8
commit 66c7ed42ed
3 files changed
+77 -78

No files matched your search

+75 -1
View File
@@ -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,
+2
View File
@@ -11,6 +11,8 @@ spec:
storageClass: ""
resources: {}
resourcesPreset: "c1.small"
users:
e2e: {}
topics:
- name: testResults
partitions: 1
@@ -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)
}
})
}
}