Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for providing SASL configuration #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions kafka/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kafka

import "github.com/Shopify/sarama"

type SASLMechanism uint8

const (
SASLMechanismPlain SASLMechanism = iota
)

func (m SASLMechanism) sarama() sarama.SASLMechanism {
switch m {
default:
fallthrough
case SASLMechanismPlain:
return sarama.SASLTypePlaintext
}
}

// SASLConfig provides a mechanism to authenticate with a Kafka cluster
// using SASL.
type SASLConfig struct {
Mechanism SASLMechanism
Username string
Password string
Version int16
}
10 changes: 10 additions & 0 deletions kafka/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type AsyncMessageSourceConfig struct {
OffsetsRetention time.Duration
SessionTimeout time.Duration
Version string
SASL *SASLConfig

Debug bool
}
Expand All @@ -58,6 +59,15 @@ func (ams *AsyncMessageSourceConfig) buildSaramaConsumerConfig() (*sarama.Config
config.Consumer.Group.Session.Timeout = st
config.Consumer.Offsets.Retention = ams.OffsetsRetention

if ams.SASL != nil {
config.Net.SASL.Enable = true
config.Net.TLS.Enable = true
config.Net.SASL.Mechanism = ams.SASL.Mechanism.sarama()
config.Net.SASL.User = ams.SASL.Username
config.Net.SASL.Password = ams.SASL.Password
config.Net.SASL.Version = ams.SASL.Version
}

if ams.Version != "" {
version, err := sarama.ParseKafkaVersion(ams.Version)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions kafka/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type AsyncMessageSinkConfig struct {
MaxMessageBytes int
KeyFunc func(substrate.Message) []byte
Version string
SASL *SASLConfig

Debug bool
}
Expand Down Expand Up @@ -154,6 +155,15 @@ func (ams *AsyncMessageSinkConfig) buildSaramaProducerConfig() (*sarama.Config,
conf.Producer.Retry.Max = 3
conf.Producer.Timeout = time.Duration(60) * time.Second

if ams.SASL != nil {
conf.Net.SASL.Enable = true
conf.Net.TLS.Enable = true
conf.Net.SASL.Mechanism = ams.SASL.Mechanism.sarama()
conf.Net.SASL.User = ams.SASL.Username
conf.Net.SASL.Password = ams.SASL.Password
conf.Net.SASL.Version = ams.SASL.Version
}

if ams.MaxMessageBytes != 0 {
if ams.MaxMessageBytes > int(sarama.MaxRequestSize) {
sarama.MaxRequestSize = int32(ams.MaxMessageBytes)
Expand Down