Skip to content

Commit 98df611

Browse files
committed
feat(cozystack-basics): restrict CA-source Secret writes to cert-manager
The CA-extraction controller's label-driven leg treats a core Secret labelled internal.cozystack.io/publish-ca-cert as a trust-anchor source and lifts its ca.crt into the tenant-readable projection. That label is the whole attribution on that leg and sits on an ordinary Secret, so without a platform-side control any principal that can write a Secret in the namespace could forge a source or rewrite an existing one's ca.crt and swap the trust anchor a tenant is handed as vouched. Add a ValidatingAdmissionPolicy that allows a Secret carrying that label to be created or updated only by cert-manager's controller ServiceAccount, the sole legitimate writer of these Secrets. It covers both the forge-new (CREATE) and tamper-existing (UPDATE, including a relabel-away evasion via the old object) shapes, and by keying on the publish-ca-cert label it never touches the declared-leg source, the projection, or any unlabelled Secret. The allowed identity is pinned as a literal to the shipped cert-manager package's controller ServiceAccount; a comment marks it as the fragile seam that must move if cert-manager is repackaged. Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent b8d4e7f commit 98df611

2 files changed

Lines changed: 204 additions & 0 deletions

File tree

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
{{/*
2+
cozystack-publish-ca-cert-writer-policy
3+
4+
Restricts who may write a CA-source Secret on the CA-extraction
5+
controller's label-driven leg. A core Secret labelled
6+
internal.cozystack.io/publish-ca-cert is a trust-anchor SOURCE: the
7+
extraction controller lifts its ca.crt into the tenant-readable
8+
<release>-tenant-ca projection. That label is the whole attribution on
9+
that leg, and it sits on an ordinary Secret — so without this policy any
10+
principal that can create or update a Secret in the namespace could forge
11+
a source (a Secret with the publish-ca-cert + publish-ca-cert-release
12+
labels and an attacker ca.crt) or tamper an existing one (rewrite its
13+
ca.crt while leaving the labels intact), and thereby swap the trust anchor
14+
a tenant is handed as vouched. This is the platform-side control that
15+
closes that gap; the controller itself keeps the declared leg authoritative
16+
so a labelled Secret can never override an operator-declared CA.
17+
18+
Rule: a Secret carrying internal.cozystack.io/publish-ca-cert may be
19+
created or updated ONLY by cert-manager's controller ServiceAccount. Every
20+
other principal is denied. cert-manager is the sole legitimate writer:
21+
it is the only component that mints these Secrets (the label rides
22+
Certificate.spec.secretTemplate.labels, which cert-manager applies), and
23+
its own issuance and rotation are that same ServiceAccount, so they pass.
24+
The extraction controller only READS sources, so it needs no allowance.
25+
26+
Coverage — both attack shapes:
27+
- Forge-new (CREATE of a labelled Secret) is matched because the new
28+
object carries the label.
29+
- Tamper-existing (UPDATE of a labelled Secret's data, labels intact) is
30+
matched because the new object still carries the label; and a
31+
relabel-away evasion (UPDATE that strips the label) is matched because
32+
the OLD object carried it. So neither shape can dodge the check.
33+
34+
Out of scope — three things this policy must never match, by construction:
35+
- The declared-leg source (CloudNativePG's <release>-ca): it carries no
36+
publish-ca-cert label, so the match condition is false and CNPG writes
37+
it freely.
38+
- The projection (<release>-tenant-ca): it carries
39+
internal.cozystack.io/tenant-ca, a DIFFERENT label, so the extraction
40+
controller writes it freely.
41+
- Any Secret without the publish-ca-cert label: never matched.
42+
43+
DELETE is INTENTIONALLY ungated (operations are CREATE + UPDATE only), and
44+
that is a deliberate design decision, not an oversight:
45+
- This policy protects INTEGRITY — it stops a forged or tampered source
46+
from becoming a tenant's trust anchor. Deleting a source cannot forge an
47+
anchor: the extraction controller treats an absent recorded source as
48+
merely-absent, not an opt-out, so it FREEZES the projection at its last
49+
good value and waits. Losing a source is therefore an availability
50+
concern (a stalled rotation), addressed by ordinary Secret RBAC, not an
51+
integrity one for this policy to carry.
52+
- Gating DELETE would also be actively harmful. A DELETE-matching VAP is
53+
evaluated for the garbage-collector and the namespace-controller too —
54+
see cozystack-no-delete-guardrail
55+
(packages/core/platform/templates/deletion-protection.yaml), which uses
56+
exactly that to block deletion — so a cert-manager-only DELETE gate here
57+
would stop those system principals from cleaning the source up during
58+
tenant/namespace teardown, wedging namespaces in Terminating with no
59+
natural bypass. Fail-closed on DELETE is the wrong trade.
60+
61+
FRAGILE SEAM — the allowed writer is pinned as a literal:
62+
system:serviceaccount:cozy-cert-manager:cert-manager. That is the
63+
controller ServiceAccount of the shipped cert-manager package
64+
(packages/system/cert-manager, installed with releaseName "cert-manager"
65+
into namespace "cozy-cert-manager"; the controller Deployment resolves
66+
serviceAccountName to "cert-manager"). If cert-manager is repackaged, its
67+
release renamed, or its ServiceAccount/namespace moved, THIS allow-list
68+
must move with it: a wrong identity here silently breaks the entire label
69+
leg the moment an engine converges onto it (cert-manager's own writes get
70+
denied), or silently leaves the hole open (the real writer no longer
71+
matches, so nothing is enforced). Keep it in lockstep with the
72+
cert-manager package.
73+
74+
failurePolicy is Fail: a Secret whose write cannot be evaluated is denied,
75+
not admitted — fail-closed, as a trust-anchor control must be.
76+
*/}}
77+
{{- $certManagerWriter := "system:serviceaccount:cozy-cert-manager:cert-manager" }}
78+
{{- $publishLabel := "internal.cozystack.io/publish-ca-cert" }}
79+
---
80+
apiVersion: admissionregistration.k8s.io/v1
81+
kind: ValidatingAdmissionPolicy
82+
metadata:
83+
name: cozystack-publish-ca-cert-writer-policy
84+
labels:
85+
internal.cozystack.io/managed-by-cozystack: ""
86+
spec:
87+
failurePolicy: Fail
88+
matchConstraints:
89+
resourceRules:
90+
- apiGroups: [""]
91+
apiVersions: ["v1"]
92+
operations: ["CREATE", "UPDATE"]
93+
resources: ["secrets"]
94+
matchConditions:
95+
# Only labelled CA sources are gated. On UPDATE the old object is checked
96+
# too, so stripping the label to dodge the rule is itself caught.
97+
- name: is-ca-source
98+
expression: >-
99+
(has(object.metadata.labels) && {{ $publishLabel | quote }} in object.metadata.labels)
100+
|| (oldObject != null && has(oldObject.metadata.labels) && {{ $publishLabel | quote }} in oldObject.metadata.labels)
101+
validations:
102+
# Allowed iff the writer is cert-manager's controller ServiceAccount.
103+
- expression: request.userInfo.username == {{ $certManagerWriter | quote }}
104+
messageExpression: >-
105+
{{ printf "a Secret labelled %s is a tenant trust-anchor source and may only be written by cert-manager (%s); " $publishLabel $certManagerWriter | quote }} + request.userInfo.username + " is not permitted to create or modify it"
106+
reason: Forbidden
107+
---
108+
apiVersion: admissionregistration.k8s.io/v1
109+
kind: ValidatingAdmissionPolicyBinding
110+
metadata:
111+
name: cozystack-publish-ca-cert-writer-policy
112+
labels:
113+
internal.cozystack.io/managed-by-cozystack: ""
114+
spec:
115+
policyName: cozystack-publish-ca-cert-writer-policy
116+
validationActions: [Deny]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
suite: cozystack-publish-ca-cert-writer-policy VAP
2+
templates:
3+
- templates/publish-ca-cert-writer-policy.yaml
4+
5+
release:
6+
name: cozystack-basics
7+
namespace: cozy-system
8+
9+
# A live-admission e2e (forge-new rejected, tamper-existing rejected,
10+
# cert-manager allowed, the three must-not-match cases) waits for the first
11+
# engine to converge onto the label leg. Until then this pins the render and
12+
# the match logic: the policy gates the right objects, by the right label, for
13+
# the right operations, and pins the exact cert-manager identity — a wrong pin
14+
# there is the one failure that silently breaks the label leg or leaves the
15+
# hole open.
16+
17+
tests:
18+
- it: renders the ValidatingAdmissionPolicy and its Binding
19+
asserts:
20+
- hasDocuments:
21+
count: 2
22+
- documentIndex: 0
23+
equal:
24+
path: kind
25+
value: ValidatingAdmissionPolicy
26+
- documentIndex: 0
27+
equal:
28+
path: metadata.name
29+
value: cozystack-publish-ca-cert-writer-policy
30+
- documentIndex: 1
31+
equal:
32+
path: kind
33+
value: ValidatingAdmissionPolicyBinding
34+
- documentIndex: 1
35+
equal:
36+
path: spec.policyName
37+
value: cozystack-publish-ca-cert-writer-policy
38+
- documentIndex: 1
39+
equal:
40+
path: spec.validationActions[0]
41+
value: Deny
42+
43+
- it: is fail-closed and gates core Secret CREATE and UPDATE
44+
documentIndex: 0
45+
asserts:
46+
- equal:
47+
path: spec.failurePolicy
48+
value: Fail
49+
- equal:
50+
path: spec.matchConstraints.resourceRules[0].apiGroups[0]
51+
value: ""
52+
- equal:
53+
path: spec.matchConstraints.resourceRules[0].resources[0]
54+
value: secrets
55+
- contains:
56+
path: spec.matchConstraints.resourceRules[0].operations
57+
content: CREATE
58+
- contains:
59+
path: spec.matchConstraints.resourceRules[0].operations
60+
content: UPDATE
61+
62+
- it: matches only the publish-ca-cert source label, on the new and the old object
63+
documentIndex: 0
64+
asserts:
65+
# The gate keys on publish-ca-cert (the SOURCE label) — never on the
66+
# projection's tenant-ca label, which must stay writable by the controller.
67+
- matchRegex:
68+
path: spec.matchConditions[0].expression
69+
pattern: 'internal\.cozystack\.io/publish-ca-cert'
70+
- notMatchRegex:
71+
path: spec.matchConditions[0].expression
72+
pattern: 'internal\.cozystack\.io/tenant-ca'
73+
# oldObject is checked too, so a relabel-away UPDATE cannot dodge the rule.
74+
- matchRegex:
75+
path: spec.matchConditions[0].expression
76+
pattern: 'oldObject'
77+
78+
- it: allows only cert-manager's controller ServiceAccount to write a source
79+
documentIndex: 0
80+
asserts:
81+
# The exact pinned identity of the shipped cert-manager package. If this
82+
# string drifts from the cert-manager controller SA, the label leg breaks.
83+
- equal:
84+
path: spec.validations[0].expression
85+
value: 'request.userInfo.username == "system:serviceaccount:cozy-cert-manager:cert-manager"'
86+
- equal:
87+
path: spec.validations[0].reason
88+
value: Forbidden

0 commit comments

Comments
 (0)