当前位置:   article > 正文

Kafka配置SSL安全认_连接kafka证书秘钥

连接kafka证书秘钥

Apache Kafka允许客户端使用SSL加密流量和身份验证。默认情况下,SSL被禁用,但可以在需要时打开。

  1. 为每个Kafka代理生成SSL密钥和证书

部署一个或多个具有SSL支持的代理的第一步是为每个服务器生成公共/私有密钥对。由于Kafka希望所有密钥和证书都存储在密钥库中,因此我们将使用Java的keytool命令执行此任务。

keytool -keystore server.keystore.jks -alias localhost -validity 365  -storetype PKCS12 -genkey -dname "CN=localhost, OU=IT, O=MyCompany, L=beijing, ST=beijing, C=CN"

注意:CN属性必须写成localhost不然会导致后期生成的证书出错!!!

Enter keystore password:test1234
Re-enter new password:test1234

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

validity: 证书的有效时间,天。

证书生成后可以使用以下命令验证是否生成成功:

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

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

2.创建自己的CA

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

openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
  1. [root@node01 opt]# openssl req -new -x509 -keyout ca-key -out ca-cert -days 365
  2. Generating a 2048 bit RSA private key
  3. ......................................................+++
  4. ...................................................+++
  5. writing new private key to 'ca-key'
  6. Enter PEM pass phrase:
  7. Verifying - Enter PEM pass phrase:
  8. -----
  9. You are about to be asked to enter information that will be incorporated
  10. into your certificate request.
  11. What you are about to enter is what is called a Distinguished Name or a DN.
  12. There are quite a few fields but you can leave some blank
  13. For some fields there will be a default value,
  14. If you enter '.', the field will be left blank.
  15. -----
  16. Country Name (2 letter code) [XX]:CN
  17. State or Province Name (full name) []:beijing
  18. Locality Name (eg, city) [Default City]:beijing
  19. Organization Name (eg, company) [Default Company Ltd]:myCompany
  20. Organizational Unit Name (eg, section) []:IT
  21. Common Name (eg, your name or your server's hostname) []:kafkaSSL
  22. Email Address []:

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

示例:

  1. [root@node01 opt]# keytool -keystore server.truststore.jks -alias CARoot -import -file ca-cert
  2. Enter keystore password:
  3. Re-enter new password:
  4. Owner: CN=kafkaSSL, OU=IT, O=myCompany, L=beijing, ST=beijing, C=CN
  5. Issuer: CN=kafkaSSL, OU=IT, O=myCompany, L=beijing, ST=beijing, C=CN
  6. Serial number: db4a52d0cfa8dabd
  7. Valid from: Tue Feb 28 18:19:12 CST 2023 until: Wed Feb 28 18:19:12 CST 2024
  8. Certificate fingerprints:
  9. MD5: D7:A8:77:A8:86:B2:27:CE:40:F4:8C:EF:F3:7D:94:15
  10. SHA1: 60:14:AA:54:87:2F:75:6C:1A:28:E9:79:9C:6F:F8:CB:1D:B6:FE:D8
  11. SHA256: 55:65:05:66:69:0E:41:AC:D8:05:33:3B:13:E6:A2:4F:93:FD:F8:0D:DA:05:E3:57:0D:BB:31:35:84:09:2E:5C
  12. Signature algorithm name: SHA256withRSA
  13. Subject Public Key Algorithm: 2048-bit RSA key
  14. Version: 3
  15. Extensions:
  16. #1: ObjectId: 2.5.29.35 Criticality=false
  17. AuthorityKeyIdentifier [
  18. KeyIdentifier [
  19. 0000: A0 BF F9 B5 D4 02 30 6C 7C E4 56 8C 84 2F 35 EA ......0l..V../5.
  20. 0010: 0A 3B EF CE .;..
  21. ]
  22. ]
  23. #2: ObjectId: 2.5.29.19 Criticality=false
  24. BasicConstraints:[
  25. CA:true
  26. PathLen:2147483647
  27. ]
  28. #3: ObjectId: 2.5.29.14 Criticality=false
  29. SubjectKeyIdentifier [
  30. KeyIdentifier [
  31. 0000: A0 BF F9 B5 D4 02 30 6C 7C E4 56 8C 84 2F 35 EA ......0l..V../5.
  32. 0010: 0A 3B EF CE .;..
  33. ]
  34. ]
  35. Trust this certificate? [no]: yes
  36. Certificate was added to keystore

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

4.签名证书

用步骤2生成的CA签名步骤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 365 -CAcreateserial -passin pass:test1234

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

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

5.配置Kafka Broker

  1. listeners=SSL://localhost:9093
  2. #server.keystore.jks的地址
  3. ssl.keystore.location=/ssl/server.keystore.jks
  4. ssl.keystore.password=test1234
  5. ssl.key.password=test1234
  6. #server.truststore.jks的地址
  7. ssl.truststore.location=/ssl/server.truststore.jks
  8. ssl.truststore.password=test1234
  9. security.inter.broker.protocol=SSL
  10. ssl.endpoint.identification.algorithm=
  11. ssl.client.auth=required
  12. ssl.enabled.protocols=TLSv1.2,TLSv1.1,TLSv1
  13. ssl.keystore.type=JKS
  14. ssl.truststore.type=JKS

6.配置Kafka 客户端

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

client-ssl.properties:

  1. security.protocol=SSL
  2. ssl.truststore.location=/ssl/client.truststore.jks
  3. ssl.truststore.password=test1234
  4. ssl.keystore.location=/ssl/client.keystore.jks
  5. ssl.keystore.password=test1234
  6. ssl.key.password=test1234
  7. ssl.endpoint.identification.algorithm=

7.启动Kafka测试

启动zookeeper:

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

启动Kafka:

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

创建topic:

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

生产者:

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

消费者:

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

注意:client-ssl.properties里需要配置ssl.endpoint.identification.algorithm=来取消验证主机名。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/889391
推荐阅读
相关标签
  

闽ICP备14008679号