在转换为.pem之后,如何将kafka python ssl身份验证使用keystore.jks和truststore.jks文件?
我有2个证书文件,truststore.jks
和keystore.jks
。 keystore.jks
包含我使用的Kafka端点的完整证书链以及我的应用程序的私钥。根据给我truststore.jks
的小组的说法,它包含“新的根CA链”,对于获取我的应用程序与Kafka端点连接至关重要。
我尝试使用此脚本生成我在Python代码中使用的3个.pem文件。
#!/bin/bash
srcFolder=$1
keyStore=$1/$2
password=$3
alias=$4
outputFolder=$5
echo $keyStore
echo "Generating certificate.pem"
keytool -exportcert -alias $alias -keystore $keyStore -rfc -file $outputFolder/certificate.pem -storepass $password
echo "Generating key.pem"
keytool -v -importkeystore -srckeystore $keyStore -srcalias $alias -destkeystore $outputFolder/cert_and_key.p12 -deststoretype PKCS12 -storepass $password -srcstorepass $password
openssl pkcs12 -in $outputFolder/cert_and_key.p12 -nodes -nocerts -out $outputFolder/key.pem -passin pass:$password
echo "Generating CARoot.pem"
keytool -exportcert -alias $alias -keystore $keyStore -rfc -file $outputFolder/CARoot.pem -storepass $password
我使用生成的caroot.pem
,证书。pem
和key.pem
我的代码中的文件。
>>> from kafka import KafkaProducer
>>> producer = KafkaProducer(bootstrap_servers='kafka-dev.kafka.com:port', security_protocol="SSL",
... ssl_check_hostname=True,
... ssl_cafile="CARoot.pem",
... ssl_certfile="certificate.pem",
... ssl_keyfile="key.pem",
... ssl_password="")
它给出了此错误:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1131)
我尝试组合truststore.jks
和keystore.jks
so:
keytool -importkeystore -srckeystore truststore.jks -destkeystore keystore_copy.jks
然后,我将keystore_copy.jks
通过相同<代码> .pem 转换过程和以前一样,但会产生相同的错误。
在某些情况下,这就是Kafka端点所有者建议在Java配置SSL证书的方式,不幸的是,我是第一个与Python建立联系的人,因此他们无法帮助我。
props.setProperty(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
props.setProperty(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, "<kafka-truststore.jks>");
props.setProperty(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "<password>");
props.setProperty(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, "<client-keystore.jks>");
props.setProperty(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "<password>");
props.setProperty(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "<password>");
我缺少什么吗?我相信我需要在我的代码中使用两种.jks
文件,但是我想到的唯一方法是失败。
我尝试过的其他事情: PIP安装 - 基于该库过时的相同错误的报告
。
I have 2 certificate files, truststore.jks
and keystore.jks
. keystore.jks
contains a full certificate chain for the kafka endpoint I'm using as well as a private key for my application. According to the group who gave me truststore.jks
it contains "a new root CA chain" and is essential for getting my application to connect with the kafka endpoint.
I have tried using this script to generate 3 .pem files that I use in my Python code.
#!/bin/bash
srcFolder=$1
keyStore=$1/$2
password=$3
alias=$4
outputFolder=$5
echo $keyStore
echo "Generating certificate.pem"
keytool -exportcert -alias $alias -keystore $keyStore -rfc -file $outputFolder/certificate.pem -storepass $password
echo "Generating key.pem"
keytool -v -importkeystore -srckeystore $keyStore -srcalias $alias -destkeystore $outputFolder/cert_and_key.p12 -deststoretype PKCS12 -storepass $password -srcstorepass $password
openssl pkcs12 -in $outputFolder/cert_and_key.p12 -nodes -nocerts -out $outputFolder/key.pem -passin pass:$password
echo "Generating CARoot.pem"
keytool -exportcert -alias $alias -keystore $keyStore -rfc -file $outputFolder/CARoot.pem -storepass $password
I use the generated CARoot.pem
, certificate.pem
, and key.pem
files in my code like so.
>>> from kafka import KafkaProducer
>>> producer = KafkaProducer(bootstrap_servers='kafka-dev.kafka.com:port', security_protocol="SSL",
... ssl_check_hostname=True,
... ssl_cafile="CARoot.pem",
... ssl_certfile="certificate.pem",
... ssl_keyfile="key.pem",
... ssl_password="")
It gives this error:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1131)
I have tried combining truststore.jks
and keystore.jks
like so:
keytool -importkeystore -srckeystore truststore.jks -destkeystore keystore_copy.jks
I then put keystore_copy.jks
through the same .pem
conversion process as before, but it produces the same error.
For some context, this is how the Kafka endpoint owners suggest to configure the ssl certs in Java, unfortunately I am the first to connect with Python so they haven't been able to help me with it.
props.setProperty(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
props.setProperty(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, "<kafka-truststore.jks>");
props.setProperty(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "<password>");
props.setProperty(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, "<client-keystore.jks>");
props.setProperty(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "<password>");
props.setProperty(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "<password>");
Is there something I'm missing? I believe I need to utilize both .jks
files in my code somehow but the only approach I can think of taking failed.
Additional things I have tried:pip install --upgrade certifi
based on reports of the same error due to that library being out of date.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我设法自己找出解决方案。这里的钥匙意识到我需要从
truststore.jks
文件中使用caroot.pem.pem
中的整个CERT链,以使代码工作。以下是我采取的步骤:从信任存储中生成
caroot.pem
:generate
证书.pem
从密钥库提取私有密钥和生成
rsakey.pem 来自KeyStore:
与Kafka-Python生产者的示例:
I managed to figure out the solution on my own. The key here was realizing that I needed to have the entire cert chain from my
truststore.jks
file present inCARoot.pem
for the code to work. Here are the steps that I took:Generate
CARoot.pem
from the trust store:Generate
certificate.pem
from keystoreExtract private key and generate
RSAkey.pem
from keystore:Example with Kafka-Python Producer:
问您正确的Kafka端点所有者的以下属性是正确的,但是您的密钥库/信托店具有自签名证书的问题。它似乎在服务器级别启用了ssl.verify或主机名验证。
这是一个使您更好地清楚此错误。
https://community.ibm.com/community/user/ibmz-and-linuxone/blogs/sheng-jie-han/2021/06/06/06/how-to-f-fix-fix-fix-fix-certificate-verificate-verife--failed-failed-neld-self - 签名-C
The following properties which KAFKA endpoint owners are asked you is correct, but looks like the issue with your keystore/truststore having a self signed certificate. It appears ssl.verify or hostName verification is enabled at the server level.
This is one give you better clarity on this error.
https://community.ibm.com/community/user/ibmz-and-linuxone/blogs/sheng-jie-han/2021/06/03/how-to-fix-certificate-verify-failed-self-signed-c