Skip to content

Commit 48e3d1d

Browse files
committed
feat(redis,postgres): certify the per-engine external hostname
The tenant Gateway's TLS-passthrough listeners are designed to route databases by SNI under a per-engine subdomain (*.redis.<apex>, *.postgres.<apex>), and a passthrough listener forwards the handshake untouched, so the engine's own server certificate must carry <release>.<engine>.<apex> or a verifying client fails the hostname check. The name is issued next to the existing flat <release>.<apex>, under the same TLS-and-external condition, so that a later move between the two hostname schemes reissues no certificate and leaves clients on the same trust anchor. Nothing renders a route yet; the certificate is prepared ahead of it. Adding the name changes the SAN list, so existing external TLS releases get their server certificate reissued once on upgrade, from the same CA. Assisted-by: LLM Signed-off-by: Aleksei Sviridkin <[email protected]>
1 parent d2e3eb4 commit 48e3d1d

10 files changed

Lines changed: 46 additions & 17 deletions

File tree

‎api/apps/v1alpha1/postgresql/types.go‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎packages/apps/postgres/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ See:
166166

167167
CNPG manages the cert chain end-to-end. The operator auto-generates a self-signed CA, signs server, client, and replication leaf certs from it, and rotates them as needed. The chart does not render any cert-manager `Issuer`/`Certificate` objects — that path is mutually exclusive with the operator-managed chain on the CNPG admission webhook.
168168

169-
What the chart contributes: when TLS is on and `external: true`, the chart sets `spec.certificates.serverAltDNSNames` on the CNPG Cluster CR to inject the external hostname `<release>.<_namespace.host>` into the auto-generated server certificate's SAN list. CNPG's default SAN coverage already includes the three built-in ClusterIP services (`-rw`, `-r`, `-ro`) across the four DNS forms (`<svc>`, `<svc>.<ns>`, `<svc>.<ns>.svc`, `<svc>.<ns>.svc.<cluster-domain>`); only the external hostname needs to be added.
169+
What the chart contributes: when TLS is on and `external: true`, the chart sets `spec.certificates.serverAltDNSNames` on the CNPG Cluster CR to inject the external hostnames `<release>.<_namespace.host>` and `<release>.postgres.<_namespace.host>` into the auto-generated server certificate's SAN list. The second is the per-engine name meant for SNI routing through a TLS-passthrough listener on the tenant Gateway. The certificate covers that name for any client, but routing through such a listener works only when the TLS handshake is the first thing on the wire. With the default negotiation a PostgreSQL client first sends a plaintext SSLRequest, so the listener can route only clients using direct TLS negotiation: PostgreSQL 17 or later on the server and a client connecting with `sslnegotiation=direct`, such as libpq 17 or pgx 5.7.5. CNPG's default SAN coverage already includes the three built-in ClusterIP services (`-rw`, `-r`, `-ro`) across the four DNS forms (`<svc>`, `<svc>.<ns>`, `<svc>.<ns>.svc`, `<svc>.<ns>.svc.<cluster-domain>`); only the external hostnames need to be added.
170170

171171
The tri-state `tls.enabled` controls whether the chart injects `serverAltDNSNames`:
172172

173-
- `tls.enabled: null` (the default) — TLS posture inherits from `external`. When `external: true`, the chart injects the external hostname into the operator-managed cert.
173+
- `tls.enabled: null` (the default) — TLS posture inherits from `external`. When `external: true`, the chart injects the external hostnames into the operator-managed cert.
174174
- `tls.enabled: true` with `external: true` — same effect as the default.
175175
- `tls.enabled: true` with `external: false` — no `serverAltDNSNames` injection is needed (there is no external hostname to add); CNPG's auto-generated cert covers internal services.
176-
- `tls.enabled: false` — the chart skips `serverAltDNSNames` injection. **Note:** CNPG keeps its built-in TLS on the wire regardless of this flag; this toggle only controls whether the external hostname is added to the cert. To force PostgreSQL to drop TLS entirely you would need to set `postgresql.parameters.ssl = "off"` at the CNPG layer, which is out of scope for this flag.
176+
- `tls.enabled: false` — the chart skips `serverAltDNSNames` injection. **Note:** CNPG keeps its built-in TLS on the wire regardless of this flag; this toggle only controls whether the external hostnames are added to the cert. To force PostgreSQL to drop TLS entirely you would need to set `postgresql.parameters.ssl = "off"` at the CNPG layer, which is out of scope for this flag.
177177

178178
**Retrieving the CA bundle** for client verification:
179179

@@ -221,7 +221,7 @@ For `sslmode=verify-full` to work, the CA bundle retrieved above must be saved t
221221
| Name | Description | Type | Value |
222222
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------ |
223223
| `tls` | TLS configuration for server connections. | `object` | `{}` |
224-
| `tls.enabled` | Tri-state switch controlling whether the chart injects the external hostname into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects the SAN if `external: true` and skips it otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = "off"` at the CNPG layer. | `*bool` | `null` |
224+
| `tls.enabled` | Tri-state switch controlling whether the chart injects the external hostnames into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects them if `external: true` and skips them otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = "off"` at the CNPG layer. | `*bool` | `null` |
225225

