Skip to content
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

find_unused_security_groups not covering all security groups in use #25

Open
NikolausBrunner opened this issue Apr 11, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@NikolausBrunner
Copy link

NikolausBrunner commented Apr 11, 2023

Describe the bug
Running find_unused_security_groups.py does not find all security groups used by AWS (e.g. ElastiCache, Firehose, ...)

** proposed fix **
Query for SGs used by ENIs (smaller script, all AWS services covered)

import boto3

if name == "main":
ec2 = boto3.client("ec2")

used_SG = set()

# Find security groups attached to ENIs
response = ec2.describe_network_interfaces()
for eni in response["NetworkInterfaces"]:
        for sg in eni["Groups"]:
            used_SG.add(sg["GroupId"])


response = ec2.describe_security_groups()
total_SG = [sg["GroupId"] for sg in response["SecurityGroups"]]
unused_SG = set(total_SG) - used_SG

print(f"Total Security Groups: {len(total_SG)}")
print(f"Used Security Groups: {len(used_SG)}\n")
print(f"Unused Security Groups: {len(unused_SG)} compiled in the following list:")
print(f"{list(unused_SG)}")
@NikolausBrunner NikolausBrunner added the bug Something isn't working label Apr 11, 2023
@NikolausBrunner NikolausBrunner changed the title find_unused_security_groups fails on LBs without securitygroup find_unused_security_groups not covering all security groups in use Apr 11, 2023
@zcapper
Copy link

zcapper commented May 8, 2024

Was about to open an issue to say the same. Querying ENIs is definitely the most robust approach and will future-proof the script from the additional of other AWS services.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants