Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keycloak Docker import users

Abstract

I need a pre-configured keycloak instance deployed automatically during tests.
Importing users into Keycloak seems to be a challenge

Approach

I created a Docker container with predefined Realm/Users/Clients
Using the export / import feature I can easily rebuild a Realm with clients, but not Users.
To be clear, Users shows up in the JSON file. So the export looks legit.

Desperate Attempts

I have tried multiple approaches including, but not limited to:

  1. ENV KEYCLOAK_IMPORT my-realm.json in the docker file => Realm ✔ Clients ✔ Users ✖

Did not create Users but was perfect in term of workflow. It runs during the docker build.

  1. Import through docker exec ... action=import => Realm ✔ Clients ✔ Users ✖
docker exec -it <id> /opt/jboss/keycloak/bin/standalone.sh -Djboss.socket.binding.port-offset=100 -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.realmName=myrealm -Dkeycloak.migration.file=/tmp/my-realm.json
  1. Set the Strategy: OVERWRITE_EXISTING for users and provider to singleFile. to have everything in one go.
  2. Manually import through the admin console, Realm ✖ Clients ✔ Users ✔

Keycloak console screenshot

Clearly not a good fit for automation, but at least my JSON users are importable.

Hence

Is there some cheat code environemt variable like KEYCLOAK_FULL_IMPORT that does everything in one go in the Dockerfile?

like image 215
MonoThreaded Avatar asked Jun 09 '20 23:06

MonoThreaded


1 Answers

I confess with limited pride this was a stupid typo in my Dockerfile

For short I was copying from the wrong file. The proper configuration looked like this

FROM jboss/keycloak:latest

COPY my-realm.json /tmp/my-realm.json

ENV KEYCLOAK_USER admin
ENV KEYCLOAK_PASSWORD admin

ENV DB_VENDOR h2

ENV KEYCLOAK_IMPORT /tmp/my-realm.json

The typo was COPY some-realm.json /tmp/my-realm.json where some-realm.json did actually not include users.

like image 171
MonoThreaded Avatar answered Oct 17 '22 20:10

MonoThreaded