-
Notifications
You must be signed in to change notification settings - Fork 6
PR: Feature to add elb scan and filter security groups for elbs #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 = "" | ||
| for region in config.REGIONS: | ||
| client = boto3.client("elb", region_name=region) | ||
| data = client.describe_load_balancers() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You overwrite |
||
| return data | ||
| 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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stuff like this should already be done in the helper function. |
||
| not_used.append(ELB['LoadBalancerName']) | ||
| flags.append(crayons.yellow(" Not used")) | ||
|
|
||
| for elb in not_used: | ||
| if len(flags) > 0: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You use one global |
||
| suffix = ",".join([str(f) for f in flags]) | ||
| print(" - {} {}".format(elb, suffix)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| scan() | ||
|
|
||
| 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 | ||
|
|
||
|
|
||
|
|
@@ -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: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a |
||
| not_used.append(group) | ||
| else: | ||
| not_used.append(group) | ||
|
|
||
| for sg in all_sg: | ||
| flags = [] | ||
|
|
||
There was a problem hiding this comment.
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 = []