Skip to content

Commit adfb9cb

Browse files
authored
Merge pull request #2 from dap-aws/dap-aws-patch-2
Update ct_configrecorder_override_consumer.py to only include globals in home region
2 parents 6af361a + fa9ab8f commit adfb9cb

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

ct_configrecorder_override_consumer.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
8686
try:
8787
role_arn = 'arn:aws:iam::' + account_id + ':role/aws-service-role/config.amazonaws.com/AWSServiceRoleForConfig'
8888

89+
## added IAM global resource list
90+
GLOBAL_IAM_RESOURCE_LIST = ['AWS::IAM::Group','AWS::IAM::Policy','AWS::IAM::Role','AWS::IAM::User']
91+
8992
CONFIG_RECORDER_DAILY_RESOURCE_STRING = os.getenv('CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST')
9093
CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST = CONFIG_RECORDER_DAILY_RESOURCE_STRING.split(
9194
',') if CONFIG_RECORDER_DAILY_RESOURCE_STRING != '' else []
@@ -98,6 +101,18 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
98101
res = [x for x in CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST if x not in CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST]
99102
CONFIG_RECORDER_OVERRIDE_DAILY_RESOURCE_LIST[:] = res
100103

104+
## create two new lists - NOT_HOME and HOME
105+
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME = []
106+
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME = []
107+
108+
## remove any of the global IAM resources from exclusion list for HOME region
109+
home = [x for x in CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST if x not in GLOBAL_IAM_RESOURCE_LIST]
110+
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME[:] = home
111+
## take home list and add globals for NOT_HOME exclusion list for linked regions
112+
CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME = home + GLOBAL_IAM_RESOURCE_LIST
113+
114+
home_region = os.getenv('CONTROL_TOWER_HOME_REGION') == aws_region
115+
101116
# Event = Delete is when stack is deleted, we rollback changed made and leave it as ControlTower Intended
102117
if event == 'Delete':
103118
response = configservice.put_configuration_recorder(
@@ -119,7 +134,8 @@ def assume_role(account_id, role='AWSControlTowerExecution'):
119134
'allSupported': False,
120135
'includeGlobalResourceTypes': False,
121136
'exclusionByResourceTypes': {
122-
'resourceTypes': CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST
137+
## for exclusion list exclusionByResourceTypes.resourceTypes: if home_region==true use home, else use not_home
138+
'resourceTypes': CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_HOME if home_region == True else CONFIG_RECORDER_EXCLUSION_RESOURCE_LIST_NOT_HOME
123139
},
124140
'recordingStrategy': {
125141
'useOnly': 'EXCLUSION_BY_RESOURCE_TYPES'

0 commit comments

Comments
 (0)