-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
47 lines (40 loc) · 932 Bytes
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// main ...
package main
import (
"crypto/tls"
"crypto/x509"
"os"
"path/filepath"
"github.com/IBM/sarama"
)
func loadCert(caFile string) *tls.Config {
caCert, err := os.ReadFile(filepath.Clean(caFile))
if err != nil {
panic(err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
RootCAs: caCertPool,
}
return tlsConfig
}
func setConfig(kafkaVersion sarama.KafkaVersion, cfg appConfig) *sarama.Config {
config := sarama.NewConfig()
config.Version = kafkaVersion
if cfg.tls {
config.Net.TLS.Enable = true
tlsCfg := loadCert(cfg.caCert)
config.Net.TLS.Config = tlsCfg
}
if cfg.user != "" && cfg.password != "" {
config.Net.SASL.Enable = true
config.Net.SASL.User = cfg.user
config.Net.SASL.Password = cfg.password
}
if cfg.oldest {
config.Consumer.Offsets.Initial = sarama.OffsetOldest
}
return config
}