Connect to Azure PostgreSQL with a Managed Identity
Self-hostedLet Entropy Data log in to Azure Database for PostgreSQL with the managed identity of its container, so there is no database password to store or rotate.
Whenever Entropy Data opens a database connection, it asks Azure for a Microsoft Entra access token of the identity and sends the token as the password. This works on Azure Container Apps, App Service, AKS with workload identity, Container Instances, and virtual machines.
Prerequisites
- An Azure Database for PostgreSQL Flexible Server.
- A managed identity on the container that runs Entropy Data, system-assigned or user-assigned.
- A Microsoft Entra administrator of the server, to create the database role.
- The Azure CLI and
psqlfor the commands below.
Step 1: Prepare the Server
Enable Microsoft Entra authentication. Password authentication can stay enabled, or be disabled once nothing uses it anymore:
az postgres flexible-server update --resource-group my-rg --name my-postgres \
--microsoft-entra-auth Enabled
Make yourself (or a group you are in) an Entra administrator of the server:
az postgres flexible-server microsoft-entra-admin create --resource-group my-rg --server-name my-postgres \
--display-name "admin@example.com" --object-id <object id of the user> --type User
Allow the three extensions that Entropy Data installs on its first start:
az postgres flexible-server parameter set --resource-group my-rg --server-name my-postgres \
--name azure.extensions --value VECTOR,HSTORE,UUID-OSSP
Step 2: Create the Database Role
Look up the object id (principal id) of the managed identity:
# user-assigned identity
az identity show --resource-group my-rg --name my-identity --query principalId -o tsv
# system-assigned identity of a container app
az containerapp show --resource-group my-rg --name my-app --query identity.principalId -o tsv
Connect as the Entra administrator. The password is an access token of your own account:
export PGPASSWORD=$(az account get-access-token --resource-type oss-rdbms --query accessToken -o tsv)
psql "host=my-postgres.postgres.database.azure.com port=5432 dbname=postgres user=admin@example.com sslmode=require"
Create the database for Entropy Data, if it does not exist yet, and a role for the identity. Use the name of the identity as the role name (for a system-assigned identity, the name of the container app):
CREATE DATABASE entropydata;
SELECT * FROM pgaadauth_create_principal_with_oid('my-identity', '<principal id>', 'service', false, false);
GRANT azure_pg_admin TO "my-identity";
If pgaadauth_create_principal_with_oid fails with OID isn't found in the tenant right after you created the identity, Entra has not replicated it yet. Wait a minute and run it again.
Then connect to the new database (\c entropydata) and grant the role access to it:
GRANT ALL ON DATABASE entropydata TO "my-identity";
GRANT ALL ON SCHEMA public TO "my-identity";
azure_pg_admin is needed for the first start. The migrations run
CREATE EXTENSION IF NOT EXISTS vector, and Azure only lets members of azure_pg_admin run that
statement, even when vector is installed already. Without the role, the first start fails with
Because vector isn't a trusted extension, only members of "azure_pg_admin" are allowed to use CREATE EXTENSION vector.
Once Entropy Data has started, you can revoke it again: REVOKE azure_pg_admin FROM "my-identity";.
Step 3: Configure Entropy Data
Add the authentication plugin to the JDBC URL, set the role as the user name, and leave SPRING_DATASOURCE_PASSWORD unset:
environment:
SPRING_DATASOURCE_URL: "jdbc:postgresql://my-postgres.postgres.database.azure.com:5432/entropydata?sslmode=require&authenticationPluginClassName=entropydata.system.AzureManagedIdentityAuthenticationPlugin"
SPRING_DATASOURCE_USERNAME: "my-identity"
AZURE_CLIENT_ID: "<client id of the user-assigned identity>"
| Environment Variable | Description |
|---|---|
SPRING_DATASOURCE_URL | The JDBC URL with authenticationPluginClassName=entropydata.system.AzureManagedIdentityAuthenticationPlugin. sslmode=require is mandatory on Azure. |
SPRING_DATASOURCE_USERNAME | The name of the role from Step 2. |
AZURE_CLIENT_ID | The client id (not the principal id) of a user-assigned identity: az identity show --resource-group my-rg --name my-identity --query clientId -o tsv. Leave it unset for a system-assigned identity. |
If you point Flyway at its own URL (SPRING_FLYWAY_URL), add the same parameter there.
Where the Token Comes From
Entropy Data finds the token endpoint through the environment variables Azure sets, the same way the Azure identity libraries do:
| Host | Recognized by | Token endpoint |
|---|---|---|
| AKS with workload identity | AZURE_FEDERATED_TOKEN_FILE | The Microsoft Entra token endpoint, in exchange for the pod's service account token. Needs AZURE_TENANT_ID and AZURE_CLIENT_ID as well. The workload identity webhook sets all three. |
| Container Apps, App Service | IDENTITY_ENDPOINT and IDENTITY_HEADER | The identity endpoint of the host. |
| Container Instances, virtual machines | none of the above | The instance metadata service. |
Service Fabric, Azure Arc and Cloud Shell are not supported, and the login fails with a message that names them. The token is requested for the Azure public cloud.
Entropy Data caches the token and requests a new one halfway through its lifetime (or five minutes before it expires, for a token that lives less than two hours). It retries a token endpoint that is unavailable or throttles for up to 20 seconds. If a refresh fails while the cached token is still valid, Entropy Data keeps using the cached token, logs a warning, and tries again a minute later. PostgreSQL checks the token only at login, so open connections are not affected when it expires.
Troubleshooting
| Error | Cause |
|---|---|
Managed identity token request to localhost failed with HTTP 400: ... No User Assigned or Delegated Managed Identity found for specified ClientId/ResourceId/PrincipalId. | On Container Apps or App Service: AZURE_CLIENT_ID names no identity that is assigned to the app. |
Managed identity token request to 169.254.169.254 failed with HTTP 400: ... Identity not found | On Container Instances or a virtual machine: AZURE_CLIENT_ID names no identity that is assigned to it. |
Because vector isn't a trusted extension, only members of "azure_pg_admin" are allowed to use CREATE EXTENSION vector | The role is not a member of azure_pg_admin during the first start. See Step 2. |
AZURE_FEDERATED_TOKEN_FILE is set, so AZURE_TENANT_ID is required as well | On AKS: the pod is not set up completely for workload identity. |
Switch an Existing Installation
The steps above set up a new database. An existing database keeps its data, but its tables belong to the password user that created them, and later migrations need the role of the identity to own them. Transfer the ownership to the new role before the next upgrade, as described in Switch an Existing Installation, then change the environment variables as in Step 3.