diff options
| author | Giacomo Leidi <goodoldpaul@autistici.org> | 2025-04-29 17:51:10 +0200 |
|---|---|---|
| committer | Maxim Cournoyer <maxim.cournoyer@gmail.com> | 2025-05-02 15:32:25 +0900 |
| commit | 9d216d2ae9f9a4ff2935c23a209499b17dcb13a5 (patch) | |
| tree | d36234b4946a07818536227b99fdf7c791944b74 /gnu/tests | |
| parent | b2b7d2a3275d5ba866ae7fecac928ed4bd416beb (diff) | |
services: postgresql-role: Add support for password files.
This commit adds a password-file to the postgresql-role field. It
allows users to provision Postgres roles with a set password.
* gnu/services/databases.scm (postgresql-role): Add password-file field.
(postgresql-role-configuration): Add requirement field.
(postgresql-create-roles): Add support for setting passwords from a
file without leaking passwords to the command line.
(postgresql-role-shepherd-service): Add support for customizable
requirements.
(postgresql-role-service-type): Pass on postgresql-role-configuration
fields values by default, this way user configured fields are not lost.
* gnu/tests/databases.scm: Test it.
* doc/guix.texi: Document the new field and fix the extension point example.
Change-Id: I3aabaa10b0c5e826c5aa874e5649e25a3508a585
Signed-off-by: Maxim Cournoyer <maxim.cournoyer@gmail.com>
Diffstat (limited to 'gnu/tests')
| -rw-r--r-- | gnu/tests/databases.scm | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/gnu/tests/databases.scm b/gnu/tests/databases.scm index fd5041344b6..0b2a8acfbba 100644 --- a/gnu/tests/databases.scm +++ b/gnu/tests/databases.scm | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> | 2 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> |
| 3 | ;;; Copyright © 2020, 2022 Marius Bakke <marius@gnu.org> | 3 | ;;; Copyright © 2020, 2022 Marius Bakke <marius@gnu.org> |
| 4 | ;;; Copyright © 2025 Giacomo Leidi <goodoldpaul@autistici.org> | ||
| 4 | ;;; | 5 | ;;; |
| 5 | ;;; This file is part of GNU Guix. | 6 | ;;; This file is part of GNU Guix. |
| 6 | ;;; | 7 | ;;; |
| @@ -142,6 +143,8 @@ | |||
| 142 | 143 | ||
| 143 | (define %postgresql-os | 144 | (define %postgresql-os |
| 144 | (simple-operating-system | 145 | (simple-operating-system |
| 146 | (extra-special-file "/password" | ||
| 147 | (plain-file "password" "hello")) | ||
| 145 | (service postgresql-service-type | 148 | (service postgresql-service-type |
| 146 | (postgresql-configuration | 149 | (postgresql-configuration |
| 147 | (postgresql postgresql) | 150 | (postgresql postgresql) |
| @@ -158,6 +161,10 @@ | |||
| 158 | (roles | 161 | (roles |
| 159 | (list (postgresql-role | 162 | (list (postgresql-role |
| 160 | (name "root") | 163 | (name "root") |
| 164 | (create-database? #t)) | ||
| 165 | (postgresql-role | ||
| 166 | (name "a_database") | ||
| 167 | (password-file "/password") | ||
| 161 | (create-database? #t)))))))) | 168 | (create-database? #t)))))))) |
| 162 | 169 | ||
| 163 | (define (run-postgresql-test) | 170 | (define (run-postgresql-test) |
| @@ -230,17 +237,53 @@ | |||
| 230 | (marionette-eval | 237 | (marionette-eval |
| 231 | '(begin | 238 | '(begin |
| 232 | (use-modules (gnu services herd) | 239 | (use-modules (gnu services herd) |
| 240 | (srfi srfi-1) | ||
| 233 | (ice-9 popen)) | 241 | (ice-9 popen)) |
| 234 | (current-output-port | 242 | (current-output-port |
| 235 | (open-file "/dev/console" "w0")) | 243 | (open-file "/dev/console" "w0")) |
| 244 | (every | ||
| 245 | (lambda (role) | ||
| 246 | (let* ((port (open-pipe* | ||
| 247 | OPEN_READ | ||
| 248 | #$(file-append postgresql "/bin/psql") | ||
| 249 | "-tA" "-c" | ||
| 250 | (string-append | ||
| 251 | "SELECT 1 FROM pg_database WHERE" | ||
| 252 | " datname='" role "'"))) | ||
| 253 | (output (get-string-all port))) | ||
| 254 | (close-pipe port) | ||
| 255 | (string-contains output "1"))) | ||
| 256 | '("root" "a_database"))) | ||
| 257 | marionette)) | ||
| 258 | |||
| 259 | (test-assert "database use fails without a password" | ||
| 260 | (marionette-eval | ||
| 261 | '(begin | ||
| 262 | (setgid (passwd:gid (getpwnam "alice"))) | ||
| 263 | (setuid (passwd:uid (getpw "alice"))) | ||
| 264 | (not (zero? | ||
| 265 | (system* #$(file-append postgresql "/bin/psql") | ||
| 266 | "-tA" "-h" "localhost" "-U" "a_database" "-c" | ||
| 267 | (string-append "SELECT 1 FROM pg_database " | ||
| 268 | "WHERE datname='a_database'"))))) | ||
| 269 | marionette)) | ||
| 270 | |||
| 271 | (test-assert "database passwords are set" | ||
| 272 | (marionette-eval | ||
| 273 | '(begin | ||
| 274 | (use-modules (ice-9 popen)) | ||
| 275 | (setgid (passwd:gid (getpwnam "alice"))) | ||
| 276 | (setuid (passwd:uid (getpw "alice"))) | ||
| 277 | (setenv "PGPASSWORD" | ||
| 278 | (call-with-input-file "/password" get-string-all)) | ||
| 236 | (let* ((port (open-pipe* | 279 | (let* ((port (open-pipe* |
| 237 | OPEN_READ | 280 | OPEN_READ |
| 238 | #$(file-append postgresql "/bin/psql") | 281 | #$(file-append postgresql "/bin/psql") |
| 239 | "-tA" "-c" "SELECT 1 FROM pg_database WHERE | 282 | "-U" "a_database" "-tA" "-h" "localhost" "-c" |
| 240 | datname='root'")) | 283 | "SELECT 1 FROM pg_database WHERE datname='a_database'")) |
| 241 | (output (get-string-all port))) | 284 | (output (get-string-all port))) |
| 242 | (close-pipe port) | 285 | (close-pipe port) |
| 243 | (string-contains output "1"))) | 286 | (string=? output "1\n"))) |
| 244 | marionette)) | 287 | marionette)) |
| 245 | 288 | ||
| 246 | (test-end)))) | 289 | (test-end)))) |
