-
Notifications
You must be signed in to change notification settings - Fork 0
/
kafka.go
371 lines (328 loc) · 10.6 KB
/
kafka.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package kafka
import (
_ "encoding/json"
"fmt"
"strconv"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
dc "github.com/ory/dockertest/v3/docker"
"golang.org/x/sync/errgroup"
kithelper "github.com/rudderlabs/rudder-go-kit/testhelper"
"github.com/rudderlabs/rudder-go-kit/testhelper/docker/resource"
)
type scramHashGenerator uint8
const (
scramPlainText scramHashGenerator = iota
scramSHA256
scramSHA512
kafkaClientPort = "9092"
)
type User struct {
Username, Password string
}
type Option interface {
apply(*config)
}
type withOption struct{ setup func(*config) }
func (w withOption) apply(c *config) { w.setup(c) }
type SASLConfig struct {
BrokerUser User
Users []User
CertificatePassword string
KeyStorePath, TrustStorePath string
hashType scramHashGenerator
}
type config struct {
brokers uint
saslConfig *SASLConfig
network *dc.Network
dontUseDockerHostListeners bool
customAdvertisedListener string
useSchemaRegistry bool
}
func (c *config) defaults() {
if c.brokers < 1 {
c.brokers = 1
}
}
// WithBrokers allows to set the number of brokers in the cluster
func WithBrokers(noOfBrokers uint) Option {
return withOption{setup: func(c *config) {
c.brokers = noOfBrokers
}}
}
// WithSASLPlain is used to configure SASL authentication (PLAIN)
func WithSASLPlain(conf *SASLConfig) Option {
return withSASL(scramPlainText, conf)
}
// WithSASLScramSHA256 is used to configure SASL authentication (Scram SHA-256)
func WithSASLScramSHA256(conf *SASLConfig) Option {
return withSASL(scramSHA256, conf)
}
// WithSASLScramSHA512 is used to configure SASL authentication (Scram SHA-512)
func WithSASLScramSHA512(conf *SASLConfig) Option {
return withSASL(scramSHA512, conf)
}
func withSASL(hashType scramHashGenerator, conf *SASLConfig) Option {
conf.hashType = hashType
return withOption{setup: func(c *config) {
c.saslConfig = conf
}}
}
// WithNetwork allows to set a docker network to use for the cluster
func WithNetwork(network *dc.Network) Option {
return withOption{setup: func(c *config) {
c.network = network
}}
}
// WithoutDockerHostListeners allows to not set the advertised listener to the host mapped port
func WithoutDockerHostListeners() Option {
return withOption{setup: func(c *config) {
c.dontUseDockerHostListeners = true
}}
}
// WithCustomAdvertisedListener allows to set a custom advertised listener
func WithCustomAdvertisedListener(listener string) Option {
return withOption{setup: func(c *config) {
c.customAdvertisedListener = listener
}}
}
// WithSchemaRegistry allows to use the schema registry
func WithSchemaRegistry() Option {
return withOption{setup: func(c *config) {
c.useSchemaRegistry = true
}}
}
type Resource struct {
Ports []string
SchemaRegistryURL string
pool *dockertest.Pool
containers []*dockertest.Resource
}
func (k *Resource) Destroy() error {
g := errgroup.Group{}
for i := range k.containers {
i := i
g.Go(func() error {
return k.pool.Purge(k.containers[i])
})
}
return g.Wait()
}
func Setup(pool *dockertest.Pool, cln resource.Cleaner, opts ...Option) (*Resource, error) {
// lock so no two tests can run at the same time and try to listen on the same port
var c config
for _, opt := range opts {
opt.apply(&c)
}
c.defaults()
network := c.network
if c.network == nil {
var err error
network, err = pool.Client.CreateNetwork(dc.CreateNetworkOptions{Name: "kafka_network"})
if err != nil {
return nil, fmt.Errorf("could not create docker network: %w", err)
}
cln.Cleanup(func() {
if err := pool.Client.RemoveNetwork(network.ID); err != nil {
cln.Log(fmt.Errorf("could not remove kafka network: %w", err))
}
})
}
zookeeperPortInt, err := kithelper.GetFreePort()
if err != nil {
return nil, err
}
zookeeperPort := fmt.Sprintf("%d/tcp", zookeeperPortInt)
zookeeperContainer, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "bitnami/zookeeper",
Tag: "3.9-debian-11",
NetworkID: network.ID,
Hostname: "zookeeper",
PortBindings: map[dc.Port][]dc.PortBinding{
"2181/tcp": {{HostIP: "zookeeper", HostPort: zookeeperPort}},
},
Env: []string{"ALLOW_ANONYMOUS_LOGIN=yes"},
})
if err != nil {
return nil, err
}
cln.Cleanup(func() {
if err := pool.Purge(zookeeperContainer); err != nil {
cln.Log("Could not purge resource", err)
}
})
cln.Log("Zookeeper localhost port", zookeeperContainer.GetPort("2181/tcp"))
envVariables := []string{
"KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181",
"KAFKA_CFG_INTER_BROKER_LISTENER_NAME=INTERNAL",
"ALLOW_PLAINTEXT_LISTENER=yes",
}
var schemaRegistryURL string
if c.useSchemaRegistry {
bootstrapServers := ""
for i := uint(1); i <= c.brokers; i++ {
bootstrapServers += fmt.Sprintf("PLAINTEXT://kafka%d:9090,", i)
}
src, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "bitnami/schema-registry",
Tag: "7.5-debian-11",
NetworkID: network.ID,
Hostname: "schemaregistry",
ExposedPorts: []string{"8081"},
Env: []string{
"SCHEMA_REGISTRY_DEBUG=true",
"SCHEMA_REGISTRY_KAFKA_BROKERS=" + bootstrapServers[:len(bootstrapServers)-1],
"SCHEMA_REGISTRY_ADVERTISED_HOSTNAME=schemaregistry",
"SCHEMA_REGISTRY_CLIENT_AUTHENTICATION=NONE",
},
})
if err != nil {
return nil, err
}
cln.Cleanup(func() {
if err := pool.Purge(src); err != nil {
cln.Log("Could not purge resource", err)
}
})
var srPort int
for p, bindings := range src.Container.NetworkSettings.Ports {
if p.Port() == "8081" {
srPort, err = strconv.Atoi(bindings[0].HostPort)
if err != nil {
panic(fmt.Errorf("cannot convert port to int: %w", err))
}
break
}
}
envVariables = append(envVariables, "KAFKA_SCHEMA_REGISTRY_URL=schemaregistry:8081")
schemaRegistryURL = fmt.Sprintf("http://localhost:%d", srPort)
cln.Log("Schema Registry on", schemaRegistryURL)
}
bootstrapServers := ""
for i := uint(1); i <= c.brokers; i++ {
bootstrapServers += fmt.Sprintf("kafka%d:9090,", i)
}
bootstrapServers = bootstrapServers[:len(bootstrapServers)-1] // removing trailing comma
envVariables = append(envVariables, "BOOTSTRAP_SERVERS="+bootstrapServers)
var mounts []string
if c.saslConfig != nil {
if c.saslConfig.BrokerUser.Username == "" {
return nil, fmt.Errorf("SASL broker user must be provided")
}
if len(c.saslConfig.Users) < 1 {
return nil, fmt.Errorf("SASL users must be provided")
}
if c.saslConfig.CertificatePassword == "" {
return nil, fmt.Errorf("SASL certificate password cannot be empty")
}
if c.saslConfig.KeyStorePath == "" {
return nil, fmt.Errorf("SASL keystore path cannot be empty")
}
if c.saslConfig.TrustStorePath == "" {
return nil, fmt.Errorf("SASL truststore path cannot be empty")
}
mounts = []string{
c.saslConfig.KeyStorePath + ":/opt/bitnami/kafka/config/certs/kafka.keystore.jks",
c.saslConfig.TrustStorePath + ":/opt/bitnami/kafka/config/certs/kafka.truststore.jks",
}
var users, passwords string
for _, user := range c.saslConfig.Users {
users += user.Username + ","
passwords += user.Password + ","
}
switch c.saslConfig.hashType {
case scramPlainText:
envVariables = append(envVariables,
"KAFKA_CFG_SASL_ENABLED_MECHANISMS=PLAIN",
"KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL=PLAIN",
)
case scramSHA256:
envVariables = append(envVariables,
"KAFKA_CFG_SASL_ENABLED_MECHANISMS=SCRAM-SHA-256",
"KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL=SCRAM-SHA-256",
)
case scramSHA512:
envVariables = append(envVariables,
"KAFKA_CFG_SASL_ENABLED_MECHANISMS=SCRAM-SHA-512",
"KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL=SCRAM-SHA-512",
)
default:
return nil, fmt.Errorf("scram algorithm out of the known domain")
}
envVariables = append(envVariables,
"KAFKA_CLIENT_USERS="+users[:len(users)-1], // removing trailing comma
"KAFKA_CLIENT_PASSWORDS="+passwords[:len(passwords)-1], // removing trailing comma
"KAFKA_INTER_BROKER_USER="+c.saslConfig.BrokerUser.Username,
"KAFKA_INTER_BROKER_PASSWORD="+c.saslConfig.BrokerUser.Password,
"KAFKA_CERTIFICATE_PASSWORD="+c.saslConfig.CertificatePassword,
"KAFKA_CFG_TLS_TYPE=JKS",
"KAFKA_CFG_TLS_CLIENT_AUTH=none",
"KAFKA_CFG_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM=",
"KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:SASL_SSL,CLIENT:SASL_SSL",
"KAFKA_ZOOKEEPER_TLS_VERIFY_HOSTNAME=false",
)
} else {
envVariables = append(envVariables,
"KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=INTERNAL:PLAINTEXT,CLIENT:PLAINTEXT",
)
}
containers := make([]*dockertest.Resource, c.brokers)
for i := uint(0); i < c.brokers; i++ {
i := i
localhostPortInt, err := kithelper.GetFreePort()
if err != nil {
return nil, err
}
localhostPort := fmt.Sprintf("%d/tcp", localhostPortInt)
cln.Log("Kafka broker localhost port", i+1, localhostPort)
nodeID := fmt.Sprintf("%d", i+1)
hostname := "kafka" + nodeID
nodeEnvVars := append(envVariables, []string{ // skipcq: CRT-D0001
"KAFKA_BROKER_ID=" + nodeID,
"KAFKA_CFG_LISTENERS=" + fmt.Sprintf("INTERNAL://%s:9090,CLIENT://:%s", hostname, kafkaClientPort),
}...)
if c.dontUseDockerHostListeners {
nodeEnvVars = append(nodeEnvVars, "KAFKA_CFG_ADVERTISED_LISTENERS="+fmt.Sprintf(
"INTERNAL://%s:9090,CLIENT://%s:%s", hostname, hostname, kafkaClientPort,
))
} else if c.customAdvertisedListener != "" {
nodeEnvVars = append(nodeEnvVars, "KAFKA_CFG_ADVERTISED_LISTENERS="+fmt.Sprintf(
"INTERNAL://%s:9090,CLIENT://%s", hostname, c.customAdvertisedListener,
))
} else {
nodeEnvVars = append(nodeEnvVars, "KAFKA_CFG_ADVERTISED_LISTENERS="+fmt.Sprintf(
"INTERNAL://%s:9090,CLIENT://localhost:%d", hostname, localhostPortInt,
))
}
containers[i], err = pool.RunWithOptions(&dockertest.RunOptions{
Repository: "bitnami/kafka",
Tag: "3.6.0",
NetworkID: network.ID,
Hostname: hostname,
PortBindings: map[dc.Port][]dc.PortBinding{
kafkaClientPort + "/tcp": {{HostIP: "localhost", HostPort: localhostPort}},
},
Mounts: mounts,
Env: nodeEnvVars,
})
if err != nil {
return nil, err
}
cln.Cleanup(func() {
if err := pool.Purge(containers[i]); err != nil {
cln.Log(fmt.Errorf("could not purge Kafka resource: %w", err))
}
})
}
res := &Resource{
Ports: make([]string, 0, len(containers)),
SchemaRegistryURL: schemaRegistryURL,
pool: pool,
containers: containers,
}
for i := 0; i < len(containers); i++ {
res.Ports = append(res.Ports, containers[i].GetPort(kafkaClientPort+"/tcp"))
}
return res, nil
}