forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
67 lines (61 loc) · 1.71 KB
/
store.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 cred
import (
"encoding/base64"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/store/transform"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/rancher/pkg/api/customization/globalresource"
"github.com/rancher/types/apis/core/v1"
"strings"
)
func Wrap(store types.Store, ns v1.NamespaceInterface) types.Store {
transformStore := &transform.Store{
Store: store,
Transformer: func(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, opt *types.QueryOptions) (map[string]interface{}, error) {
if configExists(data) {
data["type"] = "cloudCredential"
if err := decodeNonPasswordFields(data); err != nil {
return nil, err
}
return data, nil
}
return nil, nil
},
}
return &globalresource.GlobalNamespaceStore{
Store: transformStore,
NamespaceInterface: ns,
}
}
func configExists(data map[string]interface{}) bool {
for key, val := range data {
if strings.HasSuffix(key, "Config") {
if convert.ToString(val) != "" {
return true
}
}
}
return false
}
func decodeNonPasswordFields(data map[string]interface{}) error {
for key, val := range data {
if strings.HasSuffix(key, "Config") {
ans := convert.ToMapInterface(val)
for field, value := range ans {
decoded, err := base64.StdEncoding.DecodeString(convert.ToString(value))
if err != nil {
return err
}
ans[field] = string(decoded)
}
}
}
return nil
}
func Validator(request *types.APIContext, schema *types.Schema, data map[string]interface{}) error {
if !configExists(data) {
return httperror.NewAPIError(httperror.MissingRequired, "a Config field must be set")
}
return nil
}