Skip to content

Commit bb8cc72

Browse files
authored
Merge pull request #42 from rckasa/elblogs
Enable Load Balancer Logging
2 parents 2266f02 + b804e96 commit bb8cc72

11 files changed

+691
-22
lines changed

README.md

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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)
9+
* Amazon S3 Server Access Logs (Single Account and Multi-Account using AWS Organizations)
10+
* NEW! Elastic Load Balancing Access Logs (Single Account and Multi-Account using AWS Organizations)
1011

1112
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/)
1213

@@ -22,10 +23,14 @@ The following is a simple diagram on how Assisted Log Enabler for AWS works in a
2223

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

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+
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:
2627

2728
![Alt text](diagrams/assisted_log_enabler_s3.png)
2829

30+
The following is a simple diagram on how Assisted Log Enabler for AWS works with turning on Elastic Load Balancing Access Logging in a single account:
31+
32+
![Alt text](diagrams/assisted_log_enabler_lb.png)
33+
2934
## Prerequisites
3035
### Permissions
3136
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:
@@ -100,7 +105,7 @@ The following permissions are needed within AWS IAM for Assisted Log Enabler for
100105
"route53resolver:AssociateResolverQueryLogConfig",
101106
"iam:CreateServiceLinkRole" # This is used to create the AWSServiceRoleForRoute53 Resolver, which is used for creating the Amazon Route 53 Query Logging Configurations.
102107
103-
# NEW! For adding Amazon S3 Server Access Logs:
108+
# For adding Amazon S3 Server Access Logs:
104109
"s3:PutBucketLogging",
105110
"s3:GetBucketLogging",
106111
"s3:ListBucket",
@@ -111,6 +116,17 @@ The following permissions are needed within AWS IAM for Assisted Log Enabler for
111116
"s3:PutBucketPublicAccessBlock",
112117
"s3:PutBucketLifecycleConfiguration"
113118
119+
# NEW! For adding Elastic Load Balancing Access Logs:
120+
"elb:DescribeLoadBalancers",
121+
"elb:DescribeLoadBalancerAttributes",
122+
"elb:ModifyLoadBalancerAttributes",
123+
"elbv2:DescribeLoadBalancers",
124+
"elbv2:DescribeLoadBalancerAttributes",
125+
"elbv2:ModifyLoadBalancerAttributes",
126+
"elasticloadbalancing:DescribeLoadBalancers",
127+
"elasticloadbalancing:DescribeLoadBalancerAttributes",
128+
"elasticloadbalancing:ModifyLoadBalancerAttributes"
129+
114130
# For cleanup of Amazon Route 53 Resolver Query Logs created by Assisted Log Enabler for AWS:
115131
"route53resolver:ListResolverQueryLogConfigs",
116132
"route53resolver:ListTagsForResource",
@@ -135,10 +151,12 @@ The following are the details of what happens within the Assisted Log Enabler fo
135151
* If no trail is configured, one is created and configured to log to the bucket created. (Single Account only as of this release)
136152
* If Amazon EKS Clusters exist, audit & authenticator logs are turned on.
137153
* 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.
154+
* Amazon S3 Server Access Logs are created for buckets that do not have it turned on already.
139155
* This does not include for S3 buckets created by Assisted Log Enabler for AWS
140156
* 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.
141-
157+
* NEW! Elastic Load Balancing Access Logs are created for Application, Network and Classic Load Balancers that do not have it turned on already.
158+
* Elastic Load Balancing Access Logs require buckets that reside in the region, so additional buckets for Elastic Load Balancing Access logs are created for this.
159+
* The following table contains the account IDs to use in place of elb-account-id in the bucket policy: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html
142160

