From ad47521c955127dfa51b89873b4037519ff78558 Mon Sep 17 00:00:00 2001 From: Mattia Eleuteri Date: Mon, 23 Nov 2026 16:14:49 +0300 Subject: [PATCH] fix(cozystack-basics): make the tenant super-admin VirtualMachine grant read-only cozy:tenant:super-admin:base carried '*' on kubevirt.io virtualmachines. A VirtualMachine written directly is sized outside any application: it is not a HelmRelease the tenant ordered, so nothing that accounts for applications sees it, and the same verbs let a super-admin raise the guest size of a VM a VMInstance release rendered. Nothing in tree needs the write: power goes through the virtualmachines start/stop/restart subresources, console and VNC through the VMI subresources, size, disks and networking through VMInstance values. The grant arrived together with a '*' on helmreleases that was later removed; this one was missed. watch stays, because the dashboard watches the VirtualMachine for its live power state and no other tenant role grants it. The tenant roles are aggregated from ClusterRoles in several packages, so a write path can reach a tenant without touching the role that names it; the install e2e now guards against that. Assisted-by: LLM Signed-off-by: Mattia Eleuteri --- hack/e2e-install-cozystack.bats | 82 +++++++++++++++++++ hack/e2e-tenant-super-admin-write-verbs.yaml | 45 ++++++++++ .../templates/clusterroles.yaml | 9 +- .../clusterroles-tenant-super-admin_test.yaml | 32 ++++++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 hack/e2e-tenant-super-admin-write-verbs.yaml create mode 100644 packages/system/cozystack-basics/tests/clusterroles-tenant-super-admin_test.yaml diff --git a/hack/e2e-install-cozystack.bats b/hack/e2e-install-cozystack.bats index f7dda2d7b..244a8d987 100644 --- a/hack/e2e-install-cozystack.bats +++ b/hack/e2e-install-cozystack.bats @@ -512,6 +512,88 @@ EOF | grep -qx '250m 128Mi 25m 128Mi' } +@test "Tenant super-admin holds no write verb outside the allowlist" { + # cozy:tenant:super-admin is aggregated from every ClusterRole labelled for + # it or for a level below, across several packages, so a write path into a + # tenant namespace can arrive without touching the role that names it. The + # raw kubevirt.io VirtualMachine grant did: a '*' that let a tenant size a VM + # outside any application. The effective rules are read back from the + # apiserver, after aggregation, and any write verb the allowlist does not + # name fails the test; adding a line there is the statement that the write + # is accounted for. + # + # The bare system:authenticated identity is subtracted first, so what + # Kubernetes grants every user (the selfsubject* reviews) does not tie the + # list to a Kubernetes version. An allowlisted line the cluster does not + # grant is reported without failing: a lane with COZY_DISABLED_PACKAGES set + # lacks the tenant roles those packages ship. + # + # Rules and status come from the same SelfSubjectRulesReview response, so + # a partial list cannot pass on the strength of a later, complete one. + # can-i --list hides both failure modes behind exit 0: it only warns on + # incomplete, and says nothing when RBAC reports an evaluationError with + # incomplete left false. The jq rendering keeps can-i's resource column, + # which the allowlist is written in. + # + # Lines covered by a wildcard grant are folded into it, so a '*' on a whole + # group (apps.cozystack.io, the gated API) does not make every new kind an + # allowlist edit. Staged through files rather than pipes: /bin/sh here has + # no pipefail, and a failed kubectl must not pass as an empty grant list. + local complete normalize subsume review allowlist granted baseline effective unexpected missing f + complete='.status.incomplete == false and (.status.evaluationError // "") == ""' + normalize='.status.resourceRules[] | . as $r + | ($r.resourceNames // [] | if length == 0 then ["-"] else . end)[] as $name + | $r.apiGroups[] as $group | $r.resources[] as $resource | $r.verbs[] as $verb + | select($verb != "get" and $verb != "list" and $verb != "watch") + | ($resource | split("/")) as $part + | $part[0] + (if $group == "" then "" else "." + $group end) + + (if ($part | length) > 1 then "/" + ($part[1:] | join("/")) else "" end) + + " " + $name + " " + $verb' + subsume='NR == FNR { have[$0] = 1; next } + { + dot = index($1, ".") + if (dot > 1 && $1 !~ /^\*\./ && (("*" substr($1, dot) " - *") in have)) next + if ($3 != "*" && (($1 " " $2 " *") in have)) next + print + }' + review=$(mktemp); allowlist=$(mktemp); granted=$(mktemp); baseline=$(mktemp) + effective=$(mktemp); unexpected=$(mktemp); missing=$(mktemp) + echo '{"apiVersion":"authorization.k8s.io/v1","kind":"SelfSubjectRulesReview","spec":{"namespace":"tenant-test"}}' > "$review" + # --validate=false: client-side validation would run as the impersonated + # identity, which cannot list CRDs. + timeout -k 5 30 kubectl create --validate=false -f "$review" -o json \ + --as=e2e-rbac-probe --as-group=system:authenticated --as-group=tenant-test-super-admin > "$granted" + timeout -k 5 30 kubectl create --validate=false -f "$review" -o json \ + --as=e2e-rbac-probe --as-group=system:authenticated > "$baseline" + for f in "$granted" "$baseline"; do + if jq -e "$complete" "$f" > /dev/null; then :; else + echo "FAIL: the apiserver could not enumerate the tenant-test rules completely:" >&2 + jq -c '.status | {incomplete, evaluationError}' "$f" >&2 + false + fi + done + jq -r "$normalize" "$granted" > "$granted.n" + jq -r "$normalize" "$baseline" > "$baseline.n" + yq '.writeVerbs[]' hack/e2e-tenant-super-admin-write-verbs.yaml > "$allowlist" + LC_ALL=C sort -u -o "$granted.n" "$granted.n" + LC_ALL=C sort -u -o "$baseline.n" "$baseline.n" + LC_ALL=C sort -u -o "$allowlist" "$allowlist" + LC_ALL=C comm -23 "$granted.n" "$baseline.n" > "$effective.n" + awk "$subsume" "$effective.n" "$effective.n" > "$effective" + LC_ALL=C comm -23 "$effective" "$allowlist" > "$unexpected" + LC_ALL=C comm -13 "$effective" "$allowlist" > "$missing" + if [ -s "$missing" ]; then + echo "allowlisted but not granted on this cluster:" + cat "$missing" + fi + if [ -s "$unexpected" ]; then + echo "FAIL: tenant-test-super-admin holds write verbs hack/e2e-tenant-super-admin-write-verbs.yaml does not allow:" >&2 + cat "$unexpected" >&2 + false + fi + rm -f "$review" "$allowlist" "$granted" "$granted.n" "$baseline" "$baseline.n" "$effective" "$effective.n" "$unexpected" "$missing" +} + @test "Deletion-protection VAP denies delete on labeled cozystack-version ConfigMap" { # Locks down the contract delivered by packages/core/platform/templates/ # deletion-protection.yaml: a DELETE on any object carrying diff --git a/hack/e2e-tenant-super-admin-write-verbs.yaml b/hack/e2e-tenant-super-admin-write-verbs.yaml new file mode 100644 index 000000000..f267e1068 --- /dev/null +++ b/hack/e2e-tenant-super-admin-write-verbs.yaml @@ -0,0 +1,45 @@ +# Write verbs a tenant super-admin may hold in its own namespace, read by the +# "Tenant super-admin holds no write verb outside the allowlist" test in +# hack/e2e-install-cozystack.bats. One "resource names verb" entry per verb, +# as that test's jq renders the SelfSubjectRulesReview rather than as +# `kubectl auth can-i --list` prints it: the resource in can-i's +# resource-column form, "-" for no resource names, any verb other than get, +# list and watch. Adding an entry is a reviewed decision that the write is +# accounted for. apps.cozystack.io is listed whole because every write to it +# goes through the aggregated apiserver rather than straight to a workload. +writeVerbs: +- "*.apps.cozystack.io - *" +- "backupjobs.backups.cozystack.io - create" +- "backupjobs.backups.cozystack.io - delete" +- "backupjobs.backups.cozystack.io - patch" +- "backupjobs.backups.cozystack.io - update" +- "endpoints - delete" +- "events - delete" +- "persistentvolumes - delete" +- "plans.backups.cozystack.io - create" +- "plans.backups.cozystack.io - delete" +- "plans.backups.cozystack.io - patch" +- "plans.backups.cozystack.io - update" +- "pods - delete" +- "restorejobs.backups.cozystack.io - create" +- "restorejobs.backups.cozystack.io - delete" +- "restorejobs.backups.cozystack.io - patch" +- "restorejobs.backups.cozystack.io - update" +- "securitygroups.sdn.cozystack.io - create" +- "securitygroups.sdn.cozystack.io - delete" +- "securitygroups.sdn.cozystack.io - patch" +- "securitygroups.sdn.cozystack.io - update" +- "services - delete" +- "uploadtokenrequests.upload.cdi.kubevirt.io - create" +- "virtualmachineinstances.subresources.kubevirt.io/portforward - update" +- "virtualmachines.subresources.kubevirt.io/restart - update" +- "virtualmachines.subresources.kubevirt.io/start - update" +- "virtualmachines.subresources.kubevirt.io/stop - update" +- "vmimportsources.forklift.cozystack.io - create" +- "vmimportsources.forklift.cozystack.io - delete" +- "vmimportsources.forklift.cozystack.io - patch" +- "vmimportsources.forklift.cozystack.io - update" +- "vmimporttasks.forklift.cozystack.io - create" +- "vmimporttasks.forklift.cozystack.io - delete" +- "vmimporttasks.forklift.cozystack.io - patch" +- "vmimporttasks.forklift.cozystack.io - update" diff --git a/packages/system/cozystack-basics/templates/clusterroles.yaml b/packages/system/cozystack-basics/templates/clusterroles.yaml index 5df762886..297667a49 100644 --- a/packages/system/cozystack-basics/templates/clusterroles.yaml +++ b/packages/system/cozystack-basics/templates/clusterroles.yaml @@ -321,11 +321,18 @@ metadata: labels: rbac.cozystack.io/aggregate-to-tenant-super-admin: "true" rules: +{{- /* Read-only. A VirtualMachine written directly is sized outside any + application, and every legitimate change already has a route: VMInstance + values for size, disks and networking, the subresources granted at the use + level for power and console. watch backs the dashboard's live power state, + which watches the VM rather than polling it. */}} - apiGroups: ["kubevirt.io"] resources: - virtualmachines verbs: - - '*' + - get + - list + - watch - apiGroups: ["apps.cozystack.io"] resources: - '*' diff --git a/packages/system/cozystack-basics/tests/clusterroles-tenant-super-admin_test.yaml b/packages/system/cozystack-basics/tests/clusterroles-tenant-super-admin_test.yaml new file mode 100644 index 000000000..b077d9959 --- /dev/null +++ b/packages/system/cozystack-basics/tests/clusterroles-tenant-super-admin_test.yaml @@ -0,0 +1,32 @@ +suite: cozystack-basics tenant super-admin cannot write a raw VirtualMachine + +templates: + - templates/clusterroles.yaml + +release: + name: cozystack-basics + namespace: cozy-system + +# A kubevirt.io VirtualMachine written directly is sized outside any +# application, so the super-admin keeps read and watch on it and nothing else. +# The cluster-side check, after every aggregated role is folded in, is the +# allowlist test in hack/e2e-install-cozystack.bats; this one fails earlier, on +# the unit lane, for the role that carried the grant. +tests: + - it: cozy:tenant:super-admin:base grants read and watch only on kubevirt.io virtualmachines + documentSelector: + path: metadata.name + value: cozy:tenant:super-admin:base + asserts: + - contains: + path: rules + content: + apiGroups: ["kubevirt.io"] + resources: ["virtualmachines"] + verbs: ["get", "list", "watch"] + - notContains: + path: rules + content: + apiGroups: ["kubevirt.io"] + resources: ["virtualmachines"] + verbs: ["*"]