-
Notifications
You must be signed in to change notification settings - Fork 785
/
helpers.go
62 lines (57 loc) · 1.78 KB
/
helpers.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
package secreturl
import (
"fmt"
"regexp"
"strings"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// ReplaceURIs will replace any URIs with the given regular expression and scheme using the secret URL client
func ReplaceURIs(s string, client Client, r *regexp.Regexp, schemePrefix string) (string, error) {
if !strings.HasSuffix(schemePrefix, ":") {
return s, fmt.Errorf("the scheme prefix should end with ':' but was %s", schemePrefix)
}
var err error
answer := r.ReplaceAllStringFunc(s, func(found string) string {
// Stop once we have an error
if err == nil {
prefix, found := trimBeforePrefix(found, schemePrefix)
pathAndKey := strings.Trim(strings.TrimPrefix(found, schemePrefix), "\"")
parts := strings.Split(pathAndKey, ":")
if len(parts) != 2 {
err = errors.Errorf("cannot parse %q as path:key", pathAndKey)
return ""
}
secret, err1 := client.Read(parts[0])
if err1 != nil {
err = errors.Wrapf(err1, "reading %q from vault", parts[0])
return ""
}
v, ok := secret[parts[1]]
if !ok {
err = errors.Errorf("unable to find %q in secret at %q", parts[1], parts[0])
return ""
}
result, err1 := util.AsString(v)
if err1 != nil {
err = errors.Wrapf(err1, "converting %v to string", v)
return ""
}
return prefix + result
}
return found
})
if err != nil {
return "", errors.Wrapf(err, "replacing vault paths in %s", s)
}
return answer, nil
}
// trimBeforePrefix remove any chars before the given prefix
func trimBeforePrefix(s string, prefix string) (string, string) {
i := strings.Index(s, prefix)
return s[:i], s[i:]
}
// ToURI constructs a vault: URI for the given path and key
func ToURI(path string, key string, scheme string) string {
return fmt.Sprintf("%s:%s:%s", scheme, path, key)
}