-
Notifications
You must be signed in to change notification settings - Fork 13
/
secure.go
44 lines (35 loc) · 1.12 KB
/
secure.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
package internal
import (
"crypto/tls"
"crypto/x509"
"os"
"github.com/Trendyol/kafka-cronsumer/pkg/kafka"
"github.com/segmentio/kafka-go/sasl"
"github.com/segmentio/kafka-go/sasl/scram"
)
func NewTLSConfig(sasl kafka.SASLConfig) *tls.Config {
rootCA, err := os.ReadFile(sasl.RootCAPath)
if err != nil {
panic("Error while reading Root CA file: " + sasl.RootCAPath + " error: " + err.Error())
}
interCA, err := os.ReadFile(sasl.IntermediateCAPath)
if err != nil {
panic("Error while reading Intermediate CA file: " + sasl.IntermediateCAPath + " error: " + err.Error())
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(rootCA)
caCertPool.AppendCertsFromPEM(interCA)
return &tls.Config{
RootCAs: caCertPool,
MinVersion: tls.VersionTLS12,
}
}
// TODO: we can support `plain` authentication type
// link: https://github.com/segmentio/kafka-go#plain
func Mechanism(sasl kafka.SASLConfig) sasl.Mechanism {
mechanism, err := scram.Mechanism(scram.SHA512, sasl.Username, sasl.Password)
if err != nil {
panic("Error while creating SCRAM configuration, error: " + err.Error())
}
return mechanism
}