Created
August 21, 2024 15:10
-
-
Save dfar-io/6b86f54d323972d32b12ebd6561e0905 to your computer and use it in GitHub Desktop.
Lists LB certs across multiple GCP projects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Gets full list of ILB certs and provides days remaining until expiration | |
# Output breakout by index: | |
# 0: NAME header | |
# 1: TYPE header | |
# 2: CREATION_TIMESTAMP header | |
# 3: EXPIRE_TIME header | |
# 4: REGION header | |
# 5: MANAGED_STATUS header (skipping implementation of this, if this gets populated it causes issues) | |
import subprocess | |
import time | |
import sys | |
from datetime import datetime, date | |
def main(): | |
gcp_project_list = [ | |
"dev-6655", | |
"it-4377", | |
"prod-4101", | |
"qv-1147", | |
"dev-1814", | |
"it-8258", | |
"prod-8934", | |
"qv-7090", | |
"dev-1707", | |
"it-9626", | |
"prod-4903", | |
"qv-9409" | |
] | |
certs = [] | |
for index, project in zip(range(len(gcp_project_list)), gcp_project_list): | |
certs.append(find_certs(project)) | |
progress_bar(index + 1, len(gcp_project_list)) | |
print() | |
sorted_certs = sorted(certs, key=lambda cert: cert['days_remaining']) | |
for cert in sorted_certs: | |
formatted_string = "{:<15} | {:<10} | {:>5} | {:<30}".format(cert['project'], cert['name'], cert['days_remaining'], cert['domain']) | |
print(formatted_string) | |
def find_certs(project): | |
# each line item returned from gcloud is this many items long (excludes MANAGED_STATUS) | |
cert_item_length = 5 | |
cert_list_output = subprocess.run(["gcloud", "compute", "ssl-certificates", "list", "--project", project], | |
check=True, | |
capture_output=True) | |
# skip output headers | |
cert_list_output_cleaned = cert_list_output.stdout.split()[6:] | |
cert_count = int(len(cert_list_output_cleaned) / cert_item_length) | |
for i in range(cert_count): | |
offset = i * cert_item_length | |
cert_name = cert_list_output_cleaned[offset].decode('utf-8') | |
if cert_name.startswith('self-cert-'): | |
continue | |
expiration_date_output = cert_list_output_cleaned[offset + 3][:10].decode('utf-8') | |
expiration_date = datetime.strptime(expiration_date_output, '%Y-%m-%d').date() | |
todays_date = date.today() | |
days_remaining = (expiration_date - todays_date).days | |
region = cert_list_output_cleaned[offset + 4] | |
cert_describe_output = subprocess.run(["gcloud", "compute", "ssl-certificates", "describe", cert_name, "--region", region, "--project", project], | |
check=True, | |
capture_output=True) | |
cert_describe_output_cleaned = cert_describe_output.stdout.split() | |
domain = cert_describe_output_cleaned[len(cert_describe_output_cleaned) - 3].decode('utf-8') | |
return {'project': project, 'name': cert_name, 'days_remaining': days_remaining, 'domain': domain} | |
def progress_bar(iteration, total, length = 50): | |
progress = int(length * iteration / total) | |
bar = '=' * progress + '-' * (length - progress) | |
percent = 100 * iteration / total | |
sys.stdout.write(f'\r[{bar}] {percent:.2f}% Complete') | |
sys.stdout.flush() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment