test(kafka): check tenant-visible Strimzi Secrets in the e2e suite (#5424)

## What this PR does

Moves the check of which Kafka Secrets a tenant can read from a unit
test in the shared lineage webhook package into the kafka e2e suite.

`internal/lineagecontrollerwebhook/kafka_rd_secrets_test.go` read the
raw `kafka-rd` ApplicationDefinition and ran it against Strimzi labels
typed by hand. The selectors are kafka's, and the labels are whatever
the Strimzi User and Cluster Operators write at run time, so the test
pinned one application's configuration against a guess of operator
behaviour, from a package that should know neither. The generic part,
how `matchLabels` and `resourceNames` select an object, is already
covered by `matcher_test.go` in the same package.

The kafka suite now creates one SCRAM user and checks the deployed
Secrets. First, the user's password Secret and both CA certificate
Secrets must carry `internal.cozystack.io/tenantresource=true`. Then no
tenant-visible Secret of the cluster may hold a `*.key` entry, which is
where Strimzi puts every private key. This check goes by content, so it
covers the CA keys and the per-node key Secrets, whose names follow the
node pools, and it does not break if a later Strimzi labels one more
certificate Secret. A positive control fails the step if no Secret of
the cluster holds a `*.key` entry at all, so the check cannot pass
vacuously if Strimzi changes how it names keys.

I also removed `hack/check-kafka-rd-secrets.bats`. It grepped the same
YAML for the CA certificate names, so it passed on a name that only
appeared in a comment or an exclude list. The e2e step now checks those
names against the real Secrets.

Nothing shipped changes: only tests are touched. The new e2e step has
not run anywhere yet; it is exercised for the first time by CI on this
PR. Locally I checked that the suite YAML parses, that the chart renders
the `KafkaUser` from the fixture, and that the step's shell logic
passes, fails on a leaked key and fails on the vacuous case when
`kubectl` is stubbed.

### Screenshots

No UI changes.

### Downstream repositories

- [x] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up:
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up:
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up:

### Release note

```release-note
test(kafka): check tenant-visible Strimzi Secrets in the kafka e2e suite instead of a lineage webhook unit test
```


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Tests**
* Expanded Kafka end-to-end checks to verify tenant-visible Secrets are
labeled correctly and do not expose private keys.
  * Added an end-to-end Kafka user configuration for testing.
* Removed earlier checks for Kafka certificate Secret naming and
selector behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
Aleksei Sviridkin authored and GitHub committed 2026-11-25 19:51:32 +03:00
commit 9baf43c632
4 files changed
+77 -108

No files matched your search

-30
View File
@@ -1,30 +0,0 @@
#!/usr/bin/env bats
# Unit test: kafka-rd cozyrds must reference the Strimzi-generated
# clients-ca-cert secret (not the bare clients-ca name, which does not exist).
load test_helper
REPO_ROOT="$(cd "$(dirname "${BATS_TEST_FILENAME:-$0}")/.." && pwd)"
COZYRDS="$REPO_ROOT/packages/system/kafka-rd/cozyrds/kafka.yaml"
@test "kafka-rd cozyrds references clients-ca-cert (Strimzi actual name)" {
grep -q "clients-ca-cert" "$COZYRDS"
}
@test "kafka-rd cozyrds does not reference bare clients-ca (wrong name)" {
if grep -qP "clients-ca(?!-)" "$COZYRDS"; then
echo "Found bare 'clients-ca' reference (missing '-cert' suffix)" >&2
exit 1
fi
}
@test "kafka-rd cozyrds references cluster-ca-cert (Strimzi actual name)" {
grep -q "cluster-ca-cert" "$COZYRDS"
}
@test "kafka-rd cozyrds does not reference bare cluster-ca (wrong name)" {
if grep -qP "cluster-ca(?!-)" "$COZYRDS"; then
echo "Found bare 'cluster-ca' reference (missing '-cert' suffix)" >&2
exit 1
fi
}
+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)
}
})
}
}