/
helper.go
649 lines (537 loc) · 17 KB
/
helper.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*
Copyright 2023 The Nuclio Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"bufio"
"bytes"
"context"
"fmt"
"math/rand"
"net/http"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"runtime"
"runtime/debug"
"strconv"
"strings"
"text/template"
"time"
"unicode/utf8"
"unsafe"
"github.com/microcosm-cc/bluemonday"
"github.com/nuclio/errors"
"github.com/nuclio/logger"
"golang.org/x/net/html"
apiresource "k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
)
var SmallLettersAndNumbers = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
var LettersAndNumbers = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
var containerHostname string
// IsFile returns true if the object @ path is a file
func IsFile(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.Mode().IsRegular()
}
// IsDir returns true if the object @ path is a dir
func IsDir(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.IsDir()
}
// FileExists returns true if the file @ path exists
func FileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
// EnsureDirExists checks if the directory exists, creates it if it doesn't
func EnsureDirExists(ctx context.Context, loggerInstance logger.Logger, dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
loggerInstance.DebugWithCtx(ctx,
"Creating directory as it does not exist",
"directory", dir)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
}
return nil
}
// StringSliceToIntSlice converts slices of strings to slices of int. e.g. ["1", "3"] -> [1, 3]
func StringSliceToIntSlice(stringSlice []string) ([]int, error) {
var result []int
for _, stringValue := range stringSlice {
var intValue int
var err error
if intValue, err = strconv.Atoi(stringValue); err != nil {
return nil, err
}
result = append(result, intValue)
}
return result, nil
}
// StringSliceContainsString returns whether the input str is in the slice
func StringSliceContainsString(slice []string, str string) bool {
for _, stringInSlice := range slice {
if stringInSlice == str {
return true
}
}
return false
}
// StringSliceContainsStringPrefix returns whether the input str has prefix
func StringSliceContainsStringPrefix(prefixes []string, str string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(str, prefix) {
return true
}
}
return false
}
// StringSliceContainsStringCaseInsensitive returns whether the input str is in the slice case-insensitive
func StringSliceContainsStringCaseInsensitive(slice []string, str string) bool {
for _, stringInSlice := range slice {
if strings.EqualFold(stringInSlice, str) {
return true
}
}
return false
}
// PrependStringToStringSlice prepends a string to a string slice
func PrependStringToStringSlice(slice []string, str string) []string {
slice = append(slice, "")
copy(slice[1:], slice)
slice[0] = str
return slice
}
// PrependStringsToStringSlice prepends multiple strings to a string slice
func PrependStringsToStringSlice(slice []string, strs ...string) []string {
totalLen := len(slice) + len(strs)
slice = append(slice, make([]string, len(strs))...)
copy(slice[len(strs):], slice)
copy(slice, strs)
return slice[:totalLen]
}
// RemoveANSIColorsFromString strips out ANSI Colors chars from string
// example: "\u001b[31mHelloWorld" -> "HelloWorld"
func RemoveANSIColorsFromString(s string) string {
ansi := "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))"
re := regexp.MustCompile(ansi)
return re.ReplaceAllString(s, "")
}
// RetryUntilSuccessful calls callback every interval for duration until it returns true
func RetryUntilSuccessful(duration time.Duration, interval time.Duration, callback func() bool) error {
return retryUntilSuccessful(duration, interval, func(int) (bool, error) {
// callback results indicate whether to retry
return !callback(), nil
})
}
// RetryUntilSuccessfulOnErrorPatterns calls callback every interval for duration as long as error pattern is matched
// the callback argument retryCounter is the number of times the callback was called
func RetryUntilSuccessfulOnErrorPatterns(
duration time.Duration,
interval time.Duration,
errorRegexPatterns []string,
callback func(retryCounter int) (string, error)) error {
return retryUntilSuccessful(duration, interval, func(retryCounter int) (bool, error) {
callbackErrorStr, err := callback(retryCounter)
if callbackErrorStr == "" {
// no error message means no retry needed
return false, err
}
// find a matching error pattern
if !MatchStringPatterns(errorRegexPatterns, callbackErrorStr) {
// no error pattern found, dont retry, bail
return false, errors.Errorf("Failed matching an error pattern for callback: %s", callbackErrorStr)
}
return true, err
})
}
// retryUntilSuccessful calls callback every interval until duration as long as it should retry
func retryUntilSuccessful(duration time.Duration,
interval time.Duration,
callback func(int) (bool, error)) error {
var lastErr error
timedOutErrorMessage := "Timed out waiting until successful"
deadline := time.Now().Add(duration)
// while we haven't passed the deadline
var retryCounter int
for !time.Now().After(deadline) {
shouldRetry, err := callback(retryCounter)
lastErr = err
if !shouldRetry {
return err
}
retryCounter++
time.Sleep(interval)
continue
}
if lastErr != nil {
// wrap last error
return errors.Wrapf(lastErr, timedOutErrorMessage)
}
// duration expired without any last error
return errors.Errorf(timedOutErrorMessage)
}
// RunningInContainer returns true if currently running in a container, false otherwise
func RunningInContainer() bool {
return FileExists("/.dockerenv")
}
// RunningContainerHostname returns the hostname (aka container id) of the running container
func RunningContainerHostname() (string, error) {
if !RunningInContainer() {
return "", errors.New("Not running in container")
}
if containerHostname != "" {
return containerHostname, nil
}
containerID, err := os.ReadFile("/etc/hostname")
if err != nil {
return "", errors.Wrap(err, "Failed to open docker daemon config file")
}
containerHostname = strings.TrimSpace(string(containerID))
return containerHostname, nil
}
func StripPrefixes(input string, prefixes []string) string {
for _, prefix := range prefixes {
if strings.HasPrefix(input, prefix) {
return strings.TrimPrefix(input, prefix)
}
}
return input
}
func StripSuffixes(input string, suffixes []string) string {
for _, suffix := range suffixes {
if strings.HasSuffix(input, suffix) {
return strings.TrimSuffix(input, suffix)
}
}
return input
}
// RemoveEmptyLines removes all empty lines from a string
func RemoveEmptyLines(input string) string {
var nonEmptyLines []string
scanner := bufio.NewScanner(strings.NewReader(input))
// iterate over input line by line. if the line is not empty, shove it to the list
for scanner.Scan() {
line := scanner.Text()
if len(line) != 0 {
nonEmptyLines = append(nonEmptyLines, line)
}
}
// join the strings with a newline between them
return strings.Join(nonEmptyLines, "\n")
}
// GenerateStringMatchVerifier generates a function that returns whether a given string matches the specified string
func GenerateStringMatchVerifier(str string) func(string) bool {
return func(toMatch string) bool {
return toMatch == str
}
}
// RemoveWindowsCarriage removes windows carriage character '\r' when it follows by '\n'
func RemoveWindowsCarriage(b []byte) []byte {
n := utf8.RuneCount(b)
for i := 0; i < n-1; i++ {
if b[i] == '\r' && b[i+1] == '\n' {
// remove \r, keep \n
b = append(b[:i], b[i+1:]...)
n--
}
}
return b
}
func FixEscapeChars(s string) string {
escapeCharsMap := map[string]string{
"\\n": "\n",
"\\t": "\t",
"\\\\": "\\",
"\\\"": "\"",
}
for oldChar, newChar := range escapeCharsMap {
s = strings.ReplaceAll(s, oldChar, newChar)
}
return s
}
func GetEnvOrDefaultString(key string, defaultValue string) string {
value := os.Getenv(key)
if value == "" {
return defaultValue
} else if value == "nil" || value == "none" {
return ""
}
return value
}
func GetEnvOrDefaultBool(key string, defaultValue bool) bool {
return strings.ToLower(GetEnvOrDefaultString(key, strconv.FormatBool(defaultValue))) == "true"
}
func GetEnvOrDefaultInt(key string, defaultValue int) int {
valueInt, err := strconv.Atoi(GetEnvOrDefaultString(key, strconv.Itoa(defaultValue)))
if err != nil {
return defaultValue
}
return valueInt
}
// IsJavaProjectDir Checks if the given @dirPath is in a java project structure
// for example if the following dir existed "/my-project/src/main/java" then IsJavaProjectDir("/my-project") -> true
func IsJavaProjectDir(dirPath string) bool {
javaProjectStructurePath := path.Join(dirPath, "src", "main", "java")
if _, err := os.Stat(javaProjectStructurePath); err != nil {
return false
}
return true
}
func RenderTemplate(text string, data map[string]interface{}) (string, error) {
templateToRender, err := template.New("t").Parse(text)
if err != nil {
return "", errors.Wrap(err, "Failed to create template")
}
return renderTemplate(templateToRender, data)
}
func RenderTemplateWithCustomDelimiters(text string,
data map[string]interface{},
leftDelimiter string,
rightDelimiter string) (string, error) {
templateToRender, err := template.New("t").
Delims(leftDelimiter, rightDelimiter).
Parse(text)
if err != nil {
return "", errors.Wrap(err, "Failed to create template with custom delimiters")
}
return renderTemplate(templateToRender, data)
}
func renderTemplate(templateToRender *template.Template, data map[string]interface{}) (string, error) {
var templateToRenderBuffer bytes.Buffer
if err := templateToRender.Execute(&templateToRenderBuffer, &data); err != nil {
return "", errors.Wrap(err, "Failed to execute template rendering")
}
return templateToRenderBuffer.String(), nil
}
func GetDurationOrInfinite(timeout *time.Duration) time.Duration {
if timeout != nil {
return *timeout
}
// essentially infinite
return 100 * 365 * 24 * time.Hour
}
func GetSourceDir() string {
// get caller filename
_, fileName, _, _ := runtime.Caller(0)
// get filename dir
dirName := path.Dir(fileName)
for {
// we determine source dir by having a `go.mod` file there
if _, err := os.Stat(filepath.Join(dirName, "go.mod")); !os.IsNotExist(err) {
return dirName
}
// if we didn't find source yet, try on parent dir
dirName = filepath.Dir(dirName)
// we're out of parents, stop here
if dirName == "/" {
return dirName
}
}
}
// Quote returns a shell-escaped version of the string s. The returned value
// is a string that can safely be used as one token in a shell command line.
func Quote(s string) string {
var specialCharPattern = regexp.MustCompile(`[^\w@%+=:,./-]`)
if len(s) == 0 {
return "''"
}
if specialCharPattern.MatchString(s) {
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
}
return s
}
func ByteSliceToString(b []byte) string {
// https://golang.org/src/strings/builder.go#L45
// effectively converts bytes to string
// !! use with caution as returned string is mutable !!
return *(*string)(unsafe.Pointer(&b))
}
func MatchStringPatterns(patterns []string, s string) bool {
for _, pattern := range patterns {
if regexp.MustCompile(pattern).MatchString(s) {
// one matching pattern is enough
return true
}
}
return false
}
func CompileImageName(registryURL string, imageName string) string {
return strings.TrimSuffix(registryURL, "/") + "/" + imageName
}
func AnyPositiveInSliceInt64(numbers []int64) bool {
for _, number := range numbers {
if number >= 0 {
return true
}
}
return false
}
func GenerateRandomString(length int, letters []rune) string {
randomString := make([]rune, length)
for i := range randomString {
randomString[i] = letters[seededRand.Intn(len(letters))]
}
return string(randomString)
}
func CatchAndLogPanicWithOptions(ctx context.Context,
loggerInstance logger.Logger,
actionName string,
options *CatchAndLogPanicOptions) error {
if err := recover(); err != nil {
callStack := debug.Stack()
LogPanic(ctx, loggerInstance, actionName, options.Args, callStack, err)
asErr := ErrorFromRecoveredError(err)
if options.CustomHandler != nil {
options.CustomHandler(asErr)
}
return asErr
}
return nil
}
// LabelsMapMatchByLabelSelector returns whether a labelsMap is matched by an encoded label selector
// corresponding to https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/
//
// Example:
//
// labelsMap:
// a: b
// c: d
// encodedLabelSelector: a=b
//
// returns true
func LabelsMapMatchByLabelSelector(labelSelector string, labelsMap map[string]string) (bool, error) {
parsedLabelSelector, err := metav1.ParseToLabelSelector(labelSelector)
if err != nil {
return false, errors.Wrap(err, "Failed to parse label selector")
}
selector, err := metav1.LabelSelectorAsSelector(parsedLabelSelector)
if err != nil {
return false, errors.Wrap(err, "Failed to get selector from label selector")
}
return selector.Matches(labels.Set(labelsMap)), nil
}
// GetRuntimeNameAndVersion return runtime name and version.
// e.g. go:1.8 -> go, 1.8
func GetRuntimeNameAndVersion(runtime string) (string, string) {
switch runtimeAndVersion := strings.Split(runtime, ":"); len(runtimeAndVersion) {
// if both are passed (e.g. python:3.9) - return them both
case 2:
return runtimeAndVersion[0], runtimeAndVersion[1]
// otherwise - return the first element (e.g. go -> go)
default:
return runtimeAndVersion[0], ""
}
}
func LogPanic(ctx context.Context,
loggerInstance logger.Logger,
actionName string,
args []interface{},
callStack []byte,
err interface{}) {
logArgs := []interface{}{
"err", err,
"stack", string(callStack),
}
if args != nil {
logArgs = append(logArgs, args...)
}
loggerInstance.ErrorWithCtx(ctx, "Panic caught while "+actionName, logArgs...)
}
func ErrorFromRecoveredError(recoveredError interface{}) error {
switch typedErr := recoveredError.(type) {
case string:
return errors.New(typedErr)
case error:
return typedErr
default:
return errors.New(fmt.Sprintf("Unknown error type: %s", reflect.TypeOf(typedErr)))
}
}
func ParseQuantityOrDefault(value string,
defaultValue string,
loggerInstance logger.Logger) apiresource.Quantity {
// no value was given, use default
if value == "" {
value = defaultValue
}
quantity, err := apiresource.ParseQuantity(value)
if err != nil {
loggerInstance.WarnWith("Failed parsing quantity, assigning default value",
"defaultValue", defaultValue,
"err", err.Error())
quantity = apiresource.MustParse(defaultValue)
}
return quantity
}
func RemoveDuplicatesFromSliceString(slice []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range slice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func RemoveStringSliceItemsFromStringSlice(slice []string, itemsToRemove []string) []string {
var list []string
for _, item := range slice {
if !StringSliceContainsString(itemsToRemove, item) {
list = append(list, item)
}
}
return list
}
// PopulateFieldsFromValues populates empty fields with values if the value is not zero.
// the fieldsToValues argument is a map of pointers to fields to populate and values.
func PopulateFieldsFromValues[T string | bool | int](fieldsToValues map[*T]T) {
var zeroValue T
for field, value := range fieldsToValues {
if *field == zeroValue && value != zeroValue {
*field = value
}
}
}
// SanitizeResponseData tries to parse byte data as html. if it succeeds, it is returned sanitized,
// otherwise the original data is returned.
// func SanitizeResponseData(data []byte) []byte {
func SanitizeResponseData(data []byte, headers http.Header) []byte {
// check if the content type contains html or javascript
contentType := headers.Get("Content-Type")
if contentType == "" || (!strings.Contains(contentType, "html") && !strings.Contains(contentType, "javascript")) {
return data
}
// attempt to parse the input data as HTML
_, err := html.Parse(bytes.NewReader(data))
if err != nil {
// if there is an error in parsing, it's not HTML, return data as is
return data
}
// the data is HTML, sanitize it
policy := bluemonday.UGCPolicy()
sanitizedData := policy.Sanitize(string(data))
return []byte(sanitizedData)
}