Skip to content

postgres: add Azure AD and GCP Cloud SQL IAM dynamic-auth backends #261

Closed

Description

@reyortiz3

Motivation

postgres.DynamicAuthConfig currently supports exactly one backend: AWS RDS IAM (AWSRDSIAM), implemented as a BeforeConnect token-swap hook in awsiam.go. Customers running Postgres on Azure Database for PostgreSQL or GCP Cloud SQL want the same passwordless option. doc.go already flags both as "not yet implemented," steering operators to verify-full + a CA bundle instead.

This issue scopes adding both, plus generalizing the backend-dispatch plumbing so a third backend isn't a special case.

Scope: two backends, same shape as AWS RDS IAM

Both fit the existing abstraction — mint a token, hand it to pgxpool's BeforeConnect hook as the password — with no change needed to pool.go's core NewPool/buildPoolConfig contract:

Azure AD (Entra ID) for Azure Database for PostgreSQL

  • New DynamicAuthAzureAD struct (likely no required fields — azidentity.NewDefaultAzureCredential covers env vars, managed identity, and workload identity the same way AWS's default credential chain does today; may want an optional client ID hint for user-assigned managed identity).
  • Token builder: cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: []string{"https://ossrdbms-aad.database.windows.net/.default"}}), used as the password — mirrors awsRDSIAMToken/awsRDSIAMBeforeConnect in shape.
  • New dependency: github.com/Azure/azure-sdk-for-go/sdk/azidentity.
  • New file azuread.go mirroring awsiam.go's structure (token builder + BeforeConnect hook builder).

GCP Cloud SQL IAM auth — token-swap variant only (see "Explicitly deferred" below)

  • New DynamicAuthGCPCloudSQLIAM struct (likely just needs nothing beyond presence — token minting uses ambient credentials via golang.org/x/oauth2/google default credentials, same shape as AWS/Azure).
  • Token builder: an OAuth2 token scoped for Cloud SQL (https://www.googleapis.com/auth/sqlservice.admin or the narrower IAM DB-auth scope — needs verification against current GCP docs), used as the password over a direct TCP+TLS connection.
  • Caveat that must ship in the doc comment: this path requires the instance to have a reachable IP (public, or private with direct network routing) and does not get Cloud SQL's automatic mTLS tunnel — that's the deliberately-deferred Cloud SQL connector path below.
  • New dependency: golang.org/x/oauth2/google (or equivalent).
  • New file gcpiam.go mirroring awsiam.go.

Plumbing generalization (do this once, for both)

  • Config.DynamicAuth (DynamicAuthConfig) gains AzureAD *DynamicAuthAzureAD and GCPCloudSQLIAM *DynamicAuthGCPCloudSQLIAM fields alongside AWSRDSIAM.
  • Config.Validate()'s "exactly one backend configured" check extends to the new fields (currently only checks AWSRDSIAM).
  • NewAuthToken and NewDynamicAuthFunc's backend dispatch (if cfg.DynamicAuth.AWSRDSIAM != nil { ... }) extends to a proper multi-branch dispatch — this is the "generalize once" part: get the dispatch shape right for 3 backends now rather than bolting on a second if later.
  • doc.go updated to drop Azure/GCP from the "not yet implemented" list, documenting the token-swap-only caveat for GCP.

Testing

Mirror the existing AWS test file's structure, but explicitly avoid asserting on real default-credential-chain success/failure — that's environment-dependent (verified the hard way: a real dev sandbox with ambient AWS credentials made an equivalent assumption flaky). Test the deterministic parts only: dispatch branching, Config.Validate()'s multi-backend exclusivity check, and connection-string/token-substitution shape — inject a stubbable token-minting seam if the real SDK call can't be isolated cleanly otherwise.

Explicitly deferred (separate issue, not in this scope)

The GCP Cloud SQL Go connector (cloudsqlconn) — the path GCP actually recommends for production, since it provides an automatic mTLS tunnel and works without a public IP or manual network routing. This is architecturally different from everything above: it replaces the dialer (pgxpool.Config.DialFunc), not just the password, which pool.go has no extension point for today. It would also need a second, structurally different code path for the one-shot-connection case (migrations) that NewAuthToken (a password-string return) can't express — a connector-based backend has no password, only a net.Conn.

Recommend deciding whether the connector path is actually needed (i.e., do customers require private-IP-only Cloud SQL) before investing in that larger abstraction change, separately from this issue's token-swap scope.

Also worth flagging, not in this scope

toolhive-registry-server maintains its own independent dynamic-auth implementation (internal/app/storage/auth), not built on this package. Every backend added here has to be re-implemented there too to keep parity — worth a deliberate decision (unify registry-server onto this package, vs. keep duplicating) before a third and fourth backend are written twice.

Activity

  1. added
    needs-triage
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions