Skip to content

Commit a80f134

Browse files
authored
fix(cozy-lib): converge the CA trust-anchor helper on the canonical name (#3408)
## What this PR does Ships `cozy-lib.tls.caCertSecret`, a render-time helper that emits a key-free trust-anchor Secret carrying only `ca.crt`. It converges the object name on `<release>.tenant-ca`, requires the whole value to be certificate blocks so a stray private-key header or trailing bytes are rejected, and coerces numeric scalars before the guard runs so a numeric value cannot slip past. No chart calls it yet. It is the render-time producer half of the trust-anchor contract; the controller half is #3407, now merged. The doc-comment on the helper shows the one-line call a chart uses, and says outright there is no caller yet, so nobody reads it as wired when it is not. Split out from #3299. Comments trimmed on review. ```release-note fix(cozy-lib): the CA trust-anchor helper emits the canonical `<release>.tenant-ca` name and rejects non-certificate input. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Updated generated TLS CA Secret naming to use the `<release>.tenant-ca` convention. - Added/ensured a tenant CA label is set alongside the existing tenant resource label. - Continued support for custom labels (caller labels still applied). - **Bug Fixes** - Strengthened CA certificate PEM validation to be fail-closed: requires non-empty, complete `BEGIN/END CERTIFICATE` blocks only. - Rejects private-key material and other malformed or improperly structured PEM input, including unexpected extra content. - **Tests** - Updated TLS CA certificate fixtures and assertions to cover additional negative cases and refined error expectations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents 2823b76 + 5cdeba7 commit a80f134

4 files changed

Lines changed: 289 additions & 82 deletions

File tree

‎packages/library/cozy-lib/templates/_tls.tpl‎

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,41 @@
11
{{/*
2-
=============================================================================
3-
TLS trust-anchor helpers
4-
=============================================================================
2+
cozy-lib.tls.caCertSecret renders the canonical CA-only trust-anchor Secret
3+
"<release>.tenant-ca", carrying ONLY ca.crt and never a private key.
54
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:
5+
Why a dedicated object: the Secrets that already hold ca.crt also hold keys —
6+
cert-manager's "<release>-ca" carries the CA key, the leaf "<release>-tls" the
7+
server key — so any RBAC that grants a tenant ca.crt through them leaks a key
8+
too. This helper emits ca.crt alone, labelled so the tenant reads it through the
9+
core.cozystack.io/tenantsecrets virtual resource the base tenant roles already
10+
grant (never through a raw core/v1 Secret).
1011
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.
12+
Name is "<release>.tenant-ca", NOT "<release>-ca-cert": Percona Server for
13+
MongoDB already claims "<release>-ca-cert" with a key-bearing Secret of its own.
1514
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.
15+
This is the render-time path, for charts that hold the CA PEM as a value. Engines
16+
whose operator mints the CA asynchronously are served instead by the CA-extraction
17+
controller (internal/controller/cacert), which projects the same canonical object
18+
out of whatever Secret the engine produces — so a tenant learns exactly one name.
4519
*/}}
4620

4721
{{/*
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):
22+
Invoked with a single dict argument. A chart holding the CA PEM as a value calls
23+
it like this (there is no production caller yet — this is the pattern one uses):
5224
5325
{{ include "cozy-lib.tls.caCertSecret" (dict
54-
"name" (printf "%s-ca-cert" .Release.Name)
26+
"name" (printf "%s.tenant-ca" .Release.Name)
5527
"namespace" .Release.Namespace
5628
"caCert" $caCertPem
5729
"labels" (dict "app.kubernetes.io/instance" .Release.Name)
5830
) }}
5931
6032
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).
33+
- name (required) Secret name. Convention: "<release>.tenant-ca".
34+
- caCert (required) the CA chain in PEM. Must be one or more COMPLETE
35+
certificate blocks and nothing else; the helper fails closed on a
36+
private-key header or on any non-certificate bytes.
37+
- namespace (optional) metadata.namespace.
38+
- labels (optional) extra labels, merged onto the mandatory tenant labels.
7139
- annotations (optional) extra annotations.
7240
*/}}
7341
{{- define "cozy-lib.tls.caCertSecret" -}}
@@ -78,23 +46,35 @@ Parameters:
7846
{{- if eq $name "" -}}
7947
{{- fail "cozy-lib.tls.caCertSecret: name is required" -}}
8048
{{- end -}}
81-
{{- $caCert := default "" .caCert -}}
49+
{{- /* Coerce to string first: an unquoted numeric YAML scalar (caCert: 12345)
50+
parses to float64, and trim on it would die with a raw Go type error
51+
instead of this helper's own fail message. (caCert: 0 reports "required",
52+
not coerced — `default ""` treats 0 as empty; pinned by a test.) */ -}}
53+
{{- $caCert := printf "%v" (default "" .caCert) -}}
8254
{{- if eq (trim $caCert) "" -}}
8355
{{- fail "cozy-lib.tls.caCertSecret: caCert is required and must be a non-empty PEM" -}}
8456
{{- 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 -}}
57+
{{- /* Reject private-key material, anchored to the PEM header and case-insensitive
58+
so it neither false-positives on certificate body text nor misses a
59+
lowercased header. Stops at "PRIVATE KEY", not the closing dashes, so PGP's
60+
"-----BEGIN PGP PRIVATE KEY BLOCK-----" is still caught. */ -}}
61+
{{- if regexMatch "(?i)-----BEGIN [A-Z0-9 ]*PRIVATE KEY" $caCert -}}
9262
{{- fail "cozy-lib.tls.caCertSecret: caCert must not contain private key material" -}}
9363
{{- end -}}
94-
{{- if not (regexMatch "(?i)-----BEGIN CERTIFICATE-----" $caCert) -}}
95-
{{- fail "cozy-lib.tls.caCertSecret: caCert must contain a PEM certificate (BEGIN CERTIFICATE)" -}}
64+
{{- /* Require the WHOLE value (\A ... \z) to be complete certificate blocks and
65+
whitespace, not merely to contain one, because ca.crt is emitted VERBATIM
66+
below: preamble or trailing bytes (e.g. a raw DER/JWK key wearing no PEM
67+
header) would otherwise ride into the tenant-readable anchor. This bounds
68+
block SHAPE only — a Helm template has no x509 parser; the Go CA-extraction
69+
controller does the full pem.Decode + x509 parse. */ -}}
70+
{{- if not (regexMatch "(?i)\\A\\s*(-----BEGIN CERTIFICATE-----\\s+[A-Za-z0-9+/=][A-Za-z0-9+/=\\s]*-----END CERTIFICATE-----\\s*)+\\z" $caCert) -}}
71+
{{- fail "cozy-lib.tls.caCertSecret: caCert must contain a complete PEM certificate block (BEGIN/END CERTIFICATE)" -}}
9672
{{- end -}}
97-
{{- $labels := merge (dict "internal.cozystack.io/tenantresource" "true") (default (dict) .labels) -}}
73+
{{- /* internal.cozystack.io/tenant-ca is the selector an ApplicationDefinition
74+
matches and the CA-extraction controller stamps, so a helper-rendered
75+
anchor must carry it to converge with a projected one. tenantresource is
76+
rendered for shape but the lineage webhook recomputes it on admission. */ -}}
77+
{{- $labels := merge (dict "internal.cozystack.io/tenant-ca" "true" "internal.cozystack.io/tenantresource" "true") (default (dict) .labels) -}}
9878
apiVersion: v1
9979
kind: Secret
10080
metadata:

‎packages/tests/cozy-lib-tests/templates/tests/tls-cacert.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{ include "cozy-lib.tls.caCertSecret" (dict
2-
"name" (printf "%s-ca-cert" .Release.Name)
2+
"name" (printf "%s.tenant-ca" .Release.Name)
33
"namespace" .Release.Namespace
44
"caCert" .Values.caCert
55
"labels" (merge (dict "app.kubernetes.io/instance" .Release.Name) (default (dict) .Values.callerLabels))

0 commit comments

Comments
 (0)