forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
292 lines (245 loc) · 6.64 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"regexp"
"strings"
"syscall"
"time"
"unicode/utf16"
"github.com/Shopify/sarama"
"golang.org/x/crypto/ssh/terminal"
)
const (
ENV_AUTH = "KT_AUTH"
ENV_ADMIN_TIMEOUT = "KT_ADMIN_TIMEOUT"
ENV_BROKERS = "KT_BROKERS"
ENV_TOPIC = "KT_TOPIC"
ENV_KAFKA_VERSION = "KT_KAFKA_VERSION"
)
var (
invalidClientIDCharactersRegExp = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
)
type command interface {
run(args []string)
}
type baseCmd struct {
verbose bool
}
func (b *baseCmd) infof(msg string, args ...interface{}) {
if b.verbose {
warnf(msg, args...)
}
}
func warnf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg, args...)
}
func outf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stdout, msg, args...)
}
func logClose(name string, c io.Closer) {
if err := c.Close(); err != nil {
warnf("failed to close %#v err=%v", name, err)
}
}
func chooseKafkaVersion(arg, env string) (sarama.KafkaVersion, error) {
switch {
case arg != "":
return sarama.ParseKafkaVersion(strings.TrimPrefix(arg, "v"))
case env != "":
return sarama.ParseKafkaVersion(strings.TrimPrefix(env, "v"))
default:
return sarama.V3_0_0_0, nil
}
}
type printContext struct {
output interface{}
done chan struct{}
}
func print(in <-chan printContext, pretty bool) {
var (
buf []byte
err error
marshal = json.Marshal
)
if pretty && terminal.IsTerminal(int(syscall.Stdout)) {
marshal = func(i interface{}) ([]byte, error) { return json.MarshalIndent(i, "", " ") }
}
for {
ctx := <-in
if buf, err = marshal(ctx.output); err != nil {
failf("failed to marshal output %#v, err=%v", ctx.output, err)
}
fmt.Println(string(buf))
close(ctx.done)
}
}
func quitf(msg string, args ...interface{}) {
exitf(0, msg, args...)
}
func failf(msg string, args ...interface{}) {
exitf(1, msg, args...)
}
func exitf(code int, msg string, args ...interface{}) {
if code == 0 {
outf(msg+"\n", args...)
} else {
warnf(msg+"\n", args...)
}
os.Exit(code)
}
// hashCode imitates the behavior of the JDK's String#hashCode method.
// https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#hashCode()
//
// As strings are encoded in utf16 on the JVM, this implementation checks wether
// s contains non-bmp runes and uses utf16 surrogate pairs for those.
func hashCode(s string) (hc int32) {
for _, r := range s {
r1, r2 := utf16.EncodeRune(r)
if r1 == 0xfffd && r1 == r2 {
hc = hc*31 + r
} else {
hc = (hc*31+r1)*31 + r2
}
}
return
}
func kafkaAbs(i int32) int32 {
switch {
case i == -2147483648: // Integer.MIN_VALUE
return 0
case i < 0:
return i * -1
default:
return i
}
}
func hashCodePartition(key string, partitions int32) int32 {
if partitions <= 0 {
return -1
}
return kafkaAbs(hashCode(key)) % partitions
}
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, "")
}
func randomString(length int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
buf := make([]byte, length)
r.Read(buf)
return fmt.Sprintf("%x", buf)[:length]
}
// setupCerts takes the paths to a tls certificate, CA, and certificate key in
// a PEM format and returns a constructed tls.Config object.
func setupCerts(certPath, caPath, keyPath string) (*tls.Config, error) {
if certPath == "" && caPath == "" && keyPath == "" {
return nil, nil
}
if certPath == "" || caPath == "" || keyPath == "" {
err := fmt.Errorf("certificate, CA and key path are required - got cert=%#v ca=%#v key=%#v", certPath, caPath, keyPath)
return nil, err
}
caString, err := ioutil.ReadFile(caPath)
if err != nil {
return nil, err
}
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caString)
if !ok {
failf("unable to add ca at %s to certificate pool", caPath)
}
clientCert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return nil, err
}
bundle := &tls.Config{
RootCAs: caPool,
Certificates: []tls.Certificate{clientCert},
}
bundle.BuildNameToCertificate()
return bundle, nil
}
type authConfig struct {
Mode string `json:"mode"`
CACert string `json:"ca-certificate"`
ClientCert string `json:"client-certificate"`
ClientCertKey string `json:"client-certificate-key"`
SASLPlainUser string `json:"sasl_plain_user"`
SASLPlainPassword string `json:"sasl_plain_password"`
}
func setupAuth(auth authConfig, saramaCfg *sarama.Config) error {
if auth.Mode == "" {
return nil
}
switch auth.Mode {
case "TLS":
return setupAuthTLS(auth, saramaCfg)
case "TLS-1way":
return setupAuthTLS1Way(auth, saramaCfg)
case "SASL":
return setupSASL(auth, saramaCfg)
default:
return fmt.Errorf("unsupport auth mode: %#v", auth.Mode)
}
}
func setupSASL(auth authConfig, saramaCfg *sarama.Config) error {
saramaCfg.Net.SASL.Enable = true
saramaCfg.Net.SASL.User = auth.SASLPlainUser
saramaCfg.Net.SASL.Password = auth.SASLPlainPassword
return nil
}
func setupAuthTLS1Way(auth authConfig, saramaCfg *sarama.Config) error {
saramaCfg.Net.TLS.Enable = true
saramaCfg.Net.TLS.Config = &tls.Config{}
return nil
}
func setupAuthTLS(auth authConfig, saramaCfg *sarama.Config) error {
if auth.CACert == "" || auth.ClientCert == "" || auth.ClientCertKey == "" {
return fmt.Errorf("client-certificate, client-certificate-key and ca-certificate are required - got auth=%#v", auth)
}
caString, err := ioutil.ReadFile(auth.CACert)
if err != nil {
return fmt.Errorf("failed to read ca-certificate err=%v", err)
}
caPool := x509.NewCertPool()
ok := caPool.AppendCertsFromPEM(caString)
if !ok {
failf("unable to add ca-certificate at %s to certificate pool", auth.CACert)
}
clientCert, err := tls.LoadX509KeyPair(auth.ClientCert, auth.ClientCertKey)
if err != nil {
return err
}
tlsCfg := &tls.Config{RootCAs: caPool, Certificates: []tls.Certificate{clientCert}}
tlsCfg.BuildNameToCertificate()
saramaCfg.Net.TLS.Enable = true
saramaCfg.Net.TLS.Config = tlsCfg
return nil
}
func readAuthFile(argFN string, envFN string, target *authConfig) {
if argFN == "" && envFN == "" {
return
}
fn := argFN
if fn == "" {
fn = envFN
}
byts, err := ioutil.ReadFile(fn)
if err != nil {
failf("failed to read auth file err=%v", err)
}
if err := json.Unmarshal(byts, target); err != nil {
failf("failed to unmarshal auth file err=%v", err)
}
}