【问题标题】:mosquitto - disable subscribing without authorizationmosquitto - 禁用未经授权的订阅
【发布时间】: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 的

标签: acl mqtt mosquitto


【解决方案1】:

将以下内容添加到您的 mosquitto.conf 中

...
allow_anonymous false
...

这将阻止没有凭据的用户登录代理。

如果您希望未经身份验证的客户端能够看到某些主题,您还可以为 anonymous 用户添加 acl 规则。

【讨论】:

  • 您好,用户不是匿名的。他是经过身份验证的用户。但他不应该被授权订阅任何他不被允许的东西。我已经用 sub 和 pub 命令更新了问题。
猜你喜欢
  • 1970-01-01
  • 2020-11-25
  • 2015-06-11
  • 2017-12-26
  • 2018-06-15
  • 1970-01-01
  • 2019-02-08
  • 2023-04-06
  • 1970-01-01
相关资源
最近更新 更多