-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_utils.go
64 lines (58 loc) · 1.67 KB
/
sign_utils.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
package httpproxy
import (
"fmt"
"strings"
"github.com/rancher/rancher/pkg/namespace"
v1 "github.com/rancher/types/apis/core/v1"
)
func getAuthData(auth string, secrets v1.SecretInterface, fields []string) (map[string]string, map[string]string, error) {
data := getRequestParams(auth)
if !requiredFieldsExist(data, fields) {
return data, nil, fmt.Errorf("required fields %s not set", fields)
}
if data["credID"] == "" {
return data, nil, nil
}
secret, err := getCredential(data["credID"], secrets)
if err != nil {
return data, nil, err
}
return data, secret, nil
}
func getRequestParams(auth string) map[string]string {
params := map[string]string{}
if auth != "" {
splitAuth := strings.Split(auth, " ")
for _, term := range splitAuth[1:] {
splitTerm := strings.SplitN(term, "=", 2)
params[splitTerm[0]] = splitTerm[1]
}
}
return params
}
func requiredFieldsExist(data map[string]string, fields []string) bool {
for _, field := range fields {
if val, ok := data[field]; !ok || val == "" {
return false
}
}
return true
}
func getCredential(credentialID string, credentials v1.SecretInterface) (map[string]string, error) {
split := strings.SplitN(credentialID, ":", 2)
if len(split) != 2 || split[0] == "" || split[1] == "" {
return nil, fmt.Errorf("invalid credential id %s", credentialID)
}
cred, err := credentials.Controller().Lister().Get(namespace.GlobalNamespace, split[1])
if err != nil {
return nil, err
}
ans := map[string]string{}
for key, val := range cred.Data {
splitKeys := strings.Split(key, "-")
if len(splitKeys) == 2 && strings.HasSuffix(splitKeys[0], "Config") {
ans[splitKeys[1]] = string(val)
}
}
return ans, nil
}