目录
概述
本文章主要介绍mosquitto一些安全及访问控制列表相关的配置例子。根据文档描述可以了解到的mosquito支持以下方式的接入:
a、无鉴权接入
默认接入方式。allow_anonymos为true时,并且paasword_file、psk_file、鉴权插件等其他鉴权均未提供的情况下,客户端用户可以直接接入到broker。
b、简单鉴权(用户名/密码)
在配置文件使用password_file命令指定用户列表时,客户端用户接入时需要提供用户名和密码才能接入到broker中。这种方式的接入下,用户名和密码是以明文的方式进行传输。
注:用户名/密码文件文件是使用mosquitto_passwd工具生成,其中密钥进行了加密。
c、使用ssl/tls的加密鉴权
在基于ssl/tls的加密情况下,配置了require_certificate为true,如果use_identity_as_username/use_subject_as_username其中之一为true,那么此时除了完成ssl通讯协商过程,同时还会使用基于证书的鉴权接入。
d、使用psk方式的加密鉴权
类似基于ssl/tls的加密情况。如果use_identity_as_username设置为true,客户端除了与broker完成加密的协商过程之外,它还会使用identity作为用户名进行鉴权过程。
e、基于用户id的接入控制
如果mosquitto配置了clientid_prefix,那么只有符合该前缀的用户才能接入到broker,否则会被broker主动断开。
准备环境:
本地主机(10.34.168.70),用于运行mosquitto服务和发布客户端;远程虚拟机(10.34.168.90),用于运行订阅客户端。
其中非加密模式下,mosquitto断开为1883;加密模式下,端口修改为8883。
鉴权配置示例
a、通过mosquitto_passwd创建用户名和密码文件,假设为user_password.conf;
b、配置文件开启 password_file 命令:
password_file user_password.conf
c、运行mosquitto
./mosquitto -c ./mosquitto.conf
d、测试过程:
略
openssl单向加密配置示例
a、按照文章生成对应的证书等密钥文件。
b、配置mosquitto.conf
cafile D:\Program Files (x86)\xxxx\ca.crt
cerfile D:\Program Files (x86)\xxxx\server.crt
keyfile D:\Program Files (x86)\xxxx\server.key
备注:这里的路径填写自己证书的存放路径
c、运行mosquitto
同上
d、测试过程
订阅消息(10.34.168.90上运行)
./mosquitto_sub -h 10.34.168.70 -p 8883 -u test_01 -P test -t 005 --cafile ./crt/ca.crt --insecure
说明:
--cafile根证书所在目录,目前设置根证书与server证书一样。
-u:用户名,这里由于之前配置了paasword_file
-P:用户密码,这里由于之前配置了paasword_file
消息发布者(本机10.34.168.70运行)
mosquitto_pub.exe -h 127.0.0.1 -p 8883 -u test_01 -P test -t 005 -m "hello ssl" --cafile ./ca.crt --insecure
openssl双向加密配置示例
a、配置
cafile D:\Program Files (x86)\xxxx\ca.crt
cerfile D:\Program Files (x86)\xxxx\server.crt
keyfile D:\Program Files (x86)\xxxx\server.key
require_certificate true
备注:这里的路径填写自己证书的存放路径
b、运行mosquitto
同上
c、测试过程
订阅消息(10.34.168.90上运行)
./mosquitto_sub -h 10.34.168.70 -p 8883 -u test_01 -P test -t 005 --cafile ./crt/server.crt --cert ./sub_client.crt --key ./sub_client.key --insecure
说明:
--cafile根证书所在目录,目前设置根证书与server证书一样。
-u:用户名,这里由于之前配置了paasword_file
-P:用户密码,这里由于之前配置了paasword_file
--cert: 本客户端的证书路径(生成方式类似于server),它将被用于服务端对客户端的证书确认。
--key: 本客户端的密钥文件(生成方式类似于server)。
消息发布者(本机10.34.168.70运行)
mosquitto_pub.exe -h 127.0.0.1 -p 8883 -u test_01 -P test -t 005 -m "hello bio - ssl" --cafile ./server.crt --cert ./pub_client.crt --key ./pub_client.key --insecure
说明:
同上
预共享密钥配置示例
a、配置
备注:ssl关闭的情况下进行
psk_hint true
psk_file pskfile.example
use_identity_as_username true
pskfile.example中的内容如下:
lilei:12abcd
b、运行mosquitto
略
c、测试过程
订阅者
./mosquitto_sub -h 10.34.168.70 -p 8883 --psk 12abcd --psk-identity lilei -t 002
发布者
mosquitto_pub.exe -h 127.0.0.1 -p 8883 -u test_01 -P test -t 002 -m "hello psk"
retain保留消息测试示例
使用mosquitto_pub进行消息发布时,携带-r参数即可将该消息作为保留消息。
多次使用-r发布消息时,最后一条将作为该消息的保留消息。
will遗嘱消息测试示例
略
桥模式示例
配置:
connection:配置桥的名称
local_clientid:配置桥作为订阅客户端与远程broker订阅时携带的clientid。如果未填写,则默认为local.<remote_clientid>
remote_clientid:配置用于连接远程broker时携带的clientid。如果未填写,则默认名称为:hostname.<connection>
notifications:是否将桥的状态通知其订阅者。
notification_topic:在notifications为true的情况下,桥会在这个topic上发布自己的状态。
bridge_attempt_unsubscribe:为true时,对于out类型的桥,在收到ConnAck之后,它会往remote topic上发送一个取消订阅的请求。
测试过程:
略