kafka使用ssl加密和认证

Apache kafka 允许clinet通过SSL连接,SSL默认是不可用的,需手动开启。

主要步骤是:

  • 生成SSL密钥和证书
  • 配置kafka broker
  • 配置kafka 客户端

1. 测试环境

os: ubuntu 18

java: 1.8.0_275

kafka: 2.7.0

2. 生成SSL密钥和证书

官方的步骤

#!/bin/bash
#Step 1
keytool -keystore server.keystore.jks -alias localhost -validity 365 -genkey
#Step 2
openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
keytool -keystore server.truststore.jks -alias CARoot -import -file ca-cert
keytool -keystore client.truststore.jks -alias CARoot -import -file ca-cert
#Step 3
keytool -keystore server.keystore.jks -alias localhost -certreq -file cert-file
openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 365 -CAcreateserial -passin pass:test1234
keytool -keystore server.keystore.jks -alias CARoot -import -file ca-cert
keytool -keystore server.keystore.jks -alias localhost -import -file cert-signed

把上面的步骤拆开来,并解释

2.1 生成新的证书文件

生成密钥和证书,可以使用java的keytool来生产。我们将生成密钥到一个临时的密钥库,之后我们可以导出并用CA签名它。

keytool -keystore server.keystore.jks -alias localhost -validity 365 -genkey

例子

$ keytool -keystore server.keystore.jks -alias localhost -validity 365  -storetype PKCS12 -genkey -dname "CN=localhost, OU=IT, O=MyCompany, L=guangzhou, ST=Guangdong, C=CN"
Enter keystore password:
Re-enter new password:
$ ls
server.keystore.jks

keystore: 密钥仓库存储证书文件。密钥仓库文件包含证书的私钥(保证私钥的安全)。

validity: 证书的有效时间,天

此步骤要注意的是,名与姓(CN)这一项必须输入域名,如 "localhost",切记不可以随意写,我曾尝试使用其他字符串,在后面客户端生成证书认证的时候一直有问题。

2.1.1 在证书中配置主机名

客户端将根据以下两个字段之一验证服务器的完全限定域名(FQDN):

  1. Common Name (CN)
  2. Subject Alternative Name (SAN)

这两个字段均有效,但是RFC-2818建议使用SAN。SAN更加的灵活,允许声明多个DNS条目。另一个优点是,出于授权目的,可以将CN设置为更有意义的值。 要添加SAN,需将以下参数-ext SAN=DNS:{FQDN}追加到keytool命令:

keytool -keystore server.keystore.jks -alias localhost -validity {validity} -genkey -keyalg RSA -ext SAN=DNS:{FQDN}

完成上面步骤,可使用命令

keytool -list -v -keystore server.keystore.jks

来验证生成证书的内容

server.keystore.jks 这个文件包含了一对公私钥,和一个证书来识别机器。但是,证书是未签名的,这意味着攻击者可以创建一个这样的证书来伪装成任何机器。

2.2 创建自己的CA

生成的CA是一个简单的公私钥对证书,用于签名其他的证书。

openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
  1. ca-cert: CA的证书
  2. ca-key: CA的私钥

例子

$ openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
Can't load /home/user/.rnd into RNG
140584606929344:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/home/user/.rnd
Generating a RSA private key
...............................+++++
.............................+++++
writing new private key to 'ca-key'
Enter PEM pass phrase:
Verifying - Enter PEM pass phrase:
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:Guangdong
Locality Name (eg, city) []:Guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany
Organizational Unit Name (eg, section) []:IT
Common Name (e.g. server FQDN or YOUR name) []:royca
Email Address []:[email protected]

2.3 创建信任库

将生成的CA添加到**clients' truststore(客户的信任库)**,以便client可以信任这个CA:

keytool -keystore server.truststore.jks -alias CARoot -import -file ca-cert
keytool -keystore client.truststore.jks -alias CARoot -import -file ca-cert

ca-cert: CA的证书

例子

$ keytool -keystore server.truststore.jks -alias CARoot -import -file ca-cert
Enter keystore password:
Re-enter new password:
Owner: [email protected], CN=royca, OU=IT, O=MyCompany, L=Guangzhou, ST=Guangdong, C=CN
Issuer: [email protected], CN=royca, OU=IT, O=MyCompany, L=Guangzhou, ST=Guangdong, C=CN
Serial number: 85b0b09eed3d0cdbd87f83612ff42f5ac6ae384
Valid from: Thu Dec 31 06:26:58 UTC 2020 until: Fri Dec 31 06:26:58 UTC 2021
Certificate fingerprints:
         MD5:  64:E4:92:12:FC:F7:9A:0F:73:D8:CC:EA:79:8F:F4:8F
         SHA1: E6:C8:DB:66:A1:11:87:48:11:58:2F:9D:6B:2E:21:27:88:53:B1:65
         SHA256: F2:B4:0C:B2:5B:27:5B:E0:5A:90:7C:02:5E:79:1C:E0:E7:66:8A:97:99:71:CE:41:EF:90:D4:2C:DC:49:B6:B1
