| 1 |
package sts |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"os" |
| 6 |
"strconv" |
| 7 |
"testing" |
| 8 |
"time" |
| 9 |
|
| 10 |
"fmt" |
| 11 |
|
| 12 |
"github.com/denverdino/aliyungo/ecs" |
| 13 |
|
| 14 |
"github.com/denverdino/aliyungo/ram" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
var ( |
| 22 |
accountId = os.Getenv("AccountId") |
| 23 |
roleName = strconv.FormatInt(time.Now().Unix(), 10) |
| 24 |
|
| 25 |
princpal = ram.AssumeRolePolicyPrincpal{RAM: []string{"acs:ram::" + accountId + ":root"}} |
| 26 |
|
| 27 |
princpalPolicyDocument = ram.AssumeRolePolicyDocument{ |
| 28 |
Statement: []ram.AssumeRolePolicyItem{ |
| 29 |
ram.AssumeRolePolicyItem{Action: "sts:AssumeRole", Effect: "Allow", Principal: princpal}, |
| 30 |
}, |
| 31 |
Version: "1"} |
| 32 |
|
| 33 |
role = ram.RoleRequest{ |
| 34 |
RoleName: roleName, |
| 35 |
AssumeRolePolicyDocument: getAssumeRolePolicyDocumentStr(), |
| 36 |
Description: "this is a role for unit test purpose", |
| 37 |
} |
| 38 |
) |
| 39 |
|
| 40 |
var policyDocument = ram.PolicyDocument{ |
| 41 |
Statement: []ram.PolicyItem{ |
| 42 |
ram.PolicyItem{ |
| 43 |
Action: "oss:GetObject", |
| 44 |
Effect: "Allow", |
| 45 |
Resource: "acs:oss:*:*:*/anyprefix", |
| 46 |
}, |
| 47 |
}, |
| 48 |
Version: "1", |
| 49 |
} |
| 50 |
|
| 51 |
func getAssumeRolePolicyDocumentStr() string { |
| 52 |
b, _ := json.Marshal(princpalPolicyDocument) |
| 53 |
return string(b) |
| 54 |
} |
| 55 |
|
| 56 |
func createAssumeRoleRequest(roleArn string) AssumeRoleRequest { |
| 57 |
document, _ := json.Marshal(policyDocument) |
| 58 |
return AssumeRoleRequest{ |
| 59 |
RoleArn: roleArn, |
| 60 |
RoleSessionName: "aliyungo-sts-unit-test", |
| 61 |
DurationSeconds: 3600, |
| 62 |
Policy: string(document), |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func createPolicyDocument() *ram.PolicyDocument { |
| 67 |
return &ram.PolicyDocument{ |
| 68 |
Statement: []ram.PolicyItem{ |
| 69 |
ram.PolicyItem{ |
| 70 |
Action: "oss:GetObject", |
| 71 |
Effect: "Allow", |
| 72 |
Resource: "acs:oss:*:*:*/*", |
| 73 |
}, |
| 74 |
}, |
| 75 |
Version: "1", |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
func createPolicyReq() *ram.PolicyRequest { |
| 80 |
policyDocument := createPolicyDocument() |
| 81 |
document, _ := json.Marshal(*policyDocument) |
| 82 |
return &ram.PolicyRequest{ |
| 83 |
PolicyName: "sts-" + strconv.FormatInt(time.Now().Unix(), 10), |
| 84 |
PolicyType: "Custom", |
| 85 |
PolicyDocument: string(document), |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
func TestAssumeRole(t *testing.T) { |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
ramClient := NewRAMTestClient() |
| 95 |
roleResp, err := ramClient.CreateRole(role) |
| 96 |
if err != nil { |
| 97 |
t.Errorf("Failed to CreateRole %v", err) |
| 98 |
return |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
policyResp, err := ramClient.CreatePolicy(*createPolicyReq()) |
| 105 |
if err != nil { |
| 106 |
t.Errorf("Failed to CreatePolicy %v", err) |
| 107 |
return |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
attachPolicyRequest := ram.AttachPolicyToRoleRequest{ |
| 114 |
PolicyRequest: ram.PolicyRequest{ |
| 115 |
PolicyType: "Custom", |
| 116 |
PolicyName: policyResp.Policy.PolicyName, |
| 117 |
}, |
| 118 |
RoleName: roleResp.Role.RoleName, |
| 119 |
} |
| 120 |
|
| 121 |
_, err = ramClient.AttachPolicyToRole(attachPolicyRequest) |
| 122 |
if err != nil { |
| 123 |
t.Errorf("Failed to AttachPolicyToRole %v", err) |
| 124 |
return |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
time.Sleep(2 * time.Second) |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
client := NewTestClient() |
| 137 |
req := createAssumeRoleRequest(roleResp.Role.Arn) |
| 138 |
resp, err := client.AssumeRole(req) |
| 139 |
if err != nil { |
| 140 |
t.Errorf("Failed to AssumeRole %v", err) |
| 141 |
return |
| 142 |
} |
| 143 |
|
| 144 |
ecsClient := ecs.NewECSClientWithSecurityToken(resp.Credentials.AccessKeyId, resp.Credentials.AccessKeySecret, resp.Credentials.SecurityToken, "") |
| 145 |
_, err = ecsClient.DescribeRegions() |
| 146 |
if err != nil { |
| 147 |
t.Errorf("Failed to DescribeRegions %v", err) |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
ramClient = ram.NewClientWithSecurityToken(resp.Credentials.AccessKeyId, resp.Credentials.AccessKeySecret, resp.Credentials.SecurityToken) |
| 152 |
req2 := ram.ListUserRequest{ |
| 153 |
Marker: "", |
| 154 |
MaxItems: 2, |
| 155 |
} |
| 156 |
_, err = ramClient.ListUsers(req2) |
| 157 |
if err != nil { |
| 158 |
t.Errorf("Failed to ListUsers %v", err) |
| 159 |
return |
| 160 |
} |
| 161 |
|
| 162 |
t.Logf("pass AssumeRole %v", resp) |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
func TestSTSClient_AssumeRole(t *testing.T) { |
| 167 |
client := NewTestClient() |
| 168 |
|
| 169 |
roleArn := os.Getenv("RoleArn") |
| 170 |
|
| 171 |
req := AssumeRoleRequest{ |
| 172 |
RoleArn: roleArn, |
| 173 |
RoleSessionName: fmt.Sprintf("commander-role-%d", time.Now().Unix()), |
| 174 |
DurationSeconds: 3600, |
| 175 |
} |
| 176 |
|
| 177 |
response, err := client.AssumeRole(req) |
| 178 |
if err != nil { |
| 179 |
t.Fatalf("%++v", err) |
| 180 |
} else { |
| 181 |
t.Logf("Response=%++v", response) |
| 182 |
} |
| 183 |
} |