From 8a4ef1b836d9e66ce3ebfed9602b17b86e08bd5a Mon Sep 17 00:00:00 2001 From: Aldrin Date: Sun, 9 Jun 2024 21:35:54 +0530 Subject: [PATCH] fix: Masked kafka credentials getting printed in logs (#33730) issue: https://github.com/milvus-io/milvus/issues/33727 Signed-off-by: Aldrin --- .../msgstream/mqwrapper/kafka/kafka_client.go | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/pkg/mq/msgstream/mqwrapper/kafka/kafka_client.go b/pkg/mq/msgstream/mqwrapper/kafka/kafka_client.go index 2df2e8d57e..5d0cb917e4 100644 --- a/pkg/mq/msgstream/mqwrapper/kafka/kafka_client.go +++ b/pkg/mq/msgstream/mqwrapper/kafka/kafka_client.go @@ -43,15 +43,32 @@ func getBasicConfig(address string) kafka.ConfigMap { } } +func ConfigtoString(config kafka.ConfigMap) string { + configString := "[" + for key := range config { + if key == "sasl.password" || key == "sasl.username" { + configString += key + ":" + "*** " + } else { + value, _ := config.Get(key, nil) + configString += key + ":" + fmt.Sprintf("%v ", value) + } + } + if len(configString) > 1 { + configString = configString[:len(configString)-1] + } + configString += "]" + return configString +} + func NewKafkaClientInstance(address string) *kafkaClient { config := getBasicConfig(address) return NewKafkaClientInstanceWithConfigMap(config, kafka.ConfigMap{}, kafka.ConfigMap{}) } func NewKafkaClientInstanceWithConfigMap(config kafka.ConfigMap, extraConsumerConfig kafka.ConfigMap, extraProducerConfig kafka.ConfigMap) *kafkaClient { - log.Info("init kafka Config ", zap.String("commonConfig", fmt.Sprintf("+%v", config)), - zap.String("extraConsumerConfig", fmt.Sprintf("+%v", extraConsumerConfig)), - zap.String("extraProducerConfig", fmt.Sprintf("+%v", extraProducerConfig)), + log.Info("init kafka Config ", zap.String("commonConfig", ConfigtoString(config)), + zap.String("extraConsumerConfig", ConfigtoString(extraConsumerConfig)), + zap.String("extraProducerConfig", ConfigtoString(extraProducerConfig)), ) return &kafkaClient{basicConfig: config, consumerConfig: extraConsumerConfig, producerConfig: extraProducerConfig} }