-
Notifications
You must be signed in to change notification settings - Fork 73
/
common-operation.go
251 lines (205 loc) · 6.57 KB
/
common-operation.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
package operations
import (
"crypto/tls"
"crypto/x509"
"github.com/Shopify/sarama"
"github.com/deviceinsight/kafkactl/output"
"github.com/pkg/errors"
"github.com/spf13/viper"
"io/ioutil"
"os/user"
"regexp"
"strings"
)
var (
invalidClientIDCharactersRegExp = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
)
type SaslConfig struct {
Enabled bool
Username string
Password string
}
type TlsConfig struct {
Enabled bool
CA string
Cert string
CertKey string
Insecure bool
}
type ClientContext struct {
Name string
Brokers []string
Tls TlsConfig
Sasl SaslConfig
ClientID string
KafkaVersion sarama.KafkaVersion
AvroSchemaRegistry string
DefaultPartitioner string
}
func CreateClientContext() (ClientContext, error) {
var context ClientContext
context.Name = viper.GetString("current-context")
if viper.Get("contexts."+context.Name) == nil {
return context, errors.Errorf("no context with name %s found", context.Name)
}
if viper.GetString("contexts."+context.Name+".tlsCA") != "" ||
viper.GetString("contexts."+context.Name+".tlsCert") != "" ||
viper.GetString("contexts."+context.Name+".tlsCertKey") != "" ||
viper.GetString("contexts."+context.Name+".tlsInsecure") != "" {
return context, errors.Errorf("Your tls config contains fields that are not longer supported. Please update your config")
}
context.Brokers = viper.GetStringSlice("contexts." + context.Name + ".brokers")
context.Tls.Enabled = viper.GetBool("contexts." + context.Name + ".tls.enabled")
context.Tls.CA = viper.GetString("contexts." + context.Name + ".tls.ca")
context.Tls.Cert = viper.GetString("contexts." + context.Name + ".tls.cert")
context.Tls.CertKey = viper.GetString("contexts." + context.Name + ".tls.certKey")
context.Tls.Insecure = viper.GetBool("contexts." + context.Name + ".tls.insecure")
context.ClientID = viper.GetString("contexts." + context.Name + ".clientID")
if version, err := kafkaVersion(viper.GetString("contexts." + context.Name + ".kafkaVersion")); err == nil {
context.KafkaVersion = version
} else {
return context, err
}
context.AvroSchemaRegistry = viper.GetString("contexts." + context.Name + ".avro.schemaRegistry")
context.DefaultPartitioner = viper.GetString("contexts." + context.Name + ".defaultPartitioner")
context.Sasl.Enabled = viper.GetBool("contexts." + context.Name + ".sasl.enabled")
context.Sasl.Username = viper.GetString("contexts." + context.Name + ".sasl.username")
context.Sasl.Password = viper.GetString("contexts." + context.Name + ".sasl.password")
return context, nil
}
func CreateClient(context *ClientContext) (sarama.Client, error) {
config, err := CreateClientConfig(context)
if err == nil {
return sarama.NewClient(context.Brokers, config)
} else {
return nil, err
}
}
func CreateClientConfig(context *ClientContext) (*sarama.Config, error) {
var config = sarama.NewConfig()
config.Version = context.KafkaVersion
config.ClientID = getClientID(context)
if context.Tls.Enabled {
output.Debugf("TLS is enabled.")
config.Net.TLS.Enable = true
tlsConfig, err := setupTlsConfig(context.Tls)
if err != nil {
return nil, errors.Wrap(err, "failed to setup tls config")
}
config.Net.TLS.Config = tlsConfig
}
if context.Sasl.Enabled {
output.Debugf("SASL is enabled.")
config.Net.SASL.Enable = true
config.Net.SASL.User = context.Sasl.Username
config.Net.SASL.Password = context.Sasl.Password
}
return config, nil
}
func CreateClusterAdmin(context *ClientContext) (sarama.ClusterAdmin, error) {
var config = sarama.NewConfig()
config.Version = context.KafkaVersion
config.ClientID = getClientID(context)
if context.Tls.Enabled {
output.Debugf("TLS is enabled.")
config.Net.TLS.Enable = true
tlsConfig, err := setupTlsConfig(context.Tls)
if err != nil {
return nil, errors.Wrap(err, "failed to setup tls config")
}
config.Net.TLS.Config = tlsConfig
}
if context.Sasl.Enabled {
output.Debugf("SASL is enabled.")
config.Net.SASL.Enable = true
config.Net.SASL.User = context.Sasl.Username
config.Net.SASL.Password = context.Sasl.Password
}
return sarama.NewClusterAdmin(context.Brokers, config)
}
func getClientID(context *ClientContext) string {
var (
err error
usr *user.User
)
if context.ClientID != "" {
return context.ClientID
} else if usr, err = user.Current(); err != nil {
output.Warnf("Failed to read current user: %v", err)
return "kafkactl"
} else {
return "kafkactl-" + sanitizeUsername(usr.Username)
}
}
func sanitizeUsername(u string) string {
// Windows user may have format "DOMAIN|MACHINE\username", remove domain/machine if present
s := strings.Split(u, "\\")
u = s[len(s)-1]
// Windows account can contain spaces or other special characters not supported
// in client ID. Keep the bare minimum and ditch the rest.
return invalidClientIDCharactersRegExp.ReplaceAllString(u, "")
}
// setupTlsConfig takes the paths to a tls certificate, CA, and certificate key in
// a PEM format and returns a constructed tls.Config object.
func setupTlsConfig(tlsConfig TlsConfig) (*tls.Config, error) {
if !tlsConfig.Enabled {
return nil, errors.Errorf("tls should be enabled at this point")
}
if tlsConfig.Insecure {
return &tls.Config{InsecureSkipVerify: true}, nil
}
var caPool *x509.CertPool = nil
if tlsConfig.CA != "" {
caString, err := ioutil.ReadFile(tlsConfig.CA)
if err != nil {
return nil, err
}
caPool = x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caString)
if !ok {
return nil, errors.Errorf("unable to add ca at %s to certificate pool", tlsConfig.CA)
}
}
var (
clientCert tls.Certificate
err error
)
if tlsConfig.Cert != "" && tlsConfig.CertKey != "" {
clientCert, err = tls.LoadX509KeyPair(tlsConfig.Cert, tlsConfig.CertKey)
if err != nil {
return nil, err
}
}
bundle := &tls.Config{
RootCAs: caPool,
Certificates: []tls.Certificate{clientCert},
}
return bundle, nil
}
func kafkaVersion(s string) (sarama.KafkaVersion, error) {
if s == "" {
output.Debugf("Assuming kafkaVersion: %s", sarama.V2_0_0_0)
return sarama.V2_0_0_0, nil
}
v, err := sarama.ParseKafkaVersion(strings.TrimPrefix(s, "v"))
if err != nil {
return sarama.KafkaVersion{}, err
}
output.Debugf("Using kafkaVersion: %s", v)
return v, nil
}
func TopicExists(client *sarama.Client, name string) (bool, error) {
var (
err error
topics []string
)
if topics, err = (*client).Topics(); err != nil {
return false, errors.Wrap(err, "failed to read topics")
}
for _, topic := range topics {
if topic == name {
return true, nil
}
}
return false, nil
}