
iegomez/mosquitto-go-auth本镜像为集成了mosquitto-go-auth插件的Mosquitto MQTT broker。mosquitto-go-auth是一个用Go语言编写的认证与授权插件,通过cgo与Mosquitto交互,支持多种后端存储,为MQTT broker提供用户认证、超级用户检查和访问控制列表(ACL)验证功能。插件兼容Mosquitto 2.0及以上版本,适用于需要灵活认证授权机制的MQTT部署场景。
插件实现了以下后端,每个后端均支持用户认证、超级用户检查和ACL验证,并包含完整测试:
prefix_username),指定特定后端处理认证请求镜像启动需挂载Mosquitto配置文件(通常为mosquitto.conf),并在配置中指定插件路径及后端。示例配置片段:
conf# 加载插件 auth_plugin /etc/mosquitto/go-auth.so # 启用的后端(按优先级排序) auth_opt_backends files,postgres,jwt # 通用缓存配置 auth_opt_cache true auth_opt_cache_type redis auth_opt_auth_cache_seconds 30 auth_opt_acl_cache_seconds 30 # 哈希算法配置(全局) auth_opt_hasher bcrypt auth_opt_hasher_cost 10
| 选项 | 说明 | 默认值 |
|---|---|---|
auth_opt_cache | 是否启用缓存 | false |
auth_opt_cache_type | 缓存类型(go-cache/redis) | go-cache |
auth_opt_auth_cache_seconds | 认证缓存过期时间(秒) | - |
auth_opt_acl_cache_seconds | ACL缓存过期时间(秒) | - |
auth_opt_cache_refresh | 缓存命中时刷新过期时间 | false |
auth_opt_cache_reset | 启动时重置缓存 | false |
支持PBKDF2、Bcrypt、Argon2ID,以Bcrypt为例:
confauth_opt_hasher bcrypt auth_opt_hasher_cost 12 # 计算强度(4-31)
confauth_opt_log_level info # 日志级别:debug/info/warn/error/fatal/panic auth_opt_log_dest file # 输出目标:stderr/stdout/file auth_opt_log_file /var/log/mosquitto/auth.log # 文件路径(仅file模式)
通过静态文件实现认证与授权:
confauth_opt_files_password_path /etc/mosquitto/passwords.txt # 密码文件路径 auth_opt_files_acl_path /etc/mosquitto/acls.txt # ACL文件路径
密码文件格式(PBKDF2哈希示例):
user1:PBKDF2$sha512$***$salt$hash user2:PBKDF2$sha512$***$salt$hash
ACL文件格式(遵循Mosquitto语法):
user user1 topic write sensor/temp topic read sensor/humidity user user2 topic read sensor/#
通过数据库查询实现认证:
confauth_opt_pg_host postgres auth_opt_pg_port 5432 auth_opt_pg_user dbuser auth_opt_pg_password dbpass auth_opt_pg_dbname mqtt_auth auth_opt_pg_userquery "SELECT password FROM users WHERE username = $1" # 用户查询 auth_opt_pg_aclquery "SELECT topic FROM acls WHERE username = $1 AND access = $2" # ACL查询
bashdocker run -d \ --name mosquitto-go-auth \ -p 1883:1883 \ # MQTT端口 -v $(pwd)/mosquitto.conf:/etc/mosquitto/mosquitto.conf \ -v $(pwd)/passwords.txt:/etc/mosquitto/passwords.txt \ -v $(pwd)/acls.txt:/etc/mosquitto/acls.txt \ eclipse-mosquitto:2.0 # 假设基础镜像为eclipse-mosquitto:2.0,需预安装插件
yamlversion: '3' services: mosquitto: image: eclipse-mosquitto:2.0 ports: - "1883:1883" volumes: - ./mosquitto.conf:/etc/mosquitto/mosquitto.conf depends_on: - postgres environment: - AUTH_PLUGIN_PATH=/etc/mosquitto/go-auth.so postgres: image: postgres:13 environment: - POSTGRES_USER=dbuser - POSTGRES_PASSWORD=dbpass - POSTGRES_DB=mqtt_auth volumes: - postgres_data:/var/lib/postgresql/data volumes: postgres_data:
debug日志级别会泄露敏感信息(如密码、哈希),生产环境必须使用info及以上级别。cache_refresh可能导致权限撤销延迟,建议结合cache_seconds合理设置过期时间。auth_opt_backends配置顺序依次检查,首个通过验证的后端生效。插件遵循MIT许可证,Mosquitto遵循EPL/EDL许可证。详细信息参见官方仓库。



manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务