Skip to content

Commit 08ee470

Browse files
authored
fix(postgres)!: refuse plaintext sessions on external instances (#4847)
## What this PR does CloudNativePG offers TLS on an external Postgres but does not require it. Its built-in last `pg_hba` rule is `host all all all`, which matches TLS and non-TLS connections alike, so a client outside the cluster that connects with `sslmode=disable` gets an unencrypted session. The chart's `tls.enabled` only decides whether the external hostname goes into the certificate, so it cannot prevent this. This is #4619. When `external: true`, the chart now renders `spec.postgresql.pg_hba`: ```yaml pg_hba: - hostssl all all all scram-sha-256 # md5 on PostgreSQL 13, matching CloudNativePG's default rule - host all all all reject ``` CloudNativePG puts user rules after its fixed rules (`local` peer, `streaming_replica` and pooler `cert`) and before its default `host all all all` rule, and PostgreSQL uses the first matching rule, so the two lines above make remote sessions TLS-only without touching replication or the metrics exporter. See the CloudNativePG [`pg_hba` section](https://cloudnative-pg.io/documentation/current/postgresql_conf/#the-pg_hba-section) and the [template](https://github.com/cloudnative-pg/cloudnative-pg/blob/v1.30.0/pkg/postgres/configuration.go) for v1.30.0, the operator version the platform ships. A new tri-state `tls.required` follows the same pattern as `tls.enabled`: when omitted it follows `external`, `false` opts out, `true` requires TLS on an in-cluster instance too. The issue is silent on a switch; I added one because the change is breaking and a tenant with a client that cannot do TLS needs a way back that does not mean patching the operator's resources. In-cluster clients: the pod network cannot be told apart from an external client (the external Service uses `externalTrafficPolicy: Local`, so the source IP is the client's), so the rules apply to every remote client. The chart's init job connects to `<release>-rw` with libpq's default `sslmode=prefer`, which already negotiates TLS, as do the backup and monitoring paths that run inside the pod over the Unix socket. Only clients that disable TLS explicitly are refused. In-cluster instances with `external: false` are unchanged. Changes: - `templates/_tls.tpl`: `postgres.tls.required` helper. - `templates/db.yaml`: renders `spec.postgresql.pg_hba` when required. - `values.yaml`: `tls.required`, with the generated `values.schema.json`, `README.md`, `api/apps/v1alpha1/postgresql/types.go` and `packages/system/postgres-rd/cozyrds/postgres.yaml`. The `Required` deepcopy block in `zz_generated.deepcopy.go` is controller-gen v0.16.4 output. - `README.md`: new "Refusing plaintext sessions" paragraph. - `tests/pg_hba_test.yaml`: 10 helm-unittest cases. Backwards compatibility: breaking for existing external instances whose clients connect without TLS. The rules apply on the next reconcile. They are refused until the client enables TLS or the instance sets `tls.required: false`. Testing done: - `make generate` in `packages/apps/postgres` with cozyvalues-gen v1.7.0; regenerated files committed. - `helm unittest .` in `packages/apps/postgres`: 206 passed (25 suites), 10 of them new. I did not run it against a live cluster, so the `pg_hba` ordering is taken from the operator source and docs rather than observed. ### Screenshots Not a UI change. The dashboard form gets a `tls.required` toggle from the schema. ### Downstream repositories I walked the trigger map against the diff and left every box empty so a human decides. The nearest hit is the website's managed-apps reference page for postgres, which a stable-tag bot regenerates from `README.md`; the Terraform provider's postgres schema may also want the new `tls.required` field. - [ ] No downstream repository is affected by this change - [ ] [cozystack/website](https://github.com/cozystack/website) - follow-up: - [ ] [cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack) - follow-up: - [ ] [cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack) - follow-up: - [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up: - [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up: - [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) - follow-up: - [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) - follow-up: - [ ] [cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server) - follow-up: - [ ] [cozystack/external-apps-example](https://github.com/cozystack/external-apps-example) - follow-up: - [ ] [cozystack/examples](https://github.com/cozystack/examples) - follow-up: - [ ] [cozystack/community](https://github.com/cozystack/community) - follow-up: ### Release note ```release-note fix(postgres)!: an external Postgres now refuses sessions that are not encrypted. The chart renders `hostssl` rules for all users and databases with scram-sha-256 (md5 on PostgreSQL 13, matching CloudNativePG's default rule) and rejects plain `host` connections, so a client using `sslmode=disable` is refused. The new `tls.required` value follows `external` by default; set it to `false` to keep accepting plaintext sessions. ``` Fixes #4619 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added a `tls.required` setting for PostgreSQL instances. By default, TLS is required for external instances and optional for others. You can override this behavior to require encrypted connections for both external and in-cluster clients, or allow plaintext connections for external instances. Rendering fails if TLS is required while PostgreSQL SSL is disabled. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents f0e3e17 + f6f879c commit 08ee470

9 files changed

Lines changed: 218 additions & 5 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/apps/v1alpha1/postgresql/zz_generated.deepcopy.go‎

Lines changed: 5 additions & 0 deletions
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ The tri-state `tls.enabled` controls whether the chart injects `serverAltDNSName
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.
176176
- `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

178+
**Refusing plaintext sessions.** CNPG offers TLS but its built-in last `pg_hba` rule, `host all all all`, also accepts plaintext, so a client using `sslmode=disable` gets an unencrypted session. The tri-state `tls.required` closes that:
179+
180+
- `tls.required: null` (the default) — follows `external`. With `external: true` the chart renders `spec.postgresql.pg_hba` as `hostssl all all all scram-sha-256` followed by `host all all all reject`. CNPG puts these after its fixed rules and before its default rule, and the first matching rule wins, so only TLS sessions are authenticated.
181+
- `tls.required: true` — the same rules regardless of `external`.
182+
- `tls.required: false` — no `pg_hba` rules are rendered and CNPG's default applies, which accepts plaintext.
183+
184+
The rules cover every client, in-cluster ones included, because the pod network cannot be told apart from an external client. libpq's default `sslmode=prefer` already negotiates TLS, so only clients that disable TLS explicitly are refused. Instances that were external before this setting existed refuse such clients after the upgrade; set `tls.required: false` to keep accepting them. See the CloudNativePG [`pg_hba` documentation](https://cloudnative-pg.io/documentation/current/postgresql_conf/#the-pg_hba-section).
185+
178186
**Retrieving the CA bundle** for client verification:
179187

180188
The trust anchor is published as `postgres-<name>.tenant-ca`, where `<name>` is the name of the Postgres resource: an object holding `ca.crt` and nothing else, created for every release and delivered to tenants through the `core.cozystack.io/tenantsecrets` API that the base tenant roles already grant. The `postgres-` prefix comes from the release naming this application definition applies, so a Postgres named `foo` gets `postgres-foo.tenant-ca`.
@@ -218,10 +226,11 @@ For `sslmode=verify-full` to work, the CA bundle retrieved above must be saved t
218226

219227
### TLS configuration
220228

221-
| Name | Description | Type | Value |
222-
| ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------ |
223-
| `tls` | TLS configuration for server connections. | `object` | `{}` |
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` |
229+
| Name | Description | Type | Value |
230+
| -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ------ |
231+
| `tls` | TLS configuration for server connections. | `object` | `{}` |
232+
| `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` |
233+
| `tls.required` | Tri-state switch controlling whether the server refuses sessions that are not encrypted. When omitted, it is on if `external: true` and off otherwise. When on, the chart renders `spec.postgresql.pg_hba` with `hostssl all all all scram-sha-256` (`md5` on PostgreSQL 13, matching CloudNativePG's default rule) followed by `host all all all reject`, so a client that connects with `sslmode=disable` is refused instead of getting a plaintext session; in-cluster clients are held to the same rule, since the pod network cannot be told apart from an external client. Set to `false` to keep accepting plaintext sessions on an external instance, or to `true` to require TLS on an in-cluster one. Rendering fails if `postgresql.parameters.ssl` is off while this is in effect. | `*bool` | `null` |
225234

226235

227236
### Application-specific parameters

‎packages/apps/postgres/templates/_tls.tpl‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ Tri-state semantics:
1515
{{- .Values.tls.enabled | toString -}}
1616
{{- end -}}
1717
{{- end -}}
18+
19+
{{/*
20+
postgres.tls.required resolves the tri-state tls.required field to "true" or
21+
"false". Unset follows external, so an externally published instance refuses
22+
plaintext sessions by default. Set explicitly, it wins in either direction.
23+
*/}}
24+
{{- define "postgres.tls.required" -}}
25+
{{- if kindIs "invalid" .Values.tls.required -}}
26+
{{- .Values.external | default false | toString -}}
27+
{{- else -}}
28+
{{- .Values.tls.required | toString -}}
29+
{{- end -}}
30+
{{- end -}}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{{- $tlsEnabled := (include "postgres.tls.enabled" .) | eq "true" -}}
2+
{{- $tlsRequired := (include "postgres.tls.required" .) | eq "true" -}}
23
{{- $autoscaling := (include "postgres.autoscaling.active" .) | eq "true" -}}
34
---
45
apiVersion: postgresql.cnpg.io/v1
@@ -269,6 +270,38 @@ spec:
269270
{{- end }}
270271
{{- end }}
271272
postgresql:
273+
{{- if $tlsRequired }}
274+
{{- /*
275+
CloudNativePG offers TLS (ssl=on) but its built-in last rule, host all all
276+
all, also accepts plaintext, so a client choosing sslmode=disable gets an
277+
unencrypted session. User rules go after the operator's fixed rules
278+
(local peer, streaming_replica cert) and before that default rule, and the
279+
first match wins, so these two lines make remote sessions TLS-only.
280+
https://cloudnative-pg.io/documentation/current/postgresql_conf/#the-pg_hba-section
281+
*/}}
282+
{{- /*
283+
Same password method as CloudNativePG's own default rule: md5 before
284+
PostgreSQL 14 (where password_encryption defaults to md5, and md5 also
285+
accepts SCRAM verifiers), scram-sha-256 from 14 on.
286+
pkg/management/postgres/configuration.go in cloudnative-pg.
287+
*/}}
288+
{{- $pgMajor := trimPrefix "v" .Values.version | atoi }}
289+
{{- $authMethod := ternary "md5" "scram-sha-256" (lt $pgMajor 14) }}
290+
{{- /*
291+
With ssl turned off, hostssl never matches and the reject rule would refuse
292+
every remote session; stop with an actionable error instead.
293+
*/}}
294+
{{- $sslParam := "" }}
295+
{{- if and .Values.postgresql .Values.postgresql.parameters }}
296+
{{- $sslParam = toString (dig "ssl" "" .Values.postgresql.parameters) | lower }}
297+
{{- end }}
298+
{{- if has $sslParam (list "off" "false" "0" "no") }}
299+
{{- fail "postgresql.parameters.ssl is off while tls.required is in effect (external: true or tls.required: true): enable ssl or set tls.required: false" }}
300+
{{- end }}
301+
pg_hba:
302+
- hostssl all all all {{ $authMethod }}
303+
- host all all all reject
304+
{{- end }}
272305
{{- /*
273306
Preloading is exposed as a closed allowlist rather than through the blocked
274307
shared_preload_libraries parameter. The parameter is free text and accepts a
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
suite: postgres pg_hba TLS enforcement
2+
3+
# CNPG appends user pg_hba rules after its fixed rules and before its default
4+
# "host all all all" rule, and the first match wins. tls.required is a tri-state
5+
# that follows external when omitted.
6+
7+
release:
8+
name: pg-test
9+
namespace: tenant-test
10+
11+
set:
12+
_cluster:
13+
scheduling: {}
14+
cluster-domain: cozy.local
15+
_namespace:
16+
host: example.cozy.local
17+
18+
templates:
19+
- templates/db.yaml
20+
21+
tests:
22+
- it: renders no pg_hba when the instance is not external
23+
documentSelector:
24+
path: kind
25+
value: Cluster
26+
asserts:
27+
- isNull:
28+
path: spec.postgresql.pg_hba
29+
30+
- it: external instance accepts TLS sessions only
31+
set:
32+
external: true
33+
documentSelector:
34+
path: kind
35+
value: Cluster
36+
asserts:
37+
- equal:
38+
path: spec.postgresql.pg_hba
39+
value:
40+
- hostssl all all all scram-sha-256
41+
- host all all all reject
42+
43+
- it: external instance still gets the TLS rules when tls.enabled is false
44+
set:
45+
external: true
46+
tls:
47+
enabled: false
48+
documentSelector:
49+
path: kind
50+
value: Cluster
51+
asserts:
52+
- equal:
53+
path: spec.postgresql.pg_hba[0]
54+
value: hostssl all all all scram-sha-256
55+
56+
- it: tls.required false opts an external instance out
57+
set:
58+
external: true
59+
tls:
60+
required: false
61+
documentSelector:
62+
path: kind
63+
value: Cluster
64+
asserts:
65+
- isNull:
66+
path: spec.postgresql.pg_hba
67+
68+
- it: tls.required true applies to an in-cluster instance
69+
set:
70+
tls:
71+
required: true
72+
documentSelector:
73+
path: kind
74+
value: Cluster
75+
asserts:
76+
- equal:
77+
path: spec.postgresql.pg_hba
78+
value:
79+
- hostssl all all all scram-sha-256
80+
- host all all all reject
81+
82+
- it: pg_hba does not disturb postgresql.parameters
83+
set:
84+
external: true
85+
documentSelector:
86+
path: kind
87+
value: Cluster
88+
asserts:
89+
- equal:
90+
path: spec.postgresql.parameters.max_connections
91+
value: "100"
92+
93+
# CloudNativePG's own default rule uses md5 before PostgreSQL 14, where
94+
# password_encryption defaults to md5; md5 also accepts SCRAM verifiers.
95+
- it: uses md5 on PostgreSQL 13 like CloudNativePG's default rule
96+
set:
97+
external: true
98+
version: v13
99+
documentSelector:
100+
path: kind
101+
value: Cluster
102+
asserts:
103+
- equal:
104+
path: spec.postgresql.pg_hba
105+
value:
106+
- hostssl all all all md5
107+
- host all all all reject
108+
109+
- it: uses scram-sha-256 from PostgreSQL 14
110+
set:
111+
external: true
112+
version: v14
113+
documentSelector:
114+
path: kind
115+
value: Cluster
116+
asserts:
117+
- equal:
118+
path: spec.postgresql.pg_hba
119+
value:
120+
- hostssl all all all scram-sha-256
121+
- host all all all reject
122+
123+
- it: refuses ssl off while TLS is required
124+
set:
125+
external: true
126+
postgresql:
127+
parameters:
128+
ssl: "off"
129+
asserts:
130+
- failedTemplate:
131+
errorPattern: "postgresql.parameters.ssl is off while tls.required is in effect"
132+
133+
- it: allows ssl off when TLS is not required
134+
set:
135+
external: true
136+
tls:
137+
required: false
138+
postgresql:
139+
parameters:
140+
ssl: "off"
141+
documentSelector:
142+
path: kind
143+
value: Cluster
144+
asserts:
145+
- isNull:
146+
path: spec.postgresql.pg_hba

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@
157157
"enabled": {
158158
"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"
160+
},
161+
"required": {
162+
"description": "Tri-state switch controlling whether the server refuses sessions that are not encrypted. When omitted, it is on if `external: true` and off otherwise. When on, the chart renders `spec.postgresql.pg_hba` with `hostssl all all all scram-sha-256` (`md5` on PostgreSQL 13, matching CloudNativePG's default rule) followed by `host all all all reject`, so a client that connects with `sslmode=disable` is refused instead of getting a plaintext session; in-cluster clients are held to the same rule, since the pod network cannot be told apart from an external client. Set to `false` to keep accepting plaintext sessions on an external instance, or to `true` to require TLS on an in-cluster one. Rendering fails if `postgresql.parameters.ssl` is off while this is in effect.",
163+
"type": "boolean"
160164
}
161165
}
162166
},

0 commit comments

Comments
 (0)