Skip to content

Commit

Permalink
Merge pull request #8 from SnuK87/jar-with-dependencies
Browse files Browse the repository at this point in the history
build a jar file with depencies for easier installation
  • Loading branch information
SnuK87 authored May 21, 2020
2 parents 35a39bf + 2a80619 commit 4bc19a5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 108 deletions.
12 changes: 3 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
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/
FROM jboss/keycloak:10.0.1

ADD ./keycloak-kafka-1.0.0-jar-with-dependencies.jar /opt/jboss/keycloak/standalone/deployments/

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

Expand Down
124 changes: 29 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Simple module for [Keycloak](https://www.keycloak.org/) to produce keycloak even

**Tested with**

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

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

Java version: `11`, `13`

Expand All @@ -24,92 +24,16 @@ Java version: `11`, `13`
`mvn clean package`

## Installation
To install the keycloak-kafka module to your keycloak server you have to first configure the module and then deploy the module.
If you deploy the module without configuration your keycloak server will fail to start up with a NullPointerException.

Add a new provider in `standalone.xml` under `<subsystem xmlns="urn:jboss:domain:keycloak-server:1.1">`.
If you want to install the module manually as described in the initial version you can follow this [guide](https://github.com/SnuK87/keycloak-kafka/wiki/Manual-Installation).

```xml
<providers>
<provider>classpath:${jboss.home.dir}/providers/*</provider>
<provider>module:com.github.snuk87.keycloak.keycloak-kafka</provider>
</providers>
```


Create a new folder in `$KEYCLOAK_HOME/modules/system/layers/keycloak/com/github/snuk87/keycloak/keycloak-kafka/main`

Copy `keycloak-kafka-1.0.0.jar` into the `main` folder and create a new file `module.xml` with the following content:

```xml
<?xml version="1.0" ?>
<module xmlns="urn:jboss:module:1.3" name="com.github.snuk87.keycloak.keycloak-kafka">
<resources>
<resource-root path="keycloak-kafka-1.0.0.jar" />
</resources>
<dependencies>
<module name="org.keycloak.keycloak-core"/>
<module name="org.keycloak.keycloak-server-spi"/>
<module name="org.keycloak.keycloak-server-spi-private"/>
<module name="com.fasterxml.jackson.core.jackson-core"/>
<module name="com.fasterxml.jackson.core.jackson-databind"/>
<module name="org.jboss.logging"/>
<module name="org.apache.kafka.kafka-clients"/>
</dependencies>
</module>
```

Create a new folder in `$KEYCLOAK_HOME/modules/system/layers/keycloak/org/apache/kafka/kafka-clients/main`
and create a new file `module.xml`.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.apache.kafka.kafka-clients">
<resources>
<resource-root path="kafka-clients-2.2.0.jar"/>
<resource-root path="lz4-java-1.5.0.jar"/>
<resource-root path="snappy-java-1.1.7.2.jar"/>
</resources>
<dependencies>
<module name="org.slf4j"/>
</dependencies>
</module>
```

Download the .jar files listed under `<resources>` from [MVN Repository](https://mvnrepository.com/) and put them into the same folder.
### Module configuration
Download the [CLI script](kafka-module.cli) from this repository and edit the properties to fit your environment. Also make sure that you use the right
server config (line 1). As a default the script will change the `standalone.xml`.

```
.
├── kafka-clients-2.2.0.jar
├── lz4-java-1.5.0.jar
├── module.xml
├── snappy-java-1.1.7.2.jar
└── zstd-jni-1.3.8-1.jar
```

## Configuration

### Enable Events in keycloak
1. Open administration console
2. Choose realm
3. Go to Events
4. Open `Config` tab and add `kafka` to Event Listeners. If you can't choose `kafka` you have to restart the keycloak server first.


### Kafka module
Add the following content to your `standalone.xml`:

```xml
<spi name="eventsListener">
<provider name="kafka" enabled="true">
<properties>
<property name="topicEvents" value="keycloak-events"/>
<property name="clientId" value="keycloak"/>
<property name="bootstrapServers" value="192.168.0.1:9092,192.168.0.2:9092"/>
<property name="events" value="REGISTER,LOGIN,LOGOUT"/>
<property name="topicAdminEvents" value="keycloak-admin-events"/>
</properties>
</provider>
</spi>
```
Currently the following properties are available and should be changed to fit your environemnt:

`topicEvents`: The name of the kafka topic to where the events will be produced to.

Expand All @@ -121,21 +45,31 @@ 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`)

Run the CLI script using the following command and check the output on the console. You should see some server logs and 6 lines of `{"outcome" => "success"}`.
```bash
$KEYCLOAK_HOME/bin/jboss-cli.sh --file /path/to/kafka-module.cli
$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.
### Module deployment
Copy the `keycloak-kafka-<version>-jar-with-dependencies.jar` into the $KEYCLOAK_HOME/standalone/deployments folder. Keycloak will automatically
install the module with all dependencies on start up. To verify that the deployment of the module was successful you can check if a new file
with the name `keycloak-kafka-<version>-jar-with-dependencies.jar.deployed` was created in the same folder.


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

### Enable Events in keycloak
1. Open administration console
2. Choose realm
3. Go to Events
4. Open `Config` tab and add `kafka` to Event Listeners.


## 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](https://hub.docker.com/r/jboss/keycloak/).
The `keycloak-kafka-<version>-jar-with-dependencies.jar` must be added to the `/standalone/deployments` folder and the CLI script must be added to the `/opt/jboss/startup-scripts/` folder
as explained in [Installation](#installation). The only difference is that the CLI script will be executed automatically in start up and doesn't have to be executed manually.
An example can be found in this [Dockerfile](Dockerfile).

## Sample Client

Expand Down
3 changes: 1 addition & 2 deletions kafka-module.cli
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
embed-server --server-config=standalone-ha.xml --std-out=echo
embed-server --server-config=standalone.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)
Expand Down
26 changes: 24 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<keycloak.version>9.0.3</keycloak.version>
<kafka.version>2.2.0</kafka.version>
<keycloak.version>10.0.1</keycloak.version>
<kafka.version>2.5.0</kafka.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -46,4 +46,26 @@
<version>${kafka.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

0 comments on commit 4bc19a5

Please sign in to comment.