-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathaws_iam_role_audit.pt
498 lines (448 loc) · 14.8 KB
/
aws_iam_role_audit.pt
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
name "AWS IAM Role Audit"
rs_pt_ver 20180301
type "policy"
short_description "This policy checks to see if a list of user-specified roles exist in an AWS account. See the [README](https://github.com/flexera-public/policy_templates/tree/master/compliance/aws/iam_role_audit) and [docs.flexera.com/flexera/EN/Automation](https://docs.flexera.com/flexera/EN/Automation/AutomationGS.htm) to learn more."
long_description ""
severity "medium"
category "Compliance"
default_frequency "daily"
info(
version: "3.0.3",
provider:"AWS",
service: "IAM",
policy_set: "Identity & Access Management",
hide_skip_approvals: "true"
)
###############################################################################
# Parameters
###############################################################################
parameter "param_email" do
type "list"
category "Policy Settings"
label "Email Addresses"
description "Email addresses of the recipients you wish to notify when new incidents are created"
default []
end
parameter "param_aws_account_number" do
type "string"
category "Policy Settings"
label "Account Number"
description "Leave blank; this is for automated use with Meta Policies. See README for more details."
default ""
end
parameter "param_role_names" do
type "list"
category "Policy Settings"
label "IAM Role Names/IDs/ARNs"
description "List of IAM role names/IDs/ARNs to check."
default ["FlexeraAutomationAccessRole"]
end
###############################################################################
# Authentication
###############################################################################
credentials "auth_aws" do
schemes "aws", "aws_sts"
label "AWS"
description "Select the AWS Credential from the list"
tags "provider=aws"
aws_account_number $param_aws_account_number
end
credentials "auth_flexera" do
schemes "oauth2"
label "Flexera"
description "Select Flexera One OAuth2 credentials"
tags "provider=flexera"
end
###############################################################################
# Pagination
###############################################################################
pagination "pagination_aws_iam_role_json" do
get_page_marker do
body_path jmes_path(response, "ListRolesResponse.ListRolesResult.Marker")
end
set_page_marker do
query "Marker"
end
end
pagination "pagination_aws_iam_role_tag_json" do
get_page_marker do
body_path jmes_path(response, "ListRoleTagsResponse.ListRoleTagsResult.Marker")
end
set_page_marker do
query "Marker"
end
end
###############################################################################
# Datasources & Scripts
###############################################################################
# Get applied policy metadata for use later
datasource "ds_applied_policy" do
request do
auth $auth_flexera
host rs_governance_host
path join(["/api/governance/projects/", rs_project_id, "/applied_policies/", policy_id])
header "Api-Version", "1.0"
end
end
# Get region-specific Flexera API endpoints
datasource "ds_flexera_api_hosts" do
run_script $js_flexera_api_hosts, rs_optima_host
end
script "js_flexera_api_hosts", type: "javascript" do
parameters "rs_optima_host"
result "result"
code <<-EOS
host_table = {
"api.optima.flexeraeng.com": {
flexera: "api.flexera.com",
fsm: "api.fsm.flexeraeng.com"
},
"api.optima-eu.flexeraeng.com": {
flexera: "api.flexera.eu",
fsm: "api.fsm-eu.flexeraeng.com"
},
"api.optima-apac.flexeraeng.com": {
flexera: "api.flexera.au",
fsm: "api.fsm-apac.flexeraeng.com"
}
}
result = host_table[rs_optima_host]
EOS
end
# Get AWS account info
datasource "ds_cloud_vendor_accounts" do
request do
auth $auth_flexera
host val($ds_flexera_api_hosts, 'flexera')
path join(["/finops-analytics/v1/orgs/", rs_org_id, "/cloud-vendor-accounts"])
header "Api-Version", "1.0"
end
result do
encoding "json"
collect jmes_path(response, "values[*]") do
field "id", jmes_path(col_item, "aws.accountId")
field "name", jmes_path(col_item, "name")
field "tags", jmes_path(col_item, "tags")
end
end
end
datasource "ds_get_caller_identity" do
request do
auth $auth_aws
host "sts.amazonaws.com"
path "/"
query "Action", "GetCallerIdentity"
query "Version", "2011-06-15"
header "User-Agent", "RS Policies"
end
result do
encoding "xml"
collect xpath(response, "//GetCallerIdentityResponse/GetCallerIdentityResult") do
field "account", xpath(col_item, "Account")
end
end
end
datasource "ds_aws_account" do
run_script $js_aws_account, $ds_cloud_vendor_accounts, $ds_get_caller_identity
end
script "js_aws_account", type:"javascript" do
parameters "ds_cloud_vendor_accounts", "ds_get_caller_identity"
result "result"
code <<-EOS
result = _.find(ds_cloud_vendor_accounts, function(account) {
return account['id'] == ds_get_caller_identity[0]['account']
})
// This is in case the API does not return the relevant account info
if (result == undefined) {
result = {
id: ds_get_caller_identity[0]['account'],
name: "",
tags: {}
}
}
EOS
end
datasource "ds_iam_roles_without_tags" do
request do
auth $auth_aws
pagination $pagination_aws_iam_role_json
host "iam.amazonaws.com"
path "/"
query "Action", "ListRoles"
query "Version", "2010-05-08"
header "Accept", "application/json"
header "User-Agent", "RS Policies"
# Header X-Meta-Flexera has no affect on datasource query, but is required for Meta Policies
# Forces `ds_is_deleted` datasource to run first during policy execution
header "Meta-Flexera", val($ds_is_deleted, "path")
end
result do
encoding "json"
collect jmes_path(response, "ListRolesResponse.ListRolesResult.Roles[*]") do
field "path", jmes_path(col_item, "Path")
field "arn", jmes_path(col_item, "Arn")
field "roleName", jmes_path(col_item, "RoleName")
field "createDate", jmes_path(col_item, "CreateDate")
field "roleId", jmes_path(col_item, "RoleId")
field "description", jmes_path(col_item, "Description")
field "maxSessionDuration", jmes_path(col_item, "MaxSessionDuration")
end
end
end
datasource "ds_iam_roles_filtered" do
run_script $js_iam_roles_filtered, $ds_iam_roles_without_tags, $param_role_names
end
script "js_iam_roles_filtered", type:"javascript" do
parameters "ds_iam_roles_without_tags", "param_role_names"
result "result"
code <<-EOS
result = _.filter(ds_iam_roles_without_tags, function(role) {
return _.contains(param_role_names, role['roleId']) || _.contains(param_role_names, role['roleName']) || _.contains(param_role_names, role['arn'])
})
EOS
end
datasource "ds_iam_roles" do
iterate $ds_iam_roles_filtered
request do
auth $auth_aws
pagination $pagination_aws_iam_role_tag_json
host "iam.amazonaws.com"
path "/"
query "Action", "ListRoleTags"
query "Version", "2010-05-08"
query "RoleName", val(iter_item, "roleName")
header "Accept", "application/json"
header "User-Agent", "RS Policies"
end
result do
encoding "json"
collect jmes_path(response, "ListRoleTagsResponse.ListRoleTagsResult") do
field "tags" do
collect jmes_path(col_item, "Tags") do
field "key", jmes_path(col_item, "Key")
field "value", jmes_path(col_item, "Value")
end
end
field "path", val(iter_item, "path")
field "arn", val(iter_item, "arn")
field "roleName", val(iter_item, "roleName")
field "createDate", val(iter_item, "createDate")
field "roleId", val(iter_item, "roleId")
field "description", val(iter_item, "description")
field "maxSessionDuration", val(iter_item, "maxSessionDuration")
end
end
end
datasource "ds_missing_roles" do
run_script $js_missing_roles, $ds_iam_roles, $ds_aws_account, $ds_applied_policy, $param_role_names
end
script "js_missing_roles", type: "javascript" do
parameters "ds_iam_roles", "ds_aws_account", "ds_applied_policy", "param_role_names"
result "result"
code <<-EOS
existing_roles_by_name = {}
existing_roles_by_id = {}
existing_roles_by_arn = {}
_.each(ds_iam_roles, function(role) {
existing_roles_by_name[role["roleName"]] = role
existing_roles_by_id[role["roleId"]] = role
existing_roles_by_arn[role["arn"]] = role
})
result = _.map(param_role_names, function(role) {
found = false
description = ""
id = ""
name = ""
arn = ""
createDate = ""
tags = []
found_role = null
if (existing_roles_by_name[role] != undefined) {
found_role = existing_roles_by_name[role]
}
if (existing_roles_by_id[role] != undefined && found_role == null) {
found_role = existing_roles_by_id[role]
}
if (existing_roles_by_arn[role] != undefined && found_role == null) {
found_role = existing_roles_by_arn[role]
}
if (found_role != null) {
found = true
description = found_role["description"]
id = found_role["roleId"]
name = found_role["roleName"]
arn = found_role["arn"]
createDate = new Date(found_role["createDate"]).toISOString()
if (typeof(found_role['tags']) == 'object') {
tags = _.map(found_role['tags'], function(tag) {
return tag['key'] + "=" + tag['value']
})
}
}
return {
accountID: ds_aws_account["id"],
accountName: ds_aws_account["name"],
searchName: role,
resourceID: id,
resourceARN: arn,
resourceName: name,
description: description,
createDate: createDate,
found: found,
tags: tags.join(", "),
policy_name: ds_applied_policy["name"]
}
})
// Dummy entry to ensure validation occurs at least once
result.push({
accountID: "",
accountName: "",
searchName: "",
resourceID: "",
resourceARN: "",
resourceName: "",
description: "",
createDate: "",
found: "",
tags: "",
policy_name: ""
})
EOS
end
###############################################################################
# Policy
###############################################################################
policy "pol_iam_role_audit" do
validate_each $ds_missing_roles do
summary_template "{{ with index data 0 }}{{ .policy_name }}{{ end }}: {{ len data }} AWS IAM Roles Checked"
check logic_or($ds_parent_policy_terminated, eq(val(item, "searchName"), ""))
escalate $esc_email
hash_exclude "tags"
export do
resource_level true
field "accountID" do
label "Account ID"
end
field "accountName" do
label "Account Name"
end
field "searchName" do
label "Search Name/ID/ARN"
end
field "found" do
label "Role Exists?"
end
field "resourceID" do
label "Role ID"
end
field "resourceName" do
label "Role Name"
end
field "resourceARN" do
label "Role ARN"
end
field "description" do
label "Description"
end
field "createDate" do
label "Creation Date"
end
field "tags" do
label "Tags"
end
field "id" do
label "ID"
path "resourceID"
end
end
end
end
###############################################################################
# Escalations
###############################################################################
escalation "esc_email" do
automatic true
label "Send Email"
description "Send incident email"
email $param_email
end
###############################################################################
# Meta Policy [alpha]
# Not intended to be modified or used by policy developers
###############################################################################
# If the meta_parent_policy_id is not set it will evaluate to an empty string and we will look for the policy itself,
# if it is set we will look for the parent policy.
datasource "ds_get_policy" do
request do
auth $auth_flexera
host rs_governance_host
ignore_status [404]
path join(["/api/governance/projects/", rs_project_id, "/applied_policies/", switch(ne(meta_parent_policy_id, ""), meta_parent_policy_id, policy_id)])
header "Api-Version", "1.0"
end
result do
encoding "json"
field "id", jmes_path(response, "id")
end
end
datasource "ds_parent_policy_terminated" do
run_script $js_decide_if_self_terminate, $ds_get_policy, policy_id, meta_parent_policy_id
end
# If the policy was applied by a meta_parent_policy we confirm it exists if it doesn't we confirm we are deleting
# This information is used in two places:
# - determining whether or not we make a delete call
# - determining if we should create an incident (we don't want to create an incident on the run where we terminate)
script "js_decide_if_self_terminate", type: "javascript" do
parameters "found", "self_policy_id", "meta_parent_policy_id"
result "result"
code <<-EOS
var result
if (meta_parent_policy_id != "" && found.id == undefined) {
result = true
} else {
result = false
}
EOS
end
# Two potentials ways to set this up:
# - this way and make a unneeded 'get' request when not deleting
# - make the delete request an interate and have it iterate over an empty array when not deleting and an array with one item when deleting
script "js_make_terminate_request", type: "javascript" do
parameters "should_delete", "policy_id", "rs_project_id", "rs_governance_host"
result "request"
code <<-EOS
var request = {
auth: 'auth_flexera',
host: rs_governance_host,
path: "/api/governance/projects/" + rs_project_id + "/applied_policies/" + policy_id,
headers: {
"API-Version": "1.0",
"Content-Type":"application/json"
},
}
if (should_delete) {
request.verb = 'DELETE'
}
EOS
end
datasource "ds_terminate_self" do
request do
run_script $js_make_terminate_request, $ds_parent_policy_terminated, policy_id, rs_project_id, rs_governance_host
end
end
datasource "ds_is_deleted" do
run_script $js_check_deleted, $ds_terminate_self
end
# This is just a way to have the check delete request connect to the farthest leaf from policy.
# We want the delete check to the first thing the policy does to avoid the policy erroring before it can decide whether or not it needs to self terminate
# Example a customer deletes a credential and then terminates the parent policy. We still want the children to self terminate
# The only way I could see this not happening is if the user who applied the parent_meta_policy was offboarded or lost policy access, the policies who are impersonating the user
# would not have access to self-terminate
# It may be useful for the backend to enable a mass terminate at some point for all meta_child_policies associated with an id.
script "js_check_deleted", type: "javascript" do
parameters "response"
result "result"
code <<-EOS
result = { "path": "/" }
EOS
end