-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example of regex checking for IAM arns (#144)
* Added example of regex checking for IAM arns * Fixed line formatting * Fixed lint issues * Bugfix: role patterns * Fix: Removed 2 lines - unnecessary return * Added test case for role pattern * Changed test case so it reaches the intended condition * Fixed tests
- Loading branch information
1 parent
dce2f08
commit 9341679
Showing
2 changed files
with
71 additions
and
13 deletions.
There are no files selected for viewing
41 changes: 28 additions & 13 deletions
41
aws_cloudtrail_rules/aws_iam_entity_created_without_cloudformation.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,45 @@ | ||
import re | ||
|
||
# The role dedicated for IAM administration | ||
IAM_ADMIN_ROLES = { | ||
'arn:aws:iam::123456789012:role/IdentityCFNServiceRole', | ||
"arn:aws:iam::123456789012:role/IdentityCFNServiceRole", | ||
} | ||
|
||
# The role patterns dedicated for IAM Service Roles | ||
IAM_ADMIN_ROLE_PATTERNS = {"arn:aws:iam::[0-9]+:role/IdentityCFNServiceRole"} | ||
|
||
# API calls that are indicative of IAM entity creation | ||
IAM_ENTITY_CREATION_EVENTS = { | ||
'BatchCreateUser', | ||
'CreateGroup', | ||
'CreateInstanceProfile', | ||
'CreatePolicy', | ||
'CreatePolicyVersion', | ||
'CreateRole', | ||
'CreateServiceLinkedRole', | ||
'CreateUser', | ||
"BatchCreateUser", | ||
"CreateGroup", | ||
"CreateInstanceProfile", | ||
"CreatePolicy", | ||
"CreatePolicyVersion", | ||
"CreateRole", | ||
"CreateServiceLinkedRole", | ||
"CreateUser", | ||
} | ||
|
||
|
||
def rule(event): | ||
# Check if this event is in scope | ||
if event['eventName'] not in IAM_ENTITY_CREATION_EVENTS: | ||
if event["eventName"] not in IAM_ENTITY_CREATION_EVENTS: | ||
return False | ||
|
||
# All IAM changes MUST go through CloudFormation | ||
if event['userIdentity'].get('invokedBy') != 'cloudformation.amazonaws.com': | ||
if event["userIdentity"].get("invokedBy") != "cloudformation.amazonaws.com": | ||
return True | ||
|
||
# Only approved IAM Roles can make IAM Changes | ||
return event['userIdentity']['sessionContext']['sessionIssuer'][ | ||
'arn'] not in IAM_ADMIN_ROLES | ||
for admin_role_pattern in IAM_ADMIN_ROLE_PATTERNS: | ||
# Check if the arn matches any role patterns, returns False (whitelisting it) if there is a match | ||
if (len( | ||
re.findall( | ||
admin_role_pattern, | ||
event["userIdentity"]["sessionContext"]["sessionIssuer"] | ||
["arn"], | ||
)) > 0): | ||
return False | ||
|
||
return (event["userIdentity"]["sessionContext"]["sessionIssuer"]["arn"] | ||
not in IAM_ADMIN_ROLES) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters