in Lambda/BackupOrgPolicyManager/src/BackupOrgPolicyManager.py [0:0]
def get_policy(policy_content, target_regions, variables=[]):
"""
Helper function for getting the policy contents from the event object. This function
will extract the policy from an S3 location if PolicyBucket and PolicyLocation is provided
"""
try:
if 'S3' in policy_content:
if 'PolicyBucket' in policy_content['S3']:
s3_bucket = policy_content['S3']['PolicyBucket']
else:
logger.error('S3 specified but no "PolicyBucket" indicated')
raise Exception('S3 specified but no "PolicyBucket" indicated')
if 'PolicyKey' in policy_content['S3']:
s3_object = policy_content['S3']['PolicyKey']
else:
logger.error('S3 specified but no "PolicyKey" indicated')
raise Exception('S3 specified but no "PolicyKey" indicated')
s3 = boto3.resource('s3')
policy_file = s3.Object(s3_bucket, s3_object)
policy_content = policy_file.get()['Body'].read().decode('utf-8')
# logger.info(f"policy_contents : {policy_contents}")
# Check for replacement variables
for variable in variables:
for key, value in variable.items():
logger.info(f"Replacing Key : {key} with value : {value}")
policy_content = policy_content.replace(key, value)
policy_content = json.loads(policy_content)
logger.info(f'policy is: {policy_content}')
for plan in policy_content['plans'].keys():
logger.info(f'processing plan: {plan}')
policy_content['plans'][plan]['regions'] = {'@@append': target_regions}
return policy_content
except Exception as e:
logger.error("Exception getting policy: {}".format(e))
raise