楔子
近期公司程式被安全掃描出 遠端主機允許明文身份驗證
中風險漏洞,查了下修復方案,RabbitMQ官方提供了SSL連線方式,而且 SpringBoot AMQP 也支援 SSL 連線。以下將配置RabbitMQ開啟SSL 並使用 SpringBoot Demo 測試連線。
PS : 寫文章時此配置還未安全掃描複測,如果測試通過,本人將更新此文章狀態為驗證通過。
配置 RabbitMQ 開啟 SSL
本文基於 CentOS 7 + Git + OpenSSL + yum 安裝的 RabbitMQ,需要讀者提交安裝好。其他方式也可變通參考本文。
生成證照
#克隆生成證照的倉庫到當前目錄
git clone --depth 1 https://github.com/Berico-Technologies/CMF-AMQP-Configuration.git
cd CMF-AMQP-Configuration/ssl
#生成ca證照,“MyRabbitMQCA”為自定義名稱,名稱任意。在當前目錄下生成ca目錄
sh setup_ca.sh MyRabbitMQCA
#生成服務端證照,第一個引數是服務端證照字首,第二個引數是密碼。密碼任意,在當前目錄下生成server目錄
sh make_server_cert.sh rabbitmq-server 123456
#生成客戶端證照,第一個引數是客戶端證照字首,第二個引數是密碼。密碼任意,在當前目錄下生成client目錄
sh create_client_cert.sh rabbitmq-client 654321
配置 RabbitMQ 服務端的證照如下:
ca/cacert.pem #CA證照
server/rabbitmq-server.cert.pem #服務端公鑰
server/rabbitmq-server.key.pem #服務端私鑰
使用 RabbitMQ 服務端公鑰證照生成 JKS 證照
# -alias後為別稱,-file後是服務端公鑰位置,-keystore後是輸出JSK證照位置,此處相對路徑
keytool -import -alias rabbitmq-server \
-file server/rabbitmq-server.cert.pem \
-keystore rabbitmqTrustStore -storepass changeit
#輸入y回車
配置 RabbitMQ 客戶端的證照如下:
client/rabbitmq-client.keycert.p12 #PKCS12證照,包含客戶端所需公私鑰及中間證照
rabbitmqTrustStore #服務端JKS格式公鑰
預設 RabbitMQ 配置目錄在 /etc/rabbitmq
,我們建立個證照目錄存放服務端證照
mkdir -p /etc/rabbitmq/ssl
#複製服務端必要證照
cp ca/cacert.pem \
server/rabbitmq-server.cert.pem \
server/rabbitmq-server.key.pem /etc/rabbitmq/ssl/
修改 RabbitMQ 配置檔案
修改 RabbitMQ 配置檔案 /etc/rabbitmq/rabbitmq.config
,此檔案預設不存在,需要手動建立
[{rabbit, [
{ssl_listeners, [5671]},
{ssl_options, [
{cacertfile, "/etc/rabbitmq/ssl/cacert.pem"},
{certfile, "/etc/rabbitmq/ssl/rabbitmq-server.cert.pem"},
{keyfile, "/etc/rabbitmq/ssl/rabbitmq-server.key.pem"},
{verify, verify_peer},
{fail_if_no_peer_cert, true},
{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"
]}
]}
]}].
主要配置項說明:
ssl_listeners
指定 SSL協議的埠號,官方文件5671
ssl_options
SSL 認證配置項
cacertfile
CA 證照位置certfile
公鑰證照位置keyfile
金鑰證照位置verify
verify_peer
客戶端與服務端互相傳送證照verify_none
禁用證照交換與校驗fail_if_no_peer_cert
true
不接受沒證照的客戶端連線false
接受沒證照的客戶端連線ciphers
加密器(這個翻譯不知道算不算對?)
重啟 RabbitMQ
#關閉
rabbitmqctl stop
#啟動
rabbitmq-server -detached
驗證開啟 SSL 是否成功
使用 Rabbitmq 自帶的診斷工具檢視埠監聽狀態及使用協議
#檢視監聽
rabbitmq-diagnostics listeners
#檢視支援的TLS版本
rabbitmq-diagnostics --silent tls_versions
使用 OpenSSL CLI 工具驗證證照是否有效
cd 生成證照的ssl目錄
#使用客戶端證照+CA證照連線RabbitMQ驗證。本處MQ與生成證照是同一主機,其他情況請自行考慮。
openssl s_client -connect localhost:5671 \
-cert client/rabbitmq-client.cert.pem \
-key client/rabbitmq-client.key.pem \
-CAfile ca/cacert.pem
除了命令列檢視外,還可以通過管理介面檢視,不過只能確定開啟了 SSL 監聽,無法確認證照是否通過驗證。
編寫 SpringBoot 程式碼連線測試
程式碼結構
只是使用 start.spring.io 生成的 Maven 工程,依賴了 WEB 和 AMQP
程式碼及配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動類 DemoApplication.java
package com.hellxz.rabbitmq.ssl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
RabbitMQ客戶端配置類 RabbitFanoutExchangeConfig.java
package com.hellxz.rabbitmq.ssl;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitFanoutExchangeConfig {
public static final String FANOUT_EXCHANGE = "fanout.exchange";
public static final String FANOUT_QUEUE1 = "fanout.queue1";
@Bean(name = FANOUT_EXCHANGE)
public FanoutExchange fanoutExchange() {
return new FanoutExchange(FANOUT_EXCHANGE, true, false);
}
@Bean(name = FANOUT_QUEUE1)
public Queue fanoutQueue1() {
return new Queue(FANOUT_QUEUE1, true, false, false);
}
@Bean
public Binding bindingSimpleQueue1(@Qualifier(FANOUT_QUEUE1) Queue fanoutQueue1,
@Qualifier(FANOUT_EXCHANGE) FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
}
}
發訊息測試類 TestController.java
package com.hellxz.rabbitmq.ssl;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
RabbitMQSenderService rabbitMQSenderService;
@GetMapping("/test")
public void sendMsg() {
Message msg = new Message("hello world".getBytes());
try {
rabbitMQSenderService.send(RabbitFanoutExchangeConfig.FANOUT_EXCHANGE,
RabbitFanoutExchangeConfig.FANOUT_QUEUE1, msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}
發訊息服務 RabbitMQSenderService.java
package com.hellxz.rabbitmq.ssl;
import java.util.UUID;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class RabbitMQSenderService {
@Autowired
private RabbitTemplate rabbitTemplate;
public void send(String exchange, String routingkey, Message message) {
CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
System.out.println("start send msg : " + message);
rabbitTemplate.convertAndSend(exchange, routingkey, message, correlationId);
System.out.println("end send msg : " + message);
}
}
訊息接收者 RabbitMQReciver.java
package com.hellxz.rabbitmq.ssl;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
class RabbitMQReciver {
@RabbitListener(queues = RabbitFanoutExchangeConfig.FANOUT_QUEUE1)
public void reciveLogAll(String msg) throws Exception {
System.out.println("received msg:" + msg);
}
}
配置檔案 application.properties
server.port=8085
#基礎配置請根據實際配置
spring.rabbitmq.host=192.168.56.104
#ssl協議埠
spring.rabbitmq.port=5671
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring.rabbitmq.virtual-host=/
#啟用rabbitmq客戶端SSL連線
spring.rabbitmq.ssl.enabled=true
#客戶端PKCS12證照及密碼
spring.rabbitmq.ssl.key-store=classpath:ssl/rabbitmq-client.keycert.p12
spring.rabbitmq.ssl.key-store-password=654321
#公鑰證照及型別
spring.rabbitmq.ssl.trust-store=classpath:ssl/rabbitmqTrustStore
spring.rabbitmq.ssl.trust-store-type=JKS
#不校驗主機名,預設開啟會導致連線失敗
spring.rabbitmq.ssl.verify-hostname=false
src/main/resources 下建立 ssl 目錄,將 客戶端證照和服務端JKS公鑰複製到 ssl 目錄中。
執行程式碼驗證
執行 DemoApplication.java
,檢視控制檯是否有報錯:
如圖,提示建立連線成功,說明已經連線成功了。
我們再呼叫 TestController.java
中定義的 /test
介面
訊息傳送與消費成功。
參考
- https://www.rabbitmq.com/access-control.html
- https://www.rabbitmq.com/ssl.html
- https://www.rabbitmq.com/troubleshooting-ssl.html
- 加密器部分參考 https://www.cnblogs.com/ybyn/p/13959135.html
- 程式碼部分參考 Github,地址已不可考
本文同步於本人部落格園(hellxz.cnblogs.com) 與 CSDN(https://blog.csdn.net/u012586326),禁止轉載。