forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
207 lines (178 loc) · 4.68 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
package main
import (
"bufio"
"crypto/tls"
"encoding/json"
"flag"
"fmt"
"io"
"math/rand"
"os"
"os/signal"
"os/user"
"regexp"
"strings"
"syscall"
"time"
"unicode/utf16"
"golang.org/x/crypto/ssh/terminal"
"github.com/Shopify/sarama"
)
type connectionArgs struct {
version string
tls bool
clientCert string
}
var (
invalidClientIDCharactersRegExp = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
)
func listenForInterrupt(q chan struct{}) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Kill, os.Interrupt)
sig := <-signals
fmt.Fprintf(os.Stderr, "received signal %s\n", sig)
close(q)
}
func kafkaVersion(s string) sarama.KafkaVersion {
dflt := sarama.V0_10_0_0
switch s {
case "v0.8.2.0":
return sarama.V0_8_2_0
case "v0.8.2.1":
return sarama.V0_8_2_1
case "v0.8.2.2":
return sarama.V0_8_2_2
case "v0.9.0.0":
return sarama.V0_9_0_0
case "v0.9.0.1":
return sarama.V0_9_0_1
case "v0.10.0.0":
return sarama.V0_10_0_0
case "v0.10.0.1":
return sarama.V0_10_0_1
case "v0.10.1.0":
return sarama.V0_10_1_0
case "v0.10.2.0":
return sarama.V0_10_2_0
case "":
return dflt
}
failf("unsupported kafka version %#v - supported: v0.8.2.0, v0.8.2.1, v0.8.2.2, v0.9.0.0, v0.9.0.1, v0.10.0.0, v0.10.0.1, v0.10.1.0, v0.10.2.0", s)
return dflt
}
func logClose(name string, c io.Closer) {
if err := c.Close(); err != nil {
fmt.Fprintf(os.Stderr, "failed to close %#v err=%v", name, err)
}
}
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 failf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
func readStdinLines(max int, out chan string) {
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, max), max)
for scanner.Scan() {
out <- scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "scanning input failed err=%v\n", err)
}
close(out)
}
// 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]
}
func parseConnectionFlags(flags *flag.FlagSet, args *connectionArgs) {
flags.StringVar(&args.version, "version", "", "Kafka protocol version")
flags.BoolVar(&args.tls, "tls", false, "Enable TLS")
flags.StringVar(&args.clientCert, "clientCert", "", "Path to client certificate")
}
func saramaConfig(args *connectionArgs, clientType string) *sarama.Config {
cfg := sarama.NewConfig()
cfg.Version = kafkaVersion(args.version)
usr, err := user.Current()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read current user err=%v", err)
}
cfg.ClientID = fmt.Sprintf("kt-%s-%s", clientType, sanitizeUsername(usr.Username))
if args.tls {
cfg.Net.TLS.Enable = true
}
if args.clientCert != "" {
cfg.Net.TLS.Config = makeTLSConfig(args.clientCert)
}
return cfg
}
func makeTLSConfig(path string) *tls.Config {
cert, err := tls.LoadX509KeyPair(path, path)
if err != nil {
failf("Unable to load client certificate", err)
}
return &tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
}