Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

DB Guardian - Database Availability System

Purpose

The DB Guardian system ensures that services across the infrastructure are aware of the availability of centralised databases hosted on the Database Coordinator. It provides graceful startup and shutdown coordination between the database host and dependent services on other servers.

Entry Point

Architecture / Services / Scope

The system consists of two components:

  1. Guardian Server (runs on client servers)

    • WebSocket-over-TLS server that listens for commands from the coordinator
    • Requires a client certificate issued by the DB Guardian intermediate CA and only accepts the common names in server.database.guardian.allowedClientCommonNames
    • Runs as the unprivileged db-guardian user; a Polkit rule grants it start and stop on db-databases.target only
    • Executes drain/undrain commands by controlling db-databases.target
  2. Guardian Client (runs on the Database Coordinator)

    • WebSocket client that connects to all guardian servers over wss://
    • Verifies each guardian’s certificate chain and hostname, and presents the single db-guardian-coordinator client certificate
    • Sends undrain command after databases are online (start dependent services)
    • Sends drain command before database shutdown (stop dependent services)

How It Works

System Startup

  1. Client servers boot and run wait-for-db-databases.service
  2. This service waits (with retries) until PostgreSQL and Redis on the Database Coordinator are reachable
  3. Once databases are confirmed available, the service completes
  4. The db-databases.target is now ready to be activated
  5. When the Database Coordinator’s db-database-coordinator.service starts, it sends undrain to all clients
  6. Clients start db-databases.target, which starts all dependent services
  7. If any client is unreachable, the coordinator fails and retries every 30 seconds until every client acknowledges the undrain

Database Shutdown (Graceful Drain)

  1. When db-database-coordinator.service stops (before databases stop)
  2. It connects to all guardian servers via WebSocket
  3. Sends drain command to each server
  4. Guardian servers stop db-databases.target
  5. Dependent services stop gracefully before databases go down

db-database-coordinator.service is bound to PostgreSQL and Redis. A database stop or restart therefore stops the coordinator and triggers the drain. The coordinator is started again whenever either database starts, so a database restart cannot leave clients drained indefinitely.

Database Startup (Undrain)

  1. When databases come online on the Database Coordinator
  2. db-database-coordinator.service starts
  3. It sends undrain command to all guardian servers
  4. Guardian servers start db-databases.target
  5. All dependent services start

If a guardian server is not reachable yet, the coordinator exits non-zero and systemd retries it every 30 seconds. This closes the race where a client boots slower than the coordinator and would otherwise stay drained.

Reconciliation

While db-database-coordinator.service is active, db-database-coordinator-reconcile.timer re-sends undrain every 5 minutes. The command is idempotent, so clients that are already undrained are unaffected.

This covers failures that happen after the initial undrain succeeded:

  • A client boots while the databases are down, so its readiness check times out and its target fails. When the databases return, the next reconciliation starts the target again.
  • A client’s readiness check is restarted and fails, stopping the target. The next reconciliation starts it again.
  • A client’s target is stopped for any other reason while the coordinator is active.

The timer is started and stopped with the coordinator. An intentional drain stops the coordinator, which stops the timer, so reconciliation cannot undo the drain. If a reconciliation run fails, the timer retries at the next interval.

Systemd Units

On Client Servers

UnitTypeDescription
db-guardian.servicesimpleWebSocket server for receiving commands
db-databases.targettargetRepresents “databases are online”
wait-for-db-primary.serviceoneshotWaits for the database primary host to be reachable
wait-for-db-databases.serviceoneshotWaits for databases at boot (runs once)

The target is bound to wait-for-db-databases.service and is also wanted by it. Restarting the readiness check therefore restarts the target instead of leaving it inactive.

On the Database Coordinator

UnitTypeDescription
db-database-coordinator.serviceoneshotSends undrain on start, drain on stop; retries failed undrains
db-database-coordinator-reconcile.serviceoneshotRe-sends undrain to all clients
db-database-coordinator-reconcile.timertimerRuns reconciliation every 5 minutes while the coordinator is active

Protocol Reference

The guardian uses a JSON-based WebSocket protocol over mutual TLS. The TLS handshake authenticates both ends, so the first frame on a connection is a command:

Commands

Coordinator sends one of the supported actions (drain, undrain, or ping):

{ "type": "command", "action": "drain" }

Server responds:

{ "type": "response", "action": "<action>", "status": "ok", "message": "..." }

A client that sends any other message type receives an error response naming the type, and no command is executed. That is how a stale client that still opens with an authentication frame is diagnosed.

Secrets

Connections are mutually authenticated with TLS. There is no shared secret in the protocol: the coordinator proves its identity with a client certificate, and the guardian proves its own with a per-host server certificate. Both sides trust the DB Guardian Intermediate CA and nothing else, so a certificate issued by another service’s intermediate is rejected during the handshake.

IdentityLeaf profileIssued toAllowed by the guardian
<host> guardian serverserverAuthEvery guardian host, one leaf eachn/a
db-guardian-coordinatorclientAuthThe database primary host onlyCommon name allowlist

The guardian rejects a connection whose client certificate common name is not in server.database.guardian.allowedClientCommonNames, and refuses to start at all when that list is empty. An empty list is a Nix evaluation error, so a misconfigured generation cannot be built.

Declared secrets

Certificate material is stored as binary SOPS files and passed to the units as systemd credentials. No secret is a YAML value.

