DEV Community

Cover image for Configure Keycloak by a CLI extension
Ulrich VACHON
Ulrich VACHON

Posted on • Edited on

Configure Keycloak by a CLI extension

Today I will show you how to configure a Keycloak property by command-line interface.

The main advantage in the Keycloak world is the possibility to extend it. πŸ”§ Indeed, it exists several way to extend or manage this software and one of these is to use a CLI script.

In this article we will change the value at the startup time of the global property named staticMaxAge responsible of the caching for all assets of all themes. To be noted that we cannot configure this value by the UI but only by the standalone(-ha).xml file.

Search the node path of the property

Whatever the selected Keycloak profile (standalone or cluster mode), we will find the expected node here :

server > profile > subsystem:keycloak-server > theme > staticMaxAge
Enter fullscreen mode Exit fullscreen mode

Create the CLI script

As soon as we have the node we have to translate it to JBoss script.

embed-server --server-config=standalone.xml

/subsystem=keycloak-server/theme=defaults/:write-attribute(name=staticMaxAge,value=3600)

quit
Enter fullscreen mode Exit fullscreen mode
  • The first one line indicates to the engine the target profile,
  • The second one line writes the expected value in the property (staticMaxAge).

πŸ“ One little tip, you can use a environment variable to fill the property :

embed-server --server-config=standalone.xml

/subsystem=keycloak-server/theme=defaults/:write-attribute(name=staticMaxAge,value="${env.KEYCLOAK_ADMIN_STATIC_MAX_AGE:3600})

quit
Enter fullscreen mode Exit fullscreen mode

Push the CLI script in the right folder

If you want to configure your instance at the startup time (the common case for the production environment) you have to push the script in the following path : /opt/jboss/startup-scripts

This is an example of my Docker πŸ‹ configuration :

RUN mkdir /opt/jboss/startup-scripts

ADD --chown=jboss:root cli/update_assets_cache.cli /opt/jboss/startup-scripts/
Enter fullscreen mode Exit fullscreen mode

Let me try it

If you want a complete example with a Docker configuration, you can clone the repository https://github.com/ulrich/keycloak-configuration-with-cli

CrΓ©dit photo : Vanille

Top comments (0)