Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ansible/files/adminapi.sudoers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Cmnd_Alias PGBOUNCER = /bin/systemctl start pgbouncer.service, /bin/systemctl st
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/common.sh
%adminapi ALL= NOPASSWD: /etc/adminapi/pg_upgrade_scripts/pgsodium_getkey.sh
%adminapi ALL= NOPASSWD: /usr/bin/systemctl daemon-reload
%adminapi ALL= NOPASSWD: /usr/local/lib/supabase-admin-agent/pgdata-chown
%adminapi ALL=(postgres) NOPASSWD: /usr/local/lib/supabase-admin-agent/pgdata-signal
%adminapi ALL= NOPASSWD: /usr/bin/systemctl start postgresql.service
%adminapi ALL= NOPASSWD: /usr/bin/systemctl stop postgresql.service
%adminapi ALL= NOPASSWD: /usr/bin/systemctl reload postgresql.service
%adminapi ALL= NOPASSWD: /usr/bin/systemctl restart postgresql.service
%adminapi ALL= NOPASSWD: /usr/bin/systemctl show -p NRestarts postgresql.service
Expand Down
7 changes: 1 addition & 6 deletions ansible/files/pgbackrest_config/pgbackrest.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ archive-copy = y
backup-standby = prefer
compress-type = zst
delta = y
expire-auto = n
expire-auto = y
link-all = y
log-level-console = info
log-level-file = detail
log-subprocess = y
resume = n
start-fast = y

[supabase]
pg1-path = /var/lib/postgresql/data
pg1-socket-path = /run/postgresql
pg1-user = supabase_admin
9 changes: 9 additions & 0 deletions ansible/files/pgbackrest_config/pgbackrest.logrotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/var/log/pgbackrest/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0660 pgbackrest postgres
}
2 changes: 1 addition & 1 deletion ansible/files/postgresql_config/pg_hba.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
# TYPE DATABASE USER ADDRESS METHOD

# trust local connections
local all supabase_admin scram-sha-256
local all supabase_admin trust
local all all peer map=supabase_map
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
Expand Down
37 changes: 37 additions & 0 deletions ansible/files/supabase_admin_agent_config/pgdata-chown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash
# pgdata-chown — transfers PGDATA ownership for pgBackRest restore operations.
#
# Called via sudo by supabase-admin-agent (running as adminapi). Only two
# actions are accepted, and the target path must resolve to /data/pgdata or a
# path beneath it. realpath(1) is used to expand symlinks before the check,
# which prevents directory-traversal attacks (e.g. /data/pgdata/../../etc/sudoers).
#
# Usage: pgdata-chown <to-pgbackrest|to-postgres> <path>
set -euo pipefail