143161
## Running the Code
144162
The code in its current form can be ran inside the following:
@@ -177,10 +195,10 @@ No valid option selected. Please run with -h to display valid options.
177195
```
178196
python3 assisted_log_enabler.py -h
179197
usage: assisted_log_enabler.py [-h] [--mode MODE] [--all] [--eks] [--vpcflow]
180-
[--r53querylogs] [--s3logs] [--cloudtrail]
198+
[--r53querylogs] [--s3logs] [--lblogs] [--cloudtrail]
181199
[--single_r53querylogs] [--single_cloudtrail]
182200
[--single_vpcflow] [--single_all]
183-
[--single_s3logs] [--single_account]
201+
[--single_s3logs] [--single_lblogs] [--single_account]
184202
[--multi_account]
185203
186204
Assisted Log Enabler - Find resources that are not logging, and turn them on.
@@ -203,6 +221,7 @@ Single & Multi Account Options:
203221
--vpcflow Turns on Amazon VPC Flow Logs.
204222
--r53querylogs Turns on Amazon Route 53 Resolver Query Logs.
205223
--s3logs Turns on Amazon Bucket Logs.
224+
--lblogs Turns on Elastic Load Balancing Logs.
206225
--cloudtrail Turns on AWS CloudTrail.
207226
208227
Cleanup Options:
@@ -220,6 +239,8 @@ Cleanup Options:
220239
Enabler for AWS.
221240
--single_s3logs Removes Amazon Bucket Log resources created by
222241
Assisted Log Enabler for AWS.
242+
--single_lblogs Removes Elastic Load Balancing Log resources created by
243+
Assisted Log Enabler for AWS.
223244
224245
Dry Run Options:
225246
Use these flags to run Assisted Log Enabler for AWS in Dry Run mode.
@@ -256,8 +277,10 @@ python3 assisted_log_enabler.py --mode single_account --vpcflow
256277
python3 assisted_log_enabler.py --mode single_account --r53querylogs
257278
# For AWS CloudTrail:
258279
python3 assisted_log_enabler.py --mode single_account --cloudtrail
259-
# NEW! For Amazon S3 Server Access Logs:
280+
# For Amazon S3 Server Access Logs:
260281
python3 assisted_log_enabler.py --mode single_account --s3logs
282+
# NEW! For Elastic Load Balancing Access Logs:
283+
python3 assisted_log_enabler.py --mode single_account --lblogs
261284
```
262285

263286
### Step-by-Step Instructions (for running in AWS CloudShell, multi account mode)
@@ -315,8 +338,10 @@ python3 assisted_log_enabler.py --mode multi_account --eks
315338
python3 assisted_log_enabler.py --mode multi_account --vpcflow
316339
# For Amazon Route 53 Resolver Query Logs:
317340
python3 assisted_log_enabler.py --mode multi_account --r53querylogs
318-
# NEW! For Amazon S3 Server Access Logs:
341+
For Amazon S3 Server Access Logs:
319342
python3 assisted_log_enabler.py --mode multi_account --s3logs
343+
# NEW! For Elastic Load Balancing Access Logs:
344+
python3 assisted_log_enabler.py --mode multi_account --lblogs
320345
321346
```
322347

@@ -341,7 +366,7 @@ Sample output within the log file:
341366
2021-02-23 05:31:54,984 - INFO - Turning on audit and authenticator logging for EKS clusters in region af-south-1.
342367
```
343368

344-
## NEW! Dry Run Mode
369+
## Dry Run Mode
345370
Dry Run modes for single and multi-account are both available. These modes allow you to check for resources in your environment that do not have logging turned on, but does not activate the logging for said resources.
346371

