设置Python KafkaProducer sasl机制属性

时间:2019-07-31 12:25:35

标签: python ssl apache-kafka kafka-producer-api apache-kafka-security

我们正在使用的sasl机制为SCRAM-SHA-256,但kafka生产者仅将sasl_mechanism接受为PLAINGSSAPIOAUTHBEARER

以下配置将显示错误

sasl_mechanism must be in PLAIN, GSSAPI, OAUTHBEARER

config

    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机制。

谢谢

2 个答案:

答案 0 :(得分:4)

kafka-python2.0.0版本中同时支持SCRAM-SHA-256SCRAM-SHA-512

答案 1 :(得分:1)

据我了解,您正在使用kafka-python客户端。从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'的{​​{3}}客户端:

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..