Skip to content

feat(storage): sign in to PostgreSQL with a Microsoft Entra ID token - #173

Merged
NSchatz merged 1 commit into
mainfrom
feat/postgres-entra-auth
Nov 30, 2026
Merged

NSchatz merged 1 commit into
mainfrom
feat/postgres-entra-auth

Conversation

@NSchatz

@NSchatz NSchatz commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

What

A storage.postgres block now takes auth: entra (or auth: { env: NAME, default: password }). With it, Pathways signs in to Azure Database for PostgreSQL with a Microsoft Entra ID access token in place of a password. It covers the engine's pool (pathways start) and pathways migrate, which both open the store through dialectFor.

  • src/storage/entra.ts (new): entraPassword(credential) returns the async password function pg calls for each new connection. It asks for a token for https://ossrdbms-aad.database.windows.net/.default, reuses it until 5 minutes before expiresOnTimestamp, then asks again; connections opened together share one request. When no token can be had, it rejects with a fixed text and the code ERR_PATHWAYS_ENTRA_TOKEN, with no cause, so neither the credential's own text nor a token reaches an error or a log. defaultEntraCredential() makes DefaultAzureCredential from @azure/identity (already an optional peer dependency, and in the image), which reads AZURE_CLIENT_ID for a user-assigned identity.
  • src/storage/dialects.ts: with auth: entra, the URL must name a user (the database user) and hold no password. The URL is taken apart into host, port, user, database and its other options, with the password function beside them. pg reads a connection string's empty password over the one it is given, so connectionString cannot carry the function. A missing @azure/identity is a ConfigError naming the package. Without auth: entra, pg gets the same options as before.
  • src/config/schema.ts, src/config/load.ts: the auth key (password or entra, password by default), refused on mysql and sqlserver blocks. ServerStorage gains an optional auth, set only for entra, so a block without it loads to the same object as before.
  • Docs: a "PostgreSQL with Microsoft Entra ID" section on the storage page, the key in the configuration reference, a README next-release bullet, and a patch changeset.

The names follow the Azure Terraform module's: its postgres.database_user output is the user in the URL (postgres://<database_user>@<host>:5432/pathways), and its identity's client_id is the value for AZURE_CLIENT_ID.

Why

A PostgreSQL server that takes only Entra ID sign-in has no password to put in the URL. Pathways read its URL once at start and sent the password in it, so neither the engine nor the migrate job could sign in to such a server. Access tokens expire, so each new connection needs a current one.

Proof

  • pnpm typecheck, pnpm lint, pnpm format:check: pass.
  • pnpm phi-scan, pnpm check:no-emdash, pnpm check:no-internal-refs, pnpm check:claude-card, pnpm check:docs-reference: pass.
  • pnpm build, pnpm attw: pass.
  • pnpm test:coverage: 152 files passed, 4 skipped (the database servers, which CI's databases workflow runs); 3097 tests passed.
  • pnpm quality:check --base origin/main: OK, no uncovered changed lines.
  • pnpm mutate changed --base origin/main and pnpm quality:mutation --base origin/main: OK, no surviving mutant in changed code.
  • test/postgres-entra.test.ts (new, 15 tests, mocked credential only, nothing reaches Azure): token reuse up to the margin and refresh at it; an expired token replaced; one request for concurrent connections; failures (a thrown error whose text holds a token, null, an empty or missing token) give the fixed text and code with no token or identity name in the message, the inspected error, or its JSON, and the next connection asks again; the options pg is given; the URL refusals; the missing package. Two tests run the real pg driver against a stand-in server on 127.0.0.1 that asks for a cleartext password: each new connection sends the token, a connection opened after the margin sends a new one, and a failed token request sends nothing and fails with ERR_PATHWAYS_ENTRA_TOKEN. Seen failing first: with only the token module in place and the schema and dialect unchanged, 8 of the 14 tests at that point failed; with none of the source change, the file failed to import.
  • Left to CI: pnpm test:perf (the change adds nothing to the message path), the database server jobs, Windows and macOS, tarball-smoke, and the image build.

Choices

  • DefaultAzureCredential rather than ManagedIdentityCredential, as the Azure Blob location already uses, so AZURE_CLIENT_ID works the same for both.
  • The token is cached in Pathways with a 5-minute margin, on top of the credential's own cache, so the rule holds whatever credential is passed.
  • The token failure carries no cause: Azure's text can name the identity.
  • auth sits on the shared server block in the schema and is refused for MySQL and SQL Server by a refinement. The editor JSON Schema therefore accepts it there and pathways start refuses it, which keeps the schema a subset of what start checks.
  • The auth key's hover text is marked for StrykerJS as built once at module load (// Stryker disable ObjectLiteral,StringLiteral), since a per-test mutant cannot reach it.
  • The URL taken apart for auth: entra drops pg's connection-string extras (a host query option or a Unix socket path); the plain path is unchanged.

@NSchatz
NSchatz merged commit 970fe83 into main Nov 30, 2026
42 checks passed
@NSchatz
NSchatz deleted the feat/postgres-entra-auth branch November 30, 2026 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant