-
Notifications
You must be signed in to change notification settings - Fork 18
/
iam_group_detail.sp
275 lines (232 loc) · 5.65 KB
/
iam_group_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
dashboard "iam_group_detail" {
title = "AWS IAM Group Detail"
documentation = file("./dashboards/iam/docs/iam_group_detail.md")
tags = merge(local.iam_common_tags, {
type = "Detail"
})
input "group_arn" {
title = "Select a group:"
query = query.iam_group_input
width = 4
}
container {
card {
width = 3
query = query.iam_group_inline_policy_count_for_group
args = [self.input.group_arn.value]
}
card {
width = 3
query = query.iam_group_direct_attached_policy_count_for_group
args = [self.input.group_arn.value]
}
}
with "iam_policies_for_iam_group" {
query = query.iam_policies_for_iam_group
args = [self.input.group_arn.value]
}
with "iam_users_for_iam_group" {
query = query.iam_users_for_iam_group
args = [self.input.group_arn.value]
}
container {
graph {
title = "Relationships"
type = "graph"
direction = "TD"
node {
base = node.iam_group
args = {
iam_group_arns = [self.input.group_arn.value]
}
}
node {
base = node.iam_group_inline_policy
args = {
iam_group_arns = [self.input.group_arn.value]
}
}
node {
base = node.iam_policy
args = {
iam_policy_arns = with.iam_policies_for_iam_group.rows[*].policy_arn
}
}
node {
base = node.iam_user
args = {
iam_user_arns = with.iam_users_for_iam_group.rows[*].user_arn
}
}
edge {
base = edge.iam_group_to_iam_policy
args = {
iam_group_arns = [self.input.group_arn.value]
}
}
edge {
base = edge.iam_group_to_iam_user
args = {
iam_group_arns = [self.input.group_arn.value]
}
}
edge {
base = edge.iam_group_to_inline_policy
args = {
iam_group_arns = [self.input.group_arn.value]
}
}
}
}
container {
container {
table {
type = "line"
title = "Overview"
width = 6
query = query.iam_group_overview
args = [self.input.group_arn.value]
}
}
}
container {
title = "AWS IAM Group Analysis"
table {
title = "Users"
width = 6
column "User Name" {
href = "${dashboard.iam_user_detail.url_path}?input.user_arn={{.'User ARN' | @uri}}"
}
query = query.iam_users_for_group
args = [self.input.group_arn.value]
}
table {
title = "Policies"
width = 6
query = query.iam_all_policies_for_group
args = [self.input.group_arn.value]
}
}
}
# Input queries
query "iam_group_input" {
sql = <<-EOQ
select
title as label,
arn as value,
json_build_object(
'account_id', account_id
) as tags
from
aws_iam_group
order by
title;
EOQ
}
# With queries
query "iam_policies_for_iam_group" {
sql = <<-EOQ
select
jsonb_array_elements_text(attached_policy_arns) as policy_arn
from
aws_iam_group
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
query "iam_users_for_iam_group" {
sql = <<-EOQ
select
member ->> 'Arn' as user_arn
from
aws_iam_group,
jsonb_array_elements(users) as member
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
# Card queries
query "iam_group_inline_policy_count_for_group" {
sql = <<-EOQ
select
case when inline_policies is null then 0 else jsonb_array_length(inline_policies) end as value,
'Inline Policies' as label,
case when (inline_policies is null) or (jsonb_array_length(inline_policies) = 0) then 'ok' else 'alert' end as type
from
aws_iam_group
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
query "iam_group_direct_attached_policy_count_for_group" {
sql = <<-EOQ
select
case when attached_policy_arns is null then 0 else jsonb_array_length(attached_policy_arns) end as value,
'Attached Policies' as label,
case when (attached_policy_arns is null) or (jsonb_array_length(attached_policy_arns) = 0) then 'alert' else 'ok' end as type
from
aws_iam_group
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
# Other detail page queries
query "iam_group_overview" {
sql = <<-EOQ
select
name as "Name",
create_date as "Create Date",
group_id as "Group ID",
arn as "ARN",
account_id as "Account ID"
from
aws_iam_group
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
query "iam_users_for_group" {
sql = <<-EOQ
select
u ->> 'UserName' as "User Name",
u ->> 'Arn' as "User ARN",
u ->> 'UserId' as "User ID"
from
aws_iam_group as g,
jsonb_array_elements(users) as u
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}
query "iam_all_policies_for_group" {
sql = <<-EOQ
-- Policies (attached to groups)
select
split_part(policy_arn, '/','2') as "Policy",
policy_arn as "ARN",
'Attached to Group' as "Via"
from
aws_iam_group as g,
jsonb_array_elements_text(g.attached_policy_arns) as policy_arn
where
g.arn = $1
and g.account_id = split_part($1, ':', 5)
-- Policies (inline from groups)
union select
i ->> 'PolicyName' as "Policy",
'N/A' as "ARN",
'Inline' as "Via"
from
aws_iam_group as grp,
jsonb_array_elements(grp.inline_policies_std) as i
where
arn = $1
and account_id = split_part($1, ':', 5);
EOQ
}