-
Notifications
You must be signed in to change notification settings - Fork 97
/
environment.go
67 lines (54 loc) · 1.49 KB
/
environment.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
package main
import (
"fmt"
"regexp"
"strings"
"k8s.io/klog/v2"
)
const (
envLookupRegex = `^([a-zA-Z0-9]([a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)@azurekeyvault(\?([a-zA-Z_][a-zA-Z0-9_\.]*)?)?$`
)
type EnvSecret struct {
akvsName string
query string
index int
}
func parseEnvVars(envVars []string) (map[string]EnvSecret, error) {
re := regexp.MustCompile(envLookupRegex)
result := make(map[string]EnvSecret)
for index, envVar := range envVars {
parts := strings.SplitN(envVar, "=", 2)
if len(parts) != 2 {
klog.ErrorS(fmt.Errorf("error splitting env var"), "env variable not properly formatted", "env", envVar)
continue
}
name := parts[0]
value := parts[1]
match := re.FindStringSubmatch(value)
if len(match) == 0 {
klog.V(4).InfoS("env variable not an azure key vault reference", "env", envVar)
continue
}
klog.V(4).InfoS("found env var to get azure key vault secret for", "env", name)
akvsName := match[1]
klog.V(4).InfoS("azure key vault secret name found", "akvsName", akvsName)
if akvsName == "" {
err := fmt.Errorf("error extracting secret name")
klog.ErrorS(err, "env variable not properly formatted", "env", name, "value", value)
return nil, err
}
var query string
if len(match) == 5 {
klog.V(4).InfoS("found query in env var", "env", name, "value", value, "query", query)
query = match[4]
} else {
query = ""
}
result[name] = EnvSecret{
akvsName: akvsName,
query: query,
index: index,
}
}
return result, nil
}