Skip to content

Commit 662a3f4

Browse files
Haidlir Naqvihaidlir
Haidlir Naqvi
authored andcommitted
Add SASL-PLAINTEXT authentication method to Kafka using sarama package
1 parent 8eac910 commit 662a3f4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

docs/config.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ The Kafka configuration contains the following key
103103
|compression | none | VFLOW_KAFKA_COMPRESSION | compression codecs: gzip, snappy, lz4 |
104104
|retry-max | 2 | VFLOW_KAFKA_RETRY_MAX | the total number of times to retry |
105105
|request-size-max | 104857600 | VFLOW_KAFKA_REQUEST_SIZE_MAX | the maximum size (in bytes) of any request that will be attempted to send to Kafka |
106-
|retry-backoff | 10 | VFLOW_KAFKA_RETRY_BACKOFF | wait for leader election to occur before retrying in milliseconds |
107-
|tls-enabled | false | VFLOW_KAFKA_TLS_ENABLED | connect using TLS |
106+
|retry-backoff | 10 | VFLOW_KAFKA_RETRY_BACKOFF | wait for leader election to occur before retrying in milliseconds |
107+
|tls-enabled | false | VFLOW_KAFKA_TLS_ENABLED | connect using TLS |
108108
|tls-cert | none | VFLOW_KAFKA_TLS_CERT | certificate file for client authentication |
109109
|tls-key | none | VFLOW_KAFKA_TLS_KEY | key file for client authentication |
110110
|ca-file | none | VFLOW_KAFKA_CA_FILE | certificate authority file for TLS client authentication |
111-
|tls-skip-verify | true | VFLOW_KAFKA_TLS_SKIP_VERIFY | if true, the server's certificate will not validate |
111+
|tls-skip-verify | true | VFLOW_KAFKA_TLS_SKIP_VERIFY | if true, the server's certificate will not validate |
112+
|sasl-username | none | VFLOW_KAFKA_SASL_USERNAME | username for SASL authentication |
113+
|sasl-username | none | VFLOW_KAFKA_SASL_PASSWORD | password for SASL authentication |
112114

113115
## Example
114116
```

producer/sarama.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
"time"
3535

3636
"github.com/Shopify/sarama"
37-
"gopkg.in/yaml.v2"
37+
yaml "gopkg.in/yaml.v2"
3838
)
3939

4040
// KafkaSarama represents kafka producer
@@ -55,7 +55,9 @@ type KafkaSaramaConfig struct {
5555
TLSCertFile string `yaml:"tls-cert" env:"TLS_CERT"`
5656
TLSKeyFile string `yaml:"tls-key" env:"TLS_KEY"`
5757
CAFile string `yaml:"ca-file" env:"CA_FILE"`
58-
TLSSkipVerify bool `yaml:"tls-skip-verify" env:"TLS-SKIP-VERIFY"`
58+
TLSSkipVerify bool `yaml:"tls-skip-verify" env:"TLS_SKIP_VERIFY"`
59+
SASLUsername string `yaml:"sasl-username" env:"SASL_USERNAME"`
60+
SASLPassword string `yaml:"sasl-password" env:"SASL_PASSWORD"`
5961
}
6062

6163
func (k *KafkaSarama) setup(configFile string, logger *log.Logger) error {
@@ -109,6 +111,13 @@ func (k *KafkaSarama) setup(configFile string, logger *log.Logger) error {
109111
}
110112
}
111113

114+
// Enable SASL Auth Config if username is filled
115+
if k.config.SASLUsername != "" {
116+
config.Net.SASL.Enable = true
117+
config.Net.SASL.User = k.config.SASLUsername
118+
config.Net.SASL.Password = k.config.SASLPassword
119+
}
120+
112121
// get env config
113122
k.loadEnv("VFLOW_KAFKA")
114123

0 commit comments

Comments
 (0)