Repository navigation
Prevent Vault secret resolution and injection in DCR client secrets #54463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mposolda
merged 4 commits into
keycloak:main
from
jimmychakkalakal:issues/54382-dcr-vault-reference-protection
Nov 9, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bd03e34
Prevent Vault secret resolution and injection in DCR client secrets
jimmychakkalakal bbb9772
Prevent Vault secret resolution and injection in DCR client secrets
jimmychakkalakal 6e09c29
Worked on reviewer's comments
jimmychakkalakal 17ecae6
Updated vault documentation
jimmychakkalakal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
129 changes: 129 additions & 0 deletions
129
tests/base/src/test/java/org/keycloak/tests/client/DcrVaultSecretDisclosureTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package org.keycloak.tests.client; | ||
|
|
||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Map; | ||
|
|
||
| import org.keycloak.client.registration.Auth; | ||
| import org.keycloak.client.registration.ClientRegistrationException; | ||
| import org.keycloak.client.registration.HttpErrorException; | ||
| import org.keycloak.models.ClientSecretConstants; | ||
| import org.keycloak.protocol.oidc.OIDCConfigAttributes; | ||
| import org.keycloak.representations.adapters.config.AdapterConfig; | ||
| import org.keycloak.representations.idm.ClientRepresentation; | ||
| import org.keycloak.testframework.annotations.InjectClient; | ||
| import org.keycloak.testframework.annotations.KeycloakIntegrationTest; | ||
| import org.keycloak.testframework.injection.LifeCycle; | ||
| import org.keycloak.testframework.realm.ClientBuilder; | ||
| import org.keycloak.testframework.realm.ClientConfig; | ||
| import org.keycloak.testframework.realm.ManagedClient; | ||
| import org.keycloak.testframework.server.KeycloakServerConfig; | ||
| import org.keycloak.testframework.server.KeycloakServerConfigBuilder; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.ValueSource; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| @KeycloakIntegrationTest(config = DcrVaultSecretDisclosureTest.ServerConfig.class) | ||
| public class DcrVaultSecretDisclosureTest extends AbstractClientRegistrationTest { | ||
|
|
||
| private static final String VAULT_EXPRESSION = "${vault.client_secret}"; | ||
| private static final String VAULT_SECRET_VALUE = "mysecret"; | ||
|
|
||
| @InjectClient(config = DcrVaultClientConfig.class, lifecycle = LifeCycle.METHOD) | ||
| ManagedClient client; | ||
|
|
||
| // The registration access token is only exposed by the Admin API via regenerateRegistrationAccessToken(). | ||
| // We refresh it once per test so all DCR calls in the test body use a valid token. | ||
| private ClientRepresentation clientWithRat; | ||
|
|
||
| @BeforeEach | ||
| public void refreshRegistrationAccessToken() { | ||
| clientWithRat = client.admin().regenerateRegistrationAccessToken(); | ||
| } | ||
|
jimmychakkalakal marked this conversation as resolved.
Resolved
|
||
|
|
||
| @Test | ||
| public void testDcrUpdateRejectsVaultExpression() { | ||
| reg.auth(Auth.token(clientWithRat)); | ||
|
|
||
| ClientRepresentation updateRep = new ClientRepresentation(); | ||
| updateRep.setClientId(client.getClientId()); | ||
| updateRep.setSecret(VAULT_EXPRESSION); | ||
|
|
||
| ClientRegistrationException ex = assertThrows(ClientRegistrationException.class, () -> reg.update(updateRep)); | ||
| assertInstanceOf(HttpErrorException.class, ex.getCause()); | ||
| assertEquals(400, ((HttpErrorException) ex.getCause()).getStatusLine().getStatusCode()); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ValueSource(strings = {"client-secret", "client-secret-jwt"}) | ||
| public void testAdapterInstallationNeverResolvesVaultSecret(String authenticator) throws ClientRegistrationException { | ||
| // Configure client with vault expression directly via Admin API (simulating operator setup) | ||
| ClientRepresentation rep = client.admin().toRepresentation(); | ||
| rep.setSecret(VAULT_EXPRESSION); | ||
| rep.setClientAuthenticatorType(authenticator); | ||
| if ("client-secret-jwt".equals(authenticator)) { | ||
| ClientBuilder.update(rep).attribute(OIDCConfigAttributes.TOKEN_ENDPOINT_AUTH_SIGNING_ALG, "HS256"); | ||
| } | ||
| client.admin().update(rep); | ||
|
|
||
| reg.auth(Auth.token(clientWithRat)); | ||
|
|
||
| // Layer 2 assertion: Adapter config must return raw expression, never resolved plaintext vault secret | ||
| AdapterConfig config = reg.getAdapterConfig(client.getClientId()); | ||
| assertNotNull(config); | ||
|
|
||
| if ("client-secret-jwt".equals(authenticator)) { | ||
| Map<?, ?> jwtCredentials = assertInstanceOf(Map.class, config.getCredentials().get("secret-jwt")); | ||
| assertEquals(VAULT_EXPRESSION, jwtCredentials.get("secret")); | ||
| } else { | ||
| assertEquals(VAULT_EXPRESSION, config.getCredentials().get("secret")); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testDcrUpdateRejectsVaultExpressionInRotatedSecretAttribute() { | ||
| reg.auth(Auth.token(clientWithRat)); | ||
|
|
||
| ClientRepresentation updateRep = new ClientRepresentation(); | ||
| updateRep.setClientId(client.getClientId()); | ||
| updateRep.setAttributes(Map.of(ClientSecretConstants.CLIENT_ROTATED_SECRET, VAULT_EXPRESSION)); | ||
|
|
||
| ClientRegistrationException ex = assertThrows(ClientRegistrationException.class, () -> reg.update(updateRep)); | ||
| assertInstanceOf(HttpErrorException.class, ex.getCause()); | ||
| assertEquals(400, ((HttpErrorException) ex.getCause()).getStatusLine().getStatusCode()); | ||
| } | ||
|
|
||
| public static class ServerConfig implements KeycloakServerConfig { | ||
| @Override | ||
| public KeycloakServerConfigBuilder configure(KeycloakServerConfigBuilder config) { | ||
| try { | ||
| Path vaultDir = Files.createTempDirectory("kc-dcr-vault-test"); | ||
| vaultDir.toFile().deleteOnExit(); | ||
| // Keycloak REALM_UNDERSCORE_KEY format: <realmName>_<secretNameEscaped> | ||
| Files.writeString(vaultDir.resolve(REALM_NAME + "_client__secret"), VAULT_SECRET_VALUE); | ||
| return config.option("vault", "file").option("vault-dir", vaultDir.toAbsolutePath().toString()); | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to prepare ephemeral vault fixture directory", e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static class DcrVaultClientConfig implements ClientConfig { | ||
| @Override | ||
| public ClientBuilder configure(ClientBuilder client) { | ||
| return client | ||
| .enabled(true) | ||
| .clientId("dcr-vault-test-client") | ||
| .secret("initial-literal-secret") | ||
| .publicClient(false) | ||
| .rootUrl("http://localhost"); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.