-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathsubscribe.py
356 lines (298 loc) · 13.3 KB
/
subscribe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import json
import logging
import sys
from .utils import get_account_id
from awscli.customizations.commands import BasicCommand
from awscli.customizations.utils import s3_bucket_exists
from botocore.exceptions import ClientError
LOG = logging.getLogger(__name__)
S3_POLICY_TEMPLATE = 'policy/S3/AWSCloudTrail-S3BucketPolicy-2014-12-17.json'
SNS_POLICY_TEMPLATE = 'policy/SNS/AWSCloudTrail-SnsTopicPolicy-2014-12-17.json'
class CloudTrailError(Exception):
pass
class CloudTrailSubscribe(BasicCommand):
"""
Subscribe/update a user account to CloudTrail, creating the required S3 bucket,
the optional SNS topic, and starting the CloudTrail monitoring and logging.
"""
NAME = 'create-subscription'
DESCRIPTION = ('Creates and configures the AWS resources necessary to use'
' CloudTrail, creates a trail using those resources, and '
'turns on logging.')
SYNOPSIS = ('aws cloudtrail create-subscription'
' (--s3-use-bucket|--s3-new-bucket) bucket-name'
' [--sns-new-topic topic-name]\n')
ARG_TABLE = [
{'name': 'name', 'required': True, 'help_text': 'Cloudtrail name'},
{'name': 's3-new-bucket',
'help_text': 'Create a new S3 bucket with this name'},
{'name': 's3-use-bucket',
'help_text': 'Use an existing S3 bucket with this name'},
{'name': 's3-prefix', 'help_text': 'S3 object prefix'},
{'name': 'sns-new-topic',
'help_text': 'Create a new SNS topic with this name'},
{'name': 'include-global-service-events',
'help_text': 'Whether to include global service events'},
{'name': 's3-custom-policy',
'help_text': 'Custom S3 policy template or URL'},
{'name': 'sns-custom-policy',
'help_text': 'Custom SNS policy template or URL'}
]
UPDATE = False
_UNDOCUMENTED = True
def _run_main(self, args, parsed_globals):
self.setup_services(args, parsed_globals)
# Run the command and report success
self._call(args, parsed_globals)
return 0
def setup_services(self, args, parsed_globals):
client_args = {
'region_name': None,
'verify': None
}
if parsed_globals.region is not None:
client_args['region_name'] = parsed_globals.region
if parsed_globals.verify_ssl is not None:
client_args['verify'] = parsed_globals.verify_ssl
# Initialize services
LOG.debug('Initializing S3, SNS and CloudTrail...')
self.sts = self._session.create_client('sts', **client_args)
self.s3 = self._session.create_client('s3', **client_args)
self.sns = self._session.create_client('sns', **client_args)
self.region_name = self.s3.meta.region_name
# If the endpoint is specified, it is designated for the cloudtrail
# service. Not all of the other services will use it.
if parsed_globals.endpoint_url is not None:
client_args['endpoint_url'] = parsed_globals.endpoint_url
self.cloudtrail = self._session.create_client('cloudtrail', **client_args)
def _call(self, options, parsed_globals):
"""
Run the command. Calls various services based on input options and
outputs the final CloudTrail configuration.
"""
gse = options.include_global_service_events
if gse:
if gse.lower() == 'true':
gse = True
elif gse.lower() == 'false':
gse = False
else:
raise ValueError('You must pass either true or false to'
' --include-global-service-events.')
bucket = options.s3_use_bucket
if options.s3_new_bucket:
bucket = options.s3_new_bucket
if self.UPDATE and options.s3_prefix is None:
# Prefix was not passed and this is updating the S3 bucket,
# so let's find the existing prefix and use that if possible
res = self.cloudtrail.describe_trails(
trailNameList=[options.name])
trail_info = res['trailList'][0]
if 'S3KeyPrefix' in trail_info:
LOG.debug('Setting S3 prefix to {0}'.format(
trail_info['S3KeyPrefix']))
options.s3_prefix = trail_info['S3KeyPrefix']
self.setup_new_bucket(bucket, options.s3_prefix,
options.s3_custom_policy)
elif not bucket and not self.UPDATE:
# No bucket was passed for creation.
raise ValueError('You must pass either --s3-use-bucket or'
' --s3-new-bucket to create.')
if options.sns_new_topic:
try:
topic_result = self.setup_new_topic(options.sns_new_topic,
options.sns_custom_policy)
except Exception:
# Roll back any S3 bucket creation
if options.s3_new_bucket:
self.s3.delete_bucket(Bucket=options.s3_new_bucket)
raise
try:
cloudtrail_config = self.upsert_cloudtrail_config(
options.name,
bucket,
options.s3_prefix,
options.sns_new_topic,
gse
)
except Exception:
# Roll back any S3 bucket / SNS topic creations
if options.s3_new_bucket:
self.s3.delete_bucket(Bucket=options.s3_new_bucket)
if options.sns_new_topic:
self.sns.delete_topic(TopicArn=topic_result['TopicArn'])
raise
sys.stdout.write('CloudTrail configuration:\n{config}\n'.format(
config=json.dumps(cloudtrail_config, indent=2)))
if not self.UPDATE:
# If the configure call command above completes then this should
# have a really high chance of also completing
self.start_cloudtrail(options.name)
sys.stdout.write(
'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.get_object(
Bucket='awscloudtrail-policy-' + self.region_name,
Key=key_name)
return data['Body'].read().decode('utf-8')
except Exception as e:
raise CloudTrailError(
'Unable to get regional policy template for'
' region %s: %s. Error: %s', self.region_name, key_name, e)
def setup_new_bucket(self, bucket, prefix, custom_policy=None):
"""
Creates a new S3 bucket with an appropriate policy to let CloudTrail
write to the prefix path.
"""
sys.stdout.write(
'Setting up new S3 bucket {bucket}...\n'.format(bucket=bucket))
account_id = get_account_id(self.sts)
# Clean up the prefix - it requires a trailing slash if set
if prefix and not prefix.endswith('/'):
prefix += '/'
# Fetch policy data from S3 or a custom URL
if custom_policy is not None:
policy = custom_policy
else:
policy = self._get_policy(S3_POLICY_TEMPLATE)
policy = policy.replace('<BucketName>', bucket)\
.replace('<CustomerAccountID>', account_id)
if '<Prefix>/' in policy:
policy = policy.replace('<Prefix>/', prefix or '')
else:
policy = policy.replace('<Prefix>', prefix or '')
LOG.debug('Bucket policy:\n{0}'.format(policy))
bucket_exists = s3_bucket_exists(self.s3, bucket)
if bucket_exists:
raise Exception('Bucket {bucket} already exists.'.format(
bucket=bucket))
# If we are not using the us-east-1 region, then we must set
# a location constraint on the new bucket.
params = {'Bucket': bucket}
if self.region_name != 'us-east-1':
bucket_config = {'LocationConstraint': self.region_name}
params['CreateBucketConfiguration'] = bucket_config
data = self.s3.create_bucket(**params)
try:
self.s3.put_bucket_policy(Bucket=bucket, Policy=policy)
except ClientError:
# Roll back bucket creation.
self.s3.delete_bucket(Bucket=bucket)
raise
return data
def setup_new_topic(self, topic, custom_policy=None):
"""
Creates a new SNS topic with an appropriate policy to let CloudTrail
post messages to the topic.
"""
sys.stdout.write(
'Setting up new SNS topic {topic}...\n'.format(topic=topic))
account_id = get_account_id(self.sts)
# Make sure topic doesn't already exist
# Warn but do not fail if ListTopics permissions
# are missing from the IAM role?
try:
topics = self.sns.list_topics()['Topics']
except Exception:
topics = []
LOG.warn('Unable to list topics, continuing...')
if [t for t in topics if t['TopicArn'].split(':')[-1] == topic]:
raise Exception('Topic {topic} already exists.'.format(
topic=topic))
region = self.sns.meta.region_name
# Get the SNS topic policy information to allow CloudTrail
# write-access.
if custom_policy is not None:
policy = custom_policy
else:
policy = self._get_policy(SNS_POLICY_TEMPLATE)
policy = policy.replace('<Region>', region)\
.replace('<SNSTopicOwnerAccountId>', account_id)\
.replace('<SNSTopicName>', topic)
topic_result = self.sns.create_topic(Name=topic)
try:
# Merge any existing topic policy with our new policy statements
topic_attr = self.sns.get_topic_attributes(
TopicArn=topic_result['TopicArn'])
policy = self.merge_sns_policy(topic_attr['Attributes']['Policy'],
policy)
LOG.debug('Topic policy:\n{0}'.format(policy))
# Set the topic policy
self.sns.set_topic_attributes(TopicArn=topic_result['TopicArn'],
AttributeName='Policy',
AttributeValue=policy)
except Exception:
# Roll back topic creation
self.sns.delete_topic(TopicArn=topic_result['TopicArn'])
raise
return topic_result
def merge_sns_policy(self, left, right):
"""
Merge two SNS topic policy documents. The id information from
``left`` is used in the final document, and the statements
from ``right`` are merged into ``left``.
http://docs.aws.amazon.com/sns/latest/dg/BasicStructure.html
:type left: string
:param left: First policy JSON document
:type right: string
:param right: Second policy JSON document
:rtype: string
:return: Merged policy JSON
"""
left_parsed = json.loads(left)
right_parsed = json.loads(right)
left_parsed['Statement'] += right_parsed['Statement']
return json.dumps(left_parsed)
def upsert_cloudtrail_config(self, name, bucket, prefix, topic, gse):
"""
Either create or update the CloudTrail configuration depending on
whether this command is a create or update command.
"""
sys.stdout.write('Creating/updating CloudTrail configuration...\n')
config = {
'Name': name
}
if bucket is not None:
config['S3BucketName'] = bucket
if prefix is not None:
config['S3KeyPrefix'] = prefix
if topic is not None:
config['SnsTopicName'] = topic
if gse is not None:
config['IncludeGlobalServiceEvents'] = gse
if not self.UPDATE:
self.cloudtrail.create_trail(**config)
else:
self.cloudtrail.update_trail(**config)
return self.cloudtrail.describe_trails()
def start_cloudtrail(self, name):
"""
Start the CloudTrail service, which begins logging.
"""
sys.stdout.write('Starting CloudTrail service...\n')
return self.cloudtrail.start_logging(Name=name)
class CloudTrailUpdate(CloudTrailSubscribe):
"""
Like subscribe above, but the update version of the command.
"""
NAME = 'update-subscription'
UPDATE = True
DESCRIPTION = ('Updates any of the trail configuration settings, and'
' creates and configures any new AWS resources specified.')
SYNOPSIS = ('aws cloudtrail update-subscription'
' [(--s3-use-bucket|--s3-new-bucket) bucket-name]'
' [--sns-new-topic topic-name]\n')