forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_alicloud_ram.go
186 lines (158 loc) · 4.14 KB
/
service_alicloud_ram.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
package alicloud
import (
"encoding/json"
"fmt"
"strings"
"github.com/denverdino/aliyungo/ram"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
type Effect string
const (
Allow Effect = "Allow"
Deny Effect = "Deny"
)
type Principal struct {
Service []string
RAM []string
}
type RolePolicyStatement struct {
Effect Effect
Action string
Principal Principal
}
type RolePolicy struct {
Statement []RolePolicyStatement
Version string
}
type PolicyStatement struct {
Effect Effect
Action interface{}
Resource interface{}
}
type Policy struct {
Statement []PolicyStatement
Version string
}
type RamService struct {
client *connectivity.AliyunClient
}
func (s *RamService) ParseRolePolicyDocument(policyDocument string) (RolePolicy, error) {
var policy RolePolicy
err := json.Unmarshal([]byte(policyDocument), &policy)
if err != nil {
return RolePolicy{}, err
}
return policy, nil
}
func (s *RamService) ParsePolicyDocument(policyDocument string) (statement []map[string]interface{}, version string, err error) {
policy := Policy{}
err = json.Unmarshal([]byte(policyDocument), &policy)
if err != nil {
return
}
version = policy.Version
statement = make([]map[string]interface{}, 0, len(policy.Statement))
for _, v := range policy.Statement {
item := make(map[string]interface{})
item["effect"] = v.Effect
if val, ok := v.Action.([]interface{}); ok {
item["action"] = val
} else {
item["action"] = []interface{}{v.Action}
}
if val, ok := v.Resource.([]interface{}); ok {
item["resource"] = val
} else {
item["resource"] = []interface{}{v.Resource}
}
statement = append(statement, item)
}
return
}
func (s *RamService) AssembleRolePolicyDocument(ramUser, service []interface{}, version string) (string, error) {
services := expandStringList(service)
users := expandStringList(ramUser)
statement := RolePolicyStatement{
Effect: Allow,
Action: "sts:AssumeRole",
Principal: Principal{
RAM: users,
Service: services,
},
}
policy := RolePolicy{
Version: version,
Statement: []RolePolicyStatement{statement},
}
data, err := json.Marshal(policy)
if err != nil {
return "", err
}
return string(data), nil
}
func (s *RamService) AssemblePolicyDocument(document []interface{}, version string) (string, error) {
var statements []PolicyStatement
for _, v := range document {
doc := v.(map[string]interface{})
actions := expandStringList(doc["action"].([]interface{}))
resources := expandStringList(doc["resource"].([]interface{}))
statement := PolicyStatement{
Effect: Effect(doc["effect"].(string)),
Action: actions,
Resource: resources,
}
statements = append(statements, statement)
}
policy := Policy{
Version: version,
Statement: statements,
}
data, err := json.Marshal(policy)
if err != nil {
return "", err
}
return string(data), nil
}
// Judge whether the role policy contains service "ecs.aliyuncs.com"
func (s *RamService) JudgeRolePolicyPrincipal(roleName string) error {
raw, err := s.client.WithRamClient(func(ramClient ram.RamClientInterface) (interface{}, error) {
return ramClient.GetRole(ram.RoleQueryRequest{RoleName: roleName})
})
if err != nil {
return fmt.Errorf("GetRole %s got an error: %#v", roleName, err)
}
resp, _ := raw.(ram.RoleResponse)
policy, err := s.ParseRolePolicyDocument(resp.Role.AssumeRolePolicyDocument)
if err != nil {
return err
}
for _, v := range policy.Statement {
for _, val := range v.Principal.Service {
if strings.Trim(val, " ") == "ecs.aliyuncs.com" {
return nil
}
}
}
return fmt.Errorf("Role policy services must contains 'ecs.aliyuncs.com', Now is \n%v.", resp.Role.AssumeRolePolicyDocument)
}
func (s *RamService) GetIntersection(dataMap []map[string]interface{}, allDataMap map[string]interface{}) (allData []interface{}) {
if len(dataMap) == 1 {
allDataMap = dataMap[0]
} else {
for _, v := range dataMap {
if len(v) > 0 {
for key := range allDataMap {
if _, ok := v[key]; !ok {
allDataMap[key] = nil
}
}
}
}
}
for _, v := range allDataMap {
if v != nil {
allData = append(allData, v)
}
}
return
}