softwarefactory-project / keycloak-event-listener-mqtt

A Keycloak SPI that publishes events to a MQTT broker.
Apache License 2.0
39 stars 9 forks source link

UH OH!! MqttException (0) : Message not send after user update #4

Closed ghost closed 2 years ago

ghost commented 3 years ago

Hi,

when i update a user in keycloak, i get this error. The mqtt broker does not received the message.

15:39:20,487 INFO [stdout] (default task-2) UH OH!! MqttException (0) 15:39:20,488 ERROR [stderr] (default task-2) MqttException (0) 15:39:20,489 ERROR [stderr] (default task-2) at deployment.event-listener-mqtt-9.0.0-jar-with-dependencies.jar//org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence.open(MqttDefaultFilePersistence.java:85)

Here is the associated code of the error :

https://github.com/eclipse/paho.mqtt.java/blob/master/org.eclipse.paho.client.mqttv3/src/main/java/org/eclipse/paho/client/mqttv3/persist/MqttDefaultFilePersistence.java

public void open(String clientId, String theConnection) throws MqttPersistenceException {

        if (dataDir.exists() && !dataDir.isDirectory()) {
            throw new MqttPersistenceException();
        } else if (!dataDir.exists() ) {
            if (!dataDir.mkdirs()) {
                throw new MqttPersistenceException();
            }
        } 
        if (!dataDir.canWrite()) {
            throw new MqttPersistenceException();
        }
...
}

Here is my docker-compose

version: '3'
services:
  mqtt:
      image: "hivemq/hivemq4"
      container_name: "mqtt"
      ports:
        - 1883:1883
        - 8883:8883
      networks:
        - my-project
  keycloak:
    build:
      context: ./keycloak
      dockerfile: Dockerfile
    container_name: "keycloak"
    environment:
      DB_VENDOR: mysql
      DB_ADDR: mysql
      DB_PORT: 3306
      DB_DATABASE: myproject
      DB_USER: myproject
      DB_PASSWORD: myproject
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: admin
      KEYCLOAK_IMPORT: /opt/jboss/keycloak/imports/keycloak-myproject.json -Dkeycloak.profile.feature.upload_scripts=enabled          
      PROXY_ADDRESS_FORWARDING: "true"
      KEYCLOAK_LOGLEVEL: ALL
      MQTT_HOST: tcp://mqtt:1883
    command: ["-c=standalone.xml"]
    volumes:
      - ./keycloak/imports:/opt/jboss/keycloak/imports
    ports:
      - '8092:8080'
      - '8443:8443'
      - '9990:9990'
    depends_on:
      - mysql
      - mqtt
    links:
      - mysql
      - mqtt
    networks:
      - my-project

Le standalone.xml

<spi name="eventsListener">
                <provider name="mqtt" enabled="true">
                    <properties>
                        <property name="serverUri" value="tcp://127.0.0.1:1883"/>
                        <property name="username" value=""/>
                        <property name="password" value=""/>
                        <property name="topic" value="keycloak"/>
                    </properties>
                </provider>
            </spi>

my Dockerfile

FROM jboss/keycloak:11.0.2
COPY event-listener-mqtt-9.0.0-jar-with-dependencies.jar /opt/jboss/keycloak/standalone/deployments/event-listener-mqtt-9.0.0-jar-with-dependencies.jar
COPY startup-config.cli /opt/jboss/startup-scripts/startup-config.cli
COPY ./imports/keycloak-realm.json /opt/jboss/keycloak/keycloak-realm.json

and my startup-config.cli

embed-server --std-out=echo
/subsystem=keycloak-server/spi=eventsListener/:add
/subsystem=keycloak-server/spi=eventsListener/provider=mqtt/:add(properties={"serverUri" => "tcp://localhost:1883", "topic" => "keycloak"}, enabled=true)
stop-embedded-server

In keycloak, the mqtt logger is selected. The mqtt run well. I MQTTBox to send message on it.

How can i resolve this error ?

Thanks a lot

mhuin commented 3 years ago

Hello, I think this might be related to https://github.com/softwarefactory-project/keycloak-event-listener-mqtt/issues/2 It looks like this extension doesn't work well with the keycloak docker image due to some differences with file permissions. PR #3 might fix your problem. If you could test it and confirm, I'll commit it myself.

mhuin commented 2 years ago

Hello,

Apologies for the late followup. I believe the problem is fixed in the latest release - at least it is possible to disable persistence altogether in the settings.

If the problem persists, please reopen an issue.