Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Dynamic Security Plugin: How to create clients/groups with the topic $CONTROL/dynamic-security/v1 topic

Hello to all, 

I have a backend server programmed in PHP with the Symfony framework and I am using the mqttClient/PHP package to connect and communicate with a mosquitto broker which I'm running locally. The mosquitto broker is using the Dynamic Security Plugin, defined in the mosquitto.config as the https://mosquitto.org/documentation/dynamic-security/ says to do.

My problem is as follows: 

I want the PHP server to be a client of the broker, and this client will act as the admin of the broker. I need to dynamically update the dynamic_security.json file so that I can add/remove/update clients/roles/groups. For this to happen, I am publishing on the $CONTROL/dynamic-security/v1 topic, as the documentation indicates. However, every time I try to publish on this topic, because the client is also subscribed to the topic $CONTROL/dynamic-security/#, I always get the following error:

Received message on topic "$CONTROL/dynamic-security/v1/response": {"responses":[{"command":"Unknown command","error":"Invalid/missing commands"}]}

The command (payload) I am sending to the broker is a json message with the following structure:

$client->subscribe('$CONTROL/dynamic-security/#', function (string $topic, string $message) use ($output) {
// Do something with the message
$output->writeln(sprintf('Received message on topic "%s": %s', $topic, $message));
});

$client->publish('$CONTROL/dynamic-security/v1', json_encode([
"commands" => [
"command" => "createClient",
"clientid" => "clientid",
"username" => "testing",
"password" => "password",
"roles" => ["admin"],
"topics" => [
"topic1" => [
"qos"=> 0,
"retain"=> false,
"pub"=> true,
"sub"=> true
]
]
]
]));

What am I doing wrong?

On the running mosquitto broker, when I try to publish this message, the following message is logged:

1679395256: New connection from ::1:62345 on port 1884.
1679395256: New client connected from ::1:62345 as client_id (p2, c1, k10, u'admin-user').
1679395256: No will message specified.
1679395256: Sending CONNACK to client_id (0, 0)
1679395256: Received SUBSCRIBE from client_id
1679395256:     $CONTROL/dynamic-security/# (QoS 0)
1679395256: client_id 0 $CONTROL/dynamic-security/#
1679395256: Sending SUBACK to client_id
1679395256: Received PUBLISH from client_id (d0, q0, r0, m0, '$CONTROL/dynamic-security/v1', ... (31 bytes))
1679395256: Sending PUBLISH to client_id (d0, q0, r0, m0, '$CONTROL/dynamic-security/v1/response', ... (80 bytes))
1679395266: Received PINGREQ from client_id
1679395266: Sending PINGRESP to client_id

On the other hand, if i use the mosquitto_ctrl on the command line, I can create clients and get this message on the client subscribed to the  $CONTROL/dynamic-security/# topic:

Received message on topic "$CONTROL/dynamic-security/v1/response": {"responses":[{"command":"createClient"}]}

What am I missing? I would really appreciate if anyone could help me because I am stuck here and this is really important for me.

---------------------------------------------------------------------------------------------

Summarizing:

I need to dynamically add and remove clients and I need to do that on my code, so I can't use the mosquitto_ctrl tool. To which topic should I send the message with the new clients? What commands does that topic accept? The mqttCLient/PHP is using the MQTT v3.1.1, is this the problem? It shouldn't be, right?

Best regards,
Francisco

Back to the top