# emqx-acl鉴权

author:周坤

createTime:2021-12-06

updateTime

  • 2021-12-30, 更改内容:acl-mysql外部数据库操作说明

参考地址 https://docs.emqx.cn/broker/v4.3/

# 安装

  • 使用的版本
    • 4.3.10
  • 安装方式
    • 二进制包安装
  • 参考地址 https://docs.emqx.cn/broker/v4.3/getting-started/install.html

# acl内置数据源

  • 参考地址 https://docs.emqx.cn/broker/v4.3/advanced/acl-file.html

  • 文件目录

    • /etc/emqx/acl.conf
  • 文件内容

    %% 允许 "dashboard" 用户 订阅 "$SYS/#" 主题
    {allow, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
    
    %% 允许 IP 地址为 "127.0.0.1" 的用户 发布/订阅 "$SYS/#","#" 主题
    {allow, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
    
    %% 拒绝 "所有用户" 订阅 "$SYS/#" "#" 主题
    {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
    
    %% 允许其它任意的发布订阅操作
    {allow, all}.
    
    • 元素第一位:执行权限操作
      • allow 允许
      • deny 拒绝
    • 元素第二位:匹配生效的字段
      • {user, "dashboard"} 用户dashboard生效
      • {clientid, "dashboard"} clientid=dashboard生效
      • {ipaddr, "127.0.0.1"} ip=127.0.0.1生效
      • all 所有用户生效
    • 元素第三位:控制权限操作
      • publish 发布权限
      • subscribe 订阅权限
      • pubsub 发布订阅权限
    • 元素第四位:主题规则定义
      • 定义一个,多个,以及带通配符的主题
  • 手动生效

    emqx_ctl modules reload emqx_mod_acl_internal
    

# acl外部数据源

  • 参考地址 https://docs.emqx.cn/broker/v4.3/advanced/acl.html#acl-%E6%8F%92%E4%BB%B6

  • 目前主要有mysql,postgreSql,redis,mongodb等,目前这里只介绍mysql的使用

    • 插件配置文件地址:/etc/emqx/plugins/emqx_auth_mysql.conf

    • 参数介绍

      ## 服务器访问地址与端口
      auth.mysql.server = 127.0.0.1:3306
      
      ## 连接池大小
      auth.mysql.pool = 8
      
      ##访问用户名
      auth.mysql.username = emqx
      
      ##访问密码
      auth.mysql.password = public
      
      ##访问数据库名
      auth.mysql.database = mqtt
      
      ##验证连接密码是否正确 password 字段不可更改,官网配置如下,也可以根据需求更改
      #auth.mysql.auth_query = select password from mqtt_user where username = '%u' limit 1
      # mqtt_user用户表,username用户名可以根据自定义用户表更改,案例如下
      auth.mysql.auth_query = select pwd as password from userTab where user = '%u' limit 1
      
      ##密码使用明文
      auth.mysql.password_hash = plain
      
      ##验证当前用户是否为超级管理员,如果是超管,跳过acl鉴权,官网配置如下,也可以根据需求更改
      #auth.mysql.super_query = select is_superuser from mqtt_user where username = '%u' limit 1
      auth.mysql.super_query = select isAdmin as is_superuser from userTab where user = '%u' limit 1
      
      ##ACL鉴权  allow, ipaddr, username, clientid, access, topic 字段不可更改,官网配置如下,也可以根据需求更改
      #auth.mysql.acl_query = select allow, ipaddr, username, clientid, access, topic from mqtt_acl where ipaddr = '%a' or #username = '%u' or username = '$all' or clientid = '%c'
      
      auth.mysql.acl_query = select ability as allow, ip as ipaddr, user as username, clientAddr as clientid,opition as access,topicName as topic from userAclTab where ip = '%a' or user = '%u' or user = '$all' or clientAddr = '%c'
      
    • 配置文件生效

      emqx_ctl plugins load emqx_auth_mysql
      
  • 需要注意的点

    • 当mysql的acl鉴权插件启动成功,发现所有用户还是能连接,需要修改 /etc/emqx/mqx.conf,修改内容如下

      ##是否允许匿名用户,默认true,改为false
      allow_anonymous = false
      
      ## Allow or deny if no ACL rules matched.
      ##
      # Value: allow | deny
      acl_nomatch = deny
      
      ## The action when acl check reject current operation
      ##
      ## Value: ignore | disconnect
      ## Default: ignore
      acl_deny_action = disconnect
      
    • 当用户名,密码能鉴权时,发现主题鉴权不准确,需要更改/etc/emqx/acl.conf这个文件下的默认配置,将allow的执行权限,全部改为deny

      {deny, {user, "dashboard"}, subscribe, ["$SYS/#"]}.
      
      {deny, {ipaddr, "127.0.0.1"}, pubsub, ["$SYS/#", "#"]}.
      
      {deny, all, subscribe, ["$SYS/#", {eq, "#"}]}.
      
      {deny, all}