Zookeeper 在Kafka中设置ACL后无法生成

o4tp2gmn  于 2022-12-09  发布在  Apache
关注(0)|答案(1)|浏览(218)

我在本地使用wurstmeisterKafkazookeeper docker图像来测试kafka中的SASL和ACL。
我的 docker-compose.yml 是-

version: '3'

services:
  zookeeper:
    image: wurstmeister/zookeeper
    hostname: zookeeper
    container_name: zookeeper
    volumes:
    - ./zookeeper/zookeeper.sasl.jaas.config:/etc/kafka/zookeeper_server_jaas.conf
    - ./zk/data:/var/lib/zookeeper/data
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
      ZOOKEEPER_SET_ACL: 'true'
      KAFKA_OPTS: -Djava.security.auth.login.config=/etc/kafka/zookeeper_server_jaas.conf
          -Dzookeeper.authProvider.1=org.apache.zookeeper.server.auth.SASLAuthenticationProvider
          -Dzookeeper.allowSaslFailedClients=false
          -Dzookeeper.requireClientAuthScheme=sasl

  broker:
    image: wurstmeister/kafka:2.13-2.6.0
    hostname: broker
    container_name: broker
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    volumes:
      - ./kafka/kafka.jaas.conf:/etc/kafka/kafka_server_jaas.conf
      - ./kfk/data:/kafka
    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: EXTERNAL:SASL_PLAINTEXT
      KAFKA_AUTHORIZER_CLASS_NAME: kafka.security.authorizer.AclAuthorizer
      KAFKA_AUTO_CREATE_TOPIC: 'true'
      KAFKA_LISTENERS: EXTERNAL://:9092
      KAFKA_ADVERTISED_LISTENERS: EXTERNAL://localhost:9092
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_SASL_ENABLED_MECHANISMS: PLAIN
      KAFKA_LISTENER_NAME_EXTERNAL_SASL_ENABLED_MECHANISMS: PLAIN
      KAFKA_LISTENER_NAME_EXTERNAL_PLAIN_SASL_JAAS_CONFIG: |
            org.apache.kafka.common.security.plain.PlainLoginModule required \
            username="broker" \
            password="broker" \
            user_broker="broker" \
            user_client="client-secret" \
            user_alice="alice-secret";
      KAFKA_SASL_MECHANISM_INTER_BROKER_PROTOCOL: PLAIN
      KAFKA_INTER_BROKER_LISTENER_NAME: EXTERNAL

以下是jaas给Zookeeper和Kafka的档案-

  • Zookeeper.sasl.jaas.配置 * -
Server {
   org.apache.zookeeper.server.auth.DigestLoginModule required
   user_kafka="kafka";
};
  • Kafka.jaas.配置 * -
Client {
   org.apache.zookeeper.server.auth.DigestLoginModule required
   username="kafka"
   password="kafka";
};

我创建了Zookeeper和Kafka容器,并在Kafka容器中运行命令-

/opt/kafka_2.13-2.6.0/bin # ./kafka-acls.sh --authorizer-properties zookeeper.connect=zookeeper:2181 --add --allow-principal User:alice --producer --topic testtopic
Adding ACLs for resource `ResourcePattern(resourceType=TOPIC, name=testtopic, patternType=LITERAL)`: 
    (principal=User:alice, host=*, operation=DESCRIBE, permissionType=ALLOW)
    (principal=User:alice, host=*, operation=WRITE, permissionType=ALLOW)
    (principal=User:alice, host=*, operation=CREATE, permissionType=ALLOW) 

Current ACLs for resource `ResourcePattern(resourceType=TOPIC, name=testtopic, patternType=LITERAL)`: 
    (principal=User:alice, host=*, operation=DESCRIBE, permissionType=ALLOW)
    (principal=User:alice, host=*, operation=WRITE, permissionType=ALLOW)
    (principal=User:alice, host=*, operation=CREATE, permissionType=ALLOW)

但是当我试图从我的go代码中产生事件时(使用sarama)-它给出错误

kafka server: In the middle of a leadership election, there is currently no leader for this partition and hence it is unavailable for writes.

我的行动密码是-

package main

import "github.com/Shopify/sarama"

var brokers = []string{"127.0.0.1:9092"}

func newProducer() (sarama.SyncProducer, error) {
    config := sarama.NewConfig()
    config.Producer.Partitioner = sarama.NewRandomPartitioner
    config.Producer.RequiredAcks = sarama.WaitForAll
    config.Producer.Return.Successes = true
    config.Net.SASL.User = "alice"
    config.Net.SASL.Password = "alice-secret"
    config.Net.SASL.Handshake = true
    config.Net.SASL.Enable = true
    producer, err := sarama.NewSyncProducer(brokers, config)

    return producer, err
}

func prepareMessage(topic, message string) *sarama.ProducerMessage {
    msg := &sarama.ProducerMessage{
        Topic:     topic,
        Partition: -1,
        Value:     sarama.StringEncoder(message),
    }

    return msg
}

func panicOnError(err error) {
    if err != nil {
        panic(err)
    }
}

func main() {
    producer, err := newProducer()
    panicOnError(err)
    msg := prepareMessage("testtopic", `{"key":"value"}`)
    _, _, err = producer.SendMessage(msg)
    panicOnError(err)
}

我尝试了kafka-acls.sh与--bootstrap-server(命令-./kafka-acls.sh --bootstrap-server localhost:9092 --add --allow-principal User:alice --producer --topic testtopic)参数,但脚本会卡住,我可以观察到Kafka Docker日志中的身份验证错误-

[2021-05-29 16:27:46,288] INFO [SocketServer brokerId=1002] Failed authentication with /127.0.0.1 (Unexpected Kafka request of type METADATA during SASL handshake.) (org.apache.kafka.common.network.Selector)

PS:如果我只使用SASL(没有ACL),所有事情都工作正常
现在我被困在acl部分了。有人知道我错过了什么吗(可能是在zookeeper或Kafka配置中)?
任何帮助都是感激不尽的。提前感谢。

uxhixvfz

uxhixvfz1#

对于您的第一个问题,我会尝试以下建议https://github.com/Shopify/sarama/issues/272
对于第二个问题,您应该在命令行中添加--command-config/path/cmd.cfg
指示管理客户端属性以连接您的代理,如mechainsem SASL等...
Kafka_OPTS设置jaas文件
jaas文件应该包含KafkaClient,其中包含用户名和密码,以便使用PLAIN身份验证方法连接到您的代理

相关问题