-
Notifications
You must be signed in to change notification settings - Fork 271
/
base.go
209 lines (198 loc) · 5.65 KB
/
base.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package integration
import (
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
"fmt"
"os"
"strings"
)
var role_doc = `{
"Statement": [{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"RAM": [
"acs:ram::%s:root"
]
}
}],
"Version": "1"
}`
var (
travisValue = strings.Split(os.Getenv("TRAVIS_JOB_NUMBER"), ".")
username = "test-user" + travisValue[len(travisValue)-1]
rolename = "test-role" + travisValue[len(travisValue)-1]
rolearn = fmt.Sprintf("acs:ram::%s:role/%s", os.Getenv("USER_ID"), rolename)
)
func createRole(userid string) (string, string, error) {
listRequest := ram.CreateListRolesRequest()
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
return "", "", err
}
listResponse, err := client.ListRoles(listRequest)
if err != nil {
return "", "", err
}
for _, role := range listResponse.Roles.Role {
if strings.ToLower(role.RoleName) == rolename {
return role.RoleName, role.Arn, nil
}
}
createRequest := ram.CreateCreateRoleRequest()
createRequest.Scheme = "HTTPS"
createRequest.RoleName = rolename
createRequest.AssumeRolePolicyDocument = fmt.Sprintf(role_doc, userid)
res, err := client.CreateRole(createRequest)
if err != nil {
return "", "", err
}
return res.Role.RoleName, res.Role.Arn, nil
}
func createUser() error {
listRequest := ram.CreateListUsersRequest()
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
return err
}
listResponse, err := client.ListUsers(listRequest)
if err != nil {
return err
}
for _, user := range listResponse.Users.User {
if user.UserName == username {
return nil
}
}
createRequest := ram.CreateCreateUserRequest()
createRequest.Scheme = "HTTPS"
createRequest.UserName = username
_, err = client.CreateUser(createRequest)
if err != nil {
return err
}
return nil
}
func createAttachPolicyToUser() error {
listRequest := ram.CreateListPoliciesForUserRequest()
listRequest.UserName = username
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
return err
}
listResponse, err := client.ListPoliciesForUser(listRequest)
if err != nil {
return err
}
for _, policy := range listResponse.Policies.Policy {
if policy.PolicyName == "AliyunSTSAssumeRoleAccess" {
return nil
}
}
createRequest := ram.CreateAttachPolicyToUserRequest()
createRequest.Scheme = "HTTPS"
createRequest.PolicyName = "AliyunSTSAssumeRoleAccess"
createRequest.UserName = username
createRequest.PolicyType = "System"
_, err = client.AttachPolicyToUser(createRequest)
if err != nil {
return err
}
return nil
}
func createAttachPolicyToRole() error {
listRequest := ram.CreateListPoliciesForRoleRequest()
listRequest.RoleName = rolename
listRequest.Scheme = "HTTPS"
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
return err
}
listResponse, err := client.ListPoliciesForRole(listRequest)
if err != nil {
return err
}
for _, policy := range listResponse.Policies.Policy {
if policy.PolicyName == "AdministratorAccess" {
return nil
}
}
createRequest := ram.CreateAttachPolicyToRoleRequest()
createRequest.Scheme = "HTTPS"
createRequest.PolicyName = "AdministratorAccess"
createRequest.RoleName = rolename
createRequest.PolicyType = "System"
_, err = client.AttachPolicyToRole(createRequest)
if err != nil {
return err
}
return nil
}
func createAccessKey() (string, string, error) {
client, err := ram.NewClientWithAccessKey(os.Getenv("REGION_ID"), os.Getenv("ACCESS_KEY_ID"), os.Getenv("ACCESS_KEY_SECRET"))
if err != nil {
return "", "", err
}
listrequest := ram.CreateListAccessKeysRequest()
listrequest.UserName = username
listrequest.Scheme = "HTTPS"
listresponse, err := client.ListAccessKeys(listrequest)
if err != nil {
return "", "", err
}
if listresponse.AccessKeys.AccessKey != nil {
if len(listresponse.AccessKeys.AccessKey) >= 2 {
accesskey := listresponse.AccessKeys.AccessKey[0]
deleterequest := ram.CreateDeleteAccessKeyRequest()
deleterequest.UserAccessKeyId = accesskey.AccessKeyId
deleterequest.UserName = username
deleterequest.Scheme = "HTTPS"
_, err := client.DeleteAccessKey(deleterequest)
if err != nil {
return "", "", err
}
}
}
request := ram.CreateCreateAccessKeyRequest()
request.Scheme = "HTTPS"
request.UserName = username
response, err := client.CreateAccessKey(request)
if err != nil {
return "", "", err
}
return response.AccessKey.AccessKeyId, response.AccessKey.AccessKeySecret, nil
}
func createAssumeRole() (*sts.AssumeRoleResponse, error) {
subaccesskeyid, subaccesskeysecret, err := createAccessKey()
if err != nil {
return nil, err
}
err = createUser()
if err != nil {
return nil, err
}
_, _, err = createRole(os.Getenv("USER_ID"))
if err != nil {
return nil, err
}
err = createAttachPolicyToUser()
if err != nil {
return nil, err
}
request := sts.CreateAssumeRoleRequest()
request.RoleArn = rolearn
request.RoleSessionName = "alice_test"
request.Scheme = "HTTPS"
client, err := sts.NewClientWithAccessKey(os.Getenv("REGION_ID"), subaccesskeyid, subaccesskeysecret)
if err != nil {
return nil, err
}
response, err := client.AssumeRole(request)
if err != nil {
return nil, err
}
return response, nil
}