0

I'm attempting to customize my Keycloak docker container. I'm using jboss/keycloak:12.0.4 as the base image for my docker image.

I need to run a SQL update statement on one specific table once any required updates are made by the Liquibase scripts included in the container's startup.

Where are the scripts located in the image? I can't seem to find them.

Note: I'd be happy to receive alternative approaches as well. As long as the DB records are updated before the keycloak server starts up

The script I want to add will look something like this:

db.changelog.postdeployment.json
{
  "databaseChangeLog": [
    {
      "changeSet": {
        "id": "RUNALWAYS-AFTER-DEPLOYMENT",
        "author": "DevOps",
        "runAlways": true,
        "failOnError": false,
        "changes": [
          {
            "sql": {
              "sql": "UPDATE public.table_name SET col1='VALUUE' WHERE id='id';"
            }
          }
        ]
      }
    }
  ]
}
ScrappyDev
  • 2,307
  • 8
  • 40
  • 60

1 Answers1

4

Original Keycloak liquibase changelogs are located in the Keycloak JAR files, to be clear thoses are read-only.

You'll need to build your own jar that will contain your liquibase changelogs and some config to tell Keycloak to run theses scripts.

Keycloak uses a system of SPI (Service Provider Interfaces) to extend the server, in your case you need to implement JpaEntityProvider which contains a method to tell Keycloak the location of liquibase changelogs.

Example :

public class ExampleJpaEntityProvider implements JpaEntityProvider {

// List of your JPA entities.
@Override
public List<Class<?>> getEntities() {
    return Collections.emptyList();
}

// This is used to return the location of the Liquibase changelog file.
// You can return null if you don't want Liquibase to create and update the DB schema.
@Override
public String getChangelogLocation() {
        return "META-INF/example-changelog.xml";
}

// Helper method, which will be used internally by Liquibase.
@Override
public String getFactoryId() {
    return "sample";
}

...

}

Keycloak will automatically deploy SPI jars if you put them in the following folder : standalone/deployments/

Here is the documentation specific to the JpaEntityProvider : https://www.keycloak.org/docs/latest/server_development/index.html#_extensions_jpa

Here is some more general documentation on how to build and deploy an SPI : https://www.keycloak.org/docs/latest/server_development/index.html#_providers

Lucas Declercq
  • 1,534
  • 11
  • 17