1
|
|
|
package credentials |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"encoding/json" |
5
|
|
|
"fmt" |
6
|
|
|
"io/ioutil" |
7
|
|
|
"os" |
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 defaultOIDCDurationSeconds = 3600 |
16
|
|
|
|
17
|
|
|
// OIDCCredential is a kind of credentials |
18
|
|
|
type OIDCCredential struct { |
19
|
|
|
*credentialUpdater |
20
|
|
|
AccessKeyId string |
21
|
|
|
AccessKeySecret string |
22
|
|
|
RoleArn string |
23
|
|
|
OIDCProviderArn string |
24
|
|
|
OIDCTokenFilePath string |
25
|
|
|
Policy string |
26
|
|
|
RoleSessionName string |
27
|
|
|
RoleSessionExpiration int |
28
|
|
|
sessionCredential *sessionCredential |
29
|
|
|
runtime *utils.Runtime |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
type OIDCResponse struct { |
33
|
|
|
Credentials *credentialsInResponse `json:"Credentials" xml:"Credentials"` |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
type OIDCcredentialsInResponse struct { |
37
|
|
|
AccessKeyId string `json:"AccessKeyId" xml:"AccessKeyId"` |
38
|
|
|
AccessKeySecret string `json:"AccessKeySecret" xml:"AccessKeySecret"` |
39
|
|
|
SecurityToken string `json:"SecurityToken" xml:"SecurityToken"` |
40
|
|
|
Expiration string `json:"Expiration" xml:"Expiration"` |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
func newOIDCRoleArnCredential(accessKeyId, accessKeySecret, roleArn, OIDCProviderArn, OIDCTokenFilePath, RoleSessionName, policy string, RoleSessionExpiration int, runtime *utils.Runtime) *OIDCCredential { |
44
|
|
|
return &OIDCCredential{ |
45
|
|
|
AccessKeyId: accessKeyId, |
46
|
|
|
AccessKeySecret: accessKeySecret, |
47
|
|
|
RoleArn: roleArn, |
48
|
|
|
OIDCProviderArn: OIDCProviderArn, |
49
|
|
|
OIDCTokenFilePath: OIDCTokenFilePath, |
50
|
|
|
RoleSessionName: RoleSessionName, |
51
|
|
|
Policy: policy, |
52
|
|
|
RoleSessionExpiration: RoleSessionExpiration, |
53
|
|
|
credentialUpdater: new(credentialUpdater), |
54
|
|
|
runtime: runtime, |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// GetAccessKeyId reutrns OIDCCredential's AccessKeyId |
59
|
|
|
// if AccessKeyId is not exist or out of date, the function will update it. |
60
|
|
|
func (r *OIDCCredential) GetAccessKeyId() (*string, error) { |
61
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
62
|
|
|
err := r.updateCredential() |
63
|
|
|
if err != nil { |
64
|
|
|
return tea.String(""), err |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
return tea.String(r.sessionCredential.AccessKeyId), nil |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// GetAccessSecret reutrns OIDCCredential's AccessKeySecret |
71
|
|
|
// if AccessKeySecret is not exist or out of date, the function will update it. |
72
|
|
|
func (r *OIDCCredential) GetAccessKeySecret() (*string, error) { |
73
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
74
|
|
|
err := r.updateCredential() |
75
|
|
|
if err != nil { |
76
|
|
|
return tea.String(""), err |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
return tea.String(r.sessionCredential.AccessKeySecret), nil |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// GetSecurityToken reutrns OIDCCredential's SecurityToken |
83
|
|
|
// if SecurityToken is not exist or out of date, the function will update it. |
84
|
|
|
func (r *OIDCCredential) GetSecurityToken() (*string, error) { |
85
|
|
|
if r.sessionCredential == nil || r.needUpdateCredential() { |
86
|
|
|
err := r.updateCredential() |
87
|
|
|
if err != nil { |
88
|
|
|
return tea.String(""), err |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return tea.String(r.sessionCredential.SecurityToken), nil |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// GetBearerToken is useless OIDCCredential |
95
|
|
|
func (r *OIDCCredential) GetBearerToken() *string { |
96
|
|
|
return tea.String("") |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// GetType reutrns OIDCCredential's type |
100
|
|
|
func (r *OIDCCredential) GetType() *string { |
101
|
|
|
return tea.String("oidc_role_arn") |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
func (r *OIDCCredential) GetOIDCToken(OIDCTokenFilePath string) *string { |
105
|
|
|
tokenPath := OIDCTokenFilePath |
106
|
|
|
_, err := os.Stat(tokenPath) |
107
|
|
|
if os.IsNotExist(err) { |
108
|
|
|
tokenPath = os.Getenv("ALIBABA_CLOUD_OIDC_TOKEN_FILE") |
109
|
|
|
if tokenPath == "" { |
110
|
|
|
return nil |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
byt, err := ioutil.ReadFile(tokenPath) |
114
|
|
|
if err != nil { |
115
|
|
|
return nil |
116
|
|
|
} |
117
|
|
|
return tea.String(string(byt)) |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
func (r *OIDCCredential) updateCredential() (err error) { |
121
|
|
|
if r.runtime == nil { |
122
|
|
|
r.runtime = new(utils.Runtime) |
123
|
|
|
} |
124
|
|
|
request := request.NewCommonRequest() |
125
|
|
|
request.Domain = "sts.aliyuncs.com" |
126
|
|
|
request.Scheme = "HTTPS" |
127
|
|
|
request.Method = "POST" |
128
|
|
|
request.QueryParams["Timestamp"] = utils.GetTimeInFormatISO8601() |
129
|
|
|
request.QueryParams["Action"] = "AssumeRoleWithOIDC" |
130
|
|
|
request.QueryParams["Format"] = "JSON" |
131
|
|
|
request.BodyParams["RoleArn"] = r.RoleArn |
132
|
|
|
request.BodyParams["OIDCProviderArn"] = r.OIDCProviderArn |
133
|
|
|
token := r.GetOIDCToken(r.OIDCTokenFilePath) |
134
|
|
|
request.BodyParams["OIDCToken"] = tea.StringValue(token) |
135
|
|
|
if r.Policy != "" { |
136
|
|
|
request.QueryParams["Policy"] = r.Policy |
137
|
|
|
} |
138
|
|
|
request.QueryParams["RoleSessionName"] = r.RoleSessionName |
139
|
|
|
request.QueryParams["Version"] = "2015-04-01" |
140
|
|
|
request.QueryParams["SignatureNonce"] = utils.GetUUID() |
141
|
|
|
if r.AccessKeyId != "" && r.AccessKeySecret != "" { |
142
|
|
|
signature := utils.ShaHmac1(request.BuildStringToSign(), r.AccessKeySecret+"&") |
143
|
|
|
request.QueryParams["Signature"] = signature |
144
|
|
|
request.QueryParams["AccessKeyId"] = r.AccessKeyId |
145
|
|
|
request.QueryParams["AccessKeySecret"] = r.AccessKeySecret |
146
|
|
|
} |
147
|
|
|
request.Headers["Host"] = request.Domain |
148
|
|
|
request.Headers["Accept-Encoding"] = "identity" |
149
|
|
|
request.Headers["content-type"] = "application/x-www-form-urlencoded" |
150
|
|
|
request.URL = request.BuildURL() |
151
|
|
|
content, err := doAction(request, r.runtime) |
152
|
|
|
if err != nil { |
153
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: %s", err.Error()) |
154
|
|
|
} |
155
|
|
|
var resp *OIDCResponse |
156
|
|
|
err = json.Unmarshal(content, &resp) |
157
|
|
|
if err != nil { |
158
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: Json.Unmarshal fail: %s", err.Error()) |
159
|
|
|
} |
160
|
|
|
if resp == nil || resp.Credentials == nil { |
161
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: Credentials is empty") |
162
|
|
|
} |
163
|
|
|
respCredentials := resp.Credentials |
164
|
|
|
if respCredentials.AccessKeyId == "" || respCredentials.AccessKeySecret == "" || respCredentials.SecurityToken == "" || respCredentials.Expiration == "" { |
165
|
|
|
return fmt.Errorf("refresh RoleArn sts token err: AccessKeyId: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", respCredentials.AccessKeyId, respCredentials.AccessKeySecret, respCredentials.SecurityToken, respCredentials.Expiration) |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
expirationTime, err := time.Parse("2006-01-02T15:04:05Z", respCredentials.Expiration) |
169
|
|
|
r.lastUpdateTimestamp = time.Now().Unix() |
170
|
|
|
r.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
171
|
|
|
r.sessionCredential = &sessionCredential{ |
172
|
|
|
AccessKeyId: respCredentials.AccessKeyId, |
173
|
|
|
AccessKeySecret: respCredentials.AccessKeySecret, |
174
|
|
|
SecurityToken: respCredentials.SecurityToken, |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return |
178
|
|
|
} |
179
|
|
|
|