Docker环境RabbitMq配置SSL
RabbitMQ要对外提供服务,考虑到安全性,配置SSL进行访问,ssl端口5671,内部仍然使用5672进行访问,两者同时兼容。
安装环境
- CentOS 7.5
- Docker 1.13.1
- Git 1.8.3
- jdk 1.8
- RabbitMQ镜像,rabbitmq:management
证书
RabbitMQ也提供了获取证书的方案,但是使用过程中出现错误,未来得及进行解决,在github上查找到一个项目可以生成证书,遂使用之。
安装git
要从github上将项目克隆下来,需要有git环境,安装步骤如下
# yum安装git
$ yum -y install git
# 配置邮箱以及账号
$ git config --global user.name = 'xxx'
$ git config --global user.email = 'xxx'
# 查看git配置
$ git config --list
生成证书
1.从github上克隆项目
$ git clone https://github.com/Berico-Technologies/CMF-AMQP-Configuration.git
2.下载完成后,进入CMF-AMQP-Configuration/ssl/目录
$ cd CMF-AMQP-Configuration/ssl/
3.生成证书签发机构
$ sh setup_ca.sh xxx
xxx为自定义的证书签发机构名称,该脚本会在当前目录下生成一个ca目录,存储证书颁发机构的信息以及已经签发的一些证书
执行脚本后的ca目录如下所示
4.生成服务端公钥和私钥
$ sh make_server_cert.sh rabbit-server 123456
注释:
rabbit-server:生成的密钥前缀名,自定义
123456: 访问该密钥的密码,自定义
执行该脚本后,在当前目录生成server目录,存储服务端公钥和私钥,如下所示
5.生成客户端公钥和私钥
$ sh create_client_cert.sh rabbit-client 123456
注释:
rabbit-client:生成的密钥前缀名,自定义。
123456: 访问该密钥的密码,自定义。
执行该脚本后,在当前目录下生成client目录,存储客户端公钥和私钥,如下所示
6.生成客户端需要的证书
使用java的keytool工具生成客户端需要的证书,用以支持服务端和客户端进行通信,生成该证书需要提前安装配置java环境,此处默认已正确安装java环境,不会安装的可以参考本人博客
$ keytool -import -alias rabbit-server -file server/rabbit-server.cert.pem -keystore rabbitStore -storepass 123456
注释:
-import 将已签名数字证书导入密钥库
-alias xxx 指定导入条目的别名
-file server/rabbit-server.cert.pem 需要导入的证书
-keystore xxx 指定密钥库的名称
-storepass xxx 指定密钥库的密码(获取keystore信息所需的密码)
经过以上6个步骤,我们一共生成了3个文件夹以及1个文件,分别是server/,client/,ca/以及rabbitStore证书,只要以上几个文件都存在,那么就表示生成证书成。
安装RabbitMQ并配置SSL
1.拉取management版本的镜像
$ docker pull rabbitmq:management
2.先使用以下命令启动容器
$ docker run --restart=unless-stopped -d -p 5672:5672 -p 15672:15672 --name rabbitmq rabbitmq:management
3.创建文件夹
在宿主机上创建以下文件夹
$ mkdir -p /etc/rabbitmq/ssl
4.拷贝容器中的文件到/etc/rabbitmq/目录下
$ docker cp rabbitmq:/etc/rabbitmq/conf.d /etc/rabbitmq/
$ docker cp rabbitmq:/etc/rabbitmq/enabled_plugins /etc/rabbitmq/
$ docker cp rabbitmq:/etc/rabbitmq/rabbitmq.conf /etc/rabbitmq/
5.拷贝生成的证书到/etc/rabbitmq/ssl/目录下
在./CMF-AMQP-Configuration/ssl/目录下执行以下命令
$ cp -r ca server client rabbitStore /etc/rabbitmq/ssl
6.编辑/etc/rabbitmq/rabbitmq.conf文件,输入以下内容
# SSL\TLS通信的端口
listeners.ssl.default=5671
# 服务端私钥和证书文件配置
ssl_options.cacertfile=/etc/rabbitmq/ssl/ca/cacert.pem
ssl_options.certfile=/etc/rabbitmq/ssl/server/rabbit-server.cert.pem
ssl_options.keyfile=/etc/rabbitmq/ssl/server/rabbit-server.key.pem
# 有verify_none和verify_peer两个选项,verify_none表示完全忽略验证证书的结果,verify_peer表示要求验证对方证书
ssl_options.verify=verify_peer
# 若为true,服务端会向客户端索要证书,若客户端无证书则中止SSL握手;若为false,则客户端没有证书时依然可完成SSL握手
ssl_options.fail_if_no_peer_cert=true
ssl_options.versions.1=tlsv1.2
ssl_options.versions.2=tlsv1.1
ssl_options.ciphers.1 = ECDHE-ECDSA-AES256-GCM-SHA384
ssl_options.ciphers.2 = ECDHE-RSA-AES256-GCM-SHA384
ssl_options.ciphers.3 = ECDHE-ECDSA-AES256-SHA384
ssl_options.ciphers.4 = ECDHE-RSA-AES256-SHA384
ssl_options.ciphers.5 = ECDHE-ECDSA-DES-CBC3-SHA
ssl_options.ciphers.6 = ECDH-ECDSA-AES256-GCM-SHA384
ssl_options.ciphers.7 = ECDH-RSA-AES256-GCM-SHA384
ssl_options.ciphers.8 = ECDH-ECDSA-AES256-SHA384
ssl_options.ciphers.9 = ECDH-RSA-AES256-SHA384
ssl_options.ciphers.10 = DHE-DSS-AES256-GCM-SHA384
ssl_options.ciphers.11= DHE-DSS-AES256-SHA256
ssl_options.ciphers.12 = AES256-GCM-SHA384
ssl_options.ciphers.13 = AES256-SHA256
ssl_options.ciphers.14 = ECDHE-ECDSA-AES128-GCM-SHA256
ssl_options.ciphers.15 = ECDHE-RSA-AES128-GCM-SHA256
ssl_options.ciphers.16 = ECDHE-ECDSA-AES128-SHA256
ssl_options.ciphers.17 = ECDHE-RSA-AES128-SHA256
ssl_options.ciphers.18 = ECDH-ECDSA-AES128-GCM-SHA256
ssl_options.ciphers.19= ECDH-RSA-AES128-GCM-SHA256
ssl_options.ciphers.20 = ECDH-ECDSA-AES128-SHA256
ssl_options.ciphers.21 = ECDH-RSA-AES128-SHA256
ssl_options.ciphers.22 = DHE-DSS-AES128-GCM-SHA256
ssl_options.ciphers.23 = DHE-DSS-AES128-SHA256
ssl_options.ciphers.24 = AES128-GCM-SHA256
ssl_options.ciphers.25 = AES128-SHA256
ssl_options.ciphers.26 = ECDHE-ECDSA-AES256-SHA
ssl_options.ciphers.27 = ECDHE-RSA-AES256-SHA
ssl_options.ciphers.28 = DHE-DSS-AES256-SHA
ssl_options.ciphers.29 = ECDH-ECDSA-AES256-SHA
ssl_options.ciphers.30 = ECDH-RSA-AES256-SHA
ssl_options.ciphers.31= AES256-SHA
ssl_options.ciphers.32 = ECDHE-ECDSA-AES128-SHA
ssl_options.ciphers.33 = ECDHE-RSA-AES128-SHA
ssl_options.ciphers.34 = DHE-DSS-AES128-SHA
ssl_options.ciphers.35 = DHE-DSS-AES128-SHA256
ssl_options.ciphers.36 = ECDH-ECDSA-AES128-SHA
ssl_options.ciphers.37 = ECDH-RSA-AES128-SHA
ssl_options.ciphers.38 = AES128-SHA
注释:
rabbitmq.conf文件中原有的内容可以直接删除掉,也可以保留,不影响
也可以不使用上述的rabbitmq.conf文件,而使用rabbitmq.config文件,不过后者需要自己创建,且不可以和rabbitmq.conf共存,RabbitMQ3.7版本以后,推荐使用rabbitmq.conf,因此如果要使用rabbitmq.config,需要先创建,再写入内容
# 创建rabbitmq.config文件
$ vim rabbitmq.config
# 输入以下内容保存
%%Disable SSLv3.0 and TLSv1.0 support.
[
{ssl, [{versions, ['tlsv1.2', 'tlsv1.1']}]},
{rabbit, [
{tcp_listeners, [5672]},
{ssl_listeners, [5671]},
{ssl_options, [{cacertfile,"/etc/rabbitmq/ssl/ca/cacert.pem"},
{certfile,"/etc/rabbitmq/ssl/server/rabbit-server.cert.pem"},
{keyfile,"/etc/rabbitmq/ssl/server/rabbit-server.key.pem"},
{verify, verify_peer},
{ciphers, ["ECDHE-ECDSA-AES256-GCM-SHA384","ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-ECDSA-AES256-SHA384","ECDHE-RSA-AES256-SHA384", "ECDHE-ECDSA-DES-CBC3-SHA",
"ECDH-ECDSA-AES256-GCM-SHA384","ECDH-RSA-AES256-GCM-SHA384","ECDH-ECDSA-AES256-SHA384",
"ECDH-RSA-AES256-SHA384","DHE-DSS-AES256-GCM-SHA384","DHE-DSS-AES256-SHA256",
"AES256-GCM-SHA384","AES256-SHA256","ECDHE-ECDSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES128-GCM-SHA256","ECDHE-ECDSA-AES128-SHA256","ECDHE-RSA-AES128-SHA256",
"ECDH-ECDSA-AES128-GCM-SHA256","ECDH-RSA-AES128-GCM-SHA256","ECDH-ECDSA-AES128-SHA256",
"ECDH-RSA-AES128-SHA256","DHE-DSS-AES128-GCM-SHA256","DHE-DSS-AES128-SHA256",
"AES128-GCM-SHA256","AES128-SHA256","ECDHE-ECDSA-AES256-SHA",
"ECDHE-RSA-AES256-SHA","DHE-DSS-AES256-SHA","ECDH-ECDSA-AES256-SHA",
"ECDH-RSA-AES256-SHA","AES256-SHA","ECDHE-ECDSA-AES128-SHA",
"ECDHE-RSA-AES128-SHA","DHE-DSS-AES128-SHA","ECDH-ECDSA-AES128-SHA",
"ECDH-RSA-AES128-SHA","AES128-SHA"]},
{honor_cipher_order, true},
{fail_if_no_peer_cert, true},
{versions, ['tlsv1.2', 'tlsv1.1']}
]},
{auth_mechanisms,['PLAIN', 'AMQPLAIN', 'EXTERNAL']}
]}
].
注释:
rabbitmq.conf和rabbitmq.config任选其一即可,个人认为前者可读性更好一些,但是编写的内容相对较多一些
7.删除前面创建的rabbitmq容器,重新创建
使用以下命令重新创建rabbitmq容器,添加5671端口映射以及/etc/rabbitmq/文件夹的挂载
$ docker run --restart=unless-stopped -d \
-p 5672:5672 -p 15672:15672 -p 5671:5671 \
-v /etc/rabbitmq/:/etc/rabbitmq/ \
--name rabbitmq \
rabbitmq:management
8.验证5671端口
使用以下命令查看rabbitmq容器启动时的日志
$ docker logs -f rabbitmq
看到日志中出现以下内容表示配置成功
9.创建可以远程访问的用户
由于guest用户只能本地访问,因此需要创建一个可以远程访问的用户
# 进入容器内部
$ docker exec -it rabbitmq bash
# 创建一个用户名为admin,密码为123456的用户
$ rabbitmqctl add_user admin 123456
# 设置用户权限为超级管理员
$ rabbitmqctl set_user_tags admin administrator
# 授权远程访问
$ rabbitmqctl set_permissions -p / admin "." "." ".*"
# 退出容器
$ exit
# 重启rabbitmq
$ docker restart rabbitmq
10.访问管理页面
此时可以使用上面创建的admin用户远程访问管理页面,地址为http://ip:15672
进入之后可以通过如下方式确认ssl是否配置成功
SpringBoot集成SSL
此处只给出配置ssl的部分,springboot集成rabbitmq的方法可以参考本人博客
spring:
rabbitmq:
addresses: 192.168.216.129:5671
username: admin
password: 123456
virtual-host: /
ssl:
enabled: true
key-store: classpath:keyStore/129/rabbit-client.keycert.p12
key-store-password: 123456
trust-store: classpath:keyStore/129/rabbitStore
trust-store-password: 123456
algorithm: TLSv1.2
trust-store-type: JKS
key-store-type: PKCS12
validate-server-certificate: true
verify-hostname: false
注释:
addresses: 192.168.216.129:5671 ssl使用的是5671的端口,如果不使用ssl还可以使用5672端口
ssl.enabled 是否启用ssl,默认false
key-store: 客户端证书的存储路径,前面已经生成,直接拷贝过来即可
key-store-password: 生成客户端证书的密码
trust-store: 信任证书的存储路径,前面已经生成,直接拷贝过来即可
trust-store-password:生成证书的密码
verify-hostname:是否验证hostname,默认是true,需要设置为false,否则启动报错(No subject alternative names present),原因是证书中定义的common name和程序运行的机器的domain不匹配,这个感觉没有必要纠结,直接设置为false即可。