Accept a brokered libpq environment as connection information
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 1s

AUDIT-WP-0005-T02. The rapp-postgres credential playbook has the
railiance-platform broker inject PGUSER/PGPASSWORD/PGHOST/PGPORT/PGDATABASE
into the child process. audit-core only accepted AUDIT_CORE_DATABASE_URL, so
consuming a brokered lease would have meant assembling a DSN by hand from the
injected variables - putting the credential back into audit-core's own
configuration, which is what the lane exists to avoid.

An empty conninfo lets libpq read those variables directly, so a brokered lease
now needs no DSN at all. AUDIT_CORE_DATABASE_URL still works for local and test
use. Missing both is a clear startup error naming each option.

Tests 80 -> 82.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tegwick 2026-08-11 23:24:25 +02:00
parent ec8bacbdeb
commit fc48378a3f
4 changed files with 67 additions and 10 deletions

View file

@ -140,9 +140,17 @@ class PostgresAuditBackend:
statement_timeout_ms: int = 30_000,
migrate: bool = True,
) -> None:
self.dsn = dsn or os.environ.get("AUDIT_CORE_DATABASE_URL") or ""
if not self.dsn:
raise ValueError("a DSN is required (AUDIT_CORE_DATABASE_URL)")
# An empty conninfo is valid: libpq then reads PGHOST/PGUSER/PGPASSWORD/
# PGPORT/PGDATABASE from the environment. That is exactly the shape the
# railiance-platform credential broker injects into a child process, so
# a brokered lease needs no DSN assembled by hand — and no credential
# ever passes through audit-core's own configuration.
self.dsn = dsn if dsn is not None else os.environ.get("AUDIT_CORE_DATABASE_URL", "")
if not self.dsn and not _libpq_env_present():
raise ValueError(
"no connection information: set AUDIT_CORE_DATABASE_URL, or supply "
"PGHOST/PGUSER/PGDATABASE (as the credential broker does)"
)
if not schema.isidentifier():
raise ValueError(f"unsafe schema name: {schema!r}")
self.schema = schema
@ -393,6 +401,11 @@ class PostgresAuditBackend:
raise BackendUnavailableError(str(exc)) from exc
def _libpq_env_present() -> bool:
"""Whether libpq has enough in the environment to connect on its own."""
return bool(os.environ.get("PGHOST") and os.environ.get("PGUSER"))
def _timestamp(value: str | None) -> datetime | None:
if not value:
return None