我正在尝试使用ssl创建kafka生产者。我需要有关如何在构造函数中设置SSL参数的信息,kafka-python客户端中提供的信息描述性不足。
什么是ssl_certfile
,ssl_cafile
,ssl_keyfile
参数。我不确定在哪里可以找到这些文件。
producer = KafkaProducer(bootstrap_servers=kafka_broker,
value_serializer=lambda v: json.dumps(v).encode('utf-8'),
security_protocol='SSL',
api_version=(0,10),
ssl_cafile='ca-certs.pem',ssl_certfile='server.pem',
ssl_keyfile='server.pem',ssl_password='xxx')
producer.send('rk976772_topic',{"test":0})
回溯(最近一次通话最后一次):文件“”,第1行,在 文件 “ /usr/lib/python2.7/site-packages/kafka/producer/kafka.py”,第543行, 在发送 self._wait_on_metadata(topic,self.config ['max_block_ms'] / 1000.0)文件“ /usr/lib/python2.7/site-packages/kafka/producer/kafka.py”,第664行, 在_wait_on_metadata中 “%.1f秒后未能更新元数据。” %max_wait)kafka.errors.KafkaTimeoutError:KafkaTimeoutError:更新失败 60.0秒后的元数据。
答案 0 :(得分:0)
我必须通过SASL_SSL发布消息 下面的代码用于创建具有SASL_SSL协议的生产者。
parseFloat()
答案 1 :(得分:0)
在尝试使用SSL或SASL_SSL配置kafka时,我遇到了这个问题以及许多其他问题。我在这里发布了完整的教程,以防其他人遇到相同的问题。我在CentOS 6上使用kafka-python 1.4.6和kafka 2.2.0。
以下是使用kafka-python客户端对SASL_SSL起作用的配置。这些配置可用于PLAINTEXT和SSL安全协议以及SASL_SSL和SASL_PLAINTEXT。
Bash脚本生成用于SSL的密钥文件,CARoot和自签名证书:
#!/bin/bash
#Step 1
keytool -keystore server.keystore.jks -alias localhost -validity 365 -keyalg RSA -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:admin123
keytool -keystore server.keystore.jks -alias CARoot -import -file ca-cert
keytool -keystore server.keystore.jks -alias localhost -import -file cert-signed
然后您可以使用以下命令来提取CARoot.pem:
keytool -exportcert -alias CARoot -keystore server.keystore.jks -rfc -file CARoot.pem
在我的server.properties文件中,有:
listeners=PLAINTEXT://localhost:9091,SASL_PLAINTEXT://localhost:9092,SSL://localhost:9093,SASL_SSL://localhost:9094
security.protocol=SSL
sasl.enabled.mechanisms=PLAIN
ssl.truststore.location=/var/private/ssl/server.truststore.jks
ssl.truststore.password=admin123
ssl.keystore.location=/var/private/ssl/server.keystore.jks
ssl.keystore.password=admin123
ssl.enabled.protocols=TLSv1.2,TLSv1.1,TLSv1
advertised.listeners=PLAINTEXT://localhost:9091,SASL_PLAINTEXT://localhost:9092,SSL://localhost:9093,SASL_SSL://localhost:9094
在我的JAAS配置文件中(/etc/kafka/kafka_plain_jaas.conf):
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=kafka
password=kafka-secret
user_username=password;
};
KafkaClient {
org.apache.kafka.common.security.plain.PlainLoginModule required
username=username
password=password;
};
在启动Kafka服务器之前,需要运行以下命令:
export KAFKA_OPTS="-Djava.security.auth.login.config=/etc/kafka/kafka_plain_jaas.conf"
Python使用者和生产者: ssl_context和api_version是导致我发生SSL握手错误并导致超时的原因。所以我评论了那些。 (那里有一些使用这些教程的教程。)
from kafka import KafkaConsumer, KafkaProducer
import kafka
import ssl
import logging
logging.basicConfig(level=logging.DEBUG)
try:
topic = "sendMessage"
sasl_mechanism = "PLAIN"
username = "username"
password = "password"
security_protocol = "SASL_SSL"
#context = ssl.create_default_context()
#context.options &= ssl.OP_NO_TLSv1
#context.options &= ssl.OP_NO_TLSv1_1
consumer = KafkaConsumer(topic, bootstrap_servers='localhost:9094',
#api_version=(0, 10),
security_protocol=security_protocol,
#ssl_context=context,
ssl_check_hostname=True,
ssl_cafile='../keys/CARoot.pem',
sasl_mechanism = sasl_mechanism,
sasl_plain_username = username,
sasl_plain_password = password)
#ssl_certfile='../keys/certificate.pem',
#ssl_keyfile='../keys/key.pem')#,api_version = (0, 10))
producer = KafkaProducer(bootstrap_servers='localhost:9094',
#api_version=(0, 10),
security_protocol=security_protocol,
#ssl_context=context,
ssl_check_hostname=True,
ssl_cafile='../keys/CARoot.pem',
sasl_mechanism=sasl_mechanism,
sasl_plain_username=username,
sasl_plain_password=password)
#ssl_certfile='../keys/certificate.pem',
#ssl_keyfile='../keys/key.pem')#, api_version = (0,10))
# Write hello world to test topic
producer.send(topic, bytes("Hello World SSL"))
producer.flush()
for msg in consumer:
print(msg)
except Exception as e:
print e