Skip to content

update to allow exclusions and record Global IAM resource-types in the Control Tower Home region only. #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion ct_configrecorder_override_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
try:
role_arn = 'arn:aws:iam::' + account_id + ':role/aws-service-role/config.amazonaws.com/AWSServiceRoleForConfig'

## added IAM global resource list
GLOBAL_IAM_RESOURCE_LIST = ['AWS::IAM::Group','AWS::IAM::Policy','AWS::IAM::Role','AWS::IAM::User']

CONFIG_RECORDER_DAILY_RESOURCE_STRING = os.getenv('CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST')
CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST = CONFIG_RECORDER_DAILY_RESOURCE_STRING.split(
',') if CONFIG_RECORDER_DAILY_RESOURCE_STRING != '' else []
Expand All @@ -98,6 +101,18 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
res = [x for x in CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST if x not in CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST]
CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST[:] = res

## create two new lists - NOT_HOME and HOME
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME = []
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME = []

## remove any of the global IAM resources from exclusion list for HOME region
home = [x for x in CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST if x not in GLOBAL_IAM_RESOURCE_LIST]
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME[:] = home
## take home list and add globals for NOT_HOME exclusion list for linked regions
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME = home + GLOBAL_IAM_RESOURCE_LIST

home_region = os.getenv('CONTROL_TOWER_HOME_REGION') == aws_region

# Event = Delete is when stack is deleted, we rollback changed made and leave it as ControlTower Intended
if event == 'Delete':
response = configservice.put_configuration_recorder(
Expand All @@ -119,7 +134,8 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
'allSupported': False,
'includeGlobalResourceTypes': False,
'exclusionByResourceTypes': {
'resourceTypes': CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST
## for exclusion list exclusionByResourceTypes.resourceTypes: if home_region==true use home, else use not_home
'resourceTypes': CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME if home_region == True else CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME
},
'recordingStrategy': {
'useOnly': 'EXCLUSION_BY_RESOURCE_TYPES'
Expand Down
1 change: 1 addition & 0 deletions template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ Resources:
CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST: !Ref ConfigRecorderDailyResourceTypes
CONFIG_RECORDER_OVERRIDE_EXCLUDED_RESOURCE_LIST: !Ref ConfigRecorderExcludedResourceTypes
CONFIG_RECORDER_DEFAULT_RECORDING_FREQUENCY: !Ref ConfigRecorderDefaultRecordingFrequency
CONTROL_TOWER_HOME_REGION: !Ref 'AWS::Region'

ConsumerLambdaEventSourceMapping:
Type: AWS::Lambda::EventSourceMapping
Expand Down