Skip to content

feat(Keycloak): Support admin bootstrapping for version 26 onwards#1400

Merged
HofmeisterAn merged 1 commit intotestcontainers:developfrom
paulomorgado:keycloak-admin
Mar 14, 2025
Merged

feat(Keycloak): Support admin bootstrapping for version 26 onwards#1400
HofmeisterAn merged 1 commit intotestcontainers:developfrom
paulomorgado:keycloak-admin

Conversation

@paulomorgado
Copy link
Contributor

  • Added private fields for username and password in KeycloakBuilder.
  • Modified Build method to handle environment variables based on Keycloak version.
  • Introduced KeycloakV26Configuration for testing with Keycloak version 26.

What does this PR do?

From https://www.keycloak.org/docs/26.0.0/upgrading/#admin-bootstrapping-and-recovery

It used to be difficult to regain access to a Keycloak instance when all admin users were locked out. The process required multiple advanced steps, including direct database access and manual changes. In an effort to improve the user experience, Keycloak now provides multiple ways to bootstrap a new admin account, which can be used to recover from such situations.

Consequently, the environment variables KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD have been deprecated. You should use KC_BOOTSTRAP_ADMIN_USERNAME and KC_BOOTSTRAP_ADMIN_PASSWORD instead. These are also general options, so they may be specified via the cli or other config sources, for example --bootstrap-admin-username=admin. For more information, see the new Bootstrap admin and recovery guide.

Why is it important?

Keep up to date with Keycloak.

Related issues

@netlify
Copy link

netlify bot commented Mar 13, 2025

Deploy Preview for testcontainers-dotnet ready!

Name Link
🔨 Latest commit cccacf9
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-dotnet/deploys/67d426e480bfeb0008c10be8
😎 Deploy Preview https://deploy-preview-1400--testcontainers-dotnet.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you simply use the following approach? If the environment variables are not supported by one of the version, they will be ignored.

diff --git a/src/Testcontainers.Keycloak/KeycloakBuilder.cs b/src/Testcontainers.Keycloak/KeycloakBuilder.cs
index 1f255cd..5116e15 100644
--- a/src/Testcontainers.Keycloak/KeycloakBuilder.cs
+++ b/src/Testcontainers.Keycloak/KeycloakBuilder.cs
@@ -44,6 +44,9 @@ public sealed class KeycloakBuilder : ContainerBuilder<KeycloakBuilder, Keycloak
     public KeycloakBuilder WithUsername(string username)
     {
         return Merge(DockerResourceConfiguration, new KeycloakConfiguration(username: username))
+            .WithEnvironment("KC_BOOTSTRAP_ADMIN_USERNAME", username)
+            // From version 26 onwards, the environment variables are obsolete:
+            // https://www.keycloak.org/docs/26.0.0/upgrading/#admin-bootstrapping-and-recovery.
             .WithEnvironment("KEYCLOAK_ADMIN", username);
     }
 
@@ -55,6 +58,9 @@ public sealed class KeycloakBuilder : ContainerBuilder<KeycloakBuilder, Keycloak
     public KeycloakBuilder WithPassword(string password)
     {
         return Merge(DockerResourceConfiguration, new KeycloakConfiguration(password: password))
+            .WithEnvironment("KC_BOOTSTRAP_ADMIN_PASSWORD", password)
+            // From version 26 onwards, the environment variables are obsolete:
+            // https://www.keycloak.org/docs/26.0.0/upgrading/#admin-bootstrapping-and-recovery.
             .WithEnvironment("KEYCLOAK_ADMIN_PASSWORD", password);
     } 

@paulomorgado
Copy link
Contributor Author

I'm fine with that, if you are.

@HofmeisterAn
Copy link
Collaborator

As long as it works, I prefer this approach for its simplicity. I assume that if the deprecated environment variables are removed, Keycloak will simply ignore them.

- Added private fields for username and password in KeycloakBuilder.
- Modified Build method to handle environment variables based on Keycloak version.
- Simplified wait strategy logic.
- Introduced KeycloakV26Configuration for testing with Keycloak version 26.
@paulomorgado
Copy link
Contributor Author

Done!

Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@HofmeisterAn HofmeisterAn changed the title Update KeycloakBuilder admin variable names for version 26 support feat(Keycloak): Support admin bootstrapping for version 26 onwards Mar 14, 2025
@HofmeisterAn HofmeisterAn added the enhancement New feature or request label Mar 14, 2025
@HofmeisterAn HofmeisterAn merged commit d0f81dd into testcontainers:develop Mar 14, 2025
67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement]: Update Keycloak admin variable names

2 participants