Import YAML/JSON-formatted configuration files into Keycloak - Configuration as Code for Keycloak.
Import YAML/JSON-formatted configuration files into Keycloak - Configuration as Code for Keycloak.
keycloak-config-cli provides declarative, idempotent configuration for Keycloak via the Admin API—version-controlled and CI/CD-ready—covering realms, clients, roles, flows, identity providers, and more.
It is a Keycloak utility to ensure the desired configuration state for a realm based on a JSON/YAML file. The format of the JSON/YAML file is based on the Keycloak realm export format. Store and manage configuration files in Git just like normal code. A Keycloak restart isn't required to apply the configuration.
DOCUMENTATION.md and docs/FEATURES.mdSECURITY.mdCODE_OF_CONDUCT.mdUsing Docker:
docker run --rm \
-e KEYCLOAK_URL="http://localhost:8080" \
-e KEYCLOAK_USER=admin \
-e KEYCLOAK_PASSWORD=admin123 \
-e IMPORT_FILES_LOCATIONS='/config/*' \
-v "$PWD/contrib/example-config":/config \
adorsys/keycloak-config-cli:latestUsing the JAR (after building or downloading a release):
java -jar ./target/keycloak-config-cli.jar \
--keycloak.url=http://localhost:8080 \
--keycloak.user=admin \
--keycloak.password=admin123 \
--import.files.locations=./contrib/example-config/moped.jsonThe config files are based on the keycloak export files. You can use them to re-import your settings. But keep your files as small as possible. Remove all UUIDs and all stuff which is default set by keycloak.
moped.json is a full working example file you can consider. Other examples are located in the test resources.
keycloak-config-cli supports variable substitution of config files. This could be enabled by import.var-substitution.enabled=true (disabled by default).
Variables exposed by spring boot (through configtree or external configuration) can be accessed by $(property.name).
In additional, the string substitution support multiple prefixes for different approaches
…to replace the values with java system properties or environment variables. Recursive variable replacement like $(file:UTF-8:$(env:KEYCLOAK_PASSWORD_FILE)) is enabled by default if import.var-substitution.enabled is set to true.
keycloak-config-cli supports an explicit JavaScript evaluation phase. This is an opt-in feature and can be enabled by import.var-substitution.script-evaluation-enabled=true.
Syntax: $${javascript: ... }
Example:
{
"realm": "$${javascript: 'realm-' + (env.APP_ENV || 'default').toLowerCase()}",
"enabled": "$${javascript: env.APP_ENV === 'INT'}",
"sessionTimeout": "$${javascript: 2 * 60 * 60}"
}The evaluation is sandboxed and only has access to an env object containing all environment variables and system properties. Only JSON-serializable outputs are allowed (string, number, boolean, null, array, object).
The variable substitution is running before the json parser gets executed. This allows json structures or complex values.
See docs/javascript-substitution.md for more information.
See Apache Common StringSubstitutor documentation for more information and advanced usage.
Note: Since variable substitution is a part of the keycloak-config-cli, it's done locally. This means, the environment variables need to be available where keycloak-config-cli is executed.
If import.var-substitution.prefix=${ and import.var-substitution.suffix=} (default in keycloak-config-cli 3.x) is set, then keycloak builtin variables like ${role_uma_authorization} needs to be escaped by $${role_uma_authorization}.
keycloak-config-cli supports logging in JSON format. To enable, set SPRING_PROFILES_ACTIVE=json-log.
| CLI Option | ENV Variable | Description | Default |
|---|---|---|---|
| --logging.level.root | LOGGING_LEVEL_ROOT | define the root log level | info |
| --logging.level.keycloak-config-cli | LOGGING_LEVEL_KEYCLOAKCONFIGCLI | log level of keycloak-config-cli components | value of logging.level.root |
| --logging.level.http | LOGGING_LEVEL_HTTP | log level http requests between keycloak-config-cli and Keycloak | value of logging.level.root |
| --logging.level.realm-config | LOGGING_LEVEL_REALMCONFIG | if set to trace, the realm config including sensitive information will be logged | value of logging.level.root |
See: docs/FEATURES.md
Since keycloak-config-cli 4.0 will support the latest 4 releases of keycloak, if possible. There are some exceptions:
keycloak-config-cli using maven to build and test keycloak-config-cli.
In case maven is not installed on your system, the mvnw command will download maven for you.
Further development requirements
Before running mvn verify, you have to set the JAVA_HOME environment variable to prevent some test failures.
./mvnw verify
# Windows only
mvnw.cmd verifyIf your are working with a Docker Desktop replacement, some of the Integrationtests can fail due to internal DNS Lookups (host.docker.internal is not reachable). In this case the host can be replaced by a property.
mvn verify -DJUNIT_LDAP_HOST=an.alternate.host.or.ipWe are using TestContainers in our integration tests. To run the integration tests a configured docker environment is required.
./mvnw verify
# Windows only
mvnw.cmd verifyStart a local keycloak on port 8080:
docker-compose down --remove-orphans && docker-compose up keycloakbefore performing following command:
java -jar ./target/keycloak-config-cli.jar \
--keycloak.url=http://localhost:8080 \
--keycloak.ssl-verify=true \
--keycloak.user=admin \
--keycloak.password=admin123 \
--import.files.locations=./contrib/example-config/moped.jsonA docker images is available at DockerHub (docker.io/adorsys/keycloak-config-cli) and quay.io (quay.io/adorsys/keycloak-config-cli)
Available docker tags
| Tag | Description |
|---|---|
latest |
latest available release of keycloak-config-cli which is built against the latest supported Keycloak release. |
latest-x.y.z |
latest available release of keycloak-config-cli which is built against the Keycloak version x.y.z. |
edge |
latest commit on the main branch and which is built against the latest supported Keycloak release. |
a.b.c |
keycloak-config-cli version a.b.c which is built against the latest supported Keycloak release. |
a.b.c-x.y.z |
keycloak-config-cli version a.b.c which is built against the Keycloak version x.y.z. |
maven |
See below |
Additionally, the tag maven contains the source code and compile keycloak-config-cli at runtime. This has the advantage to keycloak-config-cli with
Keycloak versions, that not official supported., e.g.:
docker run --rm -ti -v $PWD:/config/ -eKEYCLOAK_VERSION=23.0.1 -eMAVEN_CLI_OPTS="-B -ntp -q" adorsys/keycloak-config-cli:edge-buildFor docker -e you have to replace dots with underscores.
docker run \
-e KEYCLOAK_URL="http://:8080/" \
-e KEYCLOAK_USER="" \
-e KEYCLOAK_PASSWORD="" \
-e KEYCLOAK_AVAILABILITYCHECK_ENABLED=true \
-e KEYCLOAK_AVAILABILITYCHECK_TIMEOUT=120s \
-e IMPORT_FILES_LOCATIONS='/config/*' \
-v :/config \
adorsys/keycloak-config-cli:latestYou can build an own docker image by running
docker build -t keycloak-config-cli .We provide a helm chart here.
Since it makes no sense to deploy keycloak-config-cli as standalone application, you could add it as dependency to your chart deployment.
Checkout helm docs about chart dependencies!
| CLI Option | ENV Variable | Description | Default | Docs |
|---|---|---|---|---|
| --keycloak.url | KEYCLOAK_URL |
Keycloak URL including web context. Format: scheme://hostname:port/web-context. |
- | |
| --keycloak.user | KEYCLOAK_USER |
login user name | admin |
|
| --keycloak.password | `KEYCLOAK_PA |
No open issues yet, or sync has not completed.