Skip to content

Commit ae8a663

Browse files
authored
Enable account exclusions via a CSV list (#237)
1 parent b07cfae commit ae8a663

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

data-collection/deploy/account-collector.yaml

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ Resources:
114114
RESOURCE_PREFIX = os.environ['RESOURCE_PREFIX']
115115
MANAGEMENT_ACCOUNT_IDS = os.environ['MANAGEMENT_ACCOUNT_IDS']
116116
BUCKET = os.environ['BUCKET_NAME']
117-
PREDEF_ACCOUNT_LIST_KEY = os.environ['PREDEF_ACCOUNT_LIST_KEY']
118-
LINKED_ACCOUNT_LIST_KEY = os.environ['LINKED_ACCOUNT_LIST_KEY']
119-
PAYER_ACCOUNT_LIST_KEY = os.environ['PAYER_ACCOUNT_LIST_KEY']
117+
PREDEF_ACCOUNT_LIST_KEY = os.environ.get('PREDEF_ACCOUNT_LIST_KEY')
118+
LINKED_ACCOUNT_LIST_KEY = os.environ.get('LINKED_ACCOUNT_LIST_KEY')
119+
PAYER_ACCOUNT_LIST_KEY = os.environ.get('PAYER_ACCOUNT_LIST_KEY')
120+
EXCLUDED_ACCOUNT_LIST_KEY = os.environ.get('EXCLUDED_ACCOUNT_LIST_KEY')
120121
TMP_FILE = "/tmp/data.json"
121122
122123
logger = logging.getLogger(__name__)
@@ -145,7 +146,6 @@ Resources:
145146
raise Exception(f"Lambda event must have 'Type' parameter with value = ({list(functions.keys())})") #pylint: disable=broad-exception-raised
146147
147148
account_iterator = functions[account_type]
148-
149149
with open(TMP_FILE, "w") as f:
150150
count = 0
151151
f.write("[\n")
@@ -185,7 +185,7 @@ Resources:
185185
defined_accounts, ext = get_defined_list(BUCKET, PREDEF_ACCOUNT_LIST_KEY)
186186
try:
187187
if defined_accounts:
188-
logger.info(f'Using defined account list instead of payer organization')
188+
logger.info(f'Using defined account list found in s3://{BUCKET}/{PREDEF_ACCOUNT_LIST_KEY} instead of payer organization')
189189
for account_data in defined_accounts:
190190
if ext == "json":
191191
account = json.loads(account_data)
@@ -195,10 +195,20 @@ Resources:
195195
yield format_account(account[0], account[1], account[2])
196196
else:
197197
logger.info(f'Using payer organization for the account list')
198+
excluded_accounts = get_from_bucket(BUCKET, EXCLUDED_ACCOUNT_LIST_KEY)
199+
if excluded_accounts:
200+
pass
201+
logger.info(f'Found list of accounts to exclude in s3://{BUCKET}/{EXCLUDED_ACCOUNT_LIST_KEY}. Will only collect accounts that are not in the list')
202+
excluded_accounts = [a.strip() for a in excluded_accounts[0].split(',') if a]
198203
for org_account_data in iterate_admins_accounts('organizations'):
204+
logger.info(f'Collecting accounts for payer {org_account_data}')
199205
org_account = json.loads(org_account_data['account'])
206+
logger.info(f'org_account: {org_account}')
200207
organizations = get_client_with_role(service="organizations", account_id=org_account['account_id'], region="us-east-1") #MUST be us-east-1
201208
for account in organizations.get_paginator("list_accounts").paginate().search("Accounts[?Status=='ACTIVE']"):
209+
if excluded_accounts and account.get('Id') in excluded_accounts:
210+
logger.debug(f'Excluding account {account.get("Id")}')
211+
continue
202212
yield format_account(account.get('Id'), account.get('Name'), org_account['payer_id'])
203213
except Exception as exc: #pylint: disable=broad-exception-caught
204214
logger.error(f'{org_account}: {exc}')
@@ -207,14 +217,20 @@ Resources:
207217
s3 = boto3.client("s3")
208218
exts = [".json", ".csv"]
209219
for ext in exts:
210-
try:
211-
accts = s3.get_object(Bucket=bucket, Key=f"{key}{ext}")
212-
return accts['Body'].read().decode('utf-8').strip('\n').split('\n'), ext
213-
except Exception as exc: #pylint: disable=broad-exception-caught
214-
continue
220+
accts = get_from_bucket(bucket, key, s3)
221+
if accts:
222+
return accts, ext
215223
logger.debug(f'Predefined account list not retrieved or not being used')
216224
return None, None
217225
226+
def get_from_bucket(bucket, key, client=None):
227+
s3 = client if client else boto3.client("s3")
228+
try:
229+
data = s3.get_object(Bucket=bucket, Key=key)
230+
return data['Body'].read().decode('utf-8').strip('\n').split('\n')
231+
except Exception as exc: #pylint: disable=broad-exception-caught
232+
return None
233+
218234
def format_account(account_id, account_name, payer_id):
219235
return {
220236
"account": json.dumps({
@@ -251,6 +267,7 @@ Resources:
251267
PREDEF_ACCOUNT_LIST_KEY: "account-list/account-list"
252268
LINKED_ACCOUNT_LIST_KEY: "account-list/linked-account-list.json"
253269
PAYER_ACCOUNT_LIST_KEY: "account-list/payer-account-list.json"
270+
EXCLUDED_ACCOUNT_LIST_KEY: "account-list/excluded-linked-account-list.csv"
254271
Metadata:
255272
cfn_nag:
256273
rules_to_suppress:

0 commit comments

Comments
 (0)