226226

227227
### Application-specific parameters

‎packages/apps/postgres/templates/db.yaml‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ spec:
5656
# Operator-managed TLS. CNPG auto-generates its own self-signed CA and signs
5757
# server + client + replication certs from it. The operator's default SAN list
5858
# already covers the three built-in services (rw/r/ro) in four DNS forms each
59-
# (bare, .ns, .ns.svc, .ns.svc.<cluster-domain>); only the external hostname
60-
# needs to be appended via serverAltDNSNames.
59+
# (bare, .ns, .ns.svc, .ns.svc.<cluster-domain>); only the external hostnames
60+
# need to be appended via serverAltDNSNames.
6161
#
6262
# serverAltDNSNames is mutually exclusive with serverTLSSecret per the CNPG
6363
# admission webhook, which is why the chart no longer renders a cert-manager
@@ -69,9 +69,17 @@ spec:
6969
# core.cozystack.io tenantsecrets API. It is neither <release>-credentials,
7070
# which is chart-rendered and holds user passwords only, nor CNPG's
7171
# <release>-ca, which holds the CA private key next to ca.crt.
72+
#
73+
# The second name is the per-engine one, meant for SNI routing through a
74+
# TLS-passthrough listener on the tenant Gateway (*.postgres.<apex>). Both
75+
# are issued so that a later move between the two hostname schemes reissues
76+
# no certificate and changes no client's trust anchor; clients still change
77+
# the host they dial.
78+
{{- $apex := include "cozy-lib.ns-host-required" . }}
7279
certificates:
7380
serverAltDNSNames:
74-
- {{ printf "%s.%s" .Release.Name (include "cozy-lib.ns-host-required" .) }}
81+
- {{ printf "%s.%s" .Release.Name $apex }}
82+
- {{ printf "%s.postgres.%s" .Release.Name $apex }}
7583
{{- end }}
7684
{{/*
7785
s3Credentials points at the operator-supplied Secret when

‎packages/apps/postgres/tests/tls_test.yaml‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ suite: postgres TLS configuration
1212
# In TLS-on mode the chart sets spec.certificates with serverAltDNSNames so the
1313
# operator's own CA signs server certs. CNPG auto-derives SANs for the three
1414
# built-in services (rw/r/ro) in four DNS forms each; the chart only appends
15-
# the external hostname when external=true.
15+
# the external hostnames when external=true.
1616

1717
release:
1818
name: pg-test
@@ -64,7 +64,7 @@ tests:
6464
- isNull:
6565
path: spec.certificates
6666

67-
- it: Cluster has spec.certificates.serverAltDNSNames with external hostname when tls auto-on via external true
67+
- it: Cluster has spec.certificates.serverAltDNSNames with external hostnames when tls auto-on via external true
6868
set:
6969
external: true
7070
_namespace:
@@ -82,11 +82,14 @@ tests:
8282
- contains:
8383
path: spec.certificates.serverAltDNSNames
8484
content: pg-test.example.cozy.local
85+
- contains:
86+
path: spec.certificates.serverAltDNSNames
87+
content: pg-test.postgres.example.cozy.local
8588
- lengthEqual:
8689
path: spec.certificates.serverAltDNSNames
87-
count: 1
90+
count: 2
8891

89-
- it: Cluster has spec.certificates.serverAltDNSNames with external hostname when tls explicitly enabled and external true
92+
- it: Cluster has spec.certificates.serverAltDNSNames with external hostnames when tls explicitly enabled and external true
9093
set:
9194
tls:
9295
enabled: true
@@ -100,7 +103,10 @@ tests:
100103
- contains:
101104
path: spec.certificates.serverAltDNSNames
102105
content: pg-test.example.cozy.local
106+
- contains:
107+
path: spec.certificates.serverAltDNSNames
108+
content: pg-test.postgres.example.cozy.local
103109
- lengthEqual:
104110
path: spec.certificates.serverAltDNSNames
105-
count: 1
111+
count: 2
106112

‎packages/apps/postgres/values.schema.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@
155155
"default": {},
156156
"properties": {
157157
"enabled": {
158-
"description": "Tri-state switch controlling whether the chart injects the external hostname into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects the SAN if `external: true` and skips it otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = \"off\"` at the CNPG layer.",
158+
"description": "Tri-state switch controlling whether the chart injects the external hostnames into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects them if `external: true` and skips them otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = \"off\"` at the CNPG layer.",
159159
"type": "boolean"
160160
}
161161
}

‎packages/apps/postgres/values.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ flavor: postgresql
100100
##
101101

102102
## @typedef {struct} TLS - TLS configuration for PostgreSQL server connections.
103-
## @field {*bool} enabled - Tri-state switch controlling whether the chart injects the external hostname into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects the SAN if `external: true` and skips it otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = "off"` at the CNPG layer.
103+
## @field {*bool} enabled - Tri-state switch controlling whether the chart injects the external hostnames into the operator-managed CNPG cert via spec.certificates.serverAltDNSNames. When omitted, the chart injects them if `external: true` and skips them otherwise. Set explicitly to `true` to inject regardless of `external` (no-op when `external: false` since there is no external hostname to add). Set to `false` to skip injection. Note that CNPG keeps its built-in TLS on the wire regardless of this flag — this toggle only controls the chart-side SAN injection; to disable PostgreSQL TLS entirely set `postgresql.parameters.ssl = "off"` at the CNPG layer.
104104

105105
## @param {TLS} tls - TLS configuration for server connections.
106106
tls: {}

‎packages/apps/redis/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ It is reached through `tenantsecrets` rather than by reading a Secret directly,
9090

9191
`<host>` has to be a name the certificate covers. In-cluster that is any of the `rfr-`, `rfrm-`, `rfrs-` and `rfs-` service names, and `<release>-external-lb` when `external` is on; all of them resolve normally.
9292

93-
From outside the cluster the only covered name is `<release>.<tenant-host>`, and the chart does not publish DNS for it: the external Service is a plain LoadBalancer with no `external-dns` annotation, so nothing points that name at the LoadBalancer address. Connecting to the LoadBalancer IP instead fails for any client that verifies the hostname, because the only IP addresses in the certificate are the loopback ones the in-pod probes and the metrics sidecar use; `redis-cli` is not such a client, it checks the chain and not the name, so it connects to the IP and hides the mismatch. Until the name is published, an external client has to be pointed at it manually — a DNS record or a hosts entry mapping `<release>.<tenant-host>` to the LoadBalancer address.
93+
From outside the cluster the covered names are `<release>.<tenant-host>` and `<release>.redis.<tenant-host>`, the second being the per-engine name meant for SNI routing through a TLS-passthrough listener on the tenant Gateway, which `redis-cli` reaches only when given `--sni`, since it sends no SNI by default. The chart publishes DNS for neither: the external Service is a plain LoadBalancer with no `external-dns` annotation, so nothing points either name at the LoadBalancer address. Connecting to the LoadBalancer IP instead fails for any client that verifies the hostname, because the only IP addresses in the certificate are the loopback ones the in-pod probes and the metrics sidecar use; `redis-cli` is not such a client, it checks the chain and not the name, so it connects to the IP and hides the mismatch. Until a name is published, an external client has to be pointed at it manually — a DNS record or a hosts entry mapping either name to the LoadBalancer address.
9494

9595
Neither the CA and its private key (`<release>.ca-tls`) nor the server leaf and its private key (`<release>.tls`) is readable by the tenant. The first would allow minting certificates that any client trusting this release accepts; the second would allow impersonating this release's Redis endpoints. `<release>.tenant-ca` exists precisely so the certificate can be handed over without either key going with it.
9696

‎packages/apps/redis/templates/certmanager.yaml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ spec:
111111
{{- $extHost := index (.Values._namespace | default dict) "host" | default "" }}
112112
{{- if $extHost }}
113113
- {{ printf "%s.%s" .Release.Name $extHost | quote }}
114+
{{- /* The per-engine name, meant for SNI routing through a
115+
TLS-passthrough listener on the tenant Gateway (*.redis.<apex>).
116+
Issued next to the flat name so that a later move between the two
117+
hostname schemes reissues no certificate and changes no client's
118+
trust anchor; clients still change the host they dial. */}}
119+
- {{ printf "%s.redis.%s" .Release.Name $extHost | quote }}
114120
{{- end }}
115121
{{- end }}
116122
ipAddresses:

‎packages/apps/redis/tests/tls_test.yaml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ tests:
338338
- contains:
339339
path: spec.dnsNames
340340
content: "my-redis.example.com"
341+
- contains:
342+
path: spec.dnsNames
343+
content: "my-redis.redis.example.com"
341344

342345
- it: "redisfailover.yaml passes TLS settings to the operator when TLS is enabled"
343346
templates:
@@ -559,6 +562,9 @@ tests:
559562
- notContains:
560563
path: spec.dnsNames
561564
content: "my-redis.example.com"
565+
- notContains:
566+
path: spec.dnsNames
567+
content: "my-redis.redis.example.com"
562568

563569
# ------------------------------------------------------------------
564570
# authClients propagation
@@ -688,6 +694,9 @@ tests:
688694
- notContains:
689695
path: spec.dnsNames
690696
content: "my-redis."
697+
- notContains:
698+
path: spec.dnsNames
699+
content: "my-redis.redis."
691700

692701
- it: "leaf Certificate still renders with no external SAN when no namespace map is injected"
693702
templates:

‎packages/system/postgres-rd/cozyrds/postgres.yaml‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)