All checks were successful
nixos / nixos (push) Successful in 24s
asxpio, datacenterbingo, blog and czsk run on asxpio-services now and reach Postgres and Garage over the private network; vhpbot, the last container, uses neither. So Postgres no longer admits Podman's networks, the firewall no longer opens 5432 and 3900 to them, and the container plumbing for the Docker-era storage stack goes: the postgres/garage host aliases and the empty storage network. Assisted-by: Claude:opus-5-5
100 lines
3.3 KiB
Nix
100 lines
3.3 KiB
Nix
# Postgres for the apps. Roles and grants are set on every start from
|
|
# sops, so a password is changed by changing the secret.
|
|
#
|
|
# asxpio owns database asxpio (site, invoices, datacenter bingo admin)
|
|
# bingo read-only on the bingo tables, for datacenterbingo.xyz
|
|
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
let
|
|
pg = config.services.postgresql.package;
|
|
in
|
|
{
|
|
# A changed password is set by restarting postgres-roles
|
|
sops.secrets."postgres/asxpio_password".restartUnits = [ "postgres-roles.service" ];
|
|
sops.secrets."postgres/bingo_password".restartUnits = [ "postgres-roles.service" ];
|
|
|
|
services.postgresql = {
|
|
enable = true;
|
|
package = pkgs.postgresql_18;
|
|
# The apps on asxpio-services connect over the private network; the
|
|
# firewall admits only the private networks to the port
|
|
enableTCPIP = true;
|
|
authentication = ''
|
|
host asxpio bingo,asxpio 10.10.0.20/32 scram-sha-256
|
|
'';
|
|
};
|
|
asxpio.security.loginUsers = [ "postgres" ];
|
|
asxpio.security.ports = [ "tcp/5432/.postgres-wrapp" ];
|
|
|
|
asxpio.waitForData = [
|
|
"postgresql"
|
|
"postgresql-setup"
|
|
"postgres-roles"
|
|
];
|
|
|
|
systemd.services.postgres-roles = {
|
|
description = "Postgres roles, databases and grants";
|
|
after = [ "postgresql.target" ];
|
|
requires = [ "postgresql.target" ];
|
|
wantedBy = [ "multi-user.target" ];
|
|
path = [ pg ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
User = "postgres";
|
|
LoadCredential = [
|
|
"asxpio:${config.sops.secrets."postgres/asxpio_password".path}"
|
|
"bingo:${config.sops.secrets."postgres/bingo_password".path}"
|
|
];
|
|
};
|
|
script = ''
|
|
set -eu
|
|
role() {
|
|
psql -v ON_ERROR_STOP=1 -v role="$1" -v pw="$(cat "$CREDENTIALS_DIRECTORY/$1")" <<'SQL'
|
|
SELECT format('CREATE ROLE %I LOGIN', :'role')
|
|
WHERE NOT EXISTS (SELECT FROM pg_roles WHERE rolname = :'role') \gexec
|
|
SELECT format('ALTER ROLE %I WITH LOGIN PASSWORD %L', :'role', :'pw') \gexec
|
|
SQL
|
|
}
|
|
role asxpio
|
|
role bingo
|
|
|
|
psql -tAc "SELECT 1 FROM pg_database WHERE datname = 'asxpio'" | grep -q 1 \
|
|
|| psql -c 'CREATE DATABASE asxpio OWNER asxpio'
|
|
psql -d asxpio -c 'CREATE EXTENSION IF NOT EXISTS citext'
|
|
|
|
# Tables that do not exist yet (fresh install, asxpio not migrated)
|
|
# are granted on the next start
|
|
psql -c 'ALTER ROLE bingo SET default_transaction_read_only = on'
|
|
psql -d asxpio -v ON_ERROR_STOP=1 <<'SQL'
|
|
GRANT CONNECT ON DATABASE asxpio TO bingo;
|
|
GRANT USAGE ON SCHEMA public TO bingo;
|
|
DO $$
|
|
DECLARE t text;
|
|
BEGIN
|
|
FOREACH t IN ARRAY ARRAY['bingo_datacenters', 'bingo_incidents', 'bingo_monitor_targets',
|
|
'bingo_monitor_samples', 'bingo_monitor_buckets'] LOOP
|
|
IF to_regclass('public.' || t) IS NOT NULL THEN
|
|
EXECUTE format('GRANT SELECT ON public.%I TO bingo', t);
|
|
ELSE
|
|
RAISE NOTICE 'bingo: % missing, not granted yet', t;
|
|
END IF;
|
|
END LOOP;
|
|
END $$;
|
|
SQL
|
|
'';
|
|
};
|
|
|
|
asxpio.backup.hookPackages = [
|
|
pg
|
|
pkgs.util-linux
|
|
];
|
|
asxpio.backup.hooks = [
|
|
''runuser -u postgres -- pg_dumpall > "$STAGE/pg_dumpall.sql"''
|
|
''runuser -u postgres -- pg_dump -Fc asxpio > "$STAGE/asxpio.dump"''
|
|
];
|
|
}
|