Repository navigation
feat(storage): sign in to PostgreSQL with a Microsoft Entra ID token - #173
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
A
storage.postgresblock now takesauth: entra(orauth: { 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) andpathways migrate, which both open the store throughdialectFor.src/storage/entra.ts(new):entraPassword(credential)returns the async password function pg calls for each new connection. It asks for a token forhttps://ossrdbms-aad.database.windows.net/.default, reuses it until 5 minutes beforeexpiresOnTimestamp, then asks again; connections opened together share one request. When no token can be had, it rejects with a fixed text and the codeERR_PATHWAYS_ENTRA_TOKEN, with no cause, so neither the credential's own text nor a token reaches an error or a log.defaultEntraCredential()makesDefaultAzureCredentialfrom@azure/identity(already an optional peer dependency, and in the image), which readsAZURE_CLIENT_IDfor a user-assigned identity.src/storage/dialects.ts: withauth: entra, the URL must name a user (the database user) and hold no password. The URL is taken apart intohost,port,user,databaseand its other options, with the password function beside them. pg reads a connection string's empty password over the one it is given, soconnectionStringcannot carry the function. A missing@azure/identityis aConfigErrornaming the package. Withoutauth: entra, pg gets the same options as before.src/config/schema.ts,src/config/load.ts: theauthkey (passwordorentra,passwordby default), refused onmysqlandsqlserverblocks.ServerStoragegains an optionalauth, set only forentra, so a block without it loads to the same object as before.The names follow the Azure Terraform module's: its
postgres.database_useroutput is the user in the URL (postgres://<database_user>@<host>:5432/pathways), and its identity'sclient_idis the value forAZURE_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'sdatabasesworkflow runs); 3097 tests passed.pnpm quality:check --base origin/main: OK, no uncovered changed lines.pnpm mutate changed --base origin/mainandpnpm 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 realpgdriver 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 withERR_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.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
DefaultAzureCredentialrather thanManagedIdentityCredential, as the Azure Blob location already uses, soAZURE_CLIENT_IDworks the same for both.cause: Azure's text can name the identity.authsits 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 andpathways startrefuses it, which keeps the schema a subset of what start checks.authkey'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.auth: entradrops pg's connection-string extras (ahostquery option or a Unix socket path); the plain path is unchanged.