Secret keyOwnerGroupRestart unitPurpose
db-guardian/CA_CERTrootrootdb-guardian.service (clients), db-database-coordinator.service (coordinator)Intermediate CA certificate used as the trust store
db-guardian/SERVER_CERTrootrootdb-guardian.serviceServer leaf and intermediate chain
db-guardian/SERVER_KEYrootrootdb-guardian.serviceServer leaf private key
db-guardian/CLIENT_CERTrootrootdb-database-coordinator.serviceCoordinator leaf and intermediate chain
db-guardian/CLIENT_KEYrootrootdb-database-coordinator.serviceCoordinator leaf private key

The files live in hosts/server/<host>/db-guardian-tls/ and are encrypted to that host’s recipients only, so a guardian cannot decrypt another guardian’s key. Issue and rotate them with the procedure in Internal mTLS CA.

Provisioning certificates

The provision-db-guardian-certs helper issues the guardian leaves and SOPS-encrypts them into hosts/server/<host>/db-guardian-tls/. It reads the DB Guardian intermediate certificate from a KeePassXC vault and writes it to the work directory. The intermediate private key is streamed from the vault into memory and passed to OpenSSL on standard input, so it is never written to disk. The work directory still holds plaintext leaf keys until they are encrypted; protect it, and note that a --work-dir you supply is not removed. Create the intermediate with the offline Fleet root key first, as described in Internal mTLS CA; the helper only issues leaves.

provision-db-guardian-certs --vault /path/to/fleet.kdbx

The guardian hosts and the database primary are derived from the flake. Every non-primary server with PostgreSQL or Redis databases receives CA_CERT, SERVER_CERT, and SERVER_KEY; the database primary receives CA_CERT, CLIENT_CERT, and CLIENT_KEY. Pass host names to restrict the guardian set, --skip-client to leave the coordinator certificate alone, or --force to rotate. Pass --vault-password-file to avoid an interactive prompt. A key-file-only vault needs both --vault-key-file and --vault-no-password; --vault-key-file alone still prompts for the password.

The helper selects the KeePassXC entry intermediate/db-guardian/DB Guardian Intermediate CA and reads the cert.pem and key.pem attachments. Override the entry with --intermediate-entry, or the attachment names with --cert-attachment and --key-attachment. Whatever the layout, the helper rejects a self-signed certificate or another service’s intermediate before issuing anything.

Operational Notes / Assumptions

Configuration

Port: The guardian WebSocket server listens on port 9876 by default. This port is automatically opened to local subnets on servers with database dependencies.

Dependent Services: Dependent Services will be automatically populated with service names where there is a systemd.service.<name> defined from the names in server.database.postgres or server.database.redis. To manually add a service bind to the database availability target, add it to the server.database.dependentServices option. Services listed here will start only when db-databases.target is active, stop when it stops, and restart when the target restarts.

Troubleshooting

Checking Guardian Status

On client servers:

systemctl status db-guardian.service
systemctl status db-databases.target
systemctl status wait-for-db-databases.service
journalctl -u db-guardian.service -f

On the Database Coordinator:

systemctl status db-database-coordinator.service
journalctl -u db-database-coordinator.service

Manual Commands

To manually start/stop dependent services on a client:

systemctl start db-databases.target
systemctl stop db-databases.target

Common Issues

Guardian server won’t start:

  • Check that the certificate secrets decrypted and are non-empty: for f in /run/secrets/db-guardian/*; do test -s "$f" || echo "empty: $f"; done
  • Check that at least one client common name is configured: systemctl show db-guardian.service -p ExecStart
  • A startup line Refusing to start: at least one --allowed-cn is required means the allowlist came through empty.

Connections fail with a certificate error:

  • certificate verify failed: unable to get local issuer certificate on the guardian means the coordinator’s chain does not reach the DB Guardian intermediate CA; confirm CLIENT_CERT carries the leaf followed by the intermediate.
  • Hostname mismatch or IP address mismatch on the coordinator means the dialled name is missing from the guardian’s server certificate; the SAN set must contain the bare hostname used in --hosts.
  • certificate has expired means the leaf is past its validity; reissue it with the procedure in Internal mTLS CA.

Guardian rejects the client with certificate common name ... is not in the allowlist:

  • The client certificate common name is not listed in server.database.guardian.allowedClientCommonNames; the observed name is in the log line.

Services not starting after boot:

  • Check wait service logs: journalctl -u wait-for-db-databases.service
  • Verify network connectivity to the Database Coordinator on ports 5432 (Postgres) and 6379 (Redis)
  • Ensure the Database Coordinator has sent the undrain command
  • Check the coordinator is active: systemctl status db-database-coordinator.service
  • If the coordinator is inactive, start it: systemctl start db-database-coordinator.service

Clients stay drained after a database restart:

  • Check the coordinator’s last start and stop times: systemctl show db-database-coordinator.service -p ActiveEnterTimestamp,InactiveEnterTimestamp,Result
  • Check for repeated undrain attempts: journalctl -u db-database-coordinator.service
  • Check the reconciliation timer: systemctl status db-database-coordinator-reconcile.timer
  • Check reconciliation runs: journalctl -u db-database-coordinator-reconcile.service
  • The coordinator is restarted by postgresql.service and redis.service, retries failed undrains every 30 seconds, and re-asserts undrain every 5 minutes while active

Drain or undrain fails with a Polkit error:

  • Access denied or an org.freedesktop.systemd1.manage-units denial in journalctl -u db-guardian.service means the Polkit rule did not match; confirm the daemon runs as db-guardian and that the request names db-databases.target with verb start or stop.
  • journalctl -u polkit records the action, unit and verb the rule evaluated.