Skip to content

Commit d18b690

Browse files
committed
[Helm]: Generalize Password Lookup
Generalizing the secret lookup functionality that was added in postgresml#753 to work for `admin_password`, `auth_query_password`, `server_password`, and really any other one. It works by creating a `pgcat.password` template which expects an object containing values for the `password` and `secret` keys. `password` is a literal value normally supplied via `.Values.xyz` value. `secret` is an object with a key and name property that effectively functions like a _secretKeyRef_. When the literal value is not blank, that is used. Otherwise an attempt is made to lookup to supplied key from the named secret and use that. This is exactly how the current implementation of `user_password` works, which avoids any breaking changes. See the function definition for more details. > Note: it seems like `user_passwordSecret` was added (camelCase name) while all the other ones are _snake_case_. I elected to use snake case for the new values, but left `user_passwordSecret` as is to avoid any breaking changes.
1 parent 3935366 commit d18b690

File tree

4 files changed

+84
-32
lines changed

4 files changed

+84
-32
lines changed

charts/pgcat/Chart.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ maintainers:
55
- name: Wildcard
66
77
appVersion: "1.2.0"
8-
version: 0.2.1
8+
version: 0.3.0

charts/pgcat/templates/_helpers.tpl

+31
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,34 @@ Create the name of the service account to use
6060
{{- default "default" .Values.serviceAccount.name }}
6161
{{- end }}
6262
{{- end }}
63+
64+
{{/*
65+
Defines a password function which will assign the appropriate password to the supplied key.
66+
67+
It will use the literal value from `.password` if it is present. Otherwise it will fetch the value from the
68+
specified secret and use that.
69+
70+
If the password is blank, and the secret object does not contain both name and key properties this returns `""`.
71+
Similarly, if the secret lookup fails, this also returns `""`.
72+
73+
NB: For this lookup to succeed, the secret must already be defined. Notably this means that it's not likely to be
74+
managed directly by this chart. It also means that changes to the secret require an upgrade of the release, since the
75+
value of the secret is effectively copied into this manifest.
76+
77+
Args:
78+
* password = The plaintext password
79+
* secret = An object (key and name) to use as essentially as a secretKeyRef
80+
*/}}
81+
{{- define "pgcat.password" -}}
82+
{{- if .password }}
83+
{{- .password | quote }}
84+
{{- else if and .secret.name .secret.key }}
85+
{{- $secret := (lookup "v1" "Secret" $.Release.Namespace .secret.name) }}
86+
{{- if $secret }}
87+
{{- $password := index $secret.data .secret.key | b64dec }}
88+
{{- $password | quote }}
89+
{{- else }}
90+
""
91+
{{- end }}
92+
{{- end }}
93+
{{- end -}}

charts/pgcat/templates/secret.yaml

+9-15
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ stringData:
3131
tls_private_key = "{{ .Values.configuration.general.tls_private_key }}"
3232
{{- end }}
3333
admin_username = {{ .Values.configuration.general.admin_username | quote }}
34-
admin_password = {{ .Values.configuration.general.admin_password | quote }}
34+
admin_password = {{ include "pgcat.password" (dict "password" .Values.configuration.general.admin_password "secret" .Values.configuration.general.admin_password_secret) }}
3535
{{- if and .Values.configuration.general.auth_query_user .Values.configuration.general.auth_query_password .Values.configuration.general.auth_query }}
3636
auth_query = {{ .Values.configuration.general.auth_query | quote }}
3737
auth_query_user = {{ .Values.configuration.general.auth_query_user | quote }}
38-
auth_query_password = {{ .Values.configuration.general.auth_query_password | quote }}
38+
auth_query_password = {{ include "pgcat.password" (dict "password" .Values.configuration.general.auth_query_password "secret" .Values.configuration.general.auth_query_password_secret) }}
3939
{{- end }}
4040
4141
{{- range $pool := .Values.configuration.pools }}
@@ -59,26 +59,20 @@ stringData:
5959
##
6060
[pools.{{ $pool.name | quote }}.users.{{ $index }}]
6161
username = {{ $user.username | quote }}
62-
{{- if $user.password }}
63-
password = {{ $user.password | quote }}
64-
{{- else if and $user.passwordSecret.name $user.passwordSecret.key }}
65-
{{- $secret := (lookup "v1" "Secret" $.Release.Namespace $user.passwordSecret.name) }}
66-
{{- if $secret }}
67-
{{- $password := index $secret.data $user.passwordSecret.key | b64dec }}
68-
password = {{ $password | quote }}
69-
{{- end }}
70-
{{- end }}
62+
password = {{ include "pgcat.password" (dict "password" $user.password "secret" $user.passwordSecret) }}
7163
pool_size = {{ $user.pool_size }}
7264
statement_timeout = {{ default 0 $user.statement_timeout }}
7365
min_pool_size = {{ default 3 $user.min_pool_size }}
7466
{{- if $user.server_lifetime }}
7567
server_lifetime = {{ $user.server_lifetime }}
7668
{{- end }}
77-
{{- if and $user.server_username $user.server_password }}
69+
{{- if $user.server_username }}
7870
server_username = {{ $user.server_username | quote }}
79-
server_password = {{ $user.server_password | quote }}
80-
{{- end }}
81-
{{- end }}
71+
server_password = {{ include "pgcat.password" (dict "password" $user.server_password "secret" $user.server_password_secret) }}
72+
{{- end }}
73+
74+
{{/* end range users */}}
75+
{{- end }}
8276
8377
{{- range $index, $shard := $pool.shards }}
8478

