Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions full_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
cloudwatch,
cloudformation,
s3,
elb
)

all_regions = helper.get_all_regions()
Expand Down Expand Up @@ -79,6 +80,9 @@
print(crayons.yellow("Scanning elastic IPs"))
eip.scan()

print(crayons.yellow("Scanning Elbs"))
elb.scan()

stop = timeit.default_timer()
runtime = int(stop - start)
print("Scan finished after {} seconds".format(crayons.yellow(runtime)))
22 changes: 22 additions & 0 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ def get_all_regions():
response = ec2.describe_regions()
regions = response["Regions"]
return [r["RegionName"] for r in regions]


def get_elb_sg(region=None):
if not region:
region = config.REGIONS
elb_sg = []
for region in config.REGIONS:
client = boto3.client("elb", region_name=region)
data = client.describe_load_balancers()
for elbDesc in data["LoadBalancerDescriptions"]:
elb_sg.extend(elbDesc["SecurityGroups"])
return elb_sg


def get_all_elbs(region=None):
if not region:
region = config.REGIONS
data = ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please initialize the variable with the correct variable type. This should probably be data = []

for region in config.REGIONS:
client = boto3.client("elb", region_name=region)
data = client.describe_load_balancers()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You overwrite data in every iteration of the loop. This way you will always only get the ELBs from the last region in the list. You need to append the ELBs from a region onto the list and return the final list in the end

return data
26 changes: 26 additions & 0 deletions modules/elb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /usr/bin/env python3
from helper import get_all_instances, get_all_rds, get_all_sg, get_all_elbs
import crayons
import boto3
import config


def scan():
not_used = []
flags = []

response = get_all_elbs()
for ELB in response['LoadBalancerDescriptions']:
if len(ELB['Instances']) == 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stuff like this should already be done in the helper function. get_all_elbs should just return a list of all ELBs where you don't need to do any more checks or processing of the data

not_used.append(ELB['LoadBalancerName'])
flags.append(crayons.yellow(" Not used"))

for elb in not_used:
if len(flags) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You use one global flags array for all ELBs. How should this work when you have more than one ELB ?

suffix = ",".join([str(f) for f in flags])
print(" - {} {}".format(elb, suffix))


if __name__ == "__main__":
scan()

10 changes: 7 additions & 3 deletions modules/securitygroups.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /usr/bin/env python3
from helper import get_all_instances, get_all_rds, get_all_sg
from helper import get_all_instances, get_all_rds, get_all_sg, get_elb_sg
import crayons


Expand All @@ -15,14 +15,18 @@ def scan():
used_groups.extend(attached)

all_sg = get_all_sg()

elb_sg = get_elb_sg()
print("Found {} security groups".format(len(all_sg)))

not_used = []
for group in all_sg:
id = group["GroupId"]
if id not in used_groups:
not_used.append(group)
if len(elb_sg) > 0:
if id not in elb_sg:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have a used_groups array with all SGs that are in use by anything. Just append the list of your SGs from the ELBs to this list and then you don't need to modify this part of the code

not_used.append(group)
else:
not_used.append(group)

for sg in all_sg:
flags = []
Expand Down