Skip to content

Commit

Permalink
Add helpers folder (#437)
Browse files Browse the repository at this point in the history
* Return removed staging CA for letsenrypt

* Fix missing $

* Remove PAYMENTS_AUTORECHARGE_DEFAULT_MIN_BALANCE

* Add helpers scripts
  • Loading branch information
YuryHrytsuk authored Nov 14, 2023
1 parent 5248695 commit 72218cb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions scripts/helpers/gen_pwd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import secrets
import string
import sys


def generate_password(length=12):
# https://stackoverflow.com/a/39596292
alphabet = string.ascii_letters + string.digits
password = "".join(secrets.choice(alphabet) for i in range(length))
return password


if len(sys.argv) < 2:
pwd_len = 30
else:
pwd_len = int(sys.argv[1])

new_password = generate_password(pwd_len)
print(new_password)
41 changes: 41 additions & 0 deletions scripts/helpers/get_unused_aws_key_pairs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
from pprint import pprint

import boto3

# Create a session with the specified profile or use the default profile
profile_name = sys.argv[1] if len(sys.argv) > 1 else None
session = boto3.Session(profile_name=profile_name)

# Use the session to create an EC2 client to get the default region
ec2_client = session.client("ec2")
default_region = ec2_client.meta.region_name

# Use the session to create an EC2 resource with the default region
ec2 = session.resource("ec2", region_name=default_region)

unused_keys = {}

# Iterate over all regions
for region in ec2.meta.client.describe_regions()["Regions"]:
region_name = region["RegionName"]
try:
# Use the session to create an EC2 resource for the specific region
ec2conn = session.resource("ec2", region_name=region_name)

# Get all key pairs in the region
key_pairs = ec2conn.key_pairs.all()

# Get the names of key pairs used by instances in the region
used_keys = {instance.key_name for instance in ec2conn.instances.all()}

# Iterate over key pairs and delete unused ones
for key_pair in key_pairs:
if key_pair.name not in used_keys:
unused_keys[key_pair.name] = region_name
except Exception as e:
print(f"No access to region {region_name}: {e}")

# Print the results
print(f"Found {len(unused_keys)} unused key pairs across all regions:")
pprint(unused_keys)

0 comments on commit 72218cb

Please sign in to comment.