How to Rotate Keycloak RSA Signing Keys
This guide explains how to manually rotate the RSA certificates used for signing tokens in Keycloak.
Background
Keycloak uses RSA keys to sign JWTs (ID tokens, access tokens). Key rotation is important for security hygiene and may be required for compliance. Keycloak's key provider system supports seamless rotation without service interruption.
Admin Console Method
- Log into the Keycloak Admin Console
- Navigate to Realm Settings → Keys tab
- Click on the Providers sub-tab
- Find your key provider (typically
rsa-generated)
To Rotate Keys
- Click Add provider → rsa-generated
- Configure the new provider:
- Name: Give it a descriptive name (e.g.,
rsa-generated-2024) - Priority: Set higher than existing provider (e.g., if current is 100, set to 200)
- Key size: 2048 or 4096 bits
- Algorithm: RS256 (or RS384, RS512 as needed)
- Name: Give it a descriptive name (e.g.,
- Click Save
The new key with higher priority becomes active for signing. Old keys remain available for verification.
Cleanup Old Keys
After the grace period (see best practices below):
- Go to Realm Settings → Keys → Providers
- Find the old key provider
- Click the trash icon to delete it
Admin REST API Method
Prerequisites
Obtain an admin access token:
ADMIN_TOKEN=$(curl -s -X POST "https://<keycloak-host>/realms/master/protocol/openid-connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "username=admin" \
-d "password=<admin-password>" \
-d "grant_type=password" \
-d "client_id=admin-cli" | jq -r '.access_token')
View Current Keys
curl -X GET "https://<keycloak-host>/admin/realms/<realm>/keys" \
-H "Authorization: Bearer $ADMIN_TOKEN"
List Key Providers (Components)
curl -X GET "https://<keycloak-host>/admin/realms/<realm>/components?type=org.keycloak.keys.KeyProvider" \
-H "Authorization: Bearer $ADMIN_TOKEN"
Create New RSA Key Provider
curl -X POST "https://<keycloak-host>/admin/realms/<realm>/components" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "rsa-generated-rotated",
"providerId": "rsa-generated",
"providerType": "org.keycloak.keys.KeyProvider",
"config": {
"priority": ["200"],
"enabled": ["true"],
"active": ["true"],
"keySize": ["2048"],
"algorithm": ["RS256"]
}
}'
Delete Old Key Provider
First, get the component ID from the list providers response, then:
curl -X DELETE "https://<keycloak-host>/admin/realms/<realm>/components/<component-id>" \
-H "Authorization: Bearer $ADMIN_TOKEN"
Rolling Back Keys
If a key rotation causes issues, you can rollback to the previous key.
Admin Console Rollback
- Navigate to Realm Settings → Keys → Providers
- Click on the new key provider (the one causing issues)
- Lower its priority below the old key (e.g., change from 200 to 50)
- Click Save
The old key with higher priority becomes active again for signing.
Alternatively, you can delete the problematic new key provider entirely:
- Navigate to Realm Settings → Keys → Providers
- Find the new key provider
- Click the trash icon to delete it
Admin REST API Rollback
Option 1: Lower the New Key's Priority
First, get the component ID of the new key provider:
curl -X GET "https://<keycloak-host>/admin/realms/<realm>/components?type=org.keycloak.keys.KeyProvider" \
-H "Authorization: Bearer $ADMIN_TOKEN"
Then update the priority to be lower than the old key:
curl -X PUT "https://<keycloak-host>/admin/realms/<realm>/components/<new-key-component-id>" \
-H "Authorization: Bearer $ADMIN_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"id": "<new-key-component-id>",
"name": "rsa-generated-rotated",
"providerId": "rsa-generated",
"providerType": "org.keycloak.keys.KeyProvider",
"parentId": "<realm-id>",
"config": {
"priority": ["50"],
"enabled": ["true"],
"active": ["true"],
"keySize": ["2048"],
"algorithm": ["RS256"]
}
}'
Option 2: Delete the New Key Provider
curl -X DELETE "https://<keycloak-host>/admin/realms/<realm>/components/<new-key-component-id>" \
-H "Authorization: Bearer $ADMIN_TOKEN"
Rollback Considerations
- Tokens signed with the new key: If you delete the new key provider, any tokens signed with that key will become invalid immediately. Users will need to re-authenticate.
- Prefer priority adjustment: Lowering priority instead of deleting preserves both keys—new tokens use the old key, but tokens signed with the new key remain valid.
- Client cache: Clients may have cached the JWKS. After rollback, clients fetching fresh keys will see the change. Cached keys may cause temporary validation issues until refreshed.
- Force client refresh: If clients cache aggressively, you may need to restart client applications or wait for their cache TTL to expire.