Signature algorithm name: SHA256withRSA
Subject Public Key Algorithm: 2048-bit RSA key
Version: 3

Extensions:

#1: ObjectId: 2.5.29.35 Criticality=false
AuthorityKeyIdentifier [
KeyIdentifier [
0000: CC C8 9D 3D D9 31 46 04   23 55 13 16 37 5C CE A7  ...=.1F.#U..7\..
0010: CB 8F 31 E7                                        ..1.
]
]

#2: ObjectId: 2.5.29.19 Criticality=true
BasicConstraints:[
  CA:true
  PathLen:2147483647
]

#3: ObjectId: 2.5.29.14 Criticality=false
SubjectKeyIdentifier [
KeyIdentifier [
0000: CC C8 9D 3D D9 31 46 04   23 55 13 16 37 5C CE A7  ...=.1F.#U..7\..
0010: CB 8F 31 E7                                        ..1.
]
]

Trust this certificate? [no]:  yes
Certificate was added to keystore

客户端的信任库存储所有客户端信任的证书,将证书导入到一个信任仓库也意味着信任由该证书签名的所有证书,正如上面的比喻,信任政府(CA)也意味着信任它颁发的所有护照(证书),此特性称为信任链,在大型的kafka集群上部署SSL时特别有用的。可以用单个CA签名集群中的所有证书,并且所有的机器共享相同的信任仓库,这样所有的机器也可以验证其他的机器了。

2.4 签名证书

用步骤2.2 生成的CA签名 步骤2.1生成的证书。首先导出请求文件:

keytool -keystore server.keystore.jks -alias localhost -certreq -file cert-file

cert-file: 出口,服务器的未签名证书

然后用CA签名:

openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days {validity} -CAcreateserial -passin pass:{ca-password}

例子

$ openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 365 -CAcreateserial -passin pass:123456
Signature ok
subject=C = CN, ST = Guangdong, L = guangzhou, O = MyCompany, OU = IT, CN = localhost
Getting CA Private Key

最后,你需要导入CA的证书和已签名的证书到密钥仓库:

keytool -keystore server.keystore.jks -alias CARoot -import -file ca-cert
keytool -keystore server.keystore.jks -alias localhost -import -file cert-signed

参数

  1. ca-cert: CA的证书
  2. cert-signed: 已签名的服务器证书

3. 配置Kafka Broker

Kafka Broker支持监听多个端口上的连接,通过server.properteis 配置,最少监听1个端口,用逗号分隔。

下面是broker端需要的SSL配置,

more config/server.properties

listeners=SSL://localhost:9093
ssl.keystore.location=/ssl/server.keystore.jks
ssl.keystore.password=123456
ssl.key.password=123456
ssl.truststore.location=/ssl/server.truststore.jks
ssl.truststore.password=123456

security.inter.broker.protocol=SSL
ssl.endpoint.identification.algorithm=

4. 配置Kafka客户端

Producer和Consumer的SSL的配置是相同的。

如果broker中不需要client(客户端)验证,那么下面是最小的配置示例:

more client-ssl.properties

security.protocol=SSL
ssl.truststore.location=/ssl/client.truststore.jks
ssl.truststore.password=123456

如果需要客户端认证,则必须像步骤2.1一样创建密钥库,并且还必须配置以下内容:

ssl.keystore.location=/ssl/client.keystore.jks
ssl.keystore.password=123456
ssl.key.password=123456

5. 启动服务并测试

启动 zookeeper 和 kafka

在不同的Terminal 分别运行下面命令

Terminal1

bin/zookeeper-server-start.sh config/zookeeper.properties

Terminal2

bin/kafka-server-start.sh config/server.properties

Terminal3

创建topic

bin/kafka-topics.sh --create --topic test --bootstrap-server localhost:9093 --command-config client-ssl.properties

生产和消费topic

bin/kafka-console-producer.sh --bootstrap-server localhost:9093 --topic test --producer.config client-ssl.properties 

bin/kafka-console-consumer.sh --bootstrap-server localhost:9093 --topic test --consumer.config client-ssl.properties

参考文档

官网: http://kafka.apache.org/documentation/#security_ssl

https://www.orchome.com/171

https://www.orchome.com/1959

踩过的坑

Java 11 导致 SSL handshake fail https://stackoverflow.com/questions/57601284/java-11-and-12-ssl-sockets-fail-on-a-handshake-failure-error-with-tlsv1-3-enable

生成本机证书时,CN 需要填写 localhost 或者真实的域名,否则客户端连接失败。

你可能感兴趣的:(kafka使用ssl加密和认证)