mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
fix(cozystack-basics): make the tenant super-admin VirtualMachine grant read-only (#4797)
<!-- Thank you for making a contribution! Here are some tips for you: - Use Conventional Commits for the PR title: `type(scope): description` - Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore - Scopes are not an exhaustive list — pick the most specific scope for the change and extend the list when a genuinely new area appears. Examples: - System components: dashboard, platform, operator, cilium, kube-ovn, linstor, fluxcd, cluster-api - Managed apps: postgres, mariadb, redis, kafka, clickhouse, virtual-machine, kubernetes - Development and maintenance: api, hack, tests, ci, docs, maintenance - Breaking changes: append `!` after type/scope (`feat(api)!: ...`) or add a `BREAKING CHANGE:` footer - If it's a work in progress, consider creating this PR as a draft. - Don't hesistate to ask for opinion and review in the community chats, even if it's still a draft. - Add the label `kind/backport` if it's a bugfix that needs to be backported to a previous version. --> ## What this PR does `cozy:tenant:super-admin:base` granted `'*'` on `kubevirt.io/virtualmachines`, so a tenant super-admin could create a `VirtualMachine` of any size in their namespace, or edit the one a `VMInstance` release rendered and raise its guest size. Such a VM is not a HelmRelease the tenant ordered, so nothing that accounts for applications sees it. The grant arrived in #516 together with a `'*'` on `helmreleases`; that one was removed since, this one was not. Nothing in tree needs the write. Power goes through the `virtualmachines/start|stop|restart` subresources and console/VNC through the VMI subresources, both granted at the `use` level; size, disks and networking go through `VMInstance` values; Forklift's Plan admission check runs against the migration-controller's own service account, not the tenant. The rule becomes `get`, `list`, `watch`. `watch` stays on purpose: the dashboard's power controls and VNC/serial tabs watch the `VirtualMachine` for its live state, and no other tenant role grants it. The tenant roles are aggregated from ClusterRoles in several packages (`cozystack-basics`, `backup-controller`, `migration-controller`, `cozystack-scheduler`), so a write path can reach a tenant without touching the role that names it. A new test in `hack/e2e-install-cozystack.bats`, right after `tenant-test` is created, reads the effective rules of the `tenant-test-super-admin` group back from the apiserver in a single `SelfSubjectRulesReview`, fails if that review reports itself incomplete or carries an `evaluationError` (both of which `kubectl auth can-i --list` lets through with exit 0), subtracts what a bare `system:authenticated` identity already holds, and fails on any write verb that `hack/e2e-tenant-super-admin-write-verbs.yaml` does not name. A line covered by a wildcard grant is folded into it, so a new `apps.cozystack.io` kind does not need an allowlist edit. An allowlisted entry the cluster does not grant is printed without failing, for lanes that disable a package through `COZY_DISABLED_PACKAGES`. The allowlist sits under `hack/e2e-*.yaml`, which `select-e2e.sh` already escalates to the full suite. A helm-unittest on the role catches the same regression on the unit lane. Verified outside CI on a throwaway k3s v1.33 loaded with the rendered tenant ClusterRoles and the tenant RoleBindings, through `hack/cozytest.sh`: with `main`'s roles the test fails and names `virtualmachines.kubevirt.io - *`; with this change it passes; with the migration-controller roles removed it passes and prints the eight `forklift.cozystack.io` entries as not granted. The two awk programs give the same result under mawk and dash (`ubuntu:24.04`) as on macOS. The new helm-unittest fails against `main`'s template and passes here. `hack/bats-runner-coverage.bats`, `hack/bats-no-exit-trap.bats` and `hack/select-e2e_test.bats` pass. This change is the standalone fix of the RBAC prerequisite described in the tenant-quota design proposal, cozystack/community#48, where review asked for it to land on `main` independently of that design. ### Screenshots No UI change. ### Downstream repositories <!-- Cozystack is upstream for repositories that are NOT kept in sync with it automatically, and nothing in CI compares the two sides, so a change here can break them silently. Which change forces what, and which file to touch, is the trigger map in docs/agents/contributing.md under "Downstream Repositories". Walk it before you tick anything. IF YOU ARE AN AI AGENT OR AN LLM, THIS IS PART OF THE TASK, NOT A FORM TO FILL IN: 1. Walk the trigger map against the actual diff of this PR, file by file. Do not decide from the PR title or from your memory of what you changed. 2. For every repository you tick, open the follow-up PR there and link it on the same line. A ticked box with no link claims work that does not exist, which is worse than an empty box. Search that repository for an open PR or issue covering it first, and link that instead of filing a duplicate. 3. When the follow-up is out of scope, or needs a decision that is not yours to make, open an issue in that repository instead, link that, and say so in the PR body. Do not push a speculative PR into another repository just to fill a line here. 4. Do not tick "No downstream repository is affected" to make the checklist go away. If you are unsure, leave every box empty and say so in the PR body, so a human decides. --> - [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: Walked the trigger map against the diff: an RBAC narrowing in `cozystack-basics` plus e2e harness files touch no values key, CRD, `ApplicationDefinition`, package list or tooling path. The one website page that patches `virtualmachines.kubevirt.io` directly, `networking/virtual-router.md`, is a procedure that "requires a Cozystack administrator account", which this change does not affect. ### Release note ```release-note fix(cozystack-basics): tenant super-admins can no longer create, edit or delete `kubevirt.io` VirtualMachines directly; they keep read and watch. Virtual machines are managed through VMInstance and VMDisk applications, power through the start/stop/restart actions. The install e2e now fails when a tenant super-admin gains a write verb outside a checked-in allowlist. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Access Control** * Tenant super-admins can view KubeVirt virtual machines with read-only permissions through the base role; it no longer grants broad write access to these resources. * Tenant super-admin write permissions are checked against an explicit allowlist. Permissions outside the approved list cause the end-to-end check to fail, while approved permissions not currently granted do not. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
4 files changed
+167
-1
No files matched your search
@@ -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
|
||||
|
||||
@@ -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"
|
||||
@@ -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:
|
||||
- '*'
|
||||
|
||||
@@ -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: ["*"]
|
||||
Reference in new issue
Block a user