1
|
|
|
package credentials |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bufio" |
5
|
|
|
"errors" |
6
|
|
|
"fmt" |
7
|
|
|
"net/http" |
8
|
|
|
"net/url" |
9
|
|
|
"os" |
10
|
|
|
"strings" |
11
|
|
|
"time" |
12
|
|
|
|
13
|
|
|
"github.com/alibabacloud-go/debug/debug" |
14
|
|
|
"github.com/alibabacloud-go/tea/tea" |
15
|
|
|
"github.com/aliyun/credentials-go/credentials/request" |
16
|
|
|
"github.com/aliyun/credentials-go/credentials/response" |
17
|
|
|
"github.com/aliyun/credentials-go/credentials/utils" |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
var debuglog = debug.Init("credential") |
21
|
|
|
|
22
|
|
|
var hookParse = func(err error) error { |
23
|
|
|
return err |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Credential is an interface for getting actual credential |
27
|
|
|
type Credential interface { |
28
|
|
|
// Deprecated: GetAccessKeyId is deprecated, use GetCredential instead of. |
29
|
|
|
GetAccessKeyId() (*string, error) |
30
|
|
|
// Deprecated: GetAccessKeySecret is deprecated, use GetCredential instead of. |
31
|
|
|
GetAccessKeySecret() (*string, error) |
32
|
|
|
// Deprecated: GetSecurityToken is deprecated, use GetCredential instead of. |
33
|
|
|
GetSecurityToken() (*string, error) |
34
|
|
|
GetBearerToken() *string |
35
|
|
|
GetType() *string |
36
|
|
|
GetCredential() (*CredentialModel, error) |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Config is important when call NewCredential |
40
|
|
|
type Config struct { |
41
|
|
|
Type *string `json:"type"` |
42
|
|
|
AccessKeyId *string `json:"access_key_id"` |
43
|
|
|
AccessKeySecret *string `json:"access_key_secret"` |
44
|
|
|
OIDCProviderArn *string `json:"oidc_provider_arn"` |
45
|
|
|
OIDCTokenFilePath *string `json:"oidc_token"` |
46
|
|
|
RoleArn *string `json:"role_arn"` |
47
|
|
|
RoleSessionName *string `json:"role_session_name"` |
48
|
|
|
PublicKeyId *string `json:"public_key_id"` |
49
|
|
|
RoleName *string `json:"role_name"` |
50
|
|
|
EnableIMDSv2 *bool `json:"enable_imds_v2"` |
51
|
|
|
MetadataTokenDuration *int `json:"metadata_token_duration"` |
52
|
|
|
SessionExpiration *int `json:"session_expiration"` |
53
|
|
|
PrivateKeyFile *string `json:"private_key_file"` |
54
|
|
|
BearerToken *string `json:"bearer_token"` |
55
|
|
|
SecurityToken *string `json:"security_token"` |
56
|
|
|
RoleSessionExpiration *int `json:"role_session_expiratioon"` |
57
|
|
|
Policy *string `json:"policy"` |
58
|
|
|
Host *string `json:"host"` |
59
|
|
|
Timeout *int `json:"timeout"` |
60
|
|
|
ConnectTimeout *int `json:"connect_timeout"` |
61
|
|
|
Proxy *string `json:"proxy"` |
62
|
|
|
InAdvanceScale *float64 `json:"inAdvanceScale"` |
63
|
|
|
Url *string `json:"url"` |
64
|
|
|
STSEndpoint *string `json:"sts_endpoint"` |
65
|
|
|
ExternalId *string `json:"external_id"` |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
func (s Config) String() string { |
69
|
|
|
return tea.Prettify(s) |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
func (s Config) GoString() string { |
73
|
|
|
return s.String() |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
func (s *Config) SetAccessKeyId(v string) *Config { |
77
|
|
|
s.AccessKeyId = &v |
78
|
|
|
return s |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
func (s *Config) SetAccessKeySecret(v string) *Config { |
82
|
|
|
s.AccessKeySecret = &v |
83
|
|
|
return s |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
func (s *Config) SetSecurityToken(v string) *Config { |
87
|
|
|
s.SecurityToken = &v |
88
|
|
|
return s |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
func (s *Config) SetRoleArn(v string) *Config { |
92
|
|
|
s.RoleArn = &v |
93
|
|
|
return s |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
func (s *Config) SetRoleSessionName(v string) *Config { |
97
|
|
|
s.RoleSessionName = &v |
98
|
|
|
return s |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
func (s *Config) SetPublicKeyId(v string) *Config { |
102
|
|
|
s.PublicKeyId = &v |
103
|
|
|
return s |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
func (s *Config) SetRoleName(v string) *Config { |
107
|
|
|
s.RoleName = &v |
108
|
|
|
return s |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func (s *Config) SetEnableIMDSv2(v bool) *Config { |
112
|
|
|
s.EnableIMDSv2 = &v |
113
|
|
|
return s |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
func (s *Config) SetMetadataTokenDuration(v int) *Config { |
117
|
|
|
s.MetadataTokenDuration = &v |
118
|
|
|
return s |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
func (s *Config) SetSessionExpiration(v int) *Config { |
122
|
|
|
s.SessionExpiration = &v |
123
|
|
|
return s |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
func (s *Config) SetPrivateKeyFile(v string) *Config { |
127
|
|
|
s.PrivateKeyFile = &v |
128
|
|
|
return s |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
func (s *Config) SetBearerToken(v string) *Config { |
132
|
|
|
s.BearerToken = &v |
133
|
|
|
return s |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
func (s *Config) SetRoleSessionExpiration(v int) *Config { |
137
|
|
|
s.RoleSessionExpiration = &v |
138
|
|
|
return s |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
func (s *Config) SetPolicy(v string) *Config { |
142
|
|
|
s.Policy = &v |
143
|
|
|
return s |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
func (s *Config) SetHost(v string) *Config { |
147
|
|
|
s.Host = &v |
148
|
|
|
return s |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
func (s *Config) SetTimeout(v int) *Config { |
152
|
|
|
s.Timeout = &v |
153
|
|
|
return s |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
func (s *Config) SetConnectTimeout(v int) *Config { |
157
|
|
|
s.ConnectTimeout = &v |
158
|
|
|
return s |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
func (s *Config) SetProxy(v string) *Config { |
162
|
|
|
s.Proxy = &v |
163
|
|
|
return s |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
func (s *Config) SetType(v string) *Config { |
167
|
|
|
s.Type = &v |
168
|
|
|
return s |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
func (s *Config) SetOIDCTokenFilePath(v string) *Config { |
172
|
|
|
s.OIDCTokenFilePath = &v |
173
|
|
|
return s |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
func (s *Config) SetOIDCProviderArn(v string) *Config { |
177
|
|
|
s.OIDCProviderArn = &v |
178
|
|
|
return s |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
func (s *Config) SetURLCredential(v string) *Config { |
182
|
|
|
if v == "" { |
183
|
|
|
v = os.Getenv("ALIBABA_CLOUD_CREDENTIALS_URI") |
184
|
|
|
} |
185
|
|
|
s.Url = &v |
186
|
|
|
return s |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
func (s *Config) SetSTSEndpoint(v string) *Config { |
190
|
|
|
s.STSEndpoint = &v |
191
|
|
|
return s |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// NewCredential return a credential according to the type in config. |
195
|
|
|
// if config is nil, the function will use default provider chain to get credential. |
196
|
|
|
// please see README.md for detail. |
197
|
|
|
func NewCredential(config *Config) (credential Credential, err error) { |
198
|
|
|
if config == nil { |
199
|
|
|
config, err = defaultChain.resolve() |
200
|
|
|
if err != nil { |
201
|
|
|
return |
202
|
|
|
} |
203
|
|
|
return NewCredential(config) |
204
|
|
|
} |
205
|
|
|
switch tea.StringValue(config.Type) { |
206
|
|
|
case "credentials_uri": |
207
|
|
|
credential = newURLCredential(tea.StringValue(config.Url)) |
208
|
|
|
case "oidc_role_arn": |
209
|
|
|
err = checkoutAssumeRamoidc(config) |
210
|
|
|
if err != nil { |
211
|
|
|
return |
212
|
|
|
} |
213
|
|
|
runtime := &utils.Runtime{ |
214
|
|
|
Host: tea.StringValue(config.Host), |
215
|
|
|
Proxy: tea.StringValue(config.Proxy), |
216
|
|
|
ReadTimeout: tea.IntValue(config.Timeout), |
217
|
|
|
ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
218
|
|
|
STSEndpoint: tea.StringValue(config.STSEndpoint), |
219
|
|
|
} |
220
|
|
|
credential = newOIDCRoleArnCredential( |
221
|
|
|
tea.StringValue(config.AccessKeyId), |
222
|
|
|
tea.StringValue(config.AccessKeySecret), |
223
|
|
|
tea.StringValue(config.RoleArn), |
224
|
|
|
tea.StringValue(config.OIDCProviderArn), |
225
|
|
|
tea.StringValue(config.OIDCTokenFilePath), |
226
|
|
|
tea.StringValue(config.RoleSessionName), |
227
|
|
|
tea.StringValue(config.Policy), |
228
|
|
|
tea.IntValue(config.RoleSessionExpiration), |
229
|
|
|
runtime) |
230
|
|
|
case "access_key": |
231
|
|
|
err = checkAccessKey(config) |
232
|
|
|
if err != nil { |
233
|
|
|
return |
234
|
|
|
} |
235
|
|
|
credential = newAccessKeyCredential( |
236
|
|
|
tea.StringValue(config.AccessKeyId), |
237
|
|
|
tea.StringValue(config.AccessKeySecret)) |
238
|
|
|
case "sts": |
239
|
|
|
err = checkSTS(config) |
240
|
|
|
if err != nil { |
241
|
|
|
return |
242
|
|
|
} |
243
|
|
|
credential = newStsTokenCredential( |
244
|
|
|
tea.StringValue(config.AccessKeyId), |
245
|
|
|
tea.StringValue(config.AccessKeySecret), |
246
|
|
|
tea.StringValue(config.SecurityToken)) |
247
|
|
|
case "ecs_ram_role": |
248
|
|
|
runtime := &utils.Runtime{ |
249
|
|
|
Host: tea.StringValue(config.Host), |
250
|
|
|
Proxy: tea.StringValue(config.Proxy), |
251
|
|
|
ReadTimeout: tea.IntValue(config.Timeout), |
252
|
|
|
ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
253
|
|
|
} |
254
|
|
|
credential = newEcsRAMRoleCredentialWithEnableIMDSv2( |
255
|
|
|
tea.StringValue(config.RoleName), |
256
|
|
|
tea.BoolValue(config.EnableIMDSv2), |
257
|
|
|
tea.IntValue(config.MetadataTokenDuration), |
258
|
|
|
tea.Float64Value(config.InAdvanceScale), |
259
|
|
|
runtime) |
260
|
|
|
case "ram_role_arn": |
261
|
|
|
err = checkRAMRoleArn(config) |
262
|
|
|
if err != nil { |
263
|
|
|
return |
264
|
|
|
} |
265
|
|
|
runtime := &utils.Runtime{ |
266
|
|
|
Host: tea.StringValue(config.Host), |
267
|
|
|
Proxy: tea.StringValue(config.Proxy), |
268
|
|
|
ReadTimeout: tea.IntValue(config.Timeout), |
269
|
|
|
ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
270
|
|
|
STSEndpoint: tea.StringValue(config.STSEndpoint), |
271
|
|
|
} |
272
|
|
|
credential = newRAMRoleArnWithExternalIdCredential( |
273
|
|
|
tea.StringValue(config.AccessKeyId), |
274
|
|
|
tea.StringValue(config.AccessKeySecret), |
275
|
|
|
tea.StringValue(config.RoleArn), |
276
|
|
|
tea.StringValue(config.RoleSessionName), |
277
|
|
|
tea.StringValue(config.Policy), |
278
|
|
|
tea.IntValue(config.RoleSessionExpiration), |
279
|
|
|
tea.StringValue(config.ExternalId), |
280
|
|
|
runtime) |
281
|
|
|
case "rsa_key_pair": |
282
|
|
|
err = checkRSAKeyPair(config) |
283
|
|
|
if err != nil { |
284
|
|
|
return |
285
|
|
|
} |
286
|
|
|
file, err1 := os.Open(tea.StringValue(config.PrivateKeyFile)) |
287
|
|
|
if err1 != nil { |
288
|
|
|
err = fmt.Errorf("InvalidPath: Can not open PrivateKeyFile, err is %s", err1.Error()) |
289
|
|
|
return |
290
|
|
|
} |
291
|
|
|
defer file.Close() |
292
|
|
|
var privateKey string |
293
|
|
|
scan := bufio.NewScanner(file) |
294
|
|
|
for scan.Scan() { |
295
|
|
|
if strings.HasPrefix(scan.Text(), "----") { |
296
|
|
|
continue |
297
|
|
|
} |
298
|
|
|
privateKey += scan.Text() + "\n" |
299
|
|
|
} |
300
|
|
|
runtime := &utils.Runtime{ |
301
|
|
|
Host: tea.StringValue(config.Host), |
302
|
|
|
Proxy: tea.StringValue(config.Proxy), |
303
|
|
|
ReadTimeout: tea.IntValue(config.Timeout), |
304
|
|
|
ConnectTimeout: tea.IntValue(config.ConnectTimeout), |
305
|
|
|
STSEndpoint: tea.StringValue(config.STSEndpoint), |
306
|
|
|
} |
307
|
|
|
credential = newRsaKeyPairCredential( |
308
|
|
|
privateKey, |
309
|
|
|
tea.StringValue(config.PublicKeyId), |
310
|
|
|
tea.IntValue(config.SessionExpiration), |
311
|
|
|
runtime) |
312
|
|
|
case "bearer": |
313
|
|
|
if tea.StringValue(config.BearerToken) == "" { |
314
|
|
|
err = errors.New("BearerToken cannot be empty") |
315
|
|
|
return |
316
|
|
|
} |
317
|
|
|
credential = newBearerTokenCredential(tea.StringValue(config.BearerToken)) |
318
|
|
|
default: |
319
|
|
|
err = errors.New("invalid type option, support: access_key, sts, ecs_ram_role, ram_role_arn, rsa_key_pair") |
320
|
|
|
return |
321
|
|
|
} |
322
|
|
|
return credential, nil |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
func checkRSAKeyPair(config *Config) (err error) { |
326
|
|
|
if tea.StringValue(config.PrivateKeyFile) == "" { |
327
|
|
|
err = errors.New("PrivateKeyFile cannot be empty") |
328
|
|
|
return |
329
|
|
|
} |
330
|
|
|
if tea.StringValue(config.PublicKeyId) == "" { |
331
|
|
|
err = errors.New("PublicKeyId cannot be empty") |
332
|
|
|
return |
333
|
|
|
} |
334
|
|
|
return |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
func checkoutAssumeRamoidc(config *Config) (err error) { |
338
|
|
|
if tea.StringValue(config.RoleArn) == "" { |
339
|
|
|
err = errors.New("RoleArn cannot be empty") |
340
|
|
|
return |
341
|
|
|
} |
342
|
|
|
if tea.StringValue(config.OIDCProviderArn) == "" { |
343
|
|
|
err = errors.New("OIDCProviderArn cannot be empty") |
344
|
|
|
return |
345
|
|
|
} |
346
|
|
|
return |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
func checkRAMRoleArn(config *Config) (err error) { |
350
|
|
|
if tea.StringValue(config.AccessKeyId) == "" { |
351
|
|
|
err = errors.New("AccessKeyId cannot be empty") |
352
|
|
|
return |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if tea.StringValue(config.AccessKeySecret) == "" { |
356
|
|
|
err = errors.New("AccessKeySecret cannot be empty") |
357
|
|
|
return |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
if tea.StringValue(config.RoleArn) == "" { |
361
|
|
|
err = errors.New("RoleArn cannot be empty") |
362
|
|
|
return |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
if tea.StringValue(config.RoleSessionName) == "" { |
366
|
|
|
err = errors.New("RoleSessionName cannot be empty") |
367
|
|
|
return |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
func checkSTS(config *Config) (err error) { |
374
|
|
|
if tea.StringValue(config.AccessKeyId) == "" { |
375
|
|
|
err = errors.New("AccessKeyId cannot be empty") |
376
|
|
|
return |
377
|
|
|
} |
378
|
|
|
if tea.StringValue(config.AccessKeySecret) == "" { |
379
|
|
|
err = errors.New("AccessKeySecret cannot be empty") |
380
|
|
|
return |
381
|
|
|
} |
382
|
|
|
if tea.StringValue(config.SecurityToken) == "" { |
383
|
|
|
err = errors.New("SecurityToken cannot be empty") |
384
|
|
|
return |
385
|
|
|
} |
386
|
|
|
return |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
func checkAccessKey(config *Config) (err error) { |
390
|
|
|
if tea.StringValue(config.AccessKeyId) == "" { |
391
|
|
|
err = errors.New("AccessKeyId cannot be empty") |
392
|
|
|
return |
393
|
|
|
} |
394
|
|
|
if tea.StringValue(config.AccessKeySecret) == "" { |
395
|
|
|
err = errors.New("AccessKeySecret cannot be empty") |
396
|
|
|
return |
397
|
|
|
} |
398
|
|
|
return |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
func doAction(request *request.CommonRequest, runtime *utils.Runtime) (content []byte, err error) { |
402
|
|
|
var urlEncoded string |
403
|
|
|
if request.BodyParams != nil { |
404
|
|
|
urlEncoded = utils.GetURLFormedMap(request.BodyParams) |
405
|
|
|
} |
406
|
|
|
httpRequest, err := http.NewRequest(request.Method, request.URL, strings.NewReader(urlEncoded)) |
407
|
|
|
if err != nil { |
408
|
|
|
return |
409
|
|
|
} |
410
|
|
|
httpRequest.Proto = "HTTP/1.1" |
411
|
|
|
httpRequest.Host = request.Domain |
412
|
|
|
debuglog("> %s %s %s", httpRequest.Method, httpRequest.URL.RequestURI(), httpRequest.Proto) |
413
|
|
|
debuglog("> Host: %s", httpRequest.Host) |
414
|
|
|
for key, value := range request.Headers { |
415
|
|
|
if value != "" { |
416
|
|
|
debuglog("> %s: %s", key, value) |
417
|
|
|
httpRequest.Header[key] = []string{value} |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
debuglog(">") |
421
|
|
|
httpClient := &http.Client{} |
422
|
|
|
httpClient.Timeout = time.Duration(runtime.ReadTimeout) * time.Second |
423
|
|
|
proxy := &url.URL{} |
424
|
|
|
if runtime.Proxy != "" { |
425
|
|
|
proxy, err = url.Parse(runtime.Proxy) |
426
|
|
|
if err != nil { |
427
|
|
|
return |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
trans := &http.Transport{} |
431
|
|
|
if proxy != nil && runtime.Proxy != "" { |
432
|
|
|
trans.Proxy = http.ProxyURL(proxy) |
433
|
|
|
} |
434
|
|
|
trans.DialContext = utils.Timeout(time.Duration(runtime.ConnectTimeout) * time.Second) |
435
|
|
|
httpClient.Transport = trans |
436
|
|
|
httpResponse, err := hookDo(httpClient.Do)(httpRequest) |
437
|
|
|
if err != nil { |
438
|
|
|
return |
439
|
|
|
} |
440
|
|
|
debuglog("< %s %s", httpResponse.Proto, httpResponse.Status) |
441
|
|
|
for key, value := range httpResponse.Header { |
442
|
|
|
debuglog("< %s: %v", key, strings.Join(value, "")) |
443
|
|
|
} |
444
|
|
|
debuglog("<") |
445
|
|
|
|
446
|
|
|
resp := &response.CommonResponse{} |
447
|
|
|
err = hookParse(resp.ParseFromHTTPResponse(httpResponse)) |
448
|
|
|
if err != nil { |
449
|
|
|
return |
450
|
|
|
} |
451
|
|
|
debuglog("%s", resp.GetHTTPContentString()) |
452
|
|
|
if resp.GetHTTPStatus() != http.StatusOK { |
453
|
|
|
err = fmt.Errorf("httpStatus: %d, message = %s", resp.GetHTTPStatus(), resp.GetHTTPContentString()) |
454
|
|
|
return |
455
|
|
|
} |
456
|
|
|
return resp.GetHTTPContentBytes(), nil |
457
|
|
|
} |
458
|
|
|
|