MongoDB ACL

    插件:

    emqx_auth_mongo 插件同时包含认证功能,可通过注释禁用。

    MongoDB 基础连接信息,需要保证集群内所有节点均能访问。

    1. ## MongoDB 架构类型
    2. ##
    3. ## Value: single | unknown | sharded | rs
    4. auth.mongo.type = single
    5. ## rs 模式需要设置 rs name
    6. ## auth.mongo.rs_set_name =
    7. ## 服务器列表,集群模式下使用逗号分隔每个服务器
    8. ## Examples: 127.0.0.1:27017,127.0.0.2:27017...
    9. auth.mongo.server = 127.0.0.1:27017
    10. auth.mongo.pool = 8
    11. auth.mongo.login =
    12. auth.mongo.password =
    13. ## auth.mongo.auth_source = admin
    14. auth.mongo.database = mqtt
    15. auth.mongo.query_timeout = 5s
    16. ## SSL 选项
    17. # auth.mongo.ssl = false
    18. ## auth.mongo.ssl_opts.keyfile =
    19. ## auth.mongo.ssl_opts.certfile =
    20. ## auth.mongo.ssl_opts.cacertfile =
    21. ##
    22. ## Value: unsafe | safe
    23. ## Mongo read mode.
    24. ##
    25. ## Value: master | slave_ok
    26. ## auth.mongo.r_mode =
    27. ## MongoDB 拓扑配置,一般情况下用不到,详见 MongoDB 官网文档
    28. auth.mongo.topology.pool_size = 1
    29. auth.mongo.topology.max_overflow = 0
    30. ## auth.mongo.topology.overflow_ttl = 1000
    31. ## auth.mongo.topology.overflow_check_period = 1000
    32. ## auth.mongo.topology.local_threshold_ms = 1000
    33. ## auth.mongo.topology.connect_timeout_ms = 20000
    34. ## auth.mongo.topology.socket_timeout_ms = 100
    35. ## auth.mongo.topology.server_selection_timeout_ms = 30000
    36. ## auth.mongo.topology.wait_queue_timeout_ms = 1000
    37. ## auth.mongo.topology.heartbeat_frequency_ms = 10000
    38. ## auth.mongo.topology.min_heartbeat_frequency_ms = 1000

    MongoDB 认证默认配置下需要确保数据库中有如下集合:

    1. {
    2. username: "user",
    3. password: "password hash",
    4. salt: "password salt",
    5. is_superuser: false,
    6. created: "2020-02-20 12:12:14"
    7. }

    示例数据:

    ACL 规则集合

    1. {
    2. username: "username",
    3. clientid: "clientid",
    4. publish: ["topic1", "topic2", ...],
    5. subscribe: ["subtop1", "subtop2", ...],
    6. pubsub: ["topic/#", "topic1", ...]
    7. }

    MongoDB ACL 一条规则中定义了发布、订阅和发布/订阅的信息,在规则中的都是允许列表。

    • username:连接客户端的用户名
    • clientid:连接客户端的 Client ID
    • publish:允许发布的主题数值,支持通配符
    • subscribe:允许订阅的主题数值,支持通配符
    • pubsub:允许发布订阅的主题数值,支持通配符

    主题可以使用通配符,并且可以在主题中加入占位符来匹配客户端信息,例如 t/%c 则在匹配时主题将会替换为当前客户端的 Client ID

    • %u:用户名

    默认配置下示例数据:

    1. use mqtt
    2. ## 所有用户不可以订阅、发布系统主题
    3. ## 允许客户端订阅包含自身 Client ID 的 /smarthome/${clientid}/temperature 主题
    4. username: "$all",
    5. clientid: "$all",
    6. publish: ["#"],
    7. subscribe: ["/smarthome/%c/temperature"]
    8. })

    启用 MongoDB ACL 后并以用户名 emqx 成功连接后,客户端应当数据具有相应的主题权限。

    进行 ACL 鉴权时,EMQ X 将使用当前客户端信息填充并执行用户配置的超级用户查询,查询客户端是否为超级用户。客户端为超级用户时将跳过 ACL 查询。

    同一个选择器的多个条件时实际查询中使用 MongoDB and 查询:

    1. db.mqtt_user.find({
    2. "username": "wivwiv"
    3. "clientid": "$all"
    4. })

    你可以在查询条件中使用以下占位符,执行时 EMQ X 将自动填充为客户端信息:

    • %u:用户名
    • %c:Client ID
    1. 查询结果中必须包含 is_superuser 字段,is_superuser 应该显式的为 true

    如果不需要超级用户功能,注释并禁用该选项能有效提高效率

    进行 ACL 鉴权时,EMQ X 将使用当前客户端信息填充并执行用户配置的超级用户查询,如果没有启用超级用户查询或客户端不是超级用户,则使用 ACL 查询 查询出该客户端在数据库中的 ACL 规则。

    1. # etc/plugins/emqx_auth_mongo.conf
    2. auth.mongo.acl_query = on
    3. auth.mongo.acl_query.collection = mqtt_acl
    4. ## 查询选择器,多个条件时使用逗号分隔
    5. ## auth.mongo.acl_query.selector = username=%u,clientid=%c
    6. auth.mongo.acl_query.selector = username=%u
    7. ## 使用多个查询选择器
    8. ## auth.mongo.acl_query.selector.1 = username=$all
    9. ## auth.mongo.acl_query.selector.2 = username=%u

    同一个选择器的多个条件时实际查询中使用 MongoDB and 查询:

    多个选择器时实际查询中使用 MongoDB or 查询:

    1. db.mqtt_acl.find({
    2. "$or": [
    3. {
    4. "username": "$all"
    5. },
    6. {
    7. "username": "emqx"
    8. }
    9. ]
    10. })

    你可以在 ACL 查询中使用以下占位符,执行时 EMQ X 将自动填充为客户端信息:

    • %u:用户名

    MongoDB ACL 规则需严格使用上述数据结构。 MongoDB ACL 中添加的所有规则都是 允许 规则,可以搭配 etc/emqx.confacl_nomatch = deny 使用。