-
Notifications
You must be signed in to change notification settings - Fork 100
/
ecs_role.go
175 lines (145 loc) · 4.5 KB
/
ecs_role.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package lib
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
const (
AdvanceSeconds int64 = 60
)
type STSAkJson struct {
AccessKeyId string `json:"AccessKeyId,omitempty"`
AccessKeySecret string `json:"AccessKeySecret,omitempty"`
SecurityToken string `json:"SecurityToken,omitempty"`
Expiration string `json:"Expiration,omitempty"`
LastUpDated string `json:"LastUpDated,omitempty"`
Code string `json:"Code,omitempty"`
}
func (stsJson *STSAkJson) String() string {
return fmt.Sprintf("AccessKeyId:%s,AccessKeySecret:%s,SecurityToken:%s,Expiration:%s,LastUpDated:%s",
stsJson.AccessKeyId, stsJson.AccessKeySecret, stsJson.SecurityToken, stsJson.Expiration, stsJson.LastUpDated)
}
type EcsRoleAK struct {
AccessKeyId string
AccessKeySecret string
SecurityToken string
}
func (ecsRole *EcsRoleAK) GetAccessKeyID() string {
return ecsRole.AccessKeyId
}
func (ecsRole *EcsRoleAK) GetAccessKeySecret() string {
return ecsRole.AccessKeySecret
}
func (ecsRole *EcsRoleAK) GetSecurityToken() string {
return ecsRole.SecurityToken
}
// for ecs bind ram and get ak by ossutil automaticly
type EcsRoleAKBuild struct {
lock sync.Mutex
HasGet bool
url string //url for get ak,such as http://100.100.100.200/latest/meta-data/Ram/security-credentials/RamRoleName
AccessKeyId string
AccessKeySecret string
SecurityToken string
Expiration string
LastUpDated string
}
func (roleBuild *EcsRoleAKBuild) GetCredentials() oss.Credentials {
roleBuild.lock.Lock()
defer roleBuild.lock.Unlock()
akJson := STSAkJson{}
var err error = nil
bTimeOut := false
if !roleBuild.HasGet {
bTimeOut = true
} else {
bTimeOut = roleBuild.IsTimeOut()
}
if bTimeOut {
tStart := time.Now().UnixNano() / 1000 / 1000
akJson, err = roleBuild.HttpReqAk()
tEnd := time.Now().UnixNano() / 1000 / 1000
if err == nil {
roleBuild.AccessKeyId = akJson.AccessKeyId
roleBuild.AccessKeySecret = akJson.AccessKeySecret
roleBuild.SecurityToken = akJson.SecurityToken
roleBuild.Expiration = akJson.Expiration
LogInfo("get sts ak success,%s,cost:%d(ms)\n", akJson.String(), tEnd-tStart)
}
}
if err != nil {
return &EcsRoleAK{}
}
return &EcsRoleAK{
AccessKeyId: roleBuild.AccessKeyId,
AccessKeySecret: roleBuild.AccessKeySecret,
SecurityToken: roleBuild.SecurityToken,
}
}
func (roleBuild *EcsRoleAKBuild) IsTimeOut() bool {
if roleBuild.Expiration == "" {
return false
}
// attention: can't use time.ParseInLocation(),ecsRole.Expiration is UTC time
utcExpirationTime, _ := time.Parse("2006-01-02T15:04:05Z", roleBuild.Expiration)
// Now() returns the current local time
nowLocalTime := time.Now()
// Unix() returns the number of seconds elapsedsince January 1, 1970 UTC.
if utcExpirationTime.Unix()-nowLocalTime.Unix()-AdvanceSeconds <= 0 {
return true
}
return false
}
func (roleBuild *EcsRoleAKBuild) HttpReqAk() (STSAkJson, error) {
akJson := STSAkJson{}
//http time out
c := &http.Client{
Timeout: 15 * time.Second,
}
resp, err := c.Get(roleBuild.url)
if err != nil {
LogError("insight getAK,http client get error,url is %s,%s\n", roleBuild.url, err.Error())
return akJson, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return akJson, err
}
err = json.Unmarshal(body, &akJson)
if err != nil {
LogError("insight getAK,json.Unmarshal error,body is %s,%s\n", string(body), err.Error())
return akJson, err
}
// parsar json,such as
//{
// "AccessKeyId" : "XXXXXXXXX",
// "AccessKeySecret" : "XXXXXXXXX",
// "Expiration" : "2017-11-01T05:20:01Z",
// "SecurityToken" : "XXXXXXXXX",
// "LastUpdated" : "2017-10-31T23:20:01Z",
// "Code" : "Success"
// }
if akJson.Code != "" && strings.ToUpper(akJson.Code) != "SUCCESS" {
LogError("insight getAK,get sts ak error,code:%s\n", akJson.Code)
return akJson, fmt.Errorf("insight getAK,get sts ak error,code:%s", akJson.Code)
}
if akJson.AccessKeyId == "" || akJson.AccessKeySecret == "" {
LogError("insight getAK,parsar http json body error:\n%s\n", string(body))
return akJson, fmt.Errorf("insight getAK,parsar http json body error:\n%s\n", string(body))
}
if akJson.Expiration != "" {
_, err := time.Parse("2006-01-02T15:04:05Z", akJson.Expiration)
if err != nil {
LogError("time.Parse error,Expiration is %s,%s\n", akJson.Expiration, err.Error())
return akJson, err
}
}
roleBuild.HasGet = true
return akJson, nil
}