Authorization

The ordered authorization chain (ACL file, JWT, HTTP, PostgreSQL, MySQL, Redis, MongoDB), first-decision-wins evaluation, per-source superuser bypass, rule format and topic matching.

The authorization chain decides which topics a client may publish to and which filters it may subscribe to. It is an ordered list of sources declared as [[authorization]] tables, independent of the authentication chain that let the client in: a client can log in with the password file and get its rules from PostgreSQL, a JWT, or both.

Every check runs on the client's own core, with no I/O and no locks. Sources whose rules live in a database or a service load each client's rules once, at CONNECT, and keep them with the session.

How the chain works

For every PUBLISH and SUBSCRIBE, the enabled sources are asked in order and the first allow or deny wins. Each source answers:

  • allow or deny when one of its rules matches, or when none matches and its own no_match is "allow" or "deny";
  • ignore when none of its rules matches and its no_match is "ignore" (the default), or when it has nothing for this client (no rows loaded, no token, an HTTP "result": "ignore").

When every source answered ignore, [auth] no_match decides (default "deny", env ORBITMQ_AUTH_NO_MATCH). An empty authorization chain (no [[authorization]] table) allows everything, as in a broker without ACLs. Disabled sources are skipped.

orbitmq.toml
[auth]
no_match = "deny"               # chain end (the default)

[[authorization]]               # 1. per-device rules from PostgreSQL, loaded at CONNECT
kind = "postgres"
name = "device-acl"
url = { env = "MQTT_AUTH_DB_URL" }
acl_query = "SELECT permission, action, topic FROM mqtt_acl WHERE username = %u ORDER BY id"

[[authorization]]               # 2. shared rules for everyone else
kind = "file"
name = "static"
path = "/etc/orbitmq/acl.toml"

Here a device whose rows allow devices/sensor-17/# may publish there; a topic none of its rows matches falls through to the ACL file; and an action the ACL file does not match either is refused by [auth] no_match.

Source keys

Besides kind, name and enabled (see Common keys), every [[authorization]] source takes:

KeyDefaultMeaning
superuser_bypasstrueAllow superusers before this source's rules.
no_match"ignore"This source's decision when none of its rules matches: "ignore" (the next source decides), "deny" or "allow".
on_error"deny"Network kinds: a failed ACL load (error, timeout, max_in_flight overflow, more than 1024 rules or an invalid rule). "deny": this source denies every action of that session (fail closed). "ignore": it answers ignore for that session.
timeout_ms3000Network kinds: time allowed for one ACL load. All loads of one CONNECT start at once and also share [auth] timeout_ms.
max_in_flight256Network kinds: most ACL loads running at once.
[authorization.cache]see CachingNetwork kinds: remembers a client's loaded rules.

on_not_found, auth_query and the other keys of authenticating sources are errors here.

Superusers

A client is a superuser when the authentication source that accepted it says so (a JWT superuser_claim, an HTTP is_superuser field, an is_superuser column or hash field, a MongoDB superuser_field), or when its user name is listed in [auth] superusers:

orbitmq.toml
[auth]
superusers = ["operator"]   # env ORBITMQ_AUTH_SUPERUSERS=operator (comma-separated)

Each authorization source decides for itself what a superuser may do:

  • superuser_bypass = true (the default): the source allows a superuser before looking at its rules.
  • superuser_bypass = false: the source evaluates its rules for superusers like for anyone else. In an ACL file, rules with who = "superuser" match them.

Because the first decision wins, order matters: a bypassing source placed before a non-bypassing one decides for superusers. To restrict superusers with an ACL file, put that file (with superuser_bypass = false) first, or turn the bypass off on every source before it. Anonymous clients are never superusers, and superusers requires an [[authentication]] source.

Authorization sources

kindRules come fromLoaded
filean ACL file (TOML)when the source is built
jwta claim of the token the client presentedat CONNECT
httpthe response of an HTTP(S) serviceat CONNECT
postgres, mysqlrows returned by acl_queryat CONNECT
redisa Redis hash read with acl_cmdat CONNECT
mongodbdocuments of acl_collectionat CONNECT

