-
Notifications
You must be signed in to change notification settings - Fork 6
/
config_helper.go
159 lines (146 loc) · 4.69 KB
/
config_helper.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package kafka
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"github.com/jcmturner/gokrb5/v8/client"
krbconfig "github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/keytab"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/kversion"
"github.com/twmb/franz-go/pkg/sasl"
"github.com/twmb/franz-go/pkg/sasl/kerberos"
"github.com/twmb/franz-go/pkg/sasl/plain"
"github.com/twmb/franz-go/pkg/sasl/scram"
"go.uber.org/zap"
"io/ioutil"
"net"
"time"
)
// NewKgoConfig creates a new Config for the Kafka Client as exposed by the franz-go library.
// If TLS certificates can't be read an error will be returned.
func NewKgoConfig(cfg *Config, logger *zap.Logger) ([]kgo.Opt, error) {
opts := []kgo.Opt{
kgo.SeedBrokers(cfg.Brokers...),
kgo.MaxVersions(kversion.V2_6_0()),
kgo.ClientID(cfg.ClientID),
}
// Create Logger
kgoLogger := KgoZapLogger{
logger: logger.With(zap.String("source", "kafka_client")).Sugar(),
}
opts = append(opts, kgo.WithLogger(kgoLogger))
// Configure SASL
if cfg.SASL.Enabled {
// SASL Plain
if cfg.SASL.Mechanism == "PLAIN" {
mechanism := plain.Auth{
User: cfg.SASL.Username,
Pass: cfg.SASL.Password,
}.AsMechanism()
opts = append(opts, kgo.SASL(mechanism))
}
// SASL SCRAM
if cfg.SASL.Mechanism == "SCRAM-SHA-256" || cfg.SASL.Mechanism == "SCRAM-SHA-512" {
var mechanism sasl.Mechanism
scramAuth := scram.Auth{
User: cfg.SASL.Username,
Pass: cfg.SASL.Password,
}
if cfg.SASL.Mechanism == "SCRAM-SHA-256" {
mechanism = scramAuth.AsSha256Mechanism()
}
if cfg.SASL.Mechanism == "SCRAM-SHA-512" {
mechanism = scramAuth.AsSha512Mechanism()
}
opts = append(opts, kgo.SASL(mechanism))
}
// Kerberos
if cfg.SASL.Mechanism == "GSSAPI" {
kerbCfg, err := krbconfig.Load(cfg.SASL.GSSAPIConfig.KerberosConfigPath)
if err != nil {
return nil, fmt.Errorf("failed to create kerberos config from specified config filepath: %w", err)
}
var krbClient *client.Client
switch cfg.SASL.GSSAPIConfig.AuthType {
case "USER_AUTH:":
krbClient = client.NewWithPassword(
cfg.SASL.GSSAPIConfig.Username,
cfg.SASL.GSSAPIConfig.Realm,
cfg.SASL.GSSAPIConfig.Password,
kerbCfg)
case "KEYTAB_AUTH":
ktb, err := keytab.Load(cfg.SASL.GSSAPIConfig.KeyTabPath)
if err != nil {
return nil, fmt.Errorf("failed to load keytab: %w", err)
}
krbClient = client.NewWithKeytab(
cfg.SASL.GSSAPIConfig.Username,
cfg.SASL.GSSAPIConfig.Realm,
ktb,
kerbCfg)
}
kerberosMechanism := kerberos.Auth{
Client: krbClient,
Service: cfg.SASL.GSSAPIConfig.ServiceName,
PersistAfterAuth: true,
}.AsMechanism()
opts = append(opts, kgo.SASL(kerberosMechanism))
}
}
// Configure TLS
var caCertPool *x509.CertPool
if cfg.TLS.Enabled {
// Root CA
if cfg.TLS.CaFilepath != "" {
ca, err := ioutil.ReadFile(cfg.TLS.CaFilepath)
if err != nil {
return nil, err
}
caCertPool = x509.NewCertPool()
isSuccessful := caCertPool.AppendCertsFromPEM(ca)
if !isSuccessful {
logger.Warn("failed to append ca file to cert pool, is this a valid PEM format?")
}
}
// If configured load TLS cert & key - Mutual TLS
var certificates []tls.Certificate
if cfg.TLS.CertFilepath != "" && cfg.TLS.KeyFilepath != "" {
// 1. Read certificates
cert, err := ioutil.ReadFile(cfg.TLS.CertFilepath)
if err != nil {
return nil, fmt.Errorf("failed to TLS certificate: %w", err)
}
privateKey, err := ioutil.ReadFile(cfg.TLS.KeyFilepath)
if err != nil {
return nil, fmt.Errorf("failed to read TLS key: %w", err)
}
// 2. Check if private key needs to be decrypted. Decrypt it if passphrase is given, otherwise return error
pemBlock, _ := pem.Decode(privateKey)
if pemBlock == nil {
return nil, fmt.Errorf("no valid private key found")
}
if x509.IsEncryptedPEMBlock(pemBlock) {
decryptedKey, err := x509.DecryptPEMBlock(pemBlock, []byte(cfg.TLS.Passphrase))
if err != nil {
return nil, fmt.Errorf("private key is encrypted, but could not decrypt it: %s", err)
}
// If private key was encrypted we can overwrite the original contents now with the decrypted version
privateKey = pem.EncodeToMemory(&pem.Block{Type: pemBlock.Type, Bytes: decryptedKey})
}
tlsCert, err := tls.X509KeyPair(cert, privateKey)
certificates = []tls.Certificate{tlsCert}
}
tlsDialer := &tls.Dialer{
NetDialer: &net.Dialer{Timeout: 10 * time.Second},
Config: &tls.Config{
InsecureSkipVerify: cfg.TLS.InsecureSkipTLSVerify,
Certificates: certificates,
RootCAs: caCertPool,
},
}
opts = append(opts, kgo.Dialer(tlsDialer.DialContext))
}
return opts, nil
}