Files
Vladyslav SimonenkoandGitHub 9d4d05794e Add passwordCommand option for SQL datastore credentials (#9879)
## What changed?
Add a `passwordCommand` config option for SQL datastores that executes
an external command to fetch the database password instead of using a
static `password` field. When configured, each new physical database
connection re-executes the command to get a fresh credential via
`RefreshingConnector`, enabling short-lived tokens (e.g. cloud IAM
tokens) to be used as database passwords. Supported for MySQL,
PostgreSQL (both pgx and pq drivers).

## Why?
Cloud-managed databases (e.g. AWS RDS, GCP Cloud SQL) authenticate using
short-lived IAM tokens rather than static passwords. There was no way to
integrate an external credential source into Temporal's SQL config
without forking the code. `passwordCommand` lets operators plug in any
token-fetching tool (e.g. `aws rds generate-db-auth-token`) via config.

This approach is vendor-agnostic: any command that writes a password to
stdout works, with no SDK dependencies or provider-specific code in
Temporal.

## How did you test it?
- [X] built
- [X] run locally and tested manually
- [ ] covered by existing tests
- [X] added new unit test(s)
- [ ] added new functional test(s)

Tested with GCP, setting up a Postgres database and using it for the
server backend

## Potential risks
- `passwordCommand` executes an arbitrary external command.
Misconfiguration will cause connection failures
- If the command is slow or hangs, connection pool growth will stall
until the timeout (default 30s) fires.
- Token expiry must be managed by the operator via `MaxConnLifetime`,
there is no built-in mechanism to detect or handle expired credentials
on existing connections. Richer integration (e.g. parsing expiry from
the command output and automatically setting connection lifetime) is
possible but intentionally omitted here as it would be vendor-specific.
The good news, is that most cloud APIs return tokens with a known, fixed
TTL (e.g. 15 minutes for AWS RDS, 1 hour for GCP Cloud SQL).
2026-04-15 12:50:10 -07:00

44 lines
1.0 KiB
Go

package sqlplugin
import (
"context"
"database/sql/driver"
)
// RefreshingConnector is a driver.Connector that calls buildDSN on every
// Connect, so each new physical connection gets a fresh credential. This is
// used when passwordCommand is configured to fetch short-lived tokens.
type RefreshingConnector struct {
buildDSN func() (string, error)
newConnector func(dsn string) (driver.Connector, error)
driver driver.Driver
}
func NewRefreshingConnector(
buildDSN func() (string, error),
newConnector func(dsn string) (driver.Connector, error),
d driver.Driver,
) *RefreshingConnector {
return &RefreshingConnector{
buildDSN: buildDSN,
newConnector: newConnector,
driver: d,
}
}
func (c *RefreshingConnector) Connect(ctx context.Context) (driver.Conn, error) {
dsn, err := c.buildDSN()
if err != nil {
return nil, err
}
connector, err := c.newConnector(dsn)
if err != nil {
return nil, err
}
return connector.Connect(ctx)
}
func (c *RefreshingConnector) Driver() driver.Driver {
return c.driver
}