forked from ConduitIO/conduit-connector-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
108 lines (97 loc) · 3.04 KB
/
common.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
// Copyright © 2022 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kafka
import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/segmentio/kafka-go/sasl"
"github.com/segmentio/kafka-go/sasl/plain"
"github.com/segmentio/kafka-go/sasl/scram"
)
func newTLSConfig(clientCert, clientKey, caCert string, serverNoVerify bool) (*tls.Config, error) {
tlsConfig := &tls.Config{MinVersion: tls.VersionTLS12}
// Load client cert
err := loadClientCert(tlsConfig, clientCert, clientKey)
if err != nil {
return nil, fmt.Errorf("couldn't confligure client TLS: %w", err)
}
caCertPool, err := buildCertPool(caCert)
if err != nil {
return nil, fmt.Errorf("couldn't build root CAs pool: %w", err)
}
tlsConfig.RootCAs = caCertPool
tlsConfig.InsecureSkipVerify = serverNoVerify
return tlsConfig, err
}
// buildCertPool builds a cert pool with the given CA cert.
// The basis for it is a copy of the system's cert pool, if present.
// Otherwise, a new cert pool is created.
func buildCertPool(caCert string) (*x509.CertPool, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("couldn't get system cert pool: %w", err)
}
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
if caCert != "" {
rootCAs.AppendCertsFromPEM([]byte(caCert))
}
return rootCAs, nil
}
func loadClientCert(tlsConfig *tls.Config, clientCert string, clientKey string) error {
if clientCert == "" && clientKey == "" {
return nil
}
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
return err
}
tlsConfig.Certificates = []tls.Certificate{cert}
return nil
}
func newSASLMechanism(mechanismString, username, password string) (sasl.Mechanism, error) {
var mechanism sasl.Mechanism
switch mechanismString {
case "", "PLAIN":
mechanism = plain.Mechanism{
Username: username,
Password: password,
}
default:
m, err := newSCRAMMechanism(mechanismString, username, password)
if err != nil {
return nil, fmt.Errorf("couldn't configure SASL/SCRAM with %v due to: %w", mechanismString, err)
}
mechanism = m
}
return mechanism, nil
}
func newSCRAMMechanism(mechanism, username, password string) (sasl.Mechanism, error) {
var algo scram.Algorithm
switch mechanism {
case "SCRAM-SHA-256":
algo = scram.SHA256
case "SCRAM-SHA-512":
algo = scram.SHA512
default:
return nil, fmt.Errorf("unknown mechanism %q", mechanism)
}
m, err := scram.Mechanism(algo, username, password)
if err != nil {
return nil, fmt.Errorf("error creating %v mechanism: %w", mechanism, err)
}
return m, nil
}