Skip to content

Commit 9625d77

Browse files
fix(postgres): keep users.app's password across pod restarts
CNPG bootstraps an "app" owner role with a generated password stored in <release>-app, and a declared users.app adopts that role. The init-job then sets the role's password from <release>-credentials, but CNPG's instance manager re-applies the owner's password from the bootstrap secret whenever it meets a secret version it has not applied, which includes every pod start. Any restart therefore reverts users.app to CNPG's generated password, and clients holding the advertised one get "password authentication failed" with nothing changed on their side. Render the owner's bootstrap secret from the same password the credentials Secret holds, and point the live bootstrap method at it, so both writers agree on one value. Assisted-by: LLM Signed-off-by: Mattia Eleuteri <[email protected]>
1 parent aed7fa8 commit 9625d77

3 files changed

Lines changed: 250 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ spec:
260260
# cluster's name — which would read the wrong S3 prefix on restore.
261261
barmanObjectName: {{ .Release.Name }}-recovery
262262
serverName: {{ .Values.bootstrap.serverName | default .Values.bootstrap.oldName }}
263+
{{- else if hasKey (.Values.users | default dict) "app" }}
264+
{{- /*
265+
"app" is the owner CNPG bootstraps, and users.app adopts that role. CNPG's
266+
instance manager re-applies the owner's password from the bootstrap secret
267+
on every reconcile whose secret version it has not seen, so on every pod
268+
restart: left on CNPG's own <release>-app, with its generated password, the
269+
role silently loses the password the init-job set from <release>-credentials.
270+
Point the bootstrap at the chart-rendered <release>-app-owner, which holds that
271+
same password. It goes on the method the live Cluster was bootstrapped with,
272+
because CNPG rejects a second one, and a restored Cluster keeps its recovery
273+
block. Changing bootstrap on an existing Cluster is accepted, and the
274+
remaining fields come from the live object or CNPG's defaults (app/app).
275+
*/}}
276+
{{- $live := lookup "postgresql.cnpg.io/v1" "Cluster" .Release.Namespace .Release.Name }}
277+
{{- $liveBootstrap := dig "spec" "bootstrap" dict ($live | default dict) }}
278+
bootstrap:
279+
{{ ternary "recovery" "initdb" (hasKey $liveBootstrap "recovery") }}:
280+
secret:
281+
name: {{ .Release.Name }}-app-owner
263282
{{- end }}
264283

265284
resources: {{- include "cozy-lib.resources.defaultingSanitize" (list .Values.resourcesPreset .Values.resources $) | nindent 4 }}

