Skip to content
A Testcontainer implementation for Keycloak SSO.
Java Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
src disable log output by default Jan 15, 2020
tools
.editorconfig
.gitignore project setup Dec 13, 2019
LICENSE
README.md improve readme Dec 18, 2019
pom.xml

README.md

Keycloak Testcontainer

A Testcontainer implementation for Keycloak SSO.

How to use

Simply spin up a default Keycloak instance:

@Container
private KeycloakContainer keycloak = new KeycloakContainer();

Use another Keycloak Docker image/version than used in this Testcontainer:

@Container
private KeycloakContainer keycloak = new KeycloakContainer("jboss/keycloak:7.0.0");

Power up a Keycloak instance with an existing realm JSON config file (from classpath):

@Container
private KeycloakContainer keycloak = new KeycloakContainer()
    .withImportFile("test-realm.json");

Use different admin credentials than the defaut internal (admin/admin) ones:

@Container
private KeycloakContainer keycloak = new KeycloakContainer()
    .withAdminUsername("myKeycloakAdminUser")
    .withAdminPassword("tops3cr3t");

You can obtain several properties form the Keycloak container:

String authServerUrl = keycloak.getAuthServerUrl();
String adminUsername = keycloak.getAdminUsername();
String adminPassword = keycloak.getAdminPassword();

with these properties, you can create a org.keycloak.admin.client.Keycloak (Keycloak admin client, 3rd party dependency from Keycloak project) object to connect to the container and do optional further configuration:

Keycloak keycloakAdminClient = KeycloakBuilder.builder()
    .serverUrl(keycloak.getAuthServerUrl())
    .realm("master")
    .clientId("admin-cli")
    .username(keycloak.getAdminUsername())
    .password(keycloak.getAdminPassword())
    .build();

See also KeycloakContainerTest class.

TLS (SSL) Usage

You have several options to use HTTPS/TLS secured communication with your Keycloak Testcontainer.

Default Support

Plain Keycloak comes with a default Java KeyStore (JKS) with an auto-generated, self-signed certificate on first use. You can use this TLS secured connection, although your testcontainer doesn't know of anything TLS-related and returns the HTTP-only url with getAuthServerUrl(). In this case, you have to build the auth-server-url on your own, e.g. like this:

String authServerUrl = "https://localhost:" + keycloak.getHttpsPort() + "/auth";

See also KeycloakContainerHttpsTest.shouldStartKeycloakWithDefaultTlsSupport.

Built-in TLS Cert and Key

This Keycloak Testcontainer comes with built-in TLS certificate (tls.crt), key (tls.key) and Java KeyStore (tls.jks) files, located in the resources folder. You can use this configuration by only configuring your testcontainer like this:

@Container
private KeycloakContainer keycloak = new KeycloakContainer().useTls();

The password for the provided Java KeyStore file is changeit. See also KeycloakContainerHttpsTest.shouldStartKeycloakWithProvidedTlsCertAndKey.

The method getAuthServerUrl() will then return the HTTPS url.

Custom TLS Cert and Key

Of course you can also provide your own certificate and key file for usage in this Testcontainer:

@Container
private KeycloakContainer keycloak = new KeycloakContainer()
    .useTls("your_custom.crt", "your_custom.key");

See also KeycloakContainerHttpsTest.shouldStartKeycloakWithCustomTlsCertAndKey.

The method getAuthServerUrl() will also return the HTTPS url.

Setup

The release versions of this project are available at Maven Central. Simply put the dependency coordinates to your pom.xml (or something similar, if you use e.g. Gradle or something else):

<dependency>
  <groupId>com.github.dasniko</groupId>
  <artifactId>testcontainers-keycloak</artifactId>
  <version>1.2.0</version>
  <scope>test</scope>
</dependency>

Credits

Many thanks to the creators and maintainers of Testcontainers. You do an awesome job!

Same goes to the whole Keycloak team!

Kudos to @thomasdarimont for some inspiration for this project.

License

MIT License

Copyright (c) 2019 Niko Köbler

See LICENSE file for details.

You can’t perform that action at this time.