-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathutil.go
324 lines (261 loc) · 7.09 KB
/
util.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package util
import (
"bytes"
"compress/gzip"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"strconv"
"strings"
"time"
"github.com/batchcorp/plumber-schemas/build/go/protos/records"
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func DurationSec(durationSec interface{}) time.Duration {
if v, ok := durationSec.(int32); ok {
return time.Duration(v) * time.Second
}
if v, ok := durationSec.(uint32); ok {
return time.Duration(v) * time.Second
}
if v, ok := durationSec.(int64); ok {
return time.Duration(v) * time.Second
}
if v, ok := durationSec.(int); ok {
return time.Duration(v) * time.Second
}
return 0
}
// https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
var charRunes = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func RandomString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = charRunes[rand.Intn(len(charRunes))]
}
return string(b)
}
// Gunzip decompresses a slice of bytes and returns a slice of decompressed
// bytes or an error.
func Gunzip(data []byte) ([]byte, error) {
b := bytes.NewBuffer(data)
var r io.Reader
r, err := gzip.NewReader(b)
if err != nil {
return nil, errors.Wrap(err, "unable to create new reader")
}
var resB bytes.Buffer
if _, err := resB.ReadFrom(r); err != nil {
return nil, errors.Wrap(err, "unable to read data from reader")
}
return resB.Bytes(), nil
}
func DirsExist(dirs []string) error {
var errs []string
for _, dir := range dirs {
if _, err := os.Stat(dir); os.IsNotExist(err) {
errs = append(errs, fmt.Sprintf("'%s' does not exist", dir))
}
}
if errs == nil {
return nil
}
return errors.New(strings.Join(errs, "; "))
}
// WriteError is a wrapper for logging an error + writing to an error channel.
// Both the logger and error channel can be nil.
func WriteError(l *logrus.Entry, errorCh chan<- *records.ErrorRecord, err error) {
if l != nil {
l.Error(err)
}
if errorCh != nil {
errorCh <- &records.ErrorRecord{
OccurredAtUnixTsUtc: time.Now().UTC().UnixNano(),
Error: err.Error(),
}
}
}
func MapInterfaceToString(input map[string]interface{}) map[string]string {
out := make(map[string]string)
for k, v := range input {
out[k] = fmt.Sprintf("%v", v)
}
return out
}
func DerefTime(t *time.Time) int64 {
if t == nil {
return 0
}
return t.UTC().Unix()
}
func DerefUint32(v *uint32) uint32 {
if v == nil {
return 0
}
return *v
}
func DerefString(s *string) string {
if s == nil {
return ""
}
return *s
}
func DerefInt64(v *int64) int64 {
if v == nil {
return 0
}
return *v
}
func DerefInt16(v *int16) int16 {
if v == nil {
return 0
}
return *v
}
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func GenerateTLSConfig(caCert, clientCert, clientKey string, skipVerify bool, mTLS tls.ClientAuthType) (*tls.Config, error) {
certpool := x509.NewCertPool()
if len(caCert) > 0 {
if FileExists(caCert) {
// CLI input, read from file
pemCerts, err := ioutil.ReadFile(caCert)
if err == nil {
certpool.AppendCertsFromPEM(pemCerts)
}
} else {
// Server input, contents of the certificate
certpool.AppendCertsFromPEM([]byte(caCert))
}
}
// Import client certificate/key pair
var cert tls.Certificate
var err error
if len(clientCert) > 0 && len(clientKey) > 0 {
if FileExists(clientCert) {
// CLI input, read from file
cert, err = tls.LoadX509KeyPair(clientCert, clientKey)
if err != nil {
return nil, errors.Wrap(err, "unable to load client certificate")
}
} else {
// Server input, contents of the certificate
cert, err = tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
return nil, errors.Wrap(err, "unable to load client certificate")
}
}
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
if err != nil {
return nil, errors.Wrap(err, "unable to parse certificate")
}
}
// Create tls.Config with desired tls properties
return &tls.Config{
RootCAs: certpool,
ClientAuth: mTLS,
ClientCAs: nil,
InsecureSkipVerify: skipVerify,
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12,
}, nil
}
// GenerateNATSAuthJWT accepts either a path to a .creds file containing JWT credentials or the credentials
// directly in string format, and returns the correct nats connection authentication options
func GenerateNATSAuthJWT(creds string) ([]nats.Option, error) {
opts := make([]nats.Option, 0)
// Creds as a file
if FileExists(creds) {
return append(opts, nats.UserCredentials(creds)), nil
}
// Creds as string
userCB, err := nkeys.ParseDecoratedJWT([]byte(creds))
if err != nil {
return nil, errors.Wrap(err, "unable to parse user credentials")
}
sigCB := func(nonce []byte) ([]byte, error) {
kp, err := nkeys.ParseDecoratedNKey([]byte(creds))
if err != nil {
return nil, errors.Wrap(err, "unable to parse nkey from credentials")
}
defer kp.Wipe()
sig, err := kp.Sign(nonce)
if err != nil {
return nil, err
}
return sig, nil
}
jwtFunc := func(o *nats.Options) error {
o.UserJWT = func() (string, error) {
return userCB, nil
}
o.SignatureCB = sigCB
return nil
}
return append(opts, jwtFunc), nil
}
// GenerateNATSAuthNKey accepts either a path to a file containing a Nkey seed, or the Nkey seed
// directly in string format, and returns the correct nats connection authentication options
func GenerateNATSAuthNKey(nkeyPath string) ([]nats.Option, error) {
opts := make([]nats.Option, 0)
// Creds as a file
if FileExists(nkeyPath) {
// CLI, pass via filepath
nkeyCreds, err := nats.NkeyOptionFromSeed(nkeyPath)
if err != nil {
return nil, errors.Wrap(err, "unable to load nkey")
}
return append(opts, nkeyCreds), nil
}
// Server conn, pass via string
kp, err := nkeys.ParseDecoratedNKey([]byte(nkeyPath))
if err != nil {
return nil, errors.Wrap(err, "unable to parse nkey data")
}
defer kp.Wipe()
pubKey, err := kp.PublicKey()
if err != nil {
return nil, errors.Wrap(err, "unable to find public key in nkey data")
}
if !nkeys.IsValidPublicUserKey(pubKey) {
return nil, fmt.Errorf("not a valid nkey user seed")
}
sigCB := func(nonce []byte) ([]byte, error) {
sig, _ := kp.Sign(nonce)
return sig, nil
}
return append(opts, nats.Nkey(pubKey, sigCB)), nil
}
// IsBase64 determines if a string is base64 encoded or not
// We store bus headers in map[string]string and binary headers get encoded as base64
// In order to replay headers properly, we need
func IsBase64(v string) bool {
decoded, err := base64.StdEncoding.DecodeString(v)
if err != nil {
return false
}
// Definitely not base64
if base64.StdEncoding.EncodeToString(decoded) != v {
return false
}
// Might be base64, some numbers will pass DecodeString() 🤦: https://go.dev/play/p/vlDi7CLw2qu
num, err := strconv.Atoi(v)
if err == nil && fmt.Sprintf("%d", num) == v {
// Input is a number, return false
return false
}
return true
}