Search code examples
pythonmqttaclpaho

How to find out that mqtt-paho publish failed due to ACL?


I'm using paho.mqtt Python library. I have an ACL list set up on my MQTT broker. However, I can't see any difference between publishing to a topic I have rights to and publishing to a topic I don't have rights to.

I set up on_connect and on_publish callbacks in following ways:

def on_connect(args, client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT Broker!")
        msginfo = client.publish(args.topic, args.string, qos=2, retain=True)
        time.sleep(1)
        print(f"Publish rc {msginfo.rc}")
    else:
        print(f"Failed to connect, return code {rc}\n")

def on_publish(client, userdata, mid):
    print(f"On publish: userdata:{userdata}, mid:{mid}")

I get the same output:

Connected to MQTT Broker!
Publish rc 0
On publish: userdata:None, mid:1

in both cases. Is there a way to get the information "Publish failed because of authorization/ACL"?


Solution

  • The short answer is you don't (at MQTT v3.x)

    Slightly longer answer:

    MQTT v5 introduced flags in the response packets for Messages published with QOS 1 or 2 (QOS 0 are not acknowledged so no way to signal not authorised)

    https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901124

    If your broker supports MQTT v5 then you can try passing the relevant options (protocol=MQTTv5)to the paho client at connect time to see if you get a different answer. I've not tried so can't be sure if it will throw an error that can be caught by the on_error callback or if it will just not call the on_published callback.