feat(agent-relay): databaseGuard refuses wrong database credentials before install (chart 0.7.84)

Merged
KH
8d ago
+1173-7

Why

A sandbox rehearsal of a real customer install (EKS + RDS PG17, infra 13488a58, examples/aws) hand-filled examples/aws/secrets/01..05 with every database URL logging in as postgres, the RDS master, and never created the per-service logins. helm install succeeded, then one cause showed up as four unrelated-looking failures: relaycast-migrate exit 65 with a long FAIL list, relay-control-migrate's get_role_oid stack trace, hoopdb-migrate's runtime_role_missing, and everything else in CrashLoopBackOff. Working out that every URL used postgres took diagnose-install.py and a jq one-liner the next day.

What a customer sees now

helm install fails before anything is applied:

Error: INSTALLATION FAILED: execution error at (agent-relay/templates/db-guard.yaml:10:4):
AR_DB_LOGIN: the database credentials in this release's Secrets are wrong; nothing was installed or changed:
  - agent-relay-secrets: database-url logs in as "postgres" (the RDS master user). Each service needs its own login.
  - agent-relay-secrets: database-migrations-url logs in as "postgres" (the RDS master user). Each service needs its own login.
  ... (one line per URL, 10 on examples/aws)
Run scripts/selfhost/configure-rds.sh, which creates the logins and writes these Secrets, or see docs/self-hosted-rds-manual-setup.md §1.3 (https://agentrelay.com/docs/self-hosting/troubleshooting#database-logins). Then run helm again. (databaseGuard.enabled=false turns this check off, only for a setup it does not understand.)

When the render check passes but a login is still wrong (a master under another name, a login granted rds_superuser, a wrong password, an unreachable host, a CA mismatch), the pre-install hook stops the install. kubectl -n agent-relay logs job/agent-relay-db-guard shows:

Agent Relay database privilege check (databaseGuard.privilegeCheck)
  FAIL agent-relay-secrets: database-url            login=custom_admin  logs in as "custom_admin", which has CREATEROLE: a database master/administrator login. Each service needs its own login (failed: not_createrole: role has CREATEROLE)
  ok   agent-relay-secrets: database-owner-url      login=relaycast_owner  relaycast owner identity
  ...
AR_DB_LOGIN: the database credentials in this release's Secrets are wrong; the install/upgrade was stopped before any workload changed:
  - agent-relay-secrets: database-url logs in as "custom_admin", which has CREATEROLE: a database master/administrator login. Each service needs its own login.
Run scripts/selfhost/configure-rds.sh, ... Then run helm again.

Helm itself prints only failed pre-install: resource Job/agent-relay/agent-relay-db-guard not ready. status: Failed; the docs point to kubectl logs.

How

  • Render time (templates/_db-guard.tpl): lookup of every Secret the enabled components read a DB URL from. The targets are derived from values (relay-control, relaycast on postgres, external-HoopDb sessions-pipeline: worker, migrations, read API, ingest edge, search indexer and bootstrap), so the hoopdb-layout-values.yaml overlay and renamed Secrets are followed. It checks for a master login (databaseGuard.masterLogins), two jobs sharing one login, a missing Secret or key, and the wrong shape: a bare URL where {"hoopdb_url":…} / {"supabase_url":…} is expected, or JSON in a URL key. Exemptions: the HoopDb owner on rdsLayout: master-owner (the legacy layout) may be the master, and Secrets created by this release's ExternalSecrets may be absent. lookup is empty under helm template, client --dry-run and helm lint, so the check only runs when kube-system answers. --dry-run=server runs it.
  • Install time (templates/db-guard.yaml, files/db-guard/check.mjs): a pre-install/pre-upgrade hook Job that runs in the relaycast-self-host image the release already pins, so there's no new image to mirror. It connects once per URL using relaycast's own verify-full TLS (postgresTlsOptions, the rds-ca ConfigMap) and runs relaycast's verifyIdentity from @relaycast/engine/adapters/postgres, the single source of truth with no copied SQL. Relaycast's two logins get the full owner/runtime checks. Every other login gets the attribute and privileged-membership checks (not_superuser, not_createrole, not_createdb, not_replication, not_bypassrls, no_privileged_memberships, login, session_user). The Job is read-only, mounts no SA token, mounts only the URL keys, and reports a missing Secret instead of leaving the pod Pending. With before-hook-creation,hook-succeeded, a failed Job stays around to be read.
  • Opt-out: databaseGuard.enabled=false turns off both checks; databaseGuard.privilegeCheck.enabled=false turns off only the hook. NOTES warns when the guard is off.
  • CI hook ban (scripts/ci/charts.sh): non-test hooks are banned because Alchemy renders with --no-hooks. This hook is advisory, since the migrate Jobs enforce the same rules without it, so the ban now also admits pre-install/pre-upgrade hooks annotated agentworkforce.dev/preflight: "true". Any other hook still fails. I checked both directions: the annotated hook passes, and the same objects without the annotation fail. Reviewer call: this is the first non-test hook in the umbrella.

Tests

  • ci/agent-relay/db-guard/kind.sh, wired into the new db-guard-kind job in lint.yml: an RDS-shaped PostgreSQL (rdsadmin superuser, master with CREATEROLE/CREATEDB in a NOLOGIN rds_superuser) with TLS from a test CA served as rds-ca, plus real helm install. 13/13 pass. Cases: master in all 10 URLs, no release left behind, template and client dry-run are a no-op, opt-out, shared login, bare URL in 04, missing Secret, legacy master-owner layout, unnamed administrator caught by the hook, wrong password, correct logins over verify-full (install proceeds and the hook Job is deleted), and an upgrade regression.
  • scripts/selfhost/tests/rds-setup-postgres-test.sh with DB_GUARD=1: the hook script runs against the logins the real rds-setup.sh pre creates, and all 10 pass. The same Secrets rewritten to the master, with the name check off, all fail on live privileges, relaycast's owner checks included. 48/48 pass across PG 16/17/18 Ɨ psql 17/18. This test stays opt-in: it needs a sessions-pipeline checkout.
  • Also run: scripts/ci/charts.sh static agent-relay (88 cases), ci/agent-relay/render.sh, all ci/agent-relay/test_*.py, the registry check and test.sh, the customer release gate (38), release consistency, support tests, and shellcheck on the new and changed scripts. Only pre-existing warnings remain. Chart.lock is unchanged.

Coordination

  • This PR doesn't touch Chart.yaml dependency entries or the CA rendering. The hook mounts whatever relaycast.postgresCa names, today the operator-created rds-ca, with optional: true. When the chart-owned RDS CA lands, a first install's pre-install hook runs before chart-rendered ConfigMaps exist: with no CA, the hook skips the live check for each URL and says so. After that lands, a follow-up should render a hook-weighted copy of the CA so first installs get the full check.
  • Part of a series: B (actionable migrate errors in relaycast / relay-control / sessions-pipeline), C (helm test status table), and D (guided install as the primary path) follow.

šŸ¤– Generated with Claude Code


Open in [code]smith Ā· Need help on this PR? Tag @codesmith-bot with what you need.


Summary by cubic

Adds databaseGuard to the agent-relay chart (0.7.84) so helm install and upgrade fail before deploying anything when a Secret's database URL is wrong. Previously, wrong credentials — for example, every URL logging in as the RDS master postgres — installed cleanly and surfaced later as unrelated migrate failures and CrashLoopBackOffs.

Behavior

  • Refuses at render time a master login, two jobs sharing a login, a missing Secret or key, and a wrong value shape; the check is a no-op under helm template and client dry-run (--dry-run=server runs it). The probe is namespaced, so the documented namespace-scoped operator Role works.
  • A pre-install/pre-upgrade hook Job connects once per URL over verify-full TLS and checks real privileges with relaycast's own verifyIdentity rules. Each target uses its own component's CA (falling back to relaycast.postgresCa).
  • URLs are parsed with regexes, never urlParse, whose errors would echo the URL and password. Render checks also refuse a URL with a login but no host.
  • Opt out with databaseGuard.enabled=false (both checks) or databaseGuard.privilegeCheck.enabled=false (hook only); NOTES warns when the guard is off.
  • scripts/ci/charts.sh now permits pre-install/pre-upgrade hooks annotated agentworkforce.dev/preflight: "true"; other non-test hooks still fail.

Testing

  • New kind end-to-end test with an RDS-shaped PostgreSQL and TLS (17 cases, including upgrades, opt-out, operator Role, hostless URL, shared key across roles, and migrations off).
  • Opt-in DB_GUARD=1 mode in the RDS setup test runs the hook against the logins rds-setup.sh pre creates; 48/48 across PG 16/17/18 Ɨ psql 17/18.