Skip to content

Commit 2266f02

Browse files
authored
Merge pull request #35 from awslabs/1.5.0
1.5.0
2 parents e276df7 + 4fe39e6 commit 2266f02

13 files changed

Lines changed: 542 additions & 24 deletions

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,14 @@
171171

172172
### Changed
173173
* References to Team DragonCat are now referred to Customer Incident Response Team (CIRT).
174-
* Various argparse help message to be more descriptive.
174+
* Various argparse help message to be more descriptive.
175+
176+
## [1.5.0] - 2021-12-01
177+
178+
### Added
179+
* Ability to turn on Amazon S3 Server Access logs within single and multi-account modes.
180+
* Added Dry Run capabilities for Amazon S3 Server Access logs within single and multi-account modes.
181+
* Added Cleanup capabilities for Amazon S3 Server Access logs created by Assisted Log Enabler within single account mode.
182+
* Updated help (-h) message example within the README.
183+
* Permissions examples for enabling Amazon S3 Server Access logs within the permissions directory.
184+
* Diagram for Amazon S3 Server Access Logs within the README.

README.md

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ With Assisted Log Enabler for AWS, logging is turned on automatically for the va
66
* AWS CloudTrail (Single Account Only)
77
* Amazon Elastic Kubernetes Service (EKS) Audit and Authenticator Logs (Single Account and Multi-Account using AWS Organizations)
88
* Amazon Route 53 Resolver Query Logs (Single Account and Multi-Account using AWS Organizations)
9+
* NEW! Amazon S3 Server Access Logs (Single Account and Multi-Account using AWS Organizations)
910

