【问题标题】:Disable Certificate validation in SchemaRegistryClient Confluent Kafka在 SchemaRegistryClient Confluent Kafka 中禁用证书验证
【发布时间】:2021-11-07 02:35:30
【问题描述】:

所以,我想从 kafka (Confluent) 中读取一个主题,其中数据位于 Avro 格式。

出于某些不可避免的原因,我想禁用证书验证。

我正在使用 security.protocol= SASL_SSL 和 sasl.mechanisms= OUTHBEARER

我可以通过禁用 ssl 证书验证来连接到 Kafka

'enable.ssl.certificate.verification': 'false'

现在,我在尝试使用 Schema 注册表反序列化值时遇到了问题。 avro 解串器需要模式注册表客户端和模式(可选)。我两个都过去了。我通过使用 verify=False 发出单独的请求来获取第二个参数的值以获取架构,这很好。但是当我尝试从中创建一个反序列化消费者时,问题就出现了。

基本上是代码的骨架(以及问题所在的 cmets)

topic="mytopic"
registry_configuration="schema registry url"
url = urljoin(registry_configuration, f'/subjects/{topic}-value/versions/latest')


schema_registry_response = requests.get(url, verify=False) #here I intentionally used verify=False to get the schema
schema_registry_response.raise_for_status()
        
consumption_schema=schema_registry_response.json()['schema']
print(consumption_schema) # This works fine

schema_registry_client = SchemaRegistryClient({'url': registry_configuration})
avro_deserializer = AvroDeserializer(schema_registry_client,consumption_schema
                                             ) # This is the problem area which is called when the poll method is called


string_deserializer = StringDeserializer('utf_8')

basic_conf=_get_basic_configuration()
consumer_conf = {'key.deserializer': string_deserializer,
                 'value.deserializer': avro_deserializer,
                 'group.id': 'myconsumergroupid',
                 'auto.offset.reset': 'earliest'}
consumer_conf.update(basic_conf)

        # update default config with parameter
additional_consumer_conf={}
consumer_conf.update(additional_consumer_conf)
cn=DeserializingConsumer(consumer_conf)    
cn.subscribe(['topicname'])

while True:
  msg=cn.poll(10) # This fails because the deserializingconsumer calls the schema registry and the certificate validation fails

上面看到的get_basic_configuration方法有

'enable.ssl.certificate.verification': 'false'

错误是

Max retries exceeded with url: /schemas/ids/140 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain

我已经查看了 SchemaRegistryClient here 的代码,但我没有看到任何将证书验证设置为 false 的选项。

我还搜索了 S.O 帖子和其他 Confluent 文档,看看是否能找到一些东西,但没有任何帮助

希望有人在这里了解更多?我愿意详细说明或澄清任何问题吗? 如果可能的话,我尽量不使用大量自定义逻辑来反序列化。

【问题讨论】:

    标签: python confluent-platform confluent-schema-registry confluent-kafka-python


    【解决方案1】:

    我已经找到了答案。

    基本上这个 S.O 帖子here。特别是接受答案之后的答案,如果您使用的是 Confluent kafka

    还有 avro 文档 here,因为我的架构不是来自文件,而是作为 http 响应,所以我不得不使用 avro.schema.parse 解析它

    最终骨架代码

    topic="mytopic"
    registry_configuration="schema registry url"
    url = urljoin(registry_configuration, f'/subjects/{topic}-value/versions/latest')
    
    
    schema_registry_response = requests.get(url, verify=False)
    schema_registry_response.raise_for_status()
            
    consumption_schema=schema_registry_response.json()['schema']
    consumption_schema = avro.schema.parse(consumption_schema)
    
    schema_registry_client = SchemaRegistryClient({'url': registry_configuration})
    
    basic_conf=_get_basic_configuration()
    consumer_conf = {
                     'group.id': 'myconsumergroupid',
                     'auto.offset.reset': 'earliest'}
    consumer_conf.update(basic_conf)
    
    
    cn=Consumer(consumer_conf)    
    cn.subscribe(['mytopic'])
    reader = DatumReader(consumption_schema)
    
    while True:
      msg=cn.poll(10)
      if msg is None:
        break
      m=msg.value()
      message_bytes = io.BytesIO(m)
      
      message_bytes.seek(5)
      decoder = BinaryDecoder(message_bytes)
      event_dict = reader.read(decoder) 
      print(event_dict)
      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      相关资源
      最近更新 更多