Every host runs node_exporter, cAdvisor (now against Podman), Alloy and the security-metrics script, on its private address. Each service declares its own expected ports, users and privileged containers next to its definition, so the security baseline follows the config instead of a separate inventory list. On ops: Prometheus, Loki, Grafana, the blackbox prober and the Postgres exporter, with the same scrape jobs, probes, retention, dashboards and 33 alert rules. Two rules matched container-era names and now match the native ones: Forgejo's log is unit forgejo.service, Grafana's route is grafana@file. Grafana keeps its default secret_key on production, because its database was written with it; rotating it is a separate step. Prometheus, Grafana and Alloy come from nixos-unstable: their data is from newer versions than 26.05 ships. LLMNR and mDNS are off: the baseline flagged them as public listeners. Tried on the rehearsal pair: all 28 scrape targets up, logs from both hosts in Loki, Grafana WireGuard-only, no unexpected port, user or container on either host. Assisted-by: Claude:opus-5-5
101 lines
3.2 KiB
Nix
101 lines
3.2 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
|
|
{
|
|
sops.secrets."postgres/asxpio_password" = { };
|
|
sops.secrets."postgres/bingo_password" = { };
|
|
|
|
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
|
|
'';
|
|
};
|
|
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"''
|
|
];
|
|
}
|