{"id":13565266,"url":"https://github.com/SnuK87/keycloak-kafka","last_synced_at":"2025-04-03T22:30:55.450Z","repository":{"id":35896318,"uuid":"197016841","full_name":"SnuK87/keycloak-kafka","owner":"SnuK87","description":"Keycloak module to produce events to kafka","archived":false,"fork":false,"pushed_at":"2024-09-19T15:08:41.000Z","size":114,"stargazers_count":98,"open_issues_count":2,"forks_count":36,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-04T18:46:37.498Z","etag":null,"topics":["eventlistener","java","kafka","keycloak"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SnuK87.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-15T14:39:37.000Z","updated_at":"2024-11-04T08:39:16.000Z","dependencies_parsed_at":"2024-11-04T18:35:42.429Z","dependency_job_id":"56103c3a-c12d-4245-afcd-84a26d06fe45","html_url":"https://github.com/SnuK87/keycloak-kafka","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnuK87%2Fkeycloak-kafka","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnuK87%2Fkeycloak-kafka/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnuK87%2Fkeycloak-kafka/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SnuK87%2Fkeycloak-kafka/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SnuK87","download_url":"https://codeload.github.com/SnuK87/keycloak-kafka/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247089941,"owners_count":20881891,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eventlistener","java","kafka","keycloak"],"created_at":"2024-08-01T13:01:43.475Z","updated_at":"2025-04-03T22:30:55.018Z","avatar_url":"https://github.com/SnuK87.png","language":"Java","funding_links":[],"categories":["Community Extensions","Identity \u0026 Security","Java"],"sub_categories":["Keycloak"],"readme":"# Keycloak Kafka Module\nSimple module for [Keycloak](https://www.keycloak.org/) to produce keycloak events to [Kafka](https://kafka.apache.org/).\n\n- [Keycloak Kafka Module](#keycloak-kafka-module)\n  * [Build](#build)\n  * [Installation](#installation)\n  * [Module Configuration](#module-configuration)\n    + [Kafka client configuration](#kafka-client-configuration)\n    + [Kafka client using secure connection](#kafka-client-using-secure-connection)\n  * [Module Deployment](#module-deployment)\n  * [Keycloak Configuration](#keycloak-configuration)\n     + [Enable Events in keycloak](#enable-events-in-keycloak)\n  * [Docker Container](#docker-container)  \n  * [Sample Client](#sample-client)\n\n**Tested with** \n\nKafka version: `2.12-2.1.x`, `2.12-2.4.x`, `2.12-2.5.x`, `2.13-2.8`, `2.13-3.3.x`\n\nKeycloak version: `19.0.x, 21.0.x`\n\nJava version: `17`\n\nCheck out [this older version](https://github.com/SnuK87/keycloak-kafka/tree/1.1.1) to run the module on a Wildfly server\n\n\n## Build\nYou can simply use Maven to build the jar file. Thanks to the assembly plugin the build process will create a fat jar that includes all dependencies and makes the deployment quite easy.\nJust use the following command to build the jar file.\n\n```bash\nmvn clean package\n```\n\n## Installation\nFirst you need to build or [download](https://github.com/SnuK87/keycloak-kafka/releases) the keycloak-kafka module.\n\nTo install the module to your keycloak server you have to configure the module and deploy it.\nIf you deploy the module without configuration, your keycloak server will fail to start throwing a `NullPointerException`.\n\nIf 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).\n\n## Module Configuration\nThe following properties can be set via environment variables (e.g. `${KAFKA_TOPIC}`) or as parameters when starting keycloak (e.g. `--spi-events-listener-kafka-topic-events`).\n\n- `topicEvents` (env `KAFKA_TOPIC`): The name of the kafka topic to where the events will be produced to.\n\n- `clientId` (env `KAFKA_CLIENT_ID`): The `client.id` used to identify the client in kafka.\n\n- `bootstrapServers` (env `KAFKA_BOOTSTRAP_SERVERS`): A comma separated list of available brokers.\n\n- `events` (env `KAFKA_EVENTS`): The events that will be send to kafka.\n\n- `topicAdminEvents` (env `KAFKA_ADMIN_TOPIC`): (Optional) The name of the kafka topic to where the admin events will be produced to. No events will be produced when this property isn't set.\n\nA list of available events can be found [here](https://www.keycloak.org/docs/latest/server_admin/#event-types)\n\n###  Kafka client configuration\nIt's also possible to configure the kafka client with environment variables or by adding parameters to the keycloak start command. This makes it possible to connect this module to a kafka broker that requires SSL/TLS connections.\nFor example to change the timeout of how long the producer will block the thread to 10 seconds you just have to pass the following parameter to the start command.\n\n```sh\n./kc.sh start --spi-events-listener-kafka-max-block-ms 10000\n```\n\nOr set the following environnment variable.\n\n```sh\nKAFKA_MAX_BLOCK_MS=10000\n```\n\nA full list of available configurations can be found in the [official kafka docs](https://kafka.apache.org/documentation/#producerconfigs).\n\n\n| :warning: WARNING                                                                                                                                                                                                                                                                                           |\n|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Because some environments have difficulties with empty string variables, a workaround for `SSL_ENDPOINT_IDENTIFICATION_ALGORITHM` was implemented. To disable the host name verification set the value to `disabled`. The module will transfer the value to an empty string when creating the kafka client. |\n\n### Kafka client using secure connection\nAs mentioned above the kafka client can be configured by passing parameters to the start command. To make kafka open a SSL/TLS secured connection you can add the following parameters:\n\n```\n./kc.sh start \\\n  --spi-events-listener-kafka-security-protocol SSL \\\n  --spi-events-listener-kafka-ssl-truststore-location kafka.client.truststore.jks \\\n  --spi-events-listener-kafka-ssl-truststore-password test1234\n```\n\n## Module Deployment\nCopy the `keycloak-kafka-\u003cversion\u003e-jar-with-dependencies.jar` into the `$KEYCLOAK_HOME/providers` folder. Keycloak will automatically \ninstall the module with all it's dependencies on start up.\n\n\n## Keycloak Configuration\n\n### Enable Events in keycloak\n1. Open administration console\n2. Choose realm\n3. Go to Events\n4. Open `Config` tab and add `kafka` to Event Listeners.\n\n![Admin console config](images/event_config.png)\n\n## Docker Container\nThe simplest way to enable the kafka module in a docker container is to create a custom docker image from the keycloak base image. A simple example can be found in the [Dockerfile](Dockerfile).\nWhen you build this image on your local machine by using `docker build . -t keycloak-kafka`, you can test everything by running the [docker-compose](docker-compose.yml) file on your local machine. \nThis just provides a simple example to show how it's working. Please consider to read [this documentation](https://www.keycloak.org/server/containers) and create your own Dockerfile.\n\n## Sample Client\n\nThe following snippet shows a minimal Spring Boot Kafka client to consume keycloak events. Additional properties can be added to the `KeycloakEvent` class.\n\n```java\n@SpringBootApplication\n@Log4j2\npublic class KafkaConsumerApplication {\n\n\tpublic static void main(String[] args) {\n\t\tSpringApplication.run(KafkaConsumerApplication.class, args);\n\t}\n\n\t@KafkaListener(topics = \"keycloak-events\", groupId = \"event-consumer\")\n\tpublic void handleKeycloakEvent(KeycloakEvent event) {\n\t\tlog.info(\"Consumed event: \" + event);\n\t}\n\n\t@KafkaListener(topics = \"keycloak-admin-events\", groupId = \"event-consumer\")\n\tpublic void handleKeycloakAdminEvent(KeycloakAdminEvent event) {\n\t\tlog.info(\"Consumed admin event: \" + event);\n\t}\n\n\t@Bean\n\tpublic StringJsonMessageConverter jsonConverter() {\n\t\treturn new StringJsonMessageConverter();\n\t}\n}\n\n@Data\nclass KeycloakEvent {\n\tprivate String userId;\n\tprivate String type;\n}\n\n@Data\nclass KeycloakAdminEvent {\n\tprivate String realmId;\n\tprivate String operationType;\n}\n```\n\n## Contribution\n\nAny kind of contributions are welcome.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSnuK87%2Fkeycloak-kafka","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSnuK87%2Fkeycloak-kafka","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSnuK87%2Fkeycloak-kafka/lists"}