|
1
|
|
|
package credentials |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"fmt" |
|
6
|
|
|
"time" |
|
7
|
|
|
|
|
8
|
|
|
"github.com/aliyun/credentials-go/credentials/request" |
|
9
|
|
|
"github.com/aliyun/credentials-go/credentials/utils" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
var securityCredURL = "http://100.100.100.200/latest/meta-data/ram/security-credentials/" |
|
13
|
|
|
|
|
14
|
|
|
type EcsRamRoleCredential struct { |
|
|
|
|
|
|
15
|
|
|
*credentialUpdater |
|
16
|
|
|
RoleName string |
|
17
|
|
|
sessionCredential *sessionCredential |
|
18
|
|
|
runtime *utils.Runtime |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
type EcsRamRoleResponse struct { |
|
|
|
|
|
|
22
|
|
|
Code string `json:"Code" xml:"Code"` |
|
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 newEcsRamRoleCredential(roleName string, runtime *utils.Runtime) *EcsRamRoleCredential { |
|
|
|
|
|
|
30
|
|
|
return &EcsRamRoleCredential{ |
|
31
|
|
|
RoleName: roleName, |
|
32
|
|
|
credentialUpdater: new(credentialUpdater), |
|
33
|
|
|
runtime: runtime, |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// GetAccessKeyID reutrns EcsRamRoleResponse's AccessKeyID |
|
38
|
|
|
// if AccessKeyID is not exist or out of date, the function will update it. |
|
39
|
|
|
func (e *EcsRamRoleCredential) GetAccessKeyID() (string, error) { |
|
40
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
|
41
|
|
|
err := e.updateCredential() |
|
42
|
|
|
if err != nil { |
|
43
|
|
|
return "", err |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
return e.sessionCredential.AccessKeyID, nil |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// GetAccessSecret reutrns EcsRamRoleResponse's AccessKeySecret |
|
50
|
|
|
// if AccessKeySecret is not exist or out of date, the function will update it. |
|
51
|
|
|
func (e *EcsRamRoleCredential) GetAccessSecret() (string, error) { |
|
52
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
|
53
|
|
|
err := e.updateCredential() |
|
54
|
|
|
if err != nil { |
|
55
|
|
|
return "", err |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
return e.sessionCredential.AccessKeySecret, nil |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
// GetSecurityToken reutrns EcsRamRoleResponse's SecurityToken |
|
62
|
|
|
// if SecurityToken is not exist or out of date, the function will update it. |
|
63
|
|
|
func (e *EcsRamRoleCredential) GetSecurityToken() (string, error) { |
|
64
|
|
|
if e.sessionCredential == nil || e.needUpdateCredential() { |
|
65
|
|
|
err := e.updateCredential() |
|
66
|
|
|
if err != nil { |
|
67
|
|
|
return "", err |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
return e.sessionCredential.SecurityToken, nil |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// GetBearerToken is useless for EcsRamRoleCredential |
|
74
|
|
|
func (e *EcsRamRoleCredential) GetBearerToken() string { |
|
75
|
|
|
return "" |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// GetType reutrns EcsRamRoleCredential's type |
|
79
|
|
|
func (e *EcsRamRoleCredential) GetType() string { |
|
80
|
|
|
return "ecs_ram_role" |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
func (e *EcsRamRoleCredential) updateCredential() (err error) { |
|
84
|
|
|
if e.runtime == nil { |
|
85
|
|
|
e.runtime = new(utils.Runtime) |
|
86
|
|
|
} |
|
87
|
|
|
request := request.NewCommonRequest() |
|
88
|
|
|
request.Url = securityCredURL + e.RoleName |
|
89
|
|
|
request.Method = "GET" |
|
90
|
|
|
content, err := doAction(request, e.runtime) |
|
91
|
|
|
if err != nil { |
|
92
|
|
|
return fmt.Errorf("refresh Ecs sts token err: %s", err.Error()) |
|
93
|
|
|
} |
|
94
|
|
|
var resp *EcsRamRoleResponse |
|
95
|
|
|
err = json.Unmarshal(content, &resp) |
|
96
|
|
|
if err != nil { |
|
97
|
|
|
return fmt.Errorf("refresh Ecs sts token err: Json Unmarshal fail: %s", err.Error()) |
|
98
|
|
|
} |
|
99
|
|
|
if resp.Code != "Success" { |
|
100
|
|
|
return fmt.Errorf("refresh Ecs sts token err: Code is not Success") |
|
101
|
|
|
} |
|
102
|
|
|
if resp.AccessKeyID == "" || resp.AccessKeySecret == "" || resp.SecurityToken == "" || resp.Expiration == "" { |
|
103
|
|
|
return fmt.Errorf("refresh Ecs sts token err: AccessKeyID: %s, AccessKeySecret: %s, SecurityToken: %s, Expiration: %s", resp.AccessKeyID, resp.AccessKeySecret, resp.SecurityToken, resp.Expiration) |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
expirationTime, err := time.Parse("2006-01-02T15:04:05Z", resp.Expiration) |
|
107
|
|
|
e.lastUpdateTimestamp = time.Now().Unix() |
|
108
|
|
|
e.credentialExpiration = int(expirationTime.Unix() - time.Now().Unix()) |
|
109
|
|
|
e.sessionCredential = &sessionCredential{ |
|
110
|
|
|
AccessKeyID: resp.AccessKeyID, |
|
111
|
|
|
AccessKeySecret: resp.AccessKeySecret, |
|
112
|
|
|
SecurityToken: resp.SecurityToken, |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return |
|
116
|
|
|
} |
|
117
|
|
|
|