-
Notifications
You must be signed in to change notification settings - Fork 3
/
alicloud.go
181 lines (147 loc) · 4.57 KB
/
alicloud.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package credential
import (
"context"
"os"
"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/pipe-fittings/v2/modconfig"
"github.com/turbot/pipe-fittings/v2/utils"
"github.com/zclconf/go-cty/cty"
)
type AlicloudCredential struct {
CredentialImpl
AccessKey *string `json:"access_key,omitempty" cty:"access_key" hcl:"access_key,optional"`
SecretKey *string `json:"secret_key,omitempty" cty:"secret_key" hcl:"secret_key,optional"`
}
func (c *AlicloudCredential) Validate() hcl.Diagnostics {
if c.AccessKey != nil && c.SecretKey == nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "access_key defined without secret_key",
Subject: &c.DeclRange,
},
}
}
if c.SecretKey != nil && c.AccessKey == nil {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "secret_key defined without access_key",
Subject: &c.DeclRange,
},
}
}
return hcl.Diagnostics{}
}
func (c *AlicloudCredential) Resolve(ctx context.Context) (Credential, error) {
// The order of precedence for the environment variable
// 1. ALIBABACLOUD_ACCESS_KEY_ID
// 2. ALICLOUD_ACCESS_KEY_ID
// 3. ALICLOUD_ACCESS_KEY
alicloudAccessKeyEnvVar := os.Getenv("ALICLOUD_ACCESS_KEY")
if os.Getenv("ALICLOUD_ACCESS_KEY_ID") != "" {
alicloudAccessKeyEnvVar = os.Getenv("ALICLOUD_ACCESS_KEY_ID")
}
if os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID") != "" {
alicloudAccessKeyEnvVar = os.Getenv("ALIBABACLOUD_ACCESS_KEY_ID")
}
// The order of precedence for the environment variable
// 1. ALIBABACLOUD_ACCESS_KEY_SECRET
// 2. ALICLOUD_ACCESS_KEY_SECRET
// 3. ALICLOUD_SECRET_KEY
alicloudSecretKeyEnvVar := os.Getenv("ALICLOUD_SECRET_KEY")
if os.Getenv("ALICLOUD_ACCESS_KEY_SECRET") != "" {
alicloudSecretKeyEnvVar = os.Getenv("ALICLOUD_ACCESS_KEY_SECRET")
}
if os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET") != "" {
alicloudSecretKeyEnvVar = os.Getenv("ALIBABACLOUD_ACCESS_KEY_SECRET")
}
// Don't modify existing credential, resolve to a new one
newCreds := &AlicloudCredential{
CredentialImpl: c.CredentialImpl,
}
if c.AccessKey == nil {
newCreds.AccessKey = &alicloudAccessKeyEnvVar
} else {
newCreds.AccessKey = c.AccessKey
}
if c.SecretKey == nil {
newCreds.SecretKey = &alicloudSecretKeyEnvVar
} else {
newCreds.SecretKey = c.SecretKey
}
return newCreds, nil
}
// in seconds
func (c *AlicloudCredential) GetTtl() int {
return -1
}
func (c *AlicloudCredential) getEnv() map[string]cty.Value {
env := map[string]cty.Value{}
// Alicloud uses 3 different environment variables
// Hence instead of configuring one, set the value to all the variables
if c.AccessKey != nil {
accessKey := cty.StringVal(*c.AccessKey)
env["ALIBABACLOUD_ACCESS_KEY_ID"] = accessKey
env["ALICLOUD_ACCESS_KEY_ID"] = accessKey
env["ALICLOUD_ACCESS_KEY"] = accessKey
}
if c.SecretKey != nil {
secretKey := cty.StringVal(*c.SecretKey)
env["ALIBABACLOUD_ACCESS_KEY_SECRET"] = secretKey
env["ALICLOUD_ACCESS_KEY_SECRET"] = secretKey
env["ALICLOUD_SECRET_KEY"] = secretKey
}
return env
}
func (c *AlicloudCredential) CtyValue() (cty.Value, error) {
ctyValue, err := modconfig.GetCtyValue(c)
if err != nil {
return cty.NilVal, err
}
valueMap := ctyValue.AsValueMap()
valueMap["env"] = cty.ObjectVal(c.getEnv())
return cty.ObjectVal(valueMap), nil
}
func (c *AlicloudCredential) Equals(otherCredential Credential) bool {
// If both pointers are nil, they are considered equal
if c == nil && helpers.IsNil(otherCredential) {
return true
}
if (c == nil && !helpers.IsNil(otherCredential)) || (c != nil && helpers.IsNil(otherCredential)) {
return false
}
other, ok := otherCredential.(*AlicloudCredential)
if !ok {
return false
}
if !utils.PtrEqual(c.AccessKey, other.AccessKey) {
return false
}
if !utils.PtrEqual(c.SecretKey, other.SecretKey) {
return false
}
return true
}
type AlicloudConnectionConfig struct {
AccessKey *string `cty:"access_key" hcl:"access_key,optional"`
IgnoreErrorCodes []string `cty:"ignore_error_codes" hcl:"ignore_error_codes,optional"`
Regions []string `cty:"regions" hcl:"regions,optional"`
SecretKey *string `cty:"secret_key" hcl:"secret_key,optional"`
}
func (c *AlicloudConnectionConfig) GetCredential(name string, shortName string) Credential {
alicloudCred := &AlicloudCredential{
CredentialImpl: CredentialImpl{
HclResourceImpl: modconfig.HclResourceImpl{
FullName: name,
ShortName: shortName,
UnqualifiedName: name,
},
Type: "alicloud",
},
AccessKey: c.AccessKey,
SecretKey: c.SecretKey,
}
return alicloudCred
}