How rules loaded at CONNECT behave:

  • After the client is authenticated (by any source, or anonymously), every enabled authorization source with a loader fetches the client's rules. The loads run in parallel within [auth] timeout_ms, and %u in them is the authenticated user name (which a JWT username_claim or an HTTP username may have replaced).
  • The rules are kept with the session and evaluated without I/O. A rule change in the database applies to sessions that connect afterwards, or after the source's cache TTL for reconnecting clients (ttl_secs in [authorization.cache], 60 seconds by default). Kick a client through the admin API to make it reconnect sooner.
  • No data for the client (a placeholder without a value, no token, an HTTP "result": "ignore") makes the source answer ignore. Data without rules (no rows, a token without the claim) means none of its rules match, so the source's no_match applies.
  • A source disabled when the client connected, or replaced through the admin API after it connected, has no rules for that session and answers ignore until the client reconnects.

A client can have at most 1024 rules per source.

Network sources here take the same connection keys as in the authentication chain, including the transport checks: plain text or unverified TLS to a host that is not on this machine is refused unless the source sets allow_insecure_tls = true (databases) or allow_insecure_http = true (http). See Insecure connections.

ACL file

orbitmq.toml
[[authorization]]
kind = "file"
path = "/etc/orbitmq/acl.toml"
superuser_bypass = true     # the default
no_match = "ignore"         # the default: the next source, then [auth] no_match, decides

The file is read when the source is built: at start-up, and again when the source is replaced through the admin API. Rules apply to every client; see File format.

JWT

Takes the rules from a claim of the token, verified like an authenticating jwt source (the same keys, except username_claim and superuser_claim, which belong to authentication):

[[authorization]]
kind = "jwt"
name = "token-acl"
jwks_url = "https://auth.example.com/keys.json"
audience = ["mqtt"]
acl_claim = "mqtt_acl"      # required here
no_match = "deny"           # token holders get exactly what their token says
token payload
{ "sub": "device-1", "exp": 1798761600, "mqtt_acl": { "pub": ["devices/device-1/#"], "sub": ["commands/device-1/#"] } }

The token is taken from the CONNECT password (or the user name with from = "username"). A client without a token, or with a token this source cannot verify, gets ignore from it. The claim uses the JSON rule form.

HTTP

Sends one request per CONNECT to an HTTP(S) service, with the same keys and request body as an authenticating http source, except that password is always null and username is the authenticated user name:

[[authorization]]
kind = "http"
name = "acl-service"
url = "https://auth.internal.example/mqtt/acl"
headers = { Authorization = { env = "MQTT_AUTH_SERVICE_TOKEN" } }

The service answers status 200 with the client's rules in the JSON rule form:

{ "acl": [{ "permission": "allow", "action": "publish", "topic": "devices/%c/#" }] }

or {"result": "ignore"} when it has nothing for the client. Any other status, or a body with neither acl nor "result": "ignore", is a backend error (on_error).

PostgreSQL and MySQL

[[authorization]]
kind = "postgres"           # or "mysql"
url = { env = "MQTT_AUTH_DB_URL" }
acl_query = "SELECT permission, action, topic FROM mqtt_acl WHERE username = %u ORDER BY id"
schema.sql (PostgreSQL)
CREATE TABLE mqtt_acl (
  id         serial PRIMARY KEY,
  username   text NOT NULL,
  permission text NOT NULL,                    -- allow | deny
  action     text NOT NULL,                    -- publish | subscribe | both
  topic      text NOT NULL
);

acl_query (required) returns rows with text permission, action and topic columns, in rule order. url, pool_size and the [authorization.tls] table are the same as for authenticating SQL sources.

Redis

[[authorization]]
kind = "redis"
url = { env = "MQTT_AUTH_REDIS_URL" }
acl_cmd = "HGETALL mqtt_acl:%u"
redis-cli HSET mqtt_acl:alice 'devices/alice/#' both 'commands/alice/#' subscribe

acl_cmd (required) is HGETALL <key>: each field is a topic filter and its value the allowed action (publish, subscribe or both). Redis rules are allow rules. url, pool_size and [authorization.tls] are as for authenticating Redis sources.

MongoDB

[[authorization]]
kind = "mongodb"
url = { env = "MQTT_AUTH_MONGO_URL" }
database = "mqtt"
acl_collection = "acl"
acl_filter = { username = "%u" }   # the default

acl_collection (required) holds documents with action, topic (or a topics array) and an optional permission (default allow), applied in _id order. acl_filter is an equality filter whose values may hold placeholders. url, database and [authorization.tls] are as for authenticating MongoDB sources.

JSON rule form

JWT claims and HTTP responses carry rules either as a list, where permission defaults to allow and topics may replace topic:

[
  { "permission": "allow", "action": "publish", "topic": "devices/%c/#" },
  { "permission": "deny", "action": "both", "topics": ["devices/%c/secrets/#", "admin/#"] },
  { "action": "subscribe", "topic": "commands/%c/#" }
]