1011
Link to related AWS Open Source Blog Post: [Introducing Assisted Log Enabler for AWS](https://aws.amazon.com/blogs/opensource/introducing-assisted-log-enabler-for-aws/)
1112

@@ -21,6 +22,10 @@ The following is a simple diagram on how Assisted Log Enabler for AWS works in a
2122

2223
![Alt text](diagrams/assisted_log_enabler.png)
2324

25+
NEW! The following is a simple diagram on how Assisted Log Enabler for AWS works with turning on Amazon S3 Server Access Logging in a single account:
26+
27+
![Alt text](diagrams/assisted_log_enabler_s3.png)
28+
2429
## Prerequisites
2530
### Permissions
2631
The following permissions are needed within AWS IAM for Assisted Log Enabler for AWS to run. Please see each section for a breakdown per AWS Service and functionality:
@@ -48,6 +53,15 @@ The following permissions are needed within AWS IAM for Assisted Log Enabler for
4853
"route53resolver:ListTagsForResource",
4954
"route53resolver:DisassociateResolverQueryLogConfig",
5055
"route53resolver:DeleteResolverQueryLogConfig"
56+
"s3:PutBucketLogging",
57+
"s3:GetBucketLogging",
58+
"s3:ListBucket",
59+
"s3:ListAllMyBuckets",
60+
"s3:GetBucketLocation",
61+
"s3:GetBucketAcl",
62+
"s3:PutBucketAcl",
63+
"s3:PutBucketPublicAccessBlock",
64+
"s3:PutBucketLifecycleConfiguration"
5165
5266
# For adding AWS CloudTrail logs:
5367
"s3:GetBucketPolicy",
@@ -86,7 +100,18 @@ The following permissions are needed within AWS IAM for Assisted Log Enabler for
86100
"route53resolver:AssociateResolverQueryLogConfig",
87101
"iam:CreateServiceLinkRole" # This is used to create the AWSServiceRoleForRoute53 Resolver, which is used for creating the Amazon Route 53 Query Logging Configurations.
88102
89-
# NEW! For cleanup of Amazon Route 53 Resolver Query Logs created by Assisted Log Enabler for AWS:
103+
# NEW! For adding Amazon S3 Server Access Logs:
104+
"s3:PutBucketLogging",
105+
"s3:GetBucketLogging",
106+
"s3:ListBucket",
107+
"s3:ListAllMyBuckets",
108+
"s3:GetBucketLocation",
109+
"s3:GetBucketAcl",
110+
"s3:PutBucketAcl",
111+
"s3:PutBucketPublicAccessBlock",
112+
"s3:PutBucketLifecycleConfiguration"
113+
114+
# For cleanup of Amazon Route 53 Resolver Query Logs created by Assisted Log Enabler for AWS:
90115
"route53resolver:ListResolverQueryLogConfigs",
91116
"route53resolver:ListTagsForResource",
92117
"route53resolver:ListResolverQueryLogConfigAssociations",
@@ -110,6 +135,9 @@ The following are the details of what happens within the Assisted Log Enabler fo
110135
* If no trail is configured, one is created and configured to log to the bucket created. (Single Account only as of this release)
111136
* If Amazon EKS Clusters exist, audit & authenticator logs are turned on.
112137
* Amazon Route 53 Query Logging is turned on for VPCs that do not have it turned on already.
138+
* NEW! Amazon S3 Server Access Logs are created for buckets that do not have it turned on already.
139+
* This does not include for S3 buckets created by Assisted Log Enabler for AWS
140+
* Amazon S3 Server Access Logs require buckets that reside in the same account & region, so additional buckets for Amazon S3 Server Access logs are created for this.
113141

114142

115143
## Running the Code
@@ -149,27 +177,58 @@ No valid option selected. Please run with -h to display valid options.
149177
```
150178
python3 assisted_log_enabler.py -h
151179
usage: assisted_log_enabler.py [-h] [--mode MODE] [--all] [--eks] [--vpcflow]
152-
[--r53querylogs] [--cloudtrail]
180+
[--r53querylogs] [--s3logs] [--cloudtrail]
181+
[--single_r53querylogs] [--single_cloudtrail]
182+
[--single_vpcflow] [--single_all]
183+
[--single_s3logs] [--single_account]
184+
[--multi_account]
153185
154186
Assisted Log Enabler - Find resources that are not logging, and turn them on.
155187
156188
optional arguments:
157-
-h, --help show this help message and exit
158-
--mode MODE Choose the mode that you want to run Assisted Log Enabler
159-
in. Available modes: single_account, multi_account. WARNING:
160-
For multi_account, You must have the associated
161-
CloudFormation template deployed as a StackSet. See the
162-
README file for more details.
163-
164-
Service Options:
189+
-h, --help show this help message and exit
190+
--mode MODE Choose the mode that you want to run Assisted Log
191+
Enabler in. Available modes: single_account,
192+
multi_account, cleanup, dryrun. WARNING: For
193+
multi_account, You must have the associated
194+
CloudFormation template deployed as a StackSet. See
195+
the README file for more details.
196+
197+
Single & Multi Account Options:
165198
Use these flags to choose which services you want to turn logging on for.
166199
167-
--all Turns on all of the log types within the Assisted Log
168-
Enabler for AWS.
169-
--eks Turns on Amazon EKS audit & authenticator logs.
170-
--vpcflow Turns on Amazon VPC Flow Logs.
171-
--r53querylogs Turns on Amazon Route 53 Resolver Query Logs.
172-
--cloudtrail Turns on AWS CloudTrail.
200+
--all Turns on all of the log types within the Assisted Log
201+
Enabler for AWS.
202+
--eks Turns on Amazon EKS audit & authenticator logs.
203+
--vpcflow Turns on Amazon VPC Flow Logs.
204+
--r53querylogs Turns on Amazon Route 53 Resolver Query Logs.
205+
--s3logs Turns on Amazon Bucket Logs.
206+
--cloudtrail Turns on AWS CloudTrail.
207+
208+
Cleanup Options:
209+
Use these flags to choose which resources you want to turn logging off
210+
for.
211+
212+
--single_r53querylogs
213+
Removes Amazon Route 53 Resolver Query Log resources
214+
created by Assisted Log Enabler for AWS.
215+
--single_cloudtrail Removes AWS CloudTrail trails created by Assisted Log
216+
Enabler for AWS.
217+
--single_vpcflow Removes Amazon VPC Flow Log resources created by
218+
Assisted Log Enabler for AWS.
219+
--single_all Turns off all of the log types within the Assisted Log
220+
Enabler for AWS.
221+
--single_s3logs Removes Amazon Bucket Log resources created by
222+
Assisted Log Enabler for AWS.
223+
224+
Dry Run Options:
225+
Use these flags to run Assisted Log Enabler for AWS in Dry Run mode.
226+
227+
--single_account Runs Assisted Log Enabler for AWS in Dry Run mode for
228+
a single AWS account.
229+
--multi_account Runs Assisted Log Enabler for AWS in Dry Run mode for
230+
a multi-account AWS environment, using AWS
231+
Organizations.
173232
```
174233

175234
### Step-by-Step Instructions (for running in AWS CloudShell, single account mode)
@@ -197,6 +256,8 @@ python3 assisted_log_enabler.py --mode single_account --vpcflow
197256
python3 assisted_log_enabler.py --mode single_account --r53querylogs
198257
# For AWS CloudTrail:
199258
python3 assisted_log_enabler.py --mode single_account --cloudtrail
259+
# NEW! For Amazon S3 Server Access Logs:
260+
python3 assisted_log_enabler.py --mode single_account --s3logs
200261
```
201262

202263
### Step-by-Step Instructions (for running in AWS CloudShell, multi account mode)
@@ -254,6 +315,8 @@ python3 assisted_log_enabler.py --mode multi_account --eks
254315
python3 assisted_log_enabler.py --mode multi_account --vpcflow
255316
# For Amazon Route 53 Resolver Query Logs:
256317
python3 assisted_log_enabler.py --mode multi_account --r53querylogs
318+
# NEW! For Amazon S3 Server Access Logs:
319+
python3 assisted_log_enabler.py --mode multi_account --s3logs
257320
258321
```
259322

@@ -286,7 +349,7 @@ To run Assisted Log Enabler for AWS in Dry Run mode, you can use the commands be
286349
# Single Account Dry Run
287350
python3 assisted_log_enabler.py --mode dryrun --single_account
288351
# Multi-Account Dry Run
289-
python3 assisted_log_enabler.py --mode dryrun --single_account
352+
python3 assisted_log_enabler.py --mode dryrun --multi_account
290353
```
291354

292355
## Cleaning Up
@@ -303,6 +366,8 @@ python3 assisted_log_enabler.py --mode cleanup --single_r53querylogs
303366
python3 assisted_log_enabler.py --mode cleanup --single_vpcflow
304367
# To remove AWS CloudTrail trails created by Assisted Log Enabler for AWS (single account):
305368
python3 assisted_log_enabler.py --mode cleanup --single_cloudtrail
369+
# NEW! To remove Amazon S3 Server Access logging created by Assisted Log Enabler for AWS (single account):
370+
python3 assisted_log_enabler.py --mode cleanup --single_s3logs
306371
```
307372

308373
## Shared Responsibility Model

assisted_log_enabler.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from botocore.exceptions import ClientError
1414
from datetime import timezone
1515

16-
1716
from subfunctions import ALE_multi_account
1817
from subfunctions import ALE_single_account
1918
from subfunctions import ALE_cleanup_single
@@ -82,13 +81,15 @@ def assisted_log_enabler():
8281
function_parser_group.add_argument('--eks', action='store_true', help=' Turns on Amazon EKS audit & authenticator logs.')
8382
function_parser_group.add_argument('--vpcflow', action='store_true', help=' Turns on Amazon VPC Flow Logs.')
8483
function_parser_group.add_argument('--r53querylogs', action='store_true', help=' Turns on Amazon Route 53 Resolver Query Logs.')
84+
function_parser_group.add_argument('--s3logs', action='store_true', help=' Turns on Amazon Bucket Logs.')
8585
function_parser_group.add_argument('--cloudtrail', action='store_true', help=' Turns on AWS CloudTrail. Only available in Single Account version.')
8686

8787
cleanup_parser_group = parser.add_argument_group('Cleanup Options', 'Use these flags to choose which resources you want to turn logging off for.')
8888
cleanup_parser_group.add_argument('--single_r53querylogs', action='store_true', help=' Removes Amazon Route 53 Resolver Query Log resources created by Assisted Log Enabler for AWS.')
8989
cleanup_parser_group.add_argument('--single_cloudtrail', action='store_true', help=' Removes AWS CloudTrail trails created by Assisted Log Enabler for AWS.')
9090
cleanup_parser_group.add_argument('--single_vpcflow', action='store_true', help=' Removes Amazon VPC Flow Log resources created by Assisted Log Enabler for AWS.')
9191
cleanup_parser_group.add_argument('--single_all', action='store_true', help=' Turns off all of the log types within the Assisted Log Enabler for AWS.')
92+
cleanup_parser_group.add_argument('--single_s3logs', action='store_true', help=' Removes Amazon Bucket Log resources created by Assisted Log Enabler for AWS.')
9293

9394
dryrun_parser_group = parser.add_argument_group('Dry Run Options', 'Use these flags to run Assisted Log Enabler for AWS in Dry Run mode.')
9495
dryrun_parser_group.add_argument('--single_account', action='store_true', help=' Runs Assisted Log Enabler for AWS in Dry Run mode for a single AWS account.')
@@ -106,6 +107,8 @@ def assisted_log_enabler():
106107
ALE_single_account.run_vpc_flow_logs()
107108
elif args.r53querylogs:
108109
ALE_single_account.run_r53_query_logs()
110+
elif args.s3logs:
111+
ALE_single_account.run_s3_logs()
109112
elif args.cloudtrail:
110113
ALE_single_account.run_cloudtrail()
111114
elif args.all:
@@ -119,13 +122,17 @@ def assisted_log_enabler():
119122
ALE_multi_account.run_vpc_flow_logs()
120123
elif args.r53querylogs:
121124
ALE_multi_account.run_r53_query_logs()
125+
elif args.s3logs:
126+
ALE_multi_account.run_s3_logs()
122127
elif args.all:
123128
ALE_multi_account.lambda_handler(event, context)
124129
else:
125130
logging.info("No valid option selected. Please run with -h to display valid options.")
126131
elif args.mode == 'cleanup':
127132
if args.single_r53querylogs:
128133
ALE_cleanup_single.run_r53_cleanup()
134+
elif args.single_s3logs:
135+
ALE_cleanup_single.run_s3_cleanup()
129136
elif args.single_cloudtrail:
130137
ALE_cleanup_single.run_cloudtrail_cleanup()
131138
elif args.single_vpcflow:
41 KB
Loading

permissions/ALE_child_account_role.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ Resources:
5050
- route53resolver:ListResolverQueryLogConfigAssociations
5151
- route53resolver:CreateResolverQueryLogConfig
5252
- route53resolver:AssociateResolverQueryLogConfig
53+
- s3:PutBucketLogging
54+
- s3:GetBucketLogging
55+
- s3:ListBucket
56+
- s3:ListAllMyBuckets
57+
- s3:GetBucketLocation
58+
- s3:GetBucketAcl
59+
- s3:PutBucketAcl
60+
- s3:PutBucketPublicAccessBlock
61+
- s3:PutBucketLifecycleConfiguration
5362
Resource: '*'
5463
Condition:
5564
StringEquals:

permissions/ALE_permissions_example_multi_account.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,16 @@
2525
"eks:ListClusters",
2626
"route53resolver:ListResolverQueryLogConfigAssociations",
2727
"route53resolver:CreateResolverQueryLogConfig",
28-
"route53resolver:AssociateResolverQueryLogConfig"
29-
28+
"route53resolver:AssociateResolverQueryLogConfig",
29+
"s3:PutBucketLogging",
30+
"s3:GetBucketLogging",
31+
"s3:ListBucket",
32+
"s3:ListAllMyBuckets",
33+
"s3:GetBucketLocation",
34+
"s3:GetBucketAcl",
35+
"s3:PutBucketAcl",
36+
"s3:PutBucketPublicAccessBlock",
37+
"s3:PutBucketLifecycleConfiguration"
3038
],
3139
"Resource": "*",
3240
"Condition": {

permissions/ALE_permissions_example_single_account.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
"eks:ListClusters",
2727
"route53resolver:ListResolverQueryLogConfigAssociations",
2828
"route53resolver:CreateResolverQueryLogConfig",
29-
"route53resolver:AssociateResolverQueryLogConfig"
29+
"route53resolver:AssociateResolverQueryLogConfig",
30+
"s3:PutBucketLogging",
31+
"s3:GetBucketLogging",
32+
"s3:ListBucket",
33+
"s3:ListAllMyBuckets",
34+
"s3:GetBucketLocation",
35+
"s3:GetBucketAcl",
36+
"s3:PutBucketAcl",
37+
"s3:PutBucketPublicAccessBlock",
38+
"s3:PutBucketLifecycleConfiguration"
3039
],
3140
"Resource": "*",
3241
"Condition": {

subfunctions/ALE_cleanup_single.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,52 @@ def vpcflow_cleanup():
178178
except Exception as exception_handle:
179179
logging.error(exception_handle)
180180

181+
# 4. Remove the S3 Logging Resources created by Assisted Log Enabler
182+
def s3_cleanup():
183+
"""Function to clean up Bucket Logs"""
184+
logging.info("Cleaning up Bucket Logs created by Assisted Log Enabler for AWS.")
185+
for aws_region in region_list:
186+
s3 = boto3.client('s3', region_name=aws_region)
187+
try:
188+
logging.info("---- LINE BREAK BETWEEN REGIONS ----")
189+
logging.info("Cleaning up Bucket Logs created by Assisted Log Enabler for AWS in region " + aws_region + ".")
190+
removal_list: list = []
191+
logging.info("ListBuckets API Call")
192+
buckets = s3.list_buckets()
193+
for bucket in buckets['Buckets']:
194+
s3region=s3.get_bucket_location(Bucket=bucket["Name"])['LocationConstraint']
195+
if s3region == aws_region:
196+
if 'aws-s3-log-collection-' not in str(bucket["Name"]):
197+
logging.info("Parsed out buckets created by Assisted Log Enabler for AWS in " + aws_region)
198+
logging.info("Checking remaining buckets to see if logs were enabled by Assisted Log Enabler for AWS in " + aws_region)
199+
logging.info("GetBucketLogging API Call for " + bucket["Name"])
200+
s3temp=s3.get_bucket_logging(Bucket=bucket["Name"])
201+
if 'aws-s3-log-collection-' in str(s3temp):
202+
removal_list.append(bucket["Name"])
203+
elif s3region is None and aws_region == 'us-east-1':
204+
if 'aws-s3-log-collection-' not in str(bucket["Name"]):
205+
logging.info("Parsed out buckets created by Assisted Log Enabler for AWS in " + aws_region)
206+
logging.info("Checking remaining buckets to see if logs were enabled by Assisted Log Enabler for AWS in " + aws_region)
207+
logging.info("GetBucketLogging API Call for " + bucket["Name"])
208+
s3temp=s3.get_bucket_logging(Bucket=bucket["Name"])
209+
if 'aws-s3-log-collection-' in str(s3temp):
210+
removal_list.append(bucket["Name"])
211+
if removal_list != []:
212+
logging.info("List S3 Buckets with Logging enabled by by Assisted Log Enabler for AWS in " + aws_region)
213+
print(removal_list)
214+
for bucket in removal_list:
215+
logging.info("Removing S3 Bucket Logging for " + bucket)
216+
logging.info("PutBucketLogging API Call")
217+
delete_s3_log = s3.put_bucket_logging(
218+
Bucket=bucket,
219+
BucketLoggingStatus={}
220+
)
221+
logging.info("Removed S3 Bucket Logging created by Assisted Log Enabler for AWS.")
222+
time.sleep(1)
223+
else:
224+
logging.info("There are no S3 Bucket set by Log Enabler in " + aws_region)
225+
except Exception as exception_handle:
226+
logging.error(exception_handle)
181227

182228
def run_vpcflow_cleanup():
183229
"""Function to run the vpcflow_cleanup function"""
@@ -196,6 +242,10 @@ def run_r53_cleanup():
196242
r53_cleanup()
197243
logging.info("This is the end of the script. Please feel free to validate that logging resources have been cleaned up.")
198244

245+
def run_s3_cleanup():
246+
"""Function to run the s3_cleanup function"""
247+
s3_cleanup()
248+
logging.info("This is the end of the script. Please feel free to validate that logging resources have been cleaned up.")
199249

200250
def lambda_handler(event, context):
201251
"""Function that runs all of the previously defined functions"""

0 commit comments

Comments
 (0)