Mnesia ACL

Mnesia ACL uses the built-in Mnesia database of EMQX 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. "access": "allow"
  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

  • Clientid ACL:

    1. # Request
    2. POST api/v4/acl
    3. {
    4. "clientid":"emqx_c",
    5. "topic":"Topic/A",
    6. "action":"pub",
    7. "access": "allow"
    8. }
    9. # Response
    10. {
    11. "data": {
    12. "clientid":"emqx_c",
    13. "topic":"Topic/A",
    14. "action":"pub",
    15. "access": "allow"
    16. "result": "ok"
    17. },
    18. "code": 0
    19. }
  • Username ACL:

    1. # Request
    2. POST api/v4/acl
    3. {
    4. "username":"emqx_u",
    5. "topic":"Topic/A",
    6. "action":"pub",
    7. "access": "allow"
    8. }
    9. # Response
    10. {
    11. "data": {
    12. "username":"emqx_u",
    13. "topic":"Topic/A",
    14. "action":"pub",
    15. "access": "allow"
    16. "result": "ok"
    17. },
    18. "code": 0
    19. }
  • $all ACL:

    1. # Request
    2. POST api/v4/acl
    3. {
    4. "topic":"Topic/A",
    5. "action":"pub",
    6. "access": "allow"
    7. }
    8. # Response
    9. {
    10. "data": {
    11. "all": "$all",
    12. "topic":"Topic/A",
    13. "action":"pub",
    14. "access": "allow"
    15. "result": "ok"
    16. },
    17. "code": 0
    18. }

Add ACL rules in batch

  1. # Request
  2. POST api/v4/acl
  3. [
  4. {
  5. "clientid":"emqx_c_1",
  6. "topic":"Topic/A",
  7. "action":"pub",
  8. "access": "allow"
  9. },
  10. {
  11. "username":"emqx_u_1",
  12. "topic":"Topic/A",
  13. "action":"sub",
  14. "access": "allow"
  15. },
  16. {
  17. "topic":"Topic/+",
  18. "action":"pubsub",
  19. "access": "deny"
  20. }
  21. ]
  22. # Response
  23. {
  24. "data": [
  25. {
  26. "clientid":"emqx_c_1",
  27. "topic":"Topic/A",
  28. "action":"pub",
  29. "access": "allow",
  30. "result": "ok"
  31. },
  32. {
  33. "username":"emqx_u_1",
  34. "topic":"Topic/A",
  35. "action":"pub",
  36. "access": "allow"
  37. "result": "ok"
  38. },
  39. {
  40. "all": "$all",
  41. "topic":"Topic/+",
  42. "action":"pubsub",
  43. "access": "deny"
  44. },
  45. ],
  46. "code": 0
  47. }

Check the added ACL rules

  • Clientid ACL:

    1. # Request
    2. GET api/v4/acl/clientid?_like_clientid=emqx_c
    3. # Response
    4. {
    5. "meta": {
    6. "page": 1,
    7. "limit": 10,
    8. "count": 1
    9. },
    10. "data": [
    11. {
    12. "clientid": "emqx_c",
    13. "topic": "Topic/A",
    14. "action": "pub",
    15. "access": "allow"
    16. },
    17. {
    18. "clientid": "emqx_c_1",
    19. "topic": "Topic/A",
    20. "action": "pub",
    21. "access": "allow"
    22. },
    23. {
    24. "clientid": "emqx_c_2",
    25. "topic": "Topic/A",
    26. "action": "pub",
    27. "access": "allow"
    28. }
    29. ],
    30. "code": 0
    31. }
  • Username ACL:

    1. # Request
    2. GET api/v4/acl/username?_like_username=emqx_u
    3. # Response
    4. {
    5. "meta": {
    6. "page": 1,
    7. "limit": 10,
    8. "count": 1
    9. },
    10. "data": [
    11. {
    12. "username": "emqx_u",
    13. "topic": "Topic/A",
    14. "action": "pub",
    15. "access": "allow"
    16. },
    17. {
    18. "username": "emqx_u_1",
    19. "topic": "Topic/A",
    20. "action": "pub",
    21. "access": "allow"
    22. },
    23. {
    24. "username": "emqx_u_2",
    25. "topic": "Topic/A",
    26. "action": "pub",
    27. "access": "allow"
    28. }
    29. ],
    30. "code": 0
    31. }
  • $all ACL:

    1. # Request
    2. GET api/v4/acl/$all
    3. # Response
    4. {
    5. "meta": {
    6. "page": 1,
    7. "limit": 10,
    8. "count": 1
    9. },
    10. "data": [
    11. {
    12. "all": "$all",
    13. "topic": "Topic/A",
    14. "action": "pub",
    15. "access": "allow"
    16. },
    17. {
    18. "all": "$all",
    19. "topic": "Topic/+",
    20. "action": "pubsub",
    21. "access": "deny"
    22. }
    23. ],
    24. "code": 0
    25. }

Check Username/Clientid specific ACL rules

  • Clientid ACL:

    1. # Request
    2. GET api/v4/acl/clientid/emqx_c
    3. # Response
    4. {
    5. "data": [
    6. {
    7. "topic": "Topic/A",
    8. "clientid": "emqx_c",
    9. "access": "allow",
    10. "action": "pub"
    11. },
    12. {
    13. "topic": "Topic/B",
    14. "clientid": "emqx_c",
    15. "access": "allow",
    16. "action": "pub"
    17. }
    18. ],
    19. "code": 0
    20. }
  • Username ACL:

    1. # Request
    2. GET api/v4/acl/username/emqx_u
    3. # Response
    4. {
    5. "data": [
    6. {
    7. "topic": "Topic/A",
    8. "username": "emqx_u",
    9. "access": "allow",
    10. "action": "pub"
    11. },
    12. {
    13. "topic": "Topic/B",
    14. "username": "emqx_u",
    15. "access": "allow",
    16. "action": "pub"
    17. }
    18. ],
    19. "code": 0
    20. }

Delete ACL rule

  • Client ACL

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

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

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