Setup Keycloak automatic using REST API

Viewed 210

I want to setup Keycloak for my Pipeline, this should be done automatically, I thought of manually pre configuring an realm in the webinterface of keycloak and then exporting this realm using this page: exporting page on keycloak image

then i want to import it at every run of the pipeline so I get a fresh environment.

For importing my realm I used this endpoint:

http://localhost:8080/auth/admin/realms

documented here

when executing in postman, I get this error

"error": "unknown_error"

and in the console it says

Script upload is disabled

It looks like script uploads could be activated but according to the documentation docu profiles this feature is marked as deprecated. So I think this option would not be the way to go and future proof?

Maybe there is even another option that would be better for my usecase?

I am using Keycloak in a Docker Container quay.io/keycloak/keycloak - with the latest version currently version 15.0.1 of keycloak

Thank you for your time to answer the question.

1 Answers

So after some time I have found a solution that worked for me and maybe this is also helpful for others.

Some things to notice:

  • I only need the import realm feature for development - for production it would work a little different.
  • This is also for the Quarkus versions of Keycloak in older versions it also works different

Docker Compose:

services:
  keycloak:
      image: quay.io/keycloak/keycloak:19.0.1
      command: start-dev --import-realm
      volumes:
        - ./path/to/local/realm_export.json:/opt/keycloak/data/import/realm.json:ro

more details about the import can be found here: Keycloak Import Export Documentation

Related