|
| 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] |
0 commit comments