347372
To run Assisted Log Enabler for AWS in Dry Run mode, you can use the commands below:
@@ -366,8 +391,10 @@ python3 assisted_log_enabler.py --mode cleanup --single_r53querylogs
366391
python3 assisted_log_enabler.py --mode cleanup --single_vpcflow
367392
# To remove AWS CloudTrail trails created by Assisted Log Enabler for AWS (single account):
368393
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):
394+
# To remove Amazon S3 Server Access logging created by Assisted Log Enabler for AWS (single account):
370395
python3 assisted_log_enabler.py --mode cleanup --single_s3logs
396+
# NEW! To remove Elastic Load Balancing Access logging created by Assisted Log Enabler for AWS (single account):
397+
python3 assisted_log_enabler.py --mode cleanup --single_lblogs
371398
```
372399

373400
## Shared Responsibility Model
@@ -388,6 +415,7 @@ For answers to cost-related questions involved with this solution, refer to the
388415
* Amazon VPC Flow Logs Pricing: [Link](https://docs.aws.amazon.com/vpc/latest/userguide/flow-logs.html#flow-logs-pricing)
389416
* Amazon Route 53 Pricing (look for the Route 53 Resolver Query Logs section): [Link](https://aws.amazon.com/route53/pricing/)
390417
* Amazon EKS Control Plane Logging: [Link](https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html)
418+
* Elastic Load Balancing Logging: [Link](https://aws.amazon.com/elasticloadbalancing/pricing/)
391419

392420

393421
## Feedback

assisted_log_enabler.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def assisted_log_enabler():
8282
function_parser_group.add_argument('--vpcflow', action='store_true', help=' Turns on Amazon VPC Flow Logs.')
8383
function_parser_group.add_argument('--r53querylogs', action='store_true', help=' Turns on Amazon Route 53 Resolver Query Logs.')
8484
function_parser_group.add_argument('--s3logs', action='store_true', help=' Turns on Amazon Bucket Logs.')
85+
function_parser_group.add_argument('--lblogs', action='store_true', help=' Turns on Amazon Load Balancer Logs.')
8586
function_parser_group.add_argument('--cloudtrail', action='store_true', help=' Turns on AWS CloudTrail. Only available in Single Account version.')
8687

8788
cleanup_parser_group = parser.add_argument_group('Cleanup Options', 'Use these flags to choose which resources you want to turn logging off for.')
@@ -90,6 +91,7 @@ def assisted_log_enabler():
9091
cleanup_parser_group.add_argument('--single_vpcflow', action='store_true', help=' Removes Amazon VPC Flow Log resources created by Assisted Log Enabler for AWS.')
9192
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.')
9293
cleanup_parser_group.add_argument('--single_s3logs', action='store_true', help=' Removes Amazon Bucket Log resources created by Assisted Log Enabler for AWS.')
94+
cleanup_parser_group.add_argument('--single_lblogs', action='store_true', help=' Removes Amazon Load Balancer Log resources created by Assisted Log Enabler for AWS.')
9395

9496
dryrun_parser_group = parser.add_argument_group('Dry Run Options', 'Use these flags to run Assisted Log Enabler for AWS in Dry Run mode.')
9597
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.')
@@ -109,6 +111,8 @@ def assisted_log_enabler():
109111
ALE_single_account.run_r53_query_logs()
110112
elif args.s3logs:
111113
ALE_single_account.run_s3_logs()
114+
elif args.lblogs:
115+
ALE_single_account.run_lb_logs()
112116
elif args.cloudtrail:
113117
ALE_single_account.run_cloudtrail()
114118
elif args.all:
@@ -124,6 +128,8 @@ def assisted_log_enabler():
124128
ALE_multi_account.run_r53_query_logs()
125129
elif args.s3logs:
126130
ALE_multi_account.run_s3_logs()
131+
elif args.lblogs:
132+
ALE_multi_account.run_lb_logs()
127133
elif args.all:
128134
ALE_multi_account.lambda_handler(event, context)
129135
else:
@@ -133,6 +139,8 @@ def assisted_log_enabler():
133139
ALE_cleanup_single.run_r53_cleanup()
134140
elif args.single_s3logs:
135141
ALE_cleanup_single.run_s3_cleanup()
142+
elif args.single_lblogs:
143+
ALE_cleanup_single.run_lb_cleanup()
136144
elif args.single_cloudtrail:
137145
ALE_cleanup_single.run_cloudtrail_cleanup()
138146
elif args.single_vpcflow:
49.5 KB
Loading

permissions/ALE_child_account_role.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ Resources:
5959
- s3:PutBucketAcl
6060
- s3:PutBucketPublicAccessBlock
6161
- s3:PutBucketLifecycleConfiguration
62+
- elb:DescribeLoadBalancers
63+
- elb:DescribeLoadBalancerAttributes
64+
- elb:ModifyLoadBalancerAttributes
65+
- elbv2:DescribeLoadBalancers
66+
- elbv2:DescribeLoadBalancerAttributes
67+
- elbv2:ModifyLoadBalancerAttributes
68+
- elasticloadbalancing:DescribeLoadBalancers
69+
- elasticloadbalancing:DescribeLoadBalancerAttributes
70+
- elasticloadbalancing:ModifyLoadBalancerAttributes
71+
- eks:ListClusters
6272
Resource: '*'
6373
Condition:
6474
StringEquals:

permissions/ALE_permissions_example_multi_account.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,17 @@
3434
"s3:GetBucketAcl",
3535
"s3:PutBucketAcl",
3636
"s3:PutBucketPublicAccessBlock",
37-
"s3:PutBucketLifecycleConfiguration"
37+
"s3:PutBucketLifecycleConfiguration",
38+
"elb:DescribeLoadBalancers",
39+
"elb:DescribeLoadBalancerAttributes",
40+
"elb:ModifyLoadBalancerAttributes",
41+
"elbv2:DescribeLoadBalancers",
42+
"elbv2:DescribeLoadBalancerAttributes",
43+
"elbv2:ModifyLoadBalancerAttributes"
44+
"elasticloadbalancing:DescribeLoadBalancers",
45+
"elasticloadbalancing:DescribeLoadBalancerAttributes",
46+
"elasticloadbalancing:ModifyLoadBalancerAttributes",
47+
"eks:ListClusters"
3848
],
3949
"Resource": "*",
4050
"Condition": {
@@ -56,4 +66,4 @@
5666
}
5767
}
5868
]
59-
}
69+
}

permissions/ALE_permissions_example_single_account.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,17 @@
3535
"s3:GetBucketAcl",
3636
"s3:PutBucketAcl",
3737
"s3:PutBucketPublicAccessBlock",
38-
"s3:PutBucketLifecycleConfiguration"
38+
"s3:PutBucketLifecycleConfiguration",
39+
"elb:DescribeLoadBalancers",
40+
"elb:DescribeLoadBalancerAttributes",
41+
"elb:ModifyLoadBalancerAttributes",
42+
"elbv2:DescribeLoadBalancers",
43+
"elbv2:DescribeLoadBalancerAttributes",
44+
"elbv2:ModifyLoadBalancerAttributes"
45+
"elasticloadbalancing:DescribeLoadBalancers",
46+
"elasticloadbalancing:DescribeLoadBalancerAttributes",
47+
"elasticloadbalancing:ModifyLoadBalancerAttributes",
48+
"eks:ListClusters"
3949
],
4050
"Resource": "*",
4151
"Condition": {
@@ -57,4 +67,4 @@
5767
}
5868
}
5969
]
60-
}
70+
}

subfunctions/ALE_cleanup_single.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,82 @@ def s3_cleanup():
225225
except Exception as exception_handle:
226226
logging.error(exception_handle)
227227

228+
# 5. Remove the Load Balancer Logging Resources created by Assisted Log Enabler
229+
def lb_cleanup():
230+
"""Function to clean up Load Balancer Logs"""
231+
logging.info("Cleaning up Load Balancer Logs created by Assisted Log Enabler for AWS.")
232+
for aws_region in region_list:
233+
elbv1client = boto3.client('elb', region_name=aws_region)
234+
elbv2client = boto3.client('elbv2', region_name=aws_region)
235+
ELBList1: list = []
236+
ELBList2: list = []
237+
ELBv1LogList: list = []
238+
ELBv2LogList: list = []
239+
removal_list: list = []
240+
try:
241+
logging.info("---- LINE BREAK BETWEEN REGIONS ----")
242+
logging.info("Cleaning up Bucket Logs created by Assisted Log Enabler for AWS in region " + aws_region + ".")
243+
logging.info("DescribeLoadBalancers API Call")
244+
ELBList1 = elbv1client.describe_load_balancers()
245+
for lb in ELBList1['LoadBalancerDescriptions']:
246+
logging.info("DescribeLoadBalancerAttibute API Call")
247+
lblog=elbv1client.describe_load_balancer_attributes(LoadBalancerName=lb['LoadBalancerName'])
248+
logging.info("Parsing out for ELB Access Logging")
249+
if lblog['LoadBalancerAttributes']['AccessLog']['Enabled'] == True:
250+
if 'aws-lb-log-collection-' in str(lblog['LoadBalancerAttributes']['AccessLog']['S3BucketName']):
251+
ELBv1LogList.append([lb['LoadBalancerName'],'classic'])
252+
logging.info("DescribeLoadBalancers v2 API Call")
253+
ELBList2 = elbv2client.describe_load_balancers()
254+
for lb in ELBList2['LoadBalancers']:
255+
logging.info("DescribeLoadBalancerAttibute v2 API Call")
256+
lblog=elbv2client.describe_load_balancer_attributes(LoadBalancerArn=lb['LoadBalancerArn'])
257+
logging.info("Parsing out for ELBv2 Access Logging")
258+
for lbtemp in lblog['Attributes']:
259+
if lbtemp['Key'] == 'access_logs.s3.enabled':
260+
if lbtemp['Value'] == 'true':
261+
for lbtemp2 in lblog['Attributes']:
262+
if lbtemp2['Key'] == 'access_logs.s3.bucket':
263+
if 'aws-lb-log-collection-' in str(lbtemp2['Value']):
264+
ELBv2LogList.append([lb['LoadBalancerName'],lb['LoadBalancerArn']])
265+
removal_list=ELBv1LogList+ELBv2LogList
266+
if removal_list != []:
267+
logging.info("List Load Balancers with Logging enabled by by Assisted Log Enabler for AWS in " + aws_region)
268+
print(removal_list)
269+
for elb in removal_list:
270+
logging.info(elb[0] + " has Load Balancer logging on. It will be turned on within this function.")
271+
if ELBv1LogList != []:
272+
for elb in ELBv1LogList:
273+
logging.info("Removing logs for Load Balancer " + elb[0])
274+
logging.info("ModifyLoadBalancerAttributes API Call")
275+
remove_lb_log = elbv1client.modify_load_balancer_attributes(
276+
LoadBalancerName=elb[0],
277+
LoadBalancerAttributes={
278+
'AccessLog': {
279+
'Enabled': False }
280+
}
281+
)
282+
logging.info("Logging Disabled for Load Balancer " + elb[0])
283+
if ELBv2LogList != []:
284+
for elb in ELBv2LogList:
285+
logging.info("Removing logs for Load Balancer " + elb[0])
286+
logging.info("ModifyLoadBalancerAttributes v2 API Call")
287+
remove_lb_log = elbv2client.modify_load_balancer_attributes(
288+
LoadBalancerArn=elb[1],
289+
Attributes=[
290+
{
291+
'Key': 'access_logs.s3.enabled',
292+
'Value': 'false'
293+
}
294+
]
295+
)
296+
logging.info("Logging Disabled for Load Balancer " + elb[0])
297+
logging.info("Removed Load Balancers Logging created by Assisted Log Enabler for AWS.")
298+
time.sleep(1)
299+
else:
300+
logging.info("There are no Load Balancers Logs set by Log Enabler in " + aws_region)
301+
except Exception as exception_handle:
302+
logging.error(exception_handle)
303+
228304
def run_vpcflow_cleanup():
229305
"""Function to run the vpcflow_cleanup function"""
230306
vpcflow_cleanup()
@@ -247,9 +323,18 @@ def run_s3_cleanup():
247323
s3_cleanup()
248324
logging.info("This is the end of the script. Please feel free to validate that logging resources have been cleaned up.")
249325

326+
def run_lb_cleanup():
327+
"""Function to run the lb_cleanup function"""
328+
lb_cleanup()
329+
logging.info("This is the end of the script. Please feel free to validate that logging resources have been cleaned up.")
330+
250331
def lambda_handler(event, context):
251332
"""Function that runs all of the previously defined functions"""
252333
r53_cleanup()
334+
vpcflow_cleanup()
335+
cloudtrail_cleanup()
336+
s3_cleanup()
337+
lb_cleanup()
253338
logging.info("This is the end of the script. Please feel free to validate that logging resources have been cleaned up.")
254339

255340

0 commit comments

Comments
 (0)