|
| 1 | +{{/* |
| 2 | +============================================================================= |
| 3 | + TLS trust-anchor helpers |
| 4 | +============================================================================= |
| 5 | +
|
| 6 | +Per-app TLS in the Cozystack catalog issues a per-release, self-signed CA (via |
| 7 | +cert-manager or the app operator). A tenant that connects to such an endpoint |
| 8 | +needs the CA certificate (ca.crt) to verify the server — but nothing more. The |
| 9 | +objects that hold ca.crt today also hold private keys: |
| 10 | +
|
| 11 | + - the cert-manager CA secret <release>-ca carries the CA private key |
| 12 | + (tls.key) — full trust-chain compromise if leaked; |
| 13 | + - the cert-manager leaf secret <release>-tls carries the server private key |
| 14 | + (tls.key) — server impersonation / MITM if leaked. |
| 15 | +
|
| 16 | +So any RBAC path that hands a tenant ca.crt by granting read on one of those |
| 17 | +Secrets also hands over a private key. cozy-lib.tls.caCertSecret breaks that |
| 18 | +coupling: it renders a canonical trust-anchor object that carries ONLY ca.crt, |
| 19 | +labelled so tenants reach it through the core.cozystack.io/tenantsecrets API |
| 20 | +that the base tenant roles already grant — never through a Secret that contains |
| 21 | +tls.key. |
| 22 | +
|
| 23 | +Delivery mechanism (grounded in the platform RBAC, not invented here): |
| 24 | +
|
| 25 | + - pkg/registry/core/tenantsecret/rest.go surfaces, under the virtual resource |
| 26 | + core.cozystack.io/tenantsecrets, exactly the namespace Secrets labelled |
| 27 | + internal.cozystack.io/tenantresource=true (TenantResourceLabelKey / |
| 28 | + TenantResourceLabelValue in pkg/apis/core/v1alpha1/tenantresource_types.go). |
| 29 | + - packages/system/cozystack-basics/templates/clusterroles.yaml grants |
| 30 | + get/list/watch on core.cozystack.io/tenantsecrets to tenant ServiceAccounts |
| 31 | + (cozy:tenant:base) and to use/admin/super-admin subjects (cozy:tenant:use: |
| 32 | + base). No grant on raw core/v1 secrets is involved, so attaching the label |
| 33 | + to a key-free object exposes the trust anchor without exposing any key. |
| 34 | + - internal/backupcontroller/credentials_projector.go relies on the same rule |
| 35 | + in reverse: it deliberately OMITS the label so its key-bearing projection |
| 36 | + is NOT promoted to a TenantSecret. This helper is the positive counterpart — |
| 37 | + a key-FREE object that is safe to promote. |
| 38 | +
|
| 39 | +This is the chart-side shape every per-app TLS PR converges on; population of |
| 40 | +ca.crt stays the responsibility of whatever owns the PKI (the app operator, as |
| 41 | +the redis-operator fork does with its CA-only Opaque Secret, or a cert-manager |
| 42 | +chain resolved at the chart level). The helper itself is pure and value-driven |
| 43 | +so it renders deterministically and refuses, by construction and by guard, to |
| 44 | +carry a private key. |
| 45 | +*/}} |
| 46 | + |
| 47 | +{{/* |
| 48 | +cozy-lib.tls.caCertSecret renders the canonical CA-only trust-anchor Secret. |
| 49 | +
|
| 50 | +Invoked with a single dict argument (named parameters, not the (arg, $) list |
| 51 | +form, because it needs no global scope): |
| 52 | +
|
| 53 | + {{ include "cozy-lib.tls.caCertSecret" (dict |
| 54 | + "name" (printf "%s-ca-cert" .Release.Name) |
| 55 | + "namespace" .Release.Namespace |
| 56 | + "caCert" $caCertPem |
| 57 | + "labels" (dict "app.kubernetes.io/instance" .Release.Name) |
| 58 | + ) }} |
| 59 | +
|
| 60 | +Parameters: |
| 61 | + - name (required) Secret name. Convention: "<release>-ca-cert". |
| 62 | + - caCert (required) the CA certificate chain in PEM. Must contain a |
| 63 | + BEGIN CERTIFICATE PEM header and must NOT contain a |
| 64 | + BEGIN...PRIVATE KEY PEM header; the helper fails closed on either |
| 65 | + violation. Both checks are anchored to the PEM header line and are |
| 66 | + case-insensitive, so they neither false-positive on certificate |
| 67 | + body/comment text nor miss a lowercased hand-pasted header. |
| 68 | + - namespace (optional) metadata.namespace. |
| 69 | + - labels (optional) extra labels merged onto the mandatory |
| 70 | + internal.cozystack.io/tenantresource label (which always wins). |
| 71 | + - annotations (optional) extra annotations. |
| 72 | +*/}} |
| 73 | +{{- define "cozy-lib.tls.caCertSecret" -}} |
| 74 | +{{- if not (kindIs "map" .) -}} |
| 75 | +{{- fail "cozy-lib.tls.caCertSecret: expected a single dict argument" -}} |
| 76 | +{{- end -}} |
| 77 | +{{- $name := default "" .name -}} |
| 78 | +{{- if eq $name "" -}} |
| 79 | +{{- fail "cozy-lib.tls.caCertSecret: name is required" -}} |
| 80 | +{{- end -}} |
| 81 | +{{- $caCert := default "" .caCert -}} |
| 82 | +{{- if eq (trim $caCert) "" -}} |
| 83 | +{{- fail "cozy-lib.tls.caCertSecret: caCert is required and must be a non-empty PEM" -}} |
| 84 | +{{- end -}} |
| 85 | +{{- /* Anchor both guards to the PEM header form, case-insensitively. A free |
| 86 | + substring match would false-positive on a legitimate certificate whose |
| 87 | + subject/comment text happens to contain "PRIVATE KEY", and would miss a |
| 88 | + lowercase hand-pasted header. The (?i) header regex catches every key |
| 89 | + variant (PKCS#8, RSA, EC, ENCRYPTED, OPENSSH, DSA) and only a real |
| 90 | + BEGIN...PRIVATE KEY line, not arbitrary blob text. */ -}} |
| 91 | +{{- if regexMatch "(?i)-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----" $caCert -}} |
| 92 | +{{- fail "cozy-lib.tls.caCertSecret: caCert must not contain private key material" -}} |
| 93 | +{{- end -}} |
| 94 | +{{- if not (regexMatch "(?i)-----BEGIN CERTIFICATE-----" $caCert) -}} |
| 95 | +{{- fail "cozy-lib.tls.caCertSecret: caCert must contain a PEM certificate (BEGIN CERTIFICATE)" -}} |
| 96 | +{{- end -}} |
| 97 | +{{- $labels := merge (dict "internal.cozystack.io/tenantresource" "true") (default (dict) .labels) -}} |
| 98 | +apiVersion: v1 |
| 99 | +kind: Secret |
| 100 | +metadata: |
| 101 | + name: {{ $name }} |
| 102 | +{{- with .namespace }} |
| 103 | + namespace: {{ . }} |
| 104 | +{{- end }} |
| 105 | + labels: {{- toYaml $labels | nindent 4 }} |
| 106 | +{{- with .annotations }} |
| 107 | + annotations: {{- toYaml . | nindent 4 }} |
| 108 | +{{- end }} |
| 109 | +type: Opaque |
| 110 | +stringData: |
| 111 | + ca.crt: {{ $caCert | quote }} |
| 112 | +{{- end -}} |
0 commit comments