1
|
|
|
package credentials |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"os" |
7
|
|
|
"time" |
8
|
|
|
|
9
|
|
|
"github.com/alibabacloud-go/tea/tea" |
10
|
|
|
"github.com/aliyun/credentials-go/credentials/request" |
11
|
|
|
"github.com/aliyun/credentials-go/credentials/utils" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
// URLCredential is a kind of credential |
15
|
|
|
type URLCredential struct { |
16
|
|
|
URL string |
17
|
|
|
*credentialUpdater |
18
|
|
|
*sessionCredential |
19
|
|
|
runtime *utils.Runtime |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
type URLResponse struct { |
23
|
|
|
AccessKeyId string `json:"AccessKeyId" xml:"AccessKeyId"` |
24
|
|
|
AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"` |
25
|
|
|
SecurityToken string `json:"SecurityToken" xml:"SecurityToken"` |
26
|
|
|
Expiration string `json:"Expiration" xml:"Expiration"` |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
func newURLCredential(URL string) *URLCredential { |
30
|
|
|
credentialUpdater := new(credentialUpdater) |
31
|
|
|
if URL == "" { |
32
|
|
|
URL = os.Getenv("ALIBABA_CLOUD_CREDENTIALS_URI") |
33
|
|
|
} |
34
|
|
|
return &URLCredential{ |
35
|
|
|
URL: URL, |
36
|
|
|
credentialUpdater: credentialUpdater, |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// GetAccessKeyId reutrns URLCredential's AccessKeyId |
41
|
|
|
// if AccessKeyId is not exist or out of date, the function will update it. |
42
|
|
|
func (e *URLCredential) GetAccessKeyId() (*string, error) { |
43
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
44
|
|
|
err := e.updateCredential() |
45
|
|
|
if err != nil { |
46
|
|
|
if e.credentialExpiration > (int(time.Now().Unix()) - int(e.lastUpdateTimestamp)) { |
47
|
|
|
return &e.sessionCredential.AccessKeyId, nil |
48
|
|
|
} |
49
|
|
|
return tea.String(""), err |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return tea.String(e.sessionCredential.AccessKeyId), nil |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// GetAccessSecret reutrns URLCredential's AccessKeySecret |
56
|
|
|
// if AccessKeySecret is not exist or out of date, the function will update it. |
57
|
|
|
func (e *URLCredential) GetAccessKeySecret() (*string, error) { |
58
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
59
|
|
|
err := e.updateCredential() |
60
|
|
|
if err != nil { |
61
|
|
|
if e.credentialExpiration > (int(time.Now().Unix()) - int(e.lastUpdateTimestamp)) { |
62
|
|
|
return &e.sessionCredential.AccessKeySecret, nil |
63
|
|
|
} |
64
|
|
|
return tea.String(""), err |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return tea.String(e.sessionCredential.AccessKeySecret), nil |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// GetSecurityToken reutrns URLCredential's SecurityToken |
71
|
|
|
// if SecurityToken is not exist or out of date, the function will update it. |
72
|
|
|
func (e *URLCredential) GetSecurityToken() (*string, error) { |
73
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
74
|
|
|
err := e.updateCredential() |
75
|
|
|
if err != nil { |
76
|
|
|
if e.credentialExpiration > (int(time.Now().Unix()) - int(e.lastUpdateTimestamp)) { |
77
|
|
|
return &e.sessionCredential.SecurityToken, nil |
78
|
|
|
} |
79
|
|
|
return tea.String(""), err |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
return tea.String(e.sessionCredential.SecurityToken), nil |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// GetBearerToken is useless for URLCredential |
86
|
|
|
func (e *URLCredential) GetBearerToken() *string { |
87
|
|
|
return tea.String("") |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// GetType reutrns URLCredential's type |
91
|
|
|
func (e *URLCredential) GetType() *string { |
92
|
|
|
return tea.String("credential_uri") |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
func (e *URLCredential) updateCredential() (err error) { |
96
|
|
|
if e.runtime == nil { |
97
|
|
|
e.runtime = new(utils.Runtime) |
98
|
|
|
} |
99
|
|
|
request := request.NewCommonRequest() |
100
|
|
|
request.URL = e.URL |
101
|
|
|
request.Method = "GET" |
102
|
|
|
content, err := doAction(request, e.runtime) |
103
|
|
|
if err != nil { |
104
|
|
|
return fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
105
|
|
|
} |
106
|
|
|
var resp *URLResponse |
107
|
|
|
err = json.Unmarshal(content, &resp) |
108
|
|
|
if err != nil { |
109
|
|
|
return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error()) |
110
|
|
|
} |
111
|
|
|
if resp.AccessKeyId == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" { |
112
|
|
|
return fmt.Errorf("refresh Ecs sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyId, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration) |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration) |
116
|
|
|
e.lastUpdateTimestamp = time.Now().Unix() |
117
|
|
|
e.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
118
|
|
|
e.sessionCredential = &sessionCredential{ |
119
|
|
|
AccessKeyId: resp.AccessKeyId, |
120
|
|
|
AccessKeySecret: resp.AccessKeySecret, |
121
|
|
|
SecurityToken: resp.SecurityToken, |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return |
125
|
|
|
} |
126
|
|
|
|