This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
client.go
156 lines (127 loc) · 4.14 KB
/
client.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
/*
Copyright 2020 The Knative Authors
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 (
"context"
"crypto/tls"
"crypto/x509"
"time"
"github.com/Shopify/sarama"
"github.com/kelseyhightower/envconfig"
)
type AdapterSASL struct {
Enable bool `envconfig:"KAFKA_NET_SASL_ENABLE" required:"false"`
User string `envconfig:"KAFKA_NET_SASL_USER" required:"false"`
Password string `envconfig:"KAFKA_NET_SASL_PASSWORD" required:"false"`
}
type AdapterTLS struct {
Enable bool `envconfig:"KAFKA_NET_TLS_ENABLE" required:"false"`
Cert string `envconfig:"KAFKA_NET_TLS_CERT" required:"false"`
Key string `envconfig:"KAFKA_NET_TLS_KEY" required:"false"`
CACert string `envconfig:"KAFKA_NET_TLS_CA_CERT" required:"false"`
}
type AdapterNet struct {
SASL AdapterSASL
TLS AdapterTLS
}
type envConfig struct {
BootstrapServers []string `envconfig:"KAFKA_BOOTSTRAP_SERVERS" required:"true"`
Net AdapterNet
}
// NewConfig extracts the Kafka configuration from the environment.
func NewConfig(ctx context.Context) ([]string, *sarama.Config, error) {
cfg := sarama.NewConfig()
cfg.Version = sarama.V2_0_0_0
cfg.Consumer.Return.Errors = true
var env envConfig
if err := envconfig.Process("", &env); err != nil {
return nil, nil, err
}
if env.Net.SASL.Enable {
cfg.Net.SASL.Enable = true
cfg.Net.SASL.User = env.Net.SASL.User
cfg.Net.SASL.Password = env.Net.SASL.Password
}
if env.Net.TLS.Enable {
cfg.Net.TLS.Enable = true
tlsConfig, err := newTLSConfig(env.Net.TLS.Cert, env.Net.TLS.Key, env.Net.TLS.CACert)
if err != nil {
return nil, nil, err
}
cfg.Net.TLS.Config = tlsConfig
}
return env.BootstrapServers, cfg, nil
}
// NewProducer is a helper method for constructing a client for producing kafka methods.
func NewProducer(ctx context.Context) (sarama.Client, error) {
bs, cfg, err := NewConfig(ctx)
if err != nil {
return nil, err
}
cfg.Producer.Return.Successes = true
cfg.Producer.Return.Errors = true
return sarama.NewClient(bs, cfg)
}
// newTLSConfig returns a *tls.Config using the given ceClient cert, ceClient key,
// and CA certificate. If none are appropriate, a nil *tls.Config is returned.
func newTLSConfig(clientCert, clientKey, caCert string) (*tls.Config, error) {
valid := false
config := &tls.Config{}
if clientCert != "" && clientKey != "" {
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
return nil, err
}
config.Certificates = []tls.Certificate{cert}
valid = true
}
if caCert != "" {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
config.RootCAs = caCertPool
// The CN of Heroku Kafka certs do not match the hostname of the
// broker, but Go's default TLS behavior requires that they do.
config.VerifyPeerCertificate = verifyCertSkipHostname(caCertPool)
config.InsecureSkipVerify = true
valid = true
}
if !valid {
config = nil
}
return config, nil
}
// verifyCertSkipHostname verifies certificates in the same way that the
// default TLS handshake does, except it skips hostname verification. It must
// be used with InsecureSkipVerify.
func verifyCertSkipHostname(roots *x509.CertPool) func([][]byte, [][]*x509.Certificate) error {
return func(certs [][]byte, _ [][]*x509.Certificate) error {
opts := x509.VerifyOptions{
Roots: roots,
CurrentTime: time.Now(),
Intermediates: x509.NewCertPool(),
}
leaf, err := x509.ParseCertificate(certs[0])
if err != nil {
return err
}
for _, asn1Data := range certs[1:] {
cert, err := x509.ParseCertificate(asn1Data)
if err != nil {
return err
}
opts.Intermediates.AddCert(cert)
}
_, err = leaf.Verify(opts)
return err
}
}