if [[ $# -ne 2 ]]; then
echo "usage: pgdata-chown <to-pgbackrest|to-postgres> <path>" >&2
exit 1
fi

ACTION="$1"
TARGET="$2"

REAL=$(realpath "$TARGET")
if [[ "$REAL" != "/data/pgdata" && "$REAL" != /data/pgdata/* ]]; then
echo "error: '${TARGET}' resolves to '${REAL}', which is not under /data/pgdata" >&2
exit 1
fi

case "$ACTION" in
to-pgbackrest)
exec /usr/bin/chown -R pgbackrest:pgbackrest "$REAL"
;;
to-postgres)
exec /usr/bin/chown -R postgres:postgres "$REAL"
Comment on lines +27 to +31
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
to-pgbackrest)
exec /usr/bin/chown -R pgbackrest:pgbackrest "$REAL"
;;
to-postgres)
exec /usr/bin/chown -R postgres:postgres "$REAL"
to-pgbackrest|to-postgres)
exec /usr/bin/chown -R ${ACTION:3}:postgres "$REAL" ;;

we've been setting everything to group postgres thus far

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pgBackRest binary in the wrapper is executed as the pgbackrest user so the PGDATA path needs to be altered for restores and relics setup.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i get that. it'd still be owner pgbackrest with my change?

;;
*)
echo "error: unknown action '${ACTION}'; expected to-pgbackrest or to-postgres" >&2
exit 1
;;
esac
35 changes: 35 additions & 0 deletions ansible/files/supabase_admin_agent_config/pgdata-signal
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
# pgdata-signal — creates or removes PostgreSQL recovery/standby signal files.
# Called via sudo (as postgres) by supabase-admin-agent (running as adminapi).
#
# Signal file paths are hardcoded to prevent path injection. No external
# path argument is accepted.
#
# Usage: pgdata-signal <create|remove> <recovery|standby>
set -euo pipefail

if [[ $# -ne 2 ]]; then
echo "usage: pgdata-signal <create|remove> <recovery|standby>" >&2
exit 1
fi

ACTION="$1"
SIGNAL_TYPE="$2"

case "$SIGNAL_TYPE" in
recovery) FILE="/data/pgdata/recovery.signal" ;;
standby) FILE="/data/pgdata/standby.signal" ;;
Comment on lines +20 to +21
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
recovery) FILE="/data/pgdata/recovery.signal" ;;
standby) FILE="/data/pgdata/standby.signal" ;;
recovery|standby) FILE="/data/pgdata/${SIGHNAL_TYPE}.signal" ;;

*)
echo "error: unknown signal type '${SIGNAL_TYPE}'; expected recovery or standby" >&2
exit 1
;;
esac

case "$ACTION" in
create) exec /usr/bin/touch "$FILE" ;;
remove) exec /usr/bin/rm -f "$FILE" ;;
Comment on lines +28 to +30
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you make ACTION either touch or rm then you can:

Suggested change
case "$ACTION" in
create) exec /usr/bin/touch "$FILE" ;;
remove) exec /usr/bin/rm -f "$FILE" ;;
case "$ACTION" in
touch|rm) exec /usr/bin/${ACTION} -f "$FILE" ;;

*)
echo "error: unknown action '${ACTION}'; expected create or remove" >&2
exit 1
;;
esac
24 changes: 24 additions & 0 deletions ansible/tasks/internal/supabase-admin-agent.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,30 @@
dest: /etc/sudoers.d/supabase-admin-agent
mode: "0440"

- name: supabase-admin-agent - pgbackrest helper scripts dir
file:
path: /usr/local/lib/supabase-admin-agent
state: directory
owner: root
group: root
mode: "0755"

- name: supabase-admin-agent - pgdata-chown script
copy:
src: files/supabase_admin_agent_config/pgdata-chown
dest: /usr/local/lib/supabase-admin-agent/pgdata-chown
owner: root
group: root
mode: "0700"

- name: supabase-admin-agent - pgdata-signal script
copy:
src: files/supabase_admin_agent_config/pgdata-signal
dest: /usr/local/lib/supabase-admin-agent/pgdata-signal
owner: root
group: root
mode: "0700"

- name: Setting arch (x86)
set_fact:
arch: "x86"
Expand Down
19 changes: 18 additions & 1 deletion ansible/tasks/setup-pgbackrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
path: "{{ backrest_dir }}"
state: directory
loop:
- /etc/pgbackrest/conf.d
- /var/lib/pgbackrest
- /var/spool/pgbackrest
- /var/log/pgbackrest
Expand All @@ -57,6 +56,16 @@
when:
- nixpkg_mode

- name: Create pgBackRest conf.d directory with setgid
ansible.legacy.file:
group: postgres
mode: '02770'
owner: pgbackrest
path: /etc/pgbackrest/conf.d
state: directory
when:
- nixpkg_mode

- name: Symlink pgbackrest.conf
ansible.legacy.file:
force: true
Expand All @@ -82,6 +91,14 @@
when:
- stage2_nix

- name: pgBackRest - logrotate config
ansible.legacy.copy:
src: files/pgbackrest_config/pgbackrest.logrotate
dest: /etc/logrotate.d/pgbackrest
owner: root
group: root
mode: '0644'
Comment on lines +95 to +100
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ansible.legacy.copy:
src: files/pgbackrest_config/pgbackrest.logrotate
dest: /etc/logrotate.d/pgbackrest
owner: root
group: root
mode: '0644'
ansible.legacy.copy:
dest: /etc/logrotate.d/pgbackrest
group: root
mode: '0644'
owner: root
src: files/pgbackrest_config/pgbackrest.logrotate


- name: Create pgBackRest wrapper script
ansible.builtin.copy:
content: |
Expand Down
Loading