如何使用 redis 作为 mosquitto ACL 的后端

How to use redis as a backend for mosquitto ACL

我正在研究 Mosquitto,并计划使用 Redis 作为后端来处理 username/password 对身份验证和 ACL。我正在使用 JPmens 的身份验证插件来执行此操作。

蚊子会议:

auth_opt_backends redis
auth_plugin /etc/mosquitto/auth-plug.so
auth_opt_redis_host 127.0.0.1
auth_opt_redis_port 6379
auth_opt_redis_userquery GET %s
auth_opt_redis_aclquery GET %s-%s

以下 name/password 对可以正常进行身份验证

SET user1 PBKDF2$sha2561$Qh18ysY4wstXoHhk$g8d2aDzbz3rYztvJiO3dsV698jzECxSg

以下设置不适用于 ACL:

SET user1-test 2

mosquitto 的日志如下:

1507037072: Denied PUBLISH from mosqpub/3838-ip-172-31- (d0, q0, r0, m0, 'user1-test', ... (4 bytes))
1507037072: Received DISCONNECT from mosqpub/3838-ip-172-31-

因此,每当我尝试 pub/sub mosquitto 对用户进行身份验证但不允许发布并断开用户连接时。

测试:

mosquitto_pub -p 1884 -t "test" -m "demo" -u user1 -P xyz 

子:

mosquitto_sub -p 1884 -t "test" -u user1 -P xyz

好的,我已经解决了。

您提供的日志中缺少重要的一点。您应该包括以下行:

1507135115: ACL denying access to client with dangerous client id "mosqpub/2232-tiefighter"

问题是插件阻塞,因为客户端 ID 包含“/”

代码中有以下部分:

/* We are using pattern based acls. Check whether the username or  
 * client id contains a +, # or / and if so deny access.  
 *
 * Without this, a malicious client may configure its username/client  
 * id to bypass ACL checks (or have a username/client id that cannot
 * publish or receive messages to its own place in the hierarchy).  
 */

解决方案是使用 -i 选项为 mosquitto_sub 和 mosquitto_pub 设置客户端 ID 例如:

mosquitto_pub -p 1884 -t "test" -m "demo" -u user1 -P xyz -i publisher