Mnesia ACL

Mnesia ACL uses the built-in Mnesia database of EMQ X to store ACL rules, which can store data and dynamically manage ACLs to facilitate integration with external device management systems.

Plugin:

  1. emqx_auth_mnesia

ACL rules

ACL Rule Structure Body

  1. {
  2. "username":"emqx",
  3. "clientid":"client1",
  4. "topic":"testtopic/1",
  5. "action":"pub",
  6. "allow": true
  7. }

Rule field description:

  • username: Match the client’s Username.
  • clientid: Match the client’s Client.
  • topic: Control topics, you can use wildcards, and you can add placeholders to topics to match client information, such as t/%c, then the topic will be replaced with the client ID of the current client when matching
    • %u: Username
    • %c: Client ID
  • action: Operation action, optional value: pub | sub | pubsub
  • allow: Whether allow

username and clientid are optional fields, when both a missing, the rule applies to all clients.

Mnesia ACL does not set rules by default, and you can use the HTTP API to manage ACL rules.

Use the HTTP API to manage ACL rules

Add ACL rule

API definition:

  1. # Request
  2. POST api/v4/emqx_acl
  3. {
  4. "login":"emqx",
  5. "topic":"Topic/A",
  6. "action":"pub",
  7. "allow": true
  8. }
  9. # Response
  10. {
  11. "data": {
  12. "emqx": "ok"
  13. },
  14. "code": 0
  15. }

Add ACL rules in batch

API definition:

  1. # Request
  2. POST api/v4/emqx_acl
  3. [
  4. {
  5. "login":"emqx_1",
  6. "topic":"Topic/A",
  7. "action":"pub",
  8. "allow": true
  9. },
  10. {
  11. "login":"emqx_2",
  12. "topic":"Topic/A",
  13. "action":"pub",
  14. "allow": true
  15. }
  16. ]
  17. # Response
  18. {
  19. "data": {
  20. "emqx_2": "ok",
  21. "emqx_1": "ok"
  22. },
  23. "code": 0
  24. }

Check the added ACL rules

API definition:

  1. # Request
  2. GET api/v4/emqx_acl
  3. # Response
  4. {
  5. "meta": {
  6. "page": 1,
  7. "limit": 10,
  8. "count": 1
  9. },
  10. "data": [
  11. {
  12. "topic": "Topic/A",
  13. "login": "emqx",
  14. "action": "pub"
  15. }
  16. ],
  17. "code": 0
  18. }

Check the specified ACL rule

API definition:

  1. # Request
  2. GET api/v4/emqx_acl/${login}
  3. # Response
  4. {
  5. "data": {
  6. "topic": "Topic/A",
  7. "login": "emqx",
  8. "allow": true,
  9. "action": "pub"
  10. },
  11. "code": 0
  12. }

Delete ACL rule

Delete the specified ACL rule:

API definition:

  1. # Request
  2. # Please note that ${topic} needs to be encoded with UrlEncode
  3. DELETE api/v4/emqx_acl/${login}/${topic}
  4. # Response
  5. {
  6. "code": 0
  7. }