Skip to content

Extended Authorization ​

Extended authorization enables users to authorize using their own services, supporting external databases such as MySQL and Redis as data sources, or connecting to an HTTP service for authorization.

Note

Extended authorization is not supported in EMQX Serverless deployments.

Extended Authorization Data Sources ​

Authorize with HTTP

Authorize with MySQL

Authorize with PostgreSQL

Authorize with Redis

Note

Although a maximum of two extended authorizations is supported, it is recommended not to use multiple extended authorizations simultaneously for performance reasons.

Authorizer Preconditions ​

For deployments running EMQX 6.3 or later, you can assign a precondition to an extended authorizer to control whether it should be invoked for the current publish or subscribe request. A precondition is a Variform expression that evaluates client and authorization request information. If the expression does not evaluate to the string value 'true', or fails at runtime, EMQX skips the current authorizer and continues with the next enabled authorizer in the chain.

For example, select different authorization services by client attributes, publish or subscribe action, or topic range to avoid unnecessary requests to unrelated external systems. A precondition decides whether to invoke an authorizer; it does not grant permission.

Supported Client Attributes and Request Information in Preconditions ​

You can use the following client attributes in a precondition:

  • username: The username of the client.
  • clientid: The client ID.
  • client_attrs.*: A client attribute.
  • cert_common_name: The common name (CN) from the client's TLS certificate.
  • cert_subject: The subject field from the client's TLS certificate.
  • peersni: The Server Name Indication (SNI) sent by the TLS client.
  • listener: The listener ID, for example, ssl:default.

The following authorization request information is also available:

  • action: The current authorization action, either publish or subscribe.
  • topic: The publish topic or subscription topic filter being checked.

Configure a Precondition ​

When you create or edit an HTTP, MySQL, PostgreSQL, or Redis authorizer, enter a Variform expression in Precondition. You can also configure this field when editing an existing file authorizer. The field is optional. When configured, the expression must contain 1 to 256 characters. If you leave the field empty or clear an existing expression, EMQX invokes the authorizer according to its position in the authorization chain without evaluating an additional condition.

The following examples show how to control whether an authorizer is invoked:

  • Invoke the authorizer only for publish operations:

    text
    str_eq(action, 'publish')
  • Invoke the authorizer only when the request topic matches the current client's topic range:

    text
    topic_match(topic, topic_join([clientid, '#']))

If the expression is invalid, the configuration cannot be saved, and the last valid configuration remains in effect. Correct the expression based on the error displayed in the Cloud Console and try again. After saving, verify authorization with operations that satisfy and do not satisfy the condition. For more information, see Authorizer Preconditions in the EMQX documentation.

Authorization Order ​

After adding extended authorization data sources, you can sort the authorization sources. The deployment will perform authorization checks in the order from left to right. By default, the most recently added authorization is placed first, and the default authorization is placed last.

  • Actions are matched with permissions, allowing or denying client operations based on permissions.

  • If actions do not match with permissions, the check is passed to the next authorization checker.

TIP

If an external authorization service is unavailable, times out, or returns an unexpected response, the Broker's backend failure policy determines whether to deny the operation or continue to the next authorizer. Check the service and deployment logs. Contact EMQX Technical Support if you need to confirm your deployment's behavior. If the authorization check continues and no rule matches, the deployment's no-match policy determines the outcome. When default authorization provides the fallback, whitelist mode denies the operation and blacklist mode allows it.

How to Create an Authorization Chain ​

You can create an authorization chain on the Access Control -> Authorization -> Extended Authorization page in your deployment. Once an extended authorization data source is configured and connected, you can see the Order Setting section on the page.

  1. Click the Authorization Order to enter the Authorization Order page.
  2. Arrange the order of authorization by dragging the icons of the authorization data sources left and right.
  3. When you finish, click Confirm to save the order.

add_acl

Authorization Chain Requirements for Whitelist Mode ​

When the whitelist mode is enabled, clients are denied all subscription and publishing actions by default. You must explicitly grant permissions for each allowed operation. For step-by-step instructions, see Enable Authorization Whitelist Mode.

To enable whitelist mode, make sure that the "Default Authorization" source is positioned at the end of the authorization chain. You can manage this on the Authorization Order page under the Order Setting section.

External Resource Cache ​

TIP

The external resource cache feature is available for EMQX version 5.9 and later in the Dedicated Flex edition.

EMQX Broker provides a caching mechanism for authorization results retrieved from external backends, such as MySQL or Redis. This cache is designed to improve the performance of authorization result lookups and reduce repeated access to external resources, especially in high-throughput environments.

Note

The external resource cache applies only to the extended data sources. For default authorization, EMQX Broker does not use this cache.

How External Resource Cache Works ​

The external resource cache stores authorization results. These results are shared across all client sessions in the cluster and help avoid redundant queries to external authorization backends.

When a publish/subscribe operation triggers an external backend query:

  1. EMQX checks the external resource cache (shared across all clients).
  2. EMQX checks the cache for a previously stored result:
    • If a valid result is found, it counts as a Cache Hit, and no call to the external backend is made.
    • If no result is found, it counts as a Cache Miss, and EMQX queries the external backend.
  3. The result returned from the backend is stored in the cache for future use, incrementing the Cache Insert metric.

This mechanism helps reduce latency, minimize backend usage, and maintain system responsiveness under load.

Enable and Configure External Resource Cache ​

You can enable and configure the external resource cache on the Access Control -> Authorization -> Extended Authorization page in your deployment. Once an extended authorization data source is configured and connected, you can see the Cache Setting section on the page.

  1. Click the External Resource Cache Settings to open a side panel.
  2. In the panel, use the Enable Cache button to turn the caching feature on or off. Once enabled, configure the following cache settings:
    • Max Cached Items: Maximum number of cached entries per node. Default: 1,000,000.
    • Max Memory: Limit on cache memory usage. Default: 100 MB.
    • Cache TTL: Duration a cached entry remains valid. Default: 1 minute.
  3. Click Update to apply the settings.

Monitor External Resource Cache Status ​

To view cache metrics and monitor usage in real-time, hover over the arrow next to External Resource Cache Settings and select External Resource Cache Status. A side panel will appear showing cache metrics.

The metrics include the following:

  • Memory Usage: Total memory currently used by the cache.
  • Cache Entries: Total number of stored cache results.
  • Cache Hits: Number of times EMQX found a valid result in the cache, avoiding a call to the external backend.
    • Metrics shown: Current rate, 5-minute average, Maximum rate
  • Cache Misses: Number of times EMQX looked for a result in the cache but didn’t find one, resulting in a backend query.
    • Metrics shown: Current rate, 5-minute average, Maximum rate
  • Cache Inserts: Number of new results added to the cache after a miss.
    • Metrics shown: Current rate, 5-minute average, Maximum rate

You can refresh or reset the statistics using the buttons in the top-right corner of the panel.

external_cache_status_authz

Review Topic Placeholders When Upgrading ​

When upgrading to EMQX 6.3, review the values substituted into topic placeholders in authorization rules. If the configuration disallows /, +, or # in placeholder values, EMQX cannot use values containing those characters to render the authorization topic. Depending on the Broker's security policy, EMQX denies the operation or treats the rule as unmatched and continues the authorization check.

Reset Individual Metrics ​

In EMQX 6.3 and later, click the reset icon in an authenticator's or authorizer's detail metrics and confirm to reset its counters and rate statistics. Configuration permission is required. The operation affects only the selected item. It does not change configuration, enabled state, or ordering, or reset external resource cache metrics.