charts/pgcat/values.yaml

+43-16
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ podAnnotations: {}
6565
## @param podSecurityContext.enabled Enabled PgCat pods' Security Context
6666
## @param podSecurityContext.fsGroup Set PgCat pod's Security Context fsGroup
6767
##
68-
podSecurityContext: {}
68+
podSecurityContext:
69+
{}
6970
# fsGroup: 2000
7071

7172
## PgCat pods' Security Context
@@ -75,7 +76,8 @@ podSecurityContext: {}
7576
## @param containerSecurityContext.runAsUser Set PgCat container's Security Context runAsUser
7677
## @param containerSecurityContext.runAsNonRoot Set PgCat container's Security Context runAsNonRoot
7778
##
78-
containerSecurityContext: {}
79+
containerSecurityContext:
80+
{}
7981
# capabilities:
8082
# drop:
8183
# - ALL
@@ -94,7 +96,8 @@ service:
9496
ingress:
9597
enabled: false
9698
className: ""
97-
annotations: {}
99+
annotations:
100+
{}
98101
# kubernetes.io/ingress.class: nginx
99102
# kubernetes.io/tls-acme: "true"
100103
hosts:
@@ -170,16 +173,16 @@ configuration:
170173
connect_timeout: 5000
171174

172175
# How long an idle connection with a server is left open (ms).
173-
idle_timeout: 30000 # milliseconds
176+
idle_timeout: 30000 # milliseconds
174177

175178
# Max connection lifetime before it's closed, even if actively used.
176-
server_lifetime: 86400000 # 24 hours
179+
server_lifetime: 86400000 # 24 hours
177180

178181
# Whether to use TLS for server connections or not.
179182
server_tls: false
180183

181184
# How long a client is allowed to be idle while in a transaction (ms).
182-
idle_client_in_transaction_timeout: 0 # milliseconds
185+
idle_client_in_transaction_timeout: 0 # milliseconds
183186

184187
# @param configuration.general.healthcheck_timeout How much time to give `SELECT 1` health check query to return with a result (ms).
185188
healthcheck_timeout: 1000
@@ -191,7 +194,7 @@ configuration:
191194
shutdown_timeout: 60000
192195

193196
# @param configuration.general.ban_time For how long to ban a server if it fails a health check (seconds).
194-
ban_time: 60 # seconds
197+
ban_time: 60 # seconds
195198

196199
# @param configuration.general.log_client_connections If we should log client connections
197200
log_client_connections: false
@@ -205,9 +208,15 @@ configuration:
205208
tls_certificate: "-"
206209
tls_private_key: "-"
207210

208-
# Credentials to access the virtual administrative database (pgbouncer or pgcat)
211+
# Username used to access the virtual administrative database (pgbouncer or pgcat)
209212
# Connecting to that database allows running commands like `SHOW POOLS`, `SHOW DATABASES`, etc..
210213
admin_username: "postgres"
214+
215+
# Password to be used for administrative queries.
216+
#
217+
# @param configuration.general.admin_password
218+
# @param configuration.general.admin_password_password_secret.name Name of the secret containing the password
219+
# @param configuration.general.admin_password_password_secret.key Key in the secret containing the password
211220
admin_password: "postgres"
212221

213222
# Query to be sent to servers to obtain the hash used for md5 authentication. The connection will be
@@ -227,6 +236,8 @@ configuration:
227236
# in the pool. This parameter is inherited by every pool and can be redefined in pool configuration.
228237
#
229238
# @param configuration.general.auth_query_password
239+
# @param configuration.general.auth_query_password_secret.name Name of the secret containing the password
240+
# @param configuration.general.auth_query_password_secret.key Key in the secret containing the password
230241
auth_query_password: null
231242

232243
# Number of seconds of connection idleness to wait before sending a keepalive packet to the server.
@@ -244,14 +255,28 @@ configuration:
244255
## For the example below a client can connect using "postgres://sharding_user:sharding_user@pgcat_host:pgcat_port/sharded"
245256
## @param [object]
246257
pools:
247-
[{
248-
name: "simple", pool_mode: "transaction",
249-
users: [{username: "user", password: "pass", pool_size: 5, statement_timeout: 0}],
250-
shards: [{
251-
servers: [{host: "postgres", port: 5432, role: "primary"}],
252-
database: "postgres"
253-
}]
254-
}]
258+
[
259+
{
260+
name: "simple",
261+
pool_mode: "transaction",
262+
users:
263+
[
264+
{
265+
username: "user",
266+
password: "pass",
267+
pool_size: 5,
268+
statement_timeout: 0,
269+
},
270+
],
271+
shards:
272+
[
273+
{
274+
servers: [{ host: "postgres", port: 5432, role: "primary" }],
275+
database: "postgres",
276+
},
277+
],
278+
},
279+
]
255280
# - ## default values
256281
# ##
257282
# ##
@@ -322,6 +347,8 @@ configuration:
322347
# ## @param users[0].passwordSecret.name Name of the secret containing the password
323348
# ## @param users[0].passwordSecret.key Key in the secret containing the password
324349
# ## @param users[0].pool_size Maximum number of server connections that can be established for this user
350+
# ## @param users[0].server_password_secret.name Name of the secret containing the server password
351+
# ## @param users[0].server_password_secret.key Key in the secret containing the server password
325352
# ## @param users[0].statement_timeout Maximum query duration. Dangerous, but protects against DBs that died in a non-obvious way.
326353
# users: []
327354
# # - username: "user"

0 commit comments

Comments
 (0)