Skip to content
This repository was archived by the owner on May 2, 2025. It is now read-only.

Commit 5ce0873

Browse files
committed
Add username, password and tls config options for Kafka.
1 parent d3ad3bc commit 5ce0873

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

cmd/chirpstack-application-server/cmd/configfile.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ id="{{ .ApplicationServer.ID }}"
359359
# Brokers, e.g.: localhost:9092.
360360
brokers=[{{ range $index, $broker := .ApplicationServer.Integration.Kafka.Brokers }}{{ if $index }}, {{ end }}"{{ $broker }}"{{ end }}]
361361
362+
# TLS.
363+
#
364+
# Set this to true when the Kafka client must connect using TLS to the Broker.
365+
tls={{ .ApplicationServer.Integration.TLS }}
366+
362367
# Topic for events.
363368
topic="{{ .ApplicationServer.Integration.Kafka.Topic }}"
364369
@@ -370,6 +375,12 @@ id="{{ .ApplicationServer.ID }}"
370375
# message. There is no need to parse it from the key.
371376
event_key_template="{{ .ApplicationServer.Integration.Kafka.EventKeyTemplate }}"
372377
378+
# Username (optional).
379+
username="{{ .ApplicationServer.Integration.Kafka.Username }}"
380+
381+
# Password (optional).
382+
password="{{ .ApplicationServer.Integration.Kafka.Password }}"
383+
373384
374385
# PostgreSQL database integration.
375386
[application_server.integration.postgresql]

internal/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ type IntegrationAMQPConfig struct {
221221
// IntegrationKafkaConfig holds the Kafka integration configuration.
222222
type IntegrationKafkaConfig struct {
223223
Brokers []string `mapstructure:"brokers"`
224+
TLS bool `mapstructure:"tls"`
224225
Topic string `mapstructure:"topic"`
225226
EventKeyTemplate string `mapstructure:"event_key_template"`
227+
Username string `mapstructure:"username"`
228+
Password string `mapstructure:"password"`
226229
}
227230

228231
// AzurePublishMode defines the publish-mode type.

internal/integration/kafka/kafka.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@ package kafka
33
import (
44
"bytes"
55
"context"
6+
"crypto/tls"
67
"fmt"
78
"text/template"
9+
"time"
810

911
"github.com/golang/protobuf/proto"
1012
"github.com/pkg/errors"
1113
log "github.com/sirupsen/logrus"
1214

1315
"github.com/segmentio/kafka-go"
16+
"github.com/segmentio/kafka-go/sasl/plain"
1417

1518
pb "github.com/brocaar/chirpstack-api/go/v3/as/integration"
1619
"github.com/brocaar/chirpstack-application-server/internal/config"
@@ -30,11 +33,41 @@ type Integration struct {
3033

3134
// New creates a new Kafka integration.
3235
func New(m marshaler.Type, conf config.IntegrationKafkaConfig) (*Integration, error) {
33-
w := kafka.NewWriter(kafka.WriterConfig{
36+
wc := kafka.WriterConfig{
3437
Brokers: conf.Brokers,
3538
Topic: conf.Topic,
3639
Balancer: &kafka.LeastBytes{},
37-
})
40+
41+
// Equal to kafka.DefaultDialer.
42+
// We do not want to use kafka.DefaultDialer itself, as we might modify
43+
// it below to setup SASLMechanism.
44+
Dialer: &kafka.Dialer{
45+
Timeout: 10 * time.Second,
46+
DualStack: true,
47+
},
48+
}
49+
50+
if conf.TLS {
51+
wc.Dialer.TLS = &tls.Config{}
52+
}
53+
54+
if conf.Username != "" || conf.Password != "" {
55+
fmt.Println("username", conf.Username)
56+
fmt.Println("password", conf.Password)
57+
wc.Dialer.SASLMechanism = plain.Mechanism{
58+
Username: conf.Username,
59+
Password: conf.Password,
60+
}
61+
}
62+
63+
log.WithFields(log.Fields{
64+
"brokers": conf.Brokers,
65+
"topic": conf.Topic,
66+
}).Info("integration/kafka: connecting to kafka broker(s)")
67+
68+
w := kafka.NewWriter(wc)
69+
70+
log.Info("integration/kafka: connected to kafka broker(s)")
3871

3972
kt, err := template.New("key").Parse(conf.EventKeyTemplate)
4073
if err != nil {

0 commit comments

Comments
 (0)