-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram_role_detail.sp
345 lines (298 loc) · 7.46 KB
/
ram_role_detail.sp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
dashboard "ram_role_detail" {
title = "AliCloud RAM Role Detail"
documentation = file("./dashboards/ram/docs/ram_role_detail.md")
tags = merge(local.ram_common_tags, {
type = "Detail"
})
input "role_arn" {
title = "Select a role:"
query = query.ram_role_input
width = 4
}
container {
card {
width = 3
query = query.ram_role_policy_count_for_role
args = [self.input.role_arn.value]
}
card {
width = 3
query = query.ram_role_with_admin_access
args = [self.input.role_arn.value]
}
card {
width = 3
query = query.ram_role_with_cross_account_access
args = [self.input.role_arn.value]
}
}
with "action_trails_for_ram_role" {
query = query.action_trails_for_ram_role
args = [self.input.role_arn.value]
}
with "ecs_instances_for_ram_role" {
query = query.ecs_instances_for_ram_role
args = [self.input.role_arn.value]
}
with "ram_policies_for_ram_role" {
query = query.ram_policies_for_ram_role
args = [self.input.role_arn.value]
}
container {
graph {
title = "Relationships"
type = "graph"
node {
base = node.actiontrail_trail
args = {
action_trail_names = with.action_trails_for_ram_role.rows[*].trail_name
}
}
node {
base = node.ecs_instance
args = {
ecs_instance_arns = with.ecs_instances_for_ram_role.rows[*].instance_arn
}
}
node {
base = node.ram_policy
args = {
ram_policy_names = with.ram_policies_for_ram_role.rows[*].policy_name
}
}
node {
base = node.ram_role
args = {
ram_role_arns = [self.input.role_arn.value]
}
}
edge {
base = edge.actiontrail_trail_to_ram_role
args = {
action_trail_names = with.action_trails_for_ram_role.rows[*].trail_name
}
}
edge {
base = edge.ecs_instance_to_ram_role
args = {
ecs_instance_arns = with.ecs_instances_for_ram_role.rows[*].instance_arn
}
}
edge {
base = edge.ram_role_to_ram_policy
args = {
ram_role_arns = [self.input.role_arn.value]
}
}
}
}
container {
container {
width = 6
table {
title = "Overview"
type = "line"
width = 6
query = query.ram_role_overview
args = [self.input.role_arn.value]
}
}
container {
title = "AliCloud RAM Role Policy Analysis"
hierarchy {
type = "tree"
width = 6
title = "Attached Policies"
query = query.ram_user_manage_policies_hierarchy
args = [self.input.role_arn.value]
category "managed_policy" {
color = "ok"
}
}
table {
title = "Policies"
width = 6
query = query.ram_policies_for_role
args = [self.input.role_arn.value]
}
}
}
}
# Input queries
query "ram_role_input" {
sql = <<-EOQ
select
title as label,
arn as value,
json_build_object(
'account_id', account_id
) as tags
from
alicloud_ram_role
order by
title;
EOQ
}
# With queries
query "ram_policies_for_ram_role" {
sql = <<-EOQ
select
p.policy_name as policy_name
from
alicloud_ram_role as r,
alicloud_ram_policy as p,
jsonb_array_elements(r.attached_policy) as policy
where
r.arn = $1
and r.account_id = p.account_id
and policy ->> 'PolicyName' = p.policy_name;
EOQ
}
query "ecs_instances_for_ram_role" {
sql = <<-EOQ
select
i.arn as instance_arn
from
alicloud_ecs_instance as i,
alicloud_ram_role as r,
jsonb_array_elements(i.ram_role) as role
where
r.arn = $1
and r.account_id = split_part($1,':',4)
and r.name = role ->> 'RamRoleName'
and role ->> 'RamRoleName' is not null;
EOQ
}
query "action_trails_for_ram_role" {
sql = <<-EOQ
select
t.name as trail_name
from
alicloud_action_trail as t,
alicloud_ram_role as r
where
r.arn = $1
and t.account_id = r.account_id
and t.sls_write_role_arn = r.arn;
EOQ
}
# Card queries
query "ram_role_policy_count_for_role" {
sql = <<-EOQ
select
case when attached_policy = '[]' then 0 else jsonb_array_length(attached_policy) end as value,
'Policies' as label
from
alicloud_ram_role
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_role_with_admin_access" {
sql = <<-EOQ
with admin_roles as (
select
distinct name
from
alicloud_ram_role,
jsonb_array_elements(attached_policy) as policies
where
policies ->> 'PolicyName' = 'AdministratorAccess'
)
select
case when a.name is not null then 'Enabled' else 'Disabled' end as value,
'Admin Access' as label,
case when a.name is not null then 'alert' else 'ok' end as type
from
alicloud_ram_role as r
left join admin_roles as a on r.name = a.name
where
r.arn = $1
and r.account_id = split_part($1,':',4);
EOQ
}
query "ram_role_with_cross_account_access" {
sql = <<-EOQ
with roles_with_cross_account_access as (
select
distinct name as name
from
alicloud_ram_role,
jsonb_array_elements(assume_role_policy_document -> 'Statement') as stmt,
jsonb_array_elements_text(stmt -> 'Principal' -> 'RAM') as principal
where
split_part(principal, ':',4) <> account_id
)
select
case when a.name is not null then 'Enabled' else 'Disabled' end as value,
'Cross-Account Access' as label,
case when a.name is not null then 'alert' else 'ok' end as type
from
alicloud_ram_role as r
left join roles_with_cross_account_access as a on r.name = a.name
where
r.arn = $1
and r.account_id = split_part($1,':',4);
EOQ
}
# Other detail page queries
query "ram_role_overview" {
sql = <<-EOQ
select
name as "Name",
create_date as "Create Date",
update_date as "Last Update Date",
max_session_duration as "Max Session Duration",
description as "Description",
role_id as "Role ID",
arn as "ARN",
account_id as "Account ID"
from
alicloud_ram_role
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_policies_for_role" {
sql = <<-EOQ
select
policies ->> 'PolicyName' as "Name",
policies ->> 'PolicyType' as "Type",
policies ->> 'DefaultVersion' as "Default Version",
policies ->> 'AttachDate' as "Attachment Date"
from
alicloud_ram_role,
jsonb_array_elements(attached_policy) as policies
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_user_manage_policies_hierarchy" {
sql = <<-EOQ
select
r.name as id,
r.name as title,
'role' as category,
null as from_id
from
alicloud_ram_role as r
where
r.arn = $1
and r.account_id = split_part($1,':',4)
-- Policies (attached to groups)
union select
policy ->> 'PolicyName' as id,
policy ->> 'PolicyName' as title,
'managed_policy' as category,
r.name as from_id
from
alicloud_ram_role as r,
jsonb_array_elements(r.attached_policy) as policy
where
r.arn = $1
and r.account_id = split_part($1,':',4);
EOQ
}