Add option to select sha function

This commit is contained in:
Benjamin Ramser 2021-07-08 12:52:16 +02:00 committed by tidwall
parent 6dfcf896d4
commit bb2253204c
2 changed files with 14 additions and 2 deletions

View File

@ -67,6 +67,8 @@ type Endpoint struct {
Port int
TopicName string
SASL bool
SASLSHA256 bool
SASLSHA512 bool
TLS bool
CACertFile string
CertFile string
@ -426,6 +428,10 @@ func parseEndpoint(s string) (Endpoint, error) {
endpoint.Kafka.KeyFile = val[0]
case "sasl":
endpoint.Kafka.SASL, _ = strconv.ParseBool(val[0])
case "sha256":
endpoint.Kafka.SASLSHA256, _ = strconv.ParseBool(val[0])
case "sha512":
endpoint.Kafka.SASLSHA512, _ = strconv.ParseBool(val[0])
}
}
}

View File

@ -84,8 +84,14 @@ func (conn *KafkaConn) Send(msg string) error {
cfg.Net.SASL.User = os.Getenv("KAFKA_USERNAME")
cfg.Net.SASL.Password = os.Getenv("KAFKA_PASSWORD")
cfg.Net.SASL.Handshake = true
cfg.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
cfg.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
if conn.ep.Kafka.SASLSHA256 {
cfg.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA256} }
cfg.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA256
}
if conn.ep.Kafka.SASLSHA512 {
cfg.Net.SASL.SCRAMClientGeneratorFunc = func() sarama.SCRAMClient { return &XDGSCRAMClient{HashGeneratorFcn: SHA512} }
cfg.Net.SASL.Mechanism = sarama.SASLTypeSCRAMSHA512
}
}
cfg.Net.DialTimeout = time.Second