【发布时间】:2016-12-12 07:30:13
【问题描述】:
我正在使用带有 tls-certificate 的 mosquitto 版本 1.4.10。我正在使用这个插件https://github.com/mbachry/mosquitto_pyauth 来授权用户。它适用于 mosquitto_pub(例如,当有人尝试发布时,它首先得到模块的授权)。
但是,mosquitto_sub 似乎无需授权即可订阅任何内容。当有人试图以只读模式访问主题时,我如何强制安全?
我浏览了 mosquitto.conf 文件,似乎找不到与此相关的任何内容。
例如,我可以这样订阅:
mosquitto_sub --cafile /etc/mosquitto/ca.crt --cert /etc/mosquitto/client.crt --key /etc/mosquitto/client.key -h ubuntu -p 1883 -t c/# -d
并且能够看到来自某些发布者的消息,如下所示:
mosquitto_pub --cafile /etc/mosquitto/ca.crt --cert /etc/mosquitto/client.crt --key /etc/mosquitto/client.key -h ubuntu -p 1883 -t c/2/b/3/p/3/rt/13/r/123 -m 32 -q 1
我想要做的是防止 mosquitto_sub 在未经授权的情况下读取根级别的所有消息。
授权的python代码如下所示:(授权数据存储在cassandra db中)
import sys
import mosquitto_auth
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
## program entry point from mosquitto...
def plugin_init(opts):
global cluster, session, select_device_query
conf = dict(opts)
cluster = Cluster(['192.168.56.102'])
session = cluster.connect('hub')
select_device_query = session.prepare('SELECT * from devices where uid=?')
select_device_query.consistency_level = ConsistencyLevel.QUORUM
print 'Cassandra cluster initialized'
def acl_check(clientid, username, topic, access):
device_data = session.execute(select_device_query, [username])
if device_data.current_rows.__len__() > 0:
device_data = device_data[0]
# sample device data looks like this :
# Row(uid=u'08:00:27:aa:8f:91', brand=3, company=2, device=15617, property=3, room=490, room_number=u'3511', room_type=13, stamp=datetime.datetime(2016, 12, 12, 6, 29, 54, 723000))
subscribable_topic = 'c/' + str(device_data.company) \
+ '/b/' + str(device_data.brand) \
+ '/p/' + str(device_data.property) \
+ '/rt/' + str(device_data.room_type) \
+ '/r/' + str(device_data.room) \
+ '/#'
matches = mosquitto_auth.topic_matches_sub(subscribable_topic, topic)
print 'ACL: user=%s topic=%s, matches = %s' % (username, topic, matches)
return matches
return False
函数acl_check 似乎总是在 mosquitto_pub 尝试连接时被调用,但在 mosquitto_sub 连接时从未被调用。
这个 python 模块背后的 C 代码在这里:https://github.com/mbachry/mosquitto_pyauth/blob/master/auth_plugin_pyauth.c
【问题讨论】:
-
我假设您在第二段中的意思是
mosquitto_sub而不是mosquitto_pub -
我的错..是的,我的意思是 mosquitto_sub
-
您可能还想包含 python 代码,以便我们可以看到您是如何实现 ACL 的