KeycloakContainer class
Keycloak in a container in development mode, with an admin user and
optionally a realm imported from a JSON export, ready for the security
tests: the issuer URI of the realm is what an application's
@ServiceConnection() receives.
As a @ServiceConnection() it sets
spring.security.oauth2.resourceserver.jwt.issuer-uri (a resource
server validating Keycloak's tokens) and
spring.security.oauth2.client.provider.<id>.issuer-uri (a client
logging in through it; withProviderId names the provider, keycloak
by default), both http://host:port/realms/<realm>. The realm is the
one of the imported file, else master.
@Container()
@ServiceConnection()
static final keycloak = KeycloakContainer('quay.io/keycloak/keycloak:26.8')
.withRealmImport('../example/keycloak/neon-realm.json');
Testcontainers (Java): KeycloakContainer of the community module
dasniko/testcontainers-keycloak (withRealmImportFile,
getAuthServerUrl()).
- Inheritance
-
- Object
- GenericContainer
- KeycloakContainer
- Implemented types
Constructors
- KeycloakContainer([String image = 'quay.io/keycloak/keycloak:26.8'])
-
Keycloak from
image(quay.io/keycloak/keycloak:26.8unless given), started withstart-dev, with the admin useradmin/admin.
Properties
- adminPassword → String
-
The admin user's password.
no setter
- adminUsername → String
-
The admin user.
no setter
- authServerUrl → String
-
The server's address,
http://host:port.no setter -
connectionProperties
→ Map<
String, String> -
The properties that point an application at the started container,
such as
spring.datasource.url. Read after the container started: the values hold its host and mapped ports.no setteroverride - containerId → String
-
The container's id; throws before start.
no setterinherited
- docker → DockerClient
-
The client of the daemon the container runs on.
no setterinherited
-
exposedPorts
→ List<
int> -
The container ports published with
withExposedPorts.no setterinherited - hashCode → int
-
The hash code for this object.
no setterinherited
- host → String
-
The host the published ports are reached on:
127.0.0.1for a local daemon.no setterinherited - image → String
-
The image the container is created from.
finalinherited
- isStarted → bool
-
Whether start succeeded and stop was not called since.
no setterinherited
- issuerUri → String
-
The realm's issuer URI,
http://host:port/realms/<realm>: theissof its tokens.no setter - realm → String
-
The imported realm, or
master.no setter - remaining → Duration
-
How long the strategy may still take; it is the container's startup
timeout, less the time already spent.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- waitStrategy → WaitStrategy
-
The wait strategy, for modules that extend it.
no setterinherited
Methods
-
baseUrl(
int port, {String scheme = 'http'}) → String -
scheme://host:portof the container'sport:httpunlessschemesays otherwise.inherited -
execInContainer(
List< String> command) → Future<DockerExecResult> -
Runs
commandin the started container and waits for it.inherited -
getLogs(
) → Future< String> -
Everything the container printed so far, standard output and error
together.
inherited
-
inspect(
) → Future< DockerContainerState> -
The container's state, as the daemon reports it now.
inherited
-
logs(
) → Stream< DockerLogLine> -
The container's output from its start, following it until it stops.
inherited
-
mappedPort(
int port) → int -
The port on host that the container's
portis published on; the port must be listed in withExposedPorts, and the container started.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
poll(
String description, Future< bool> check(), {Duration interval = const Duration(milliseconds: 100)}) → Future<void> -
Calls
checkeveryintervaluntil it returnstrue.inherited -
start(
) → Future< void> -
Pulls the image if it is missing, creates the container, copies the
files in, starts it and waits until it is ready.
inherited
-
stop(
) → Future< void> -
Removes the container (killing it when it still runs). A container
that is not started, or was found again through withReuse, is left
as it is.
inherited
-
toString(
) → String -
A string representation of this object.
override
-
waitingFor(
WaitStrategy strategy) → GenericContainer -
Decides when the started container counts as ready: see Wait. By
default
Wait.forListeningPort().inherited -
withAdminCredentials(
String username, String password) → KeycloakContainer -
Sets the admin user created at the first start
(
KC_BOOTSTRAP_ADMIN_USERNAME,KC_BOOTSTRAP_ADMIN_PASSWORD). -
withCommand(
List< String> command) → GenericContainer -
Replaces the image's default command (
CMD) withcommand.inherited -
withCopyContent(
Object content, String containerPath, {int mode = 420}) → GenericContainer -
Creates the file
containerPathwithcontent(aStringor bytes) in the container before it starts.inherited -
withCopyFile(
String hostPath, String containerPath, {int mode = 420}) → GenericContainer -
Copies the file at
hostPathtocontainerPathin the container before it starts. A relativehostPathstarts at the working directory (the package, underdart test); the file is read at start.modeis the permission (0644by default, as0x1A4).inherited -
withEntrypoint(
List< String> entrypoint) → GenericContainer -
Replaces the image's entry point with
entrypoint.inherited -
withEnv(
String name, String value) → GenericContainer -
Sets the environment variable
name.inherited -
withEnvironment(
Map< String, String> variables) → GenericContainer -
Sets several environment variables.
inherited
-
withExposedPorts(
List< int> ports) → GenericContainer -
Publishes the container's TCP
portson random free ports of the host; mappedPort says which.inherited -
withHealthCheck(
List< String> test, {Duration interval = const Duration(seconds: 1), Duration timeout = const Duration(seconds: 3), int retries = 10}) → GenericContainer -
Gives the container a health check, for
Wait.forHealthcheck()and for images that define none:testis the command in exec form (['CMD-SHELL', 'pg_isready -U test']), run everyinterval; afterretriesfailures the container isunhealthy.inherited -
withLabel(
String name, String value) → GenericContainer -
Adds the label
name=valueto the container.inherited -
withProviderId(
String id) → KeycloakContainer -
Names the OAuth2 client provider that
connectionPropertiessets the issuer URI of (spring.security.oauth2.client.provider.<id>). -
withRealmImport(
String hostPath) → KeycloakContainer -
Imports the realm exported to the JSON file at
hostPathwhen Keycloak starts (--import-realm); the container is ready when that realm answers, and realm is its name. -
withReuse(
bool reuse) → GenericContainer -
Keeps the container alive after the tests, for the next run to use
again (off by default, and ignored when the
CIenvironment variable is set).inherited -
withStartupTimeout(
Duration timeout) → GenericContainer -
How long the wait strategy may take (60 seconds by default); the image
pull is not counted.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited