设置 Python KafkaProducer sasl 机制 属性

Setting Python KafkaProducer sasl mechanism property

我们使用的sasl机制是SCRAM-SHA-256但是kafka生产者只会接受sasl_mechanism作为PLAIN,GSSAPI,OAUTHBEARER

下面的配置会报错

sasl_mechanism must be in PLAIN, GSSAPI, OAUTHBEARER

配置

    ssl_produce = KafkaProducer(bootstrap_servers='brokerCName:9093',
                     security_protocol='SASL_SSL',
                     ssl_cafile='pemfilename.pem',
                     sasl_mechanism='SCRAM-SHA-256',
                     sasl_plain_username='password',
                     sasl_plain_password='secret')

我需要知道如何指定正确的 sasl 机制。

谢谢

kafka 的更新答案-python v2.0.0+

由于2.0.0,kafka-python同时支持SCRAM-SHA-256SCRAM-SHA-512


旧版本 kafka-python

的上一个答案

据我了解,您使用的是 kafka-python client. From the source code,我可以看出 sasl_mechanism='SCRAM-SHA-256' 不是有效选项:

    """
    ...
    sasl_mechanism (str): Authentication mechanism when security_protocol
        is configured for SASL_PLAINTEXT or SASL_SSL. Valid values are:
        PLAIN, GSSAPI, OAUTHBEARER.
    ...
    """

    if self.config['security_protocol'] in ('SASL_PLAINTEXT', 'SASL_SSL'):
        assert self.config['sasl_mechanism'] in self.SASL_MECHANISMS, (
            'sasl_mechanism must be in ' + ', '.join(self.SASL_MECHANISMS)) 

一个快速的解决方法是使用支持 sasl_mechanism='SCRAM-SHA-256':

confluent-kafka 客户端
from confluent_kafka import Producer 

# See https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md
conf = {
    'bootstrap.servers': 'localhost:9092',
    'security.protocol': 'SASL_SSL',
    'sasl.mechanisms': 'SCRAM-SHA-256',
    'sasl.username': 'yourUsername',
    'sasl.password': 'yourPassword', 
    # any other config you like ..
}

p = Producer(**conf)
 
# Rest of your code goes here.. 

kafka-python supports both SCRAM-SHA-256 and SCRAM-SHA-512 in version 2.0.0.