or as an object of allowed filters per action:

{ "pub": ["devices/%c/#"], "sub": ["commands/%c/#"], "all": ["users/%u/inbox"] }

Permissions are allow or deny; actions are publish (or pub), subscribe (or sub) and both (or all, pubsub), case-insensitive. These rules always apply to the client they were loaded for, use the same topic matching and placeholders as the ACL file, and are checked in order: the first matching rule decides.

File format

/etc/orbitmq/acl.toml
# Every device owns the subtree named after its client identifier.
[[rule]]
permission = "allow"
action = "both"
topic = "devices/%c/#"

# The dashboard user may read all telemetry, but nothing else.
[[rule]]
permission = "allow"
who = { user = "dashboard" }
action = "subscribe"
topic = "devices/+/telemetry"

# Nobody may publish to the command topics, except the operator account.
[[rule]]
permission = "allow"
who = { user = "operator" }
action = "publish"
topic = "commands/#"

[[rule]]
permission = "deny"
action = "both"
topic = "commands/#"

# Everyone may use their own per-user inbox.
[[rule]]
permission = "allow"
action = "both"
topic = "users/%u/inbox"

The file holds only [[rule]] tables. The policy when no rule matches and the superuser bypass are keys of the [[authorization]] source (no_match, superuser_bypass); a file that still sets default or superuser_bypass is refused with a message saying so.

Rule keys ([[rule]])

KeyValuesDefaultMeaning
permission"allow", "deny"requiredWhat the rule decides when it matches.
action"publish", "subscribe", "both"requiredWhich operations the rule applies to.
topictopic filterrequiredMQTT topic filter, with optional %c and %u placeholders.
who"all", "superuser", { user = "…" }, { client = "…" }"all"Whom the rule applies to: everyone, superusers, one authenticated user name, or one client identifier.

Unknown keys and invalid topic filters are errors: the broker does not start (or the API request is refused), and the message names the rule (counting from 0).

Evaluation

  1. Rules are checked in order; the first matching rule decides. Put specific deny rules before broad allow rules (or the other way round) on purpose.
  2. A rule matches when its who (ACL file only), its action and its topic filter all match.
  3. If no rule matches, the source's no_match applies.

Topic matching

OperationAn allow rule matches when…A deny rule matches when…
PUBLISH to a topic namethe rule filter matches the topicthe rule filter matches the topic
SUBSCRIBE to a filterthe rule filter covers the requested filter: every topic the requested filter can match is also matched by the rulethe two filters overlap: some topic is matched by both

So with allow sensors/+:

  • subscribing to sensors/temp or sensors/+ is allowed;
  • subscribing to sensors/# or # is not allowed by this rule, because they also match topics such as sensors/a/b.

And with deny secret/#, a subscription to # or +/x is refused, because it could receive secret/… messages.

Wildcard rules follow the MQTT $ rule: a rule filter that starts with + or # never matches topics that start with $. allow # therefore does not grant $SYS/…; write a rule for that prefix explicitly.

For shared subscriptions ($share/{group}/{filter}), the chain checks the underlying {filter}.

Placeholders

PlaceholderReplaced by
%cThe client identifier.
%uThe authenticated user name.

A value is substituted only if it is non-empty and contains none of /, +, # or the null character. Otherwise the rule is resolved fail-closed: an allow rule does not match, and a deny rule matches. This prevents a client identifier such as # from turning devices/%c/# into devices/#/#, or a/b from reaching another client's subtree. Clients without a user name (anonymous) never match rules that use %u.

Changing the chain at runtime

The authorization chain can be reordered, enabled, disabled and edited without a restart, from the dashboard's Auth & ACL page or the /api/v1/auth/authorization endpoints; changes are kept in the auth state file. See Changing the chains at runtime. Each source's answers are counted in orbitmq_auth_source_decisions_total{chain="authorization"} (see Metrics).

What clients see

Refused operationMQTT 5.0MQTT 3.1.1MQTT 3.1
SUBSCRIBESUBACK reason code 0x87 Not AuthorizedSUBACK return code 0x80 (failure)SUBACK grants the QoS, but no subscription is created (3.1 has no failure code)
PUBLISH QoS 1 or 2PUBACK or PUBREC with 0x87 Not AuthorizedAcknowledged normally, not forwardedAcknowledged normally, not forwarded
PUBLISH QoS 0Not forwardedNot forwardedNot forwarded

Refusing a publish never closes the connection.

On this page