1
|
|
|
package credentials |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"errors" |
6
|
|
|
"fmt" |
7
|
|
|
"strconv" |
8
|
|
|
"time" |
9
|
|
|
|
10
|
|
|
"github.com/alibabacloud-go/tea/tea" |
11
|
|
|
"github.com/aliyun/credentials-go/credentials/request" |
12
|
|
|
"github.com/aliyun/credentials-go/credentials/utils" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
const defaultDurationSeconds = 3600 |
16
|
|
|
|
17
|
|
|
// RAMRoleArnCredential is a kind of credentials |
18
|
|
|
type RAMRoleArnCredential struct { |
19
|
|
|
*credentialUpdater |
20
|
|
|
AccessKeyId string |
21
|
|
|
AccessKeySecret string |
22
|
|
|
RoleArn string |
23
|
|
|
RoleSessionName string |
24
|
|
|
RoleSessionExpiration int |
25
|
|
|
Policy string |
26
|
|
|
ExternalId string |
27
|
|
|
sessionCredential *sessionCredential |
28
|
|
|
runtime *utils.Runtime |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
type ramRoleArnResponse struct { |
32
|
|
|
Credentials *credentialsInResponse `json:"Credentials" xml:"Credentials"` |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
type credentialsInResponse struct { |
36
|
|
|
AccessKeyId string `json:"AccessKeyId" xml:"AccessKeyId"` |
37
|
|
|
AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"` |
38
|
|
|
SecurityToken string `json:"SecurityToken" xml:"SecurityToken"` |
39
|
|
|
Expiration string `json:"Expiration" xml:"Expiration"` |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func newRAMRoleArnCredential(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string, roleSessionExpiration int, runtime *utils.Runtime) *RAMRoleArnCredential { |
43
|
|
|
return &RAMRoleArnCredential{ |
44
|
|
|
AccessKeyId: accessKeyId, |
45
|
|
|
AccessKeySecret: accessKeySecret, |
46
|
|
|
RoleArn: roleArn, |
47
|
|
|
RoleSessionName: roleSessionName, |
48
|
|
|
RoleSessionExpiration: roleSessionExpiration, |
49
|
|
|
Policy: policy, |
50
|
|
|
credentialUpdater: new(credentialUpdater), |
51
|
|
|
runtime: runtime, |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
func newRAMRoleArnWithExternalIdCredential(accessKeyId, accessKeySecret, roleArn, roleSessionName, policy string, roleSessionExpiration int, externalId string, runtime *utils.Runtime) *RAMRoleArnCredential { |
56
|
|
|
return &RAMRoleArnCredential{ |
57
|
|
|
AccessKeyId: accessKeyId, |
58
|
|
|
AccessKeySecret: accessKeySecret, |
59
|
|
|
RoleArn: roleArn, |
60
|
|
|
RoleSessionName: roleSessionName, |
61
|
|
|
RoleSessionExpiration: roleSessionExpiration, |
62
|
|
|
Policy: policy, |
63
|
|
|
ExternalId: externalId, |
64
|
|
|
credentialUpdater: new(credentialUpdater), |
65
|
|
|
runtime: runtime, |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// GetAccessKeyId reutrns RamRoleArnCredential's AccessKeyId |
70
|
|
|
// if AccessKeyId is not exist or out of date, the function will update it. |
71
|
|
|
func (r *RAMRoleArnCredential) GetAccessKeyId() (*string, error) { |
72
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
73
|
|
|
err := r.updateCredential() |
74
|
|
|
if err != nil { |
75
|
|
|
return tea.String(""), err |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
return tea.String(r.sessionCredential.AccessKeyId), nil |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// GetAccessSecret reutrns RamRoleArnCredential's AccessKeySecret |
82
|
|
|
// if AccessKeySecret is not exist or out of date, the function will update it. |
83
|
|
|
func (r *RAMRoleArnCredential) GetAccessKeySecret() (*string, error) { |
84
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
85
|
|
|
err := r.updateCredential() |
86
|
|
|
if err != nil { |
87
|
|
|
return tea.String(""), err |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
return tea.String(r.sessionCredential.AccessKeySecret), nil |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// GetSecurityToken reutrns RamRoleArnCredential's SecurityToken |
94
|
|
|
// if SecurityToken is not exist or out of date, the function will update it. |
95
|
|
|
func (r *RAMRoleArnCredential) GetSecurityToken() (*string, error) { |
96
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
97
|
|
|
err := r.updateCredential() |
98
|
|
|
if err != nil { |
99
|
|
|
return tea.String(""), err |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return tea.String(r.sessionCredential.SecurityToken), nil |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// GetBearerToken is useless RamRoleArnCredential |
106
|
|
|
func (r *RAMRoleArnCredential) GetBearerToken() *string { |
107
|
|
|
return tea.String("") |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// GetType reutrns RamRoleArnCredential's type |
111
|
|
|
func (r *RAMRoleArnCredential) GetType() *string { |
112
|
|
|
return tea.String("ram_role_arn") |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
func (r *RAMRoleArnCredential) updateCredential() (err error) { |
116
|
|
|
if r.runtime == nil { |
117
|
|
|
r.runtime = new(utils.Runtime) |
118
|
|
|
} |
119
|
|
|
request := request.NewCommonRequest() |
120
|
|
|
request.Domain = "sts.aliyuncs.com" |
121
|
|
|
if r.runtime.STSEndpoint != "" { |
122
|
|
|
request.Domain = r.runtime.STSEndpoint |
123
|
|
|
} |
124
|
|
|
request.Scheme = "HTTPS" |
125
|
|
|
request.Method = "GET" |
126
|
|
|
request.QueryParams["AccessKeyId"] = r.AccessKeyId |
127
|
|
|
request.QueryParams["Action"] = "AssumeRole" |
128
|
|
|
request.QueryParams["Format"] = "JSON" |
129
|
|
|
if r.RoleSessionExpiration > 0 { |
130
|
|
|
if r.RoleSessionExpiration >= 900 && r.RoleSessionExpiration <= 3600 { |
131
|
|
|
request.QueryParams["DurationSeconds"] = strconv.Itoa(r.RoleSessionExpiration) |
132
|
|
|
} else { |
133
|
|
|
err = errors.New("[InvalidParam]:Assume Role session duration should be in the range of 15min - 1Hr") |
134
|
|
|
return |
135
|
|
|
} |
136
|
|
|
} else { |
137
|
|
|
request.QueryParams["DurationSeconds"] = strconv.Itoa(defaultDurationSeconds) |
138
|
|
|
} |
139
|
|
|
request.QueryParams["RoleArn"] = r.RoleArn |
140
|
|
|
if r.Policy != "" { |
141
|
|
|
request.QueryParams["Policy"] = r.Policy |
142
|
|
|
} |
143
|
|
|
if r.ExternalId != "" { |
144
|
|
|
request.QueryParams["ExternalId"] = r.ExternalId |
145
|
|
|
} |
146
|
|
|
request.QueryParams["RoleSessionName"] = r.RoleSessionName |
147
|
|
|
request.QueryParams["SignatureMethod"] = "HMAC-SHA1" |
148
|
|
|
request.QueryParams["SignatureVersion"] = "1.0" |
149
|
|
|
request.QueryParams["Version"] = "2015-04-01" |
150
|
|
|
request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
151
|
|
|
request.QueryParams["SignatureNonce"] = utils.GetUUID() |
152
|
|
|
signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&") |
153
|
|
|
request.QueryParams["Signature"] = signature |
154
|
|
|
request.Headers["Host"] = request.Domain |
155
|
|
|
request.Headers["Accept-Encoding"] = "identity" |
156
|
|
|
request.URL = request.BuildURL() |
157
|
|
|
content, err := doAction(request, r.runtime) |
158
|
|
|
if err != nil { |
159
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error()) |
160
|
|
|
} |
161
|
|
|
var resp *ramRoleArnResponse |
162
|
|
|
err = json.Unmarshal(content, &resp) |
163
|
|
|
if err != nil { |
164
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error()) |
165
|
|
|
} |
166
|
|
|
if resp == nil || resp.Credentials == nil { |
167
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty") |
168
|
|
|
} |
169
|
|
|
respCredentials := resp.Credentials |
170
|
|
|
if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
171
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
175
|
|
|
r.lastUpdateTimestamp = time.Now().Unix() |
176
|
|
|
r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
177
|
|
|
r.sessionCredential = &sessionCredential{ |
178
|
|
|
AccessKeyId: respCredentials.AccessKeyId, |
179
|
|
|
AccessKeySecret: respCredentials.AccessKeySecret, |
180
|
|
|
SecurityToken: respCredentials.SecurityToken, |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return |
184
|
|
|
} |
185
|
|
|
|