-
Notifications
You must be signed in to change notification settings - Fork 274
/
decoder.go
145 lines (131 loc) Β· 4.49 KB
/
decoder.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
package decoder
import (
b64 "encoding/base64"
"fmt"
"os"
"strconv"
"strings"
"gopkg.in/ini.v1"
apiv1 "k8s.io/api/core/v1"
)
const (
EksacloudStackCloudConfigB64SecretKey = "EKSA_CLOUDSTACK_B64ENCODED_SECRET"
CloudStackCloudConfigB64SecretKey = "CLOUDSTACK_B64ENCODED_SECRET"
EksaCloudStackHostPathToMount = "EKSA_CLOUDSTACK_HOST_PATHS_TO_MOUNT"
defaultVerifySslValue = "true"
CloudStackGlobalAZ = "global"
APIKeyKey = "api-key"
SecretKeyKey = "secret-key"
APIUrlKey = "api-url"
VerifySslKey = "verify-ssl"
)
// ParseCloudStackCredsFromSecrets parses a list of secrets to extract out the api keys, secret keys, and urls.
func ParseCloudStackCredsFromSecrets(secrets []apiv1.Secret) (*CloudStackExecConfig, error) {
if len(secrets) == 0 {
return nil, fmt.Errorf("no secrets provided - unable to generate CloudStackExecConfig")
}
cloudstackProfiles := make([]CloudStackProfileConfig, 0, len(secrets))
for _, secret := range secrets {
apiKey, ok := secret.Data[APIKeyKey]
if !ok {
return nil, fmt.Errorf("secret %s is missing required key %s", secret.Name, APIKeyKey)
}
secretKey, ok := secret.Data[SecretKeyKey]
if !ok {
return nil, fmt.Errorf("secret %s is missing required key %s", secret.Name, SecretKeyKey)
}
apiURL, ok := secret.Data[APIUrlKey]
if !ok {
return nil, fmt.Errorf("secret %s is missing required key %s", secret.Name, APIUrlKey)
}
verifySsl, ok := secret.Data[VerifySslKey]
if !ok {
verifySsl = []byte(defaultVerifySslValue)
}
cloudstackProfiles = append(
cloudstackProfiles,
CloudStackProfileConfig{
Name: secret.Name,
ApiKey: string(apiKey),
SecretKey: string(secretKey),
ManagementUrl: string(apiURL),
VerifySsl: string(verifySsl),
},
)
}
return &CloudStackExecConfig{
Profiles: cloudstackProfiles,
}, nil
}
// ParseCloudStackCredsFromEnv parses the input b64 string into the ini object to extract out the api key, secret key, and url.
func ParseCloudStackCredsFromEnv() (*CloudStackExecConfig, error) {
cloudStackB64EncodedSecret, ok := os.LookupEnv(EksacloudStackCloudConfigB64SecretKey)
if !ok {
return nil, fmt.Errorf("%s is not set or is empty", EksacloudStackCloudConfigB64SecretKey)
}
decodedString, err := b64.StdEncoding.DecodeString(cloudStackB64EncodedSecret)
if err != nil {
return nil, fmt.Errorf("decoding value for %s with base64: %v", EksacloudStackCloudConfigB64SecretKey, err)
}
cfg, err := ini.Load(decodedString)
if err != nil {
return nil, fmt.Errorf("extracting values from %s with ini: %v", EksacloudStackCloudConfigB64SecretKey, err)
}
var cloudstackProfiles []CloudStackProfileConfig
sections := cfg.Sections()
for _, section := range sections {
if section.Name() == "DEFAULT" {
continue
}
profile, err := parseCloudStackProfileSection(section)
if err != nil {
return nil, err
}
cloudstackProfiles = append(cloudstackProfiles, *profile)
}
if len(cloudstackProfiles) == 0 {
return nil, fmt.Errorf("no instance found from %s", EksacloudStackCloudConfigB64SecretKey)
}
return &CloudStackExecConfig{
Profiles: cloudstackProfiles,
}, nil
}
func parseCloudStackProfileSection(section *ini.Section) (*CloudStackProfileConfig, error) {
apiKey, err := section.GetKey(APIKeyKey)
if err != nil {
return nil, fmt.Errorf("extracting value of 'api-key' from %s: %v", section.Name(), err)
}
secretKey, err := section.GetKey(SecretKeyKey)
if err != nil {
return nil, fmt.Errorf("extracting value of 'secret-key' from %s: %v", EksacloudStackCloudConfigB64SecretKey, err)
}
apiURL, err := section.GetKey(APIUrlKey)
if err != nil {
return nil, fmt.Errorf("extracting value of 'api-url' from %s: %v", EksacloudStackCloudConfigB64SecretKey, err)
}
verifySslValue := defaultVerifySslValue
if verifySsl, err := section.GetKey(VerifySslKey); err == nil {
verifySslValue = verifySsl.Value()
if _, err := strconv.ParseBool(verifySslValue); err != nil {
return nil, fmt.Errorf("'verify-ssl' has invalid boolean string %s: %v", verifySslValue, err)
}
}
return &CloudStackProfileConfig{
Name: strings.ToLower(section.Name()),
ApiKey: apiKey.Value(),
SecretKey: secretKey.Value(),
ManagementUrl: apiURL.Value(),
VerifySsl: verifySslValue,
}, nil
}
type CloudStackExecConfig struct {
Profiles []CloudStackProfileConfig
}
type CloudStackProfileConfig struct {
Name string
ApiKey string
SecretKey string
ManagementUrl string
VerifySsl string
Timeout string
}