All checks were successful
nixos / nixos (push) Successful in 26s
asxpi/asxpio now exports the app (bundlerEnv, ImageMagick 7) and a module running the site and its monitor worker as hardened systemd services. services runs the site on :3001 next to the container on ops; no route yet, and the monitor stays off until the container's stops. pg_hba lets the asxpio role in from 10.10.0.20 too (restarts Postgres once); the settings and secrets, copied from the container's environment with the database and Garage on ops' private address, are in services' sops file. Renovate automerges its bumps like the other apps'. Assisted-by: Claude:opus-5-5
103 lines
3.4 KiB
Nix
103 lines
3.4 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;
|
|
# App containers connect from Podman's networks; the firewall decides
|
|
# who reaches the port
|
|
enableTCPIP = true;
|
|
authentication = ''
|
|
host all all 10.88.0.0/16 scram-sha-256
|
|
host all all 10.89.0.0/16 scram-sha-256
|
|
host asxpio bingo,asxpio 10.10.0.20/32 scram-sha-256
|
|
'';
|
|
};
|
|
networking.firewall.interfaces."podman*".allowedTCPPorts = [ 5432 ];
|
|
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"''
|
|
];
|
|
}
|