Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Multiple realm in keycloak?

Tags:

keycloak

I have two different realms and I've to import different configs by realm JSON for both of them(i.e import both realm).

I created Docker compose for that above said.

here is the code.

version: "3"
services:
  keycloak:
    image: jboss/keycloak
    ports:
      - "8080:8080"
    volumes:
      - "/Users/msn/Downloads/:/Users/msn/Downloads/"
    environment:
      - "KEYCLOAK_USER=admin"
      - "KEYCLOAK_PASSWORD=admin"
      - "KEYCLOAK_IMPORT=/Users/msn/Downloads/realm-export.json,/Users/msn/Downloads/realm-expor.json"

but it imports only first JSON not the second please suggest the solutions to import both of the realms.

like image 690
Mohit Singh Avatar asked Apr 13 '20 09:04

Mohit Singh


People also ask

How do I export a Keycloak realm?

To export a realm, you can use the export command. Your Keycloak server instance must not be started when invoking this command. To export a realm to a directory, you can use the --dir <dir> option. When exporting realms to a directory, the server is going to create separate files for each realm being exported.


Video Answer


3 Answers

version: "3"
services:
  keycloak:
    image: jboss/keycloak
    ports:
      - "8080:8080"
    volumes:
      - "/Users/msn/Downloads/:/Users/msn/Downloads/"
    environment:
      - "KEYCLOAK_USER=admin"
      - "KEYCLOAK_PASSWORD=admin"
      #- "KEYCLOAK_IMPORT=/Users/msn/Downloads/realm-export.json,/Users/msn/Downloads/realm-expor.json"
    command:
      - "-b 0.0.0.0"
      - "-Dkeycloak.migration.action=import"
      - "-Dkeycloak.migration.provider=singleFile"
      - "-Dkeycloak.migration.file=/Users/msn/Downloads/realm-export.json"
      - "-Dkeycloak.migration.strategy=IGNORE_EXISTING"

note: all your realms have to be in the same file "realm-export.json".

like image 24
haythem.jedidi Avatar answered Oct 19 '22 08:10

haythem.jedidi


put both of your realms to one .json file where content is array. So:

[
{...realm1...},
{...realm2...}
]

EDIT: Maybe you will need to adjust some parameters. I'm working only with Dockerfile so here is content of it. I hope it will help you.

FROM jboss/keycloak:9.0.2
COPY "src/main/jib/opt/jboss/keycloak/imports/realm.json" "/opt/jboss/keycloak/imports/realm.json"
CMD ["-Dkeycloak.migration.action=import", "-Dkeycloak.migration.provider=singleFile", "-Dkeycloak.migration.file=/opt/jboss/keycloak/imports/realm.json", "-Dkeycloak.migration.strategy=IGNORE_EXISTING"]
like image 72
bilak Avatar answered Oct 19 '22 06:10

bilak


Working on Keycloak 12.0.4 (import multiple realms in separated files)

This is from a Dockerfile, but you can easily use the same configuration in a docker-compose.

Dockerfile

First the realm files must be copied into the container

COPY keycloak-files/realm-config/* /opt/jboss/keycloak/realm-config/

Now I can configure the JAVA_OPTS environment variable, to include:

ENV JAVA_OPTS -server \
 -Xms1303m \
 -Xmx1303m \
 -XX:MetaspaceSize=96M \
 -XX:MaxMetaspaceSize=256m \
 -Djboss.modules.system.pkgs=org.jboss.byteman \
 -Djava.awt.headless=true \
 -Djava.net.preferIPv4Stack=true \
 -Dkeycloak.migration.action=import \
 -Dkeycloak.migration.provider=dir \
 -Dkeycloak.migration.dir=/opt/jboss/keycloak/realm-config \
 -Dkeycloak.migration.strategy=OVERWRITE_EXISTING 

The important lines are prefixed by keycloak.migration.- Here we set the migration dir (where files will be imported from) to be equal as the one where we copied files before.

Docker-compose

version: '3.8'
services:
  keycloak:
    image: jboss/keycloak:12.0.4
    command:
      [
        '-b',
        '0.0.0.0',
        '-Dkeycloak.migration.action=import',
        '-Dkeycloak.migration.provider=dir',
        '-Dkeycloak.migration.dir=/opt/jboss/keycloak/realm-config',
        '-Dkeycloak.migration.strategy=OVERWRITE_EXISTING',
      ]
    volumes:
      - ./keycloak-files/realm-config:/opt/jboss/keycloak/realm-config

...

Basically it's the same.

You can copy any json file inside realm-config and it should be imported.

It's possible to use IGNORE_EXISTING migration strategy to avoid overwriting.

like image 2
funder7 Avatar answered Oct 19 '22 08:10

funder7