Skip to content

Commit

Permalink
add Dockerfile, cli script and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
SnuK87 committed Apr 24, 2020
1 parent d10db3a commit 35a39bf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM jboss/keycloak:9.0.3

ADD ./keycloak-module/keycloak-kafka-1.0.0.jar /opt/jboss/keycloak/modules/system/layers/keycloak/com/github/snuk87/keycloak/keycloak-kafka/main/
ADD ./keycloak-module/module.xml /opt/jboss/keycloak/modules/system/layers/keycloak/com/github/snuk87/keycloak/keycloak-kafka/main/
ADD ./kafka-clients/kafka-clients-2.2.0.jar /opt/jboss/keycloak/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main/
ADD ./kafka-clients/lz4-java-1.5.0.jar /opt/jboss/keycloak/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main/
ADD ./kafka-clients/snappy-java-1.1.7.2.jar /opt/jboss/keycloak/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main/
ADD ./kafka-clients/zstd-jni-1.3.8-1.jar /opt/jboss/keycloak/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main/
ADD ./kafka-clients/module.xml /opt/jboss/keycloak/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main/

ADD kafka-module.cli /opt/jboss/startup-scripts/

#ADD realm-export.json /init/

# to prevent 'java.nio.file.DirectoryNotEmptyException: /opt/jboss/keycloak/standalone/configuration/standalone_xml_history/current' on startup
RUN rm -rf /opt/jboss/keycloak/standalone/configuration/standalone_xml_history

# from base image
USER 1000

EXPOSE 8080
EXPOSE 8443

ENTRYPOINT [ "/opt/jboss/tools/docker-entrypoint.sh" ]

CMD ["-b", "0.0.0.0", "-Dkeycloak.import=/init/realm-export.json"]
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ Simple module for [Keycloak](https://www.keycloak.org/) to produce keycloak even
* [Configuration](#configuration)
+ [Enable Events in keycloak](#enable-events-in-keycloak)
+ [Kafka module](#kafka-module)
* [Docker Container](#configuration)
* [Sample Client](#sample-client)

**Tested with**

Kafka version: `2.12-2.1.0`
Kafka version: `2.12-2.1.x`, `2.12-2.4.x`

Keycloak version: `4.8.3`, `6.0.1`
Keycloak version: `4.8.3`, `6.0.x`, `7.0.0`, `9.0.x`

Java version: `11`
Java version: `11`, `13`


## Build
Expand Down Expand Up @@ -120,6 +121,22 @@ Add the following content to your `standalone.xml`:

`topicAdminEvents`: (Optional) The name of the kafka topic to where the admin events will be produced to.


To automate the configuration of the event listener, it is possible to run the [CLI script](kafka-module.cli) from this repo.
Make sure to edit the properties to fit your environemnt and use the right server config (default is `standalone-ha`)

```bash
$KEYCLOAK_HOME/bin/jboss-cli.sh --file /path/to/kafka-module.cli
```

## Docker Container
The simplest way to enable the kafka module in a docker container is to create a custom docker image from the keycloak base image and use the CLI script to configure the kafka module.
First all .jar files must be added to the image and placed in their module directory as explained in [Installation](#installation). Then the CLI script must be added and placed in
the `/opt/jboss/startup-scripts/` directory, so the script will be executed automatically on startup.

An example can be found in this [Dockerfile](Dockerfile).


## Sample Client

The following snippet shows a minimal Spring Boot Kafka client to consume keycloak events. Additional properties can be added to `KeycloakEvent`.
Expand Down
12 changes: 12 additions & 0 deletions kafka-module.cli
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
embed-server --server-config=standalone-ha.xml --std-out=echo

if (outcome != success) of /subsystem=keycloak-server/spi=eventsListener:read-resource()
/subsystem=keycloak-server:list-add(name="providers",value="module:com.github.snuk87.keycloak.keycloak-kafka")
/subsystem=keycloak-server/spi=eventsListener:add()
/subsystem=keycloak-server/spi=eventsListener/provider=kafka:add(enabled=true)
/subsystem=keycloak-server/spi=eventsListener/provider=kafka:write-attribute(name=properties.topicEvents,value=keycloak-events)
/subsystem=keycloak-server/spi=eventsListener/provider=kafka:write-attribute(name=properties.clientId,value=keycloak)
/subsystem=keycloak-server/spi=eventsListener/provider=kafka:write-attribute(name=properties.bootstrapServers,value=192.168.0.1:9092)
/subsystem=keycloak-server/spi=eventsListener/provider=kafka:write-attribute(name=properties.events,value=REGISTER)
end-if
stop-embedded-server

0 comments on commit 35a39bf

Please sign in to comment.