‎packages/apps/postgres/templates/init-script.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,15 @@ stringData:
535535
EOT
536536
{{- end }}
537537
{{- end }}
538+
{{- if hasKey (.Values.users | default dict) "app" }}
539+
---
540+
{{- /* The bootstrap secret db.yaml gives CNPG for its "app" owner; see there. */}}
541+
apiVersion: v1
542+
kind: Secret
543+
metadata:
544+
name: {{ .Release.Name }}-app-owner
545+
type: kubernetes.io/basic-auth
546+
stringData:
547+
username: app
548+
password: {{ quote (index $passwords "app") }}
549+
{{- end }}
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
suite: users.app keeps its password across pod restarts
2+
3+
# "app" is the owner CNPG bootstraps, and CNPG's instance manager re-applies the
4+
# owner's password from the bootstrap secret on every pod restart. A declared
5+
# users.app must therefore hand CNPG a secret holding the very password stored
6+
# in <release>-credentials, or the role reverts to CNPG's generated one.
7+
8+
release:
9+
name: pg-test
10+
namespace: tenant-test
11+
12+
set:
13+
_cluster:
14+
scheduling: {}
15+
16+
tests:
17+
- it: gives CNPG the password users.app has in the credentials Secret
18+
templates:
19+
- templates/init-script.yaml
20+
kubernetesProvider:
21+
scheme:
22+
"v1/Secret":
23+
gvr:
24+
version: v1
25+
resource: secrets
26+
namespaced: true
27+
objects:
28+
- apiVersion: v1
29+
kind: Secret
30+
metadata:
31+
name: pg-test-credentials
32+
namespace: tenant-test
33+
data:
34+
app: Y29uc29sZS1wYXNzd29yZA==
35+
set:
36+
users:
37+
app: {}
38+
asserts:
39+
- equal:
40+
path: stringData.app
41+
value: console-password
42+
documentSelector:
43+
path: metadata.name
44+
value: pg-test-credentials
45+
- equal:
46+
path: stringData.password
47+
value: console-password
48+
documentSelector:
49+
path: metadata.name
50+
value: pg-test-app-owner
51+
- equal:
52+
path: stringData.username
53+
value: app
54+
documentSelector:
55+
path: metadata.name
56+
value: pg-test-app-owner
57+
- equal:
58+
path: type
59+
value: kubernetes.io/basic-auth
60+
documentSelector:
61+
path: metadata.name
62+
value: pg-test-app-owner
63+
64+
- it: gives a freshly generated users.app password to both Secrets
65+
templates:
66+
- templates/init-script.yaml
67+
set:
68+
users:
69+
app: {}
70+
asserts:
71+
- matchRegex:
72+
path: stringData.password
73+
pattern: ^[a-zA-Z0-9]{16}$
74+
documentSelector:
75+
path: metadata.name
76+
value: pg-test-app-owner
77+
78+
- it: renders no owner secret when no user is named app
79+
templates:
80+
- templates/init-script.yaml
81+
set:
82+
users:
83+
other: {}
84+
asserts:
85+
# pg-test-credentials and pg-test-init-script only
86+
- hasDocuments:
87+
count: 2
88+
89+
- it: points a new Cluster's initdb at the owner secret
90+
templates:
91+
- templates/db.yaml
92+
set:
93+
users:
94+
app: {}
95+
documentSelector:
96+
path: kind
97+
value: Cluster
98+
asserts:
99+
- equal:
100+
path: spec.bootstrap
101+
value:
102+
initdb:
103+
secret:
104+
name: pg-test-app-owner
105+
106+
- it: points a live initdb Cluster at the owner secret
107+
templates:
108+
- templates/db.yaml
109+
kubernetesProvider:
110+
scheme:
111+
"postgresql.cnpg.io/v1/Cluster":
112+
gvr:
113+
group: postgresql.cnpg.io
114+
version: v1
115+
resource: clusters
116+
namespaced: true
117+
objects:
118+
- apiVersion: postgresql.cnpg.io/v1
119+
kind: Cluster
120+
metadata:
121+
name: pg-test
122+
namespace: tenant-test
123+
spec:
124+
bootstrap:
125+
initdb:
126+
database: app
127+
owner: app
128+
set:
129+
users:
130+
app: {}
131+
documentSelector:
132+
path: kind
133+
value: Cluster
134+
asserts:
135+
- equal:
136+
path: spec.bootstrap.initdb.secret.name
137+
value: pg-test-app-owner
138+
- notExists:
139+
path: spec.bootstrap.recovery
140+
141+
- it: keeps a restored Cluster on its recovery method
142+
# CNPG rejects a Cluster with two bootstrap methods, and a Cluster restored
143+
# from a backup keeps bootstrap.recovery in its live spec.
144+
templates:
145+
- templates/db.yaml
146+
kubernetesProvider:
147+
scheme:
148+
"postgresql.cnpg.io/v1/Cluster":
149+
gvr:
150+
group: postgresql.cnpg.io
151+
version: v1
152+
resource: clusters
153+
namespaced: true
154+
objects:
155+
- apiVersion: postgresql.cnpg.io/v1
156+
kind: Cluster
157+
metadata:
158+
name: pg-test
159+
namespace: tenant-test
160+
spec:
161+
bootstrap:
162+
recovery:
163+
database: app
164+
owner: app
165+
source: pg-old
166+
set:
167+
users:
168+
app: {}
169+
documentSelector:
170+
path: kind
171+
value: Cluster
172+
asserts:
173+
- equal:
174+
path: spec.bootstrap.recovery.secret.name
175+
value: pg-test-app-owner
176+
- notExists:
177+
path: spec.bootstrap.initdb
178+
179+
- it: leaves bootstrap unrendered when no user is named app
180+
templates:
181+
- templates/db.yaml
182+
set:
183+
users:
184+
other: {}
185+
documentSelector:
186+
path: kind
187+
value: Cluster
188+
asserts:
189+
- notExists:
190+
path: spec.bootstrap
191+
192+
- it: leaves an in-progress restore's recovery block untouched
193+
# While bootstrap.enabled is on the init-job is skipped and the credentials
194+
# are flagged pending, so the owner password is not taken over mid-restore.
195+
templates:
196+
- templates/db.yaml
197+
set:
198+
users:
199+
app: {}
200+
backup:
201+
enabled: true
202+
useSystemBucket: false
203+
s3AccessKey: AKIAEXAMPLE
204+
s3SecretKey: s3cr3t
205+
destinationPath: s3://bucket/pg-test/
206+
endpointURL: https://s3.example:9000
207+
bootstrap:
208+
enabled: true
209+
oldName: pg-old
210+
serverName: pg-old
211+
documentSelector:
212+
path: kind
213+
value: Cluster
214+
asserts:
215+
- equal:
216+
path: spec.bootstrap.recovery.source
217+
value: pg-old
218+
- notExists:
219+
path: spec.bootstrap.recovery.secret

0 commit comments

Comments
 (0)