-
Notifications
You must be signed in to change notification settings - Fork 0
/
ram_user_detail.sp
417 lines (353 loc) · 9.06 KB
/
ram_user_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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
dashboard "ram_user_detail" {
title = "AliCloud RAM User Detail"
documentation = file("./dashboards/ram/docs/ram_user_detail.md")
tags = merge(local.ram_common_tags, {
type = "Detail"
})
input "user_arn" {
title = "Select a user:"
query = query.ram_user_input
width = 4
}
container {
card {
width = 3
query = query.ram_user_mfa_for_user
args = [self.input.user_arn.value]
}
card {
width = 3
query = query.ram_user_direct_attached_policy_count_for_user
args = [self.input.user_arn.value]
}
}
with "ram_groups_for_ram_user" {
query = query.ram_groups_for_ram_user
args = [self.input.user_arn.value]
}
with "ram_policies_for_ram_user" {
query = query.ram_policies_for_ram_user
args = [self.input.user_arn.value]
}
container {
graph {
title = "Relationships"
type = "graph"
node {
base = node.ram_group
args = {
ram_group_arns = with.ram_groups_for_ram_user.rows[*].group_arn
}
}
node {
base = node.ram_policy
args = {
ram_policy_names = with.ram_policies_for_ram_user.rows[*].policy_name
}
}
node {
base = node.ram_user
args = {
ram_user_arns = [self.input.user_arn.value]
}
}
node {
base = node.ram_access_key
args = {
ram_user_arns = [self.input.user_arn.value]
}
}
edge {
base = edge.ram_group_to_ram_user
args = {
ram_group_arns = with.ram_groups_for_ram_user.rows[*].group_arn
}
}
edge {
base = edge.ram_user_to_ram_access_key
args = {
ram_user_arns = [self.input.user_arn.value]
}
}
edge {
base = edge.ram_user_to_ram_policy
args = {
ram_user_arns = [self.input.user_arn.value]
}
}
}
}
container {
container {
width = 4
table {
title = "Overview"
type = "line"
query = query.ram_user_overview
args = [self.input.user_arn.value]
}
}
container {
width = 8
table {
title = "Access Keys"
query = query.ram_user_access_keys
args = [self.input.user_arn.value]
}
table {
title = "MFA Devices"
query = query.ram_user_mfa_devices
args = [self.input.user_arn.value]
}
}
}
container {
title = "AliCloud RAM User Policy Analysis"
flow {
type = "sankey"
title = "Attached Policies"
query = query.ram_user_manage_policies_sankey
args = [self.input.user_arn.value]
category "ram_group" {
color = "ok"
}
}
table {
title = "Groups"
width = 6
query = query.ram_groups_for_user
args = [self.input.user_arn.value]
column "Name" {
// cyclic dependency prevents use of url_path, hardcode for now
href = "/alicloud_insights.dashboard.ram_group_detail?input.group_arn={{.'Group ARN' | @uri}}"
}
}
table {
title = "Policies"
width = 6
query = query.ram_all_policies_for_user
args = [self.input.user_arn.value]
}
}
}
query "ram_user_input" {
sql = <<-EOQ
select
title as label,
arn as value,
json_build_object(
'account_id', account_id
) as tags
from
alicloud_ram_user
order by
title;
EOQ
}
query "ram_user_mfa_for_user" {
sql = <<-EOQ
select
case when mfa_enabled then 'Enabled' else 'Disabled' end as value,
'MFA Status' as label,
case when mfa_enabled then 'ok' else 'alert' end as type
from
alicloud_ram_user
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_user_direct_attached_policy_count_for_user" {
sql = <<-EOQ
select
count(*) as value,
'Direct Attached Policies' as label,
case when count(*) = 0 then 'ok' else 'alert' end as type
from
alicloud_ram_user
where
arn = $1
and attached_policy = '[]'
and account_id = split_part($1,':',4);
EOQ
}
# With queries
query "ram_groups_for_ram_user" {
sql = <<-EOQ
select
g.arn as group_arn
from
alicloud_ram_user as u,
alicloud_ram_group as g,
jsonb_array_elements(u.groups) as ugrp
where
u.arn = $1
and u.account_id = g.account_id
and g.title = ugrp ->> 'GroupName';
EOQ
}
query "ram_policies_for_ram_user" {
sql = <<-EOQ
select
p.policy_name
from
alicloud_ram_user as u,
alicloud_ram_policy as p,
jsonb_array_elements(u.attached_policy) as policy
where
u.arn = $1
and u.account_id = p.account_id
and policy ->> 'PolicyName' = p.policy_name;
EOQ
}
query "ram_user_overview" {
sql = <<-EOQ
select
name as "Name",
create_date as "Create Date",
update_date as "Last Modified Date",
user_id as "User ID",
account_id as "Account ID"
from
alicloud_ram_user
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_user_access_keys" {
sql = <<-EOQ
select
access_key_id as "Access Key ID",
a.status as "Status",
a.create_date as "Create Date"
from
alicloud_ram_access_key as a left join alicloud_ram_user as u on u.name = a.user_name
where
u.arn = $1
and u.account_id = split_part($1,':',4);
EOQ
}
query "ram_user_mfa_devices" {
sql = <<-EOQ
select
mfa -> 'User' ->> 'UserId' as "User ID",
mfa ->> 'SerialNumber' as "MFA Serial Number",
mfa ->> 'ActivateDate' as "Activate Date"
from
alicloud_ram_user,
jsonb_array_elements(virtual_mfa_devices) as mfa
where
arn = $1
and account_id = split_part($1,':',4);
EOQ
}
query "ram_user_manage_policies_sankey" {
sql = <<-EOQ
with args as (
select $1 as ram_user_arn
)
-- User
select
null as from_id,
name as id,
title,
0 as depth,
'alicloud_ram_user' as category
from
alicloud_ram_user
where
arn in (select ram_user_arn from args)
and account_id in (select split_part(ram_user_arn,':',4) from args)
-- Groups
union select
u.name as from_id,
g.name as id,
user_groups ->> 'GroupName' as title,
1 as depth,
'alicloud_ram_group' as category
from
alicloud_ram_user as u,
jsonb_array_elements(u.groups) as user_groups
inner join alicloud_ram_group g on g.name = user_groups ->> 'GroupName'
where
u.arn in (select ram_user_arn from args)
and u.account_id in (select split_part(ram_user_arn,':',4) from args)
-- Policies (attached to groups)
union select
g.name as from_id,
p.title as id,
p.title as title,
2 as depth,
'alicloud_ram_policy' as category
from
alicloud_ram_user as u,
alicloud_ram_policy as p,
jsonb_array_elements(u.groups) as user_groups
inner join alicloud_ram_group g on g.name = user_groups ->> 'GroupName',
jsonb_array_elements(g.attached_policy) as user_policy
where
user_policy ->> 'PolicyName' = p.title
and u.arn in (select ram_user_arn from args)
and u.account_id in (select split_part(ram_user_arn,':',4) from args)
-- Policies (attached to user)
union select
u.name as from_id,
p.title as id,
p.title as title,
2 as depth,
'alicloud_ram_policy' as category
from
alicloud_ram_user as u,
jsonb_array_elements(u.attached_policy) as pol_arn,
alicloud_ram_policy as p
where
pol_arn ->> 'PolicyName' = p.title
and u.arn in (select ram_user_arn from args)
and u.account_id in (select split_part(ram_user_arn,':',4) from args);
EOQ
}
query "ram_groups_for_user" {
sql = <<-EOQ
select
g ->> 'GroupName' as "Name",
'acs:ram::' || account_id || ':group/' || (g ->> 'GroupName') as "Group ARN",
g ->> 'JoinDate' as "Join Date"
from
alicloud_ram_user as u,
jsonb_array_elements(groups) as g
where
u.arn = $1
and u.account_id = split_part($1,':',4);
EOQ
}
query "ram_all_policies_for_user" {
sql = <<-EOQ
-- Policies (attached to groups)
select
p.title as "Policy",
'Group: ' || g.title as "Via"
from
alicloud_ram_user as u,
alicloud_ram_policy as p,
jsonb_array_elements(u.groups) as user_groups
inner join alicloud_ram_group g on g.title = user_groups ->> 'GroupName',
jsonb_array_elements(g.attached_policy) as group_policy
where
group_policy ->> 'PolicyName' = p.title
and u.arn = $1
and u.account_id = split_part($1,':',4)
-- Policies (attached to user)
union select
p.title as "Policy",
'Attached to User' as "Via"
from
alicloud_ram_user as u,
jsonb_array_elements(u.attached_policy) as pol_arn,
alicloud_ram_policy as p
where
pol_arn ->> 'PolicyName' = p.title
and u.arn = $1
and u.account_id = split_part($1,':',4);
EOQ
}