Skip to content

Commit

Permalink
Minor refactor, log extra info on error
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Feb 23, 2015
1 parent 99ca9a6 commit 8baecb2
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions awscli/customizations/cloudtrail.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,18 @@ def _call(self, options, parsed_globals):
'Logs will be delivered to {bucket}:{prefix}\n'.format(
bucket=bucket, prefix=options.s3_prefix or ''))

def _get_policy(self, key_name):
try:
data = self.s3.GetObject(
bucket='awscloudtrail-policy-' + self.region_name,
key=key_name)
except Exception:
LOG.error('Unable to get regional policy template for'
' region %s: %s', self.region_name, key_name)
raise

return data['Body'].read().decode('utf-8')

def setup_new_bucket(self, bucket, prefix, policy_url=None):
"""
Creates a new S3 bucket with an appropriate policy to let CloudTrail
Expand All @@ -206,10 +218,7 @@ def setup_new_bucket(self, bucket, prefix, policy_url=None):
if policy_url:
policy = requests.get(policy_url).text
else:
data = self.s3.GetObject(
bucket='awscloudtrail-policy-' + self.region_name,
key=S3_POLICY_TEMPLATE)
policy = data['Body'].read().decode('utf-8')
policy = self._get_policy(S3_POLICY_TEMPLATE)

policy = policy.replace('<BucketName>', bucket)\
.replace('<CustomerAccountID>', account_id)
Expand Down Expand Up @@ -284,10 +293,7 @@ def setup_new_topic(self, topic, policy_url=None):
if policy_url:
policy = requests.get(policy_url).text
else:
data = self.s3.GetObject(
bucket='awscloudtrail-policy-' + self.region_name,
key=SNS_POLICY_TEMPLATE)
policy = data['Body'].read().decode('utf-8')
policy = self._get_policy(SNS_POLICY_TEMPLATE)

policy = policy.replace('<Region>', region)\
.replace('<SNSTopicOwnerAccountId>', account_id)\
Expand Down

0 comments on commit 8baecb2

Please sign in to comment.