forked from fgeller/kt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
212 lines (178 loc) · 4.48 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
package main
import (
"bufio"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"unicode/utf16"
"github.com/Shopify/sarama"
"golang.org/x/crypto/ssh/terminal"
)
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 {
if s == "" {
return sarama.V2_0_0_0
}
v, err := sarama.ParseKafkaVersion(strings.TrimPrefix(s, "v"))
if err != nil {
failf(err.Error())
}
return v
}
func parseTimeout(s string) *time.Duration {
if s == "" {
return nil
}
v, err := time.ParseDuration(s)
if err != nil {
failf(err.Error())
}
return &v
}
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 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 {
fmt.Fprintf(os.Stdout, msg+"\n", args...)
} else {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
}
os.Exit(code)
}
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]
}
// 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
}