-
Notifications
You must be signed in to change notification settings - Fork 13
/
delete-expired-server-certificates.py
48 lines (36 loc) · 1.15 KB
/
delete-expired-server-certificates.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
import argparse
import subprocess
import datetime
import json
def get_certificates():
command = ["aws", "iam",
"list-server-certificates"
]
out = subprocess.run(command, check=True, capture_output=True)
cert_obj = json.loads(out.stdout)
return cert_obj["ServerCertificateMetadataList"]
def delete_certificate(name, dry_run):
command = ["aws", "iam",
"delete-server-certificate",
"--server-certificate-name", name]
print(command)
if not dry_run:
subprocess.run(command)
def is_too_old(cert):
now = datetime.datetime.now()
expiration = cert["Expiration"]
expiration = datetime.datetime.strptime(expiration, "%Y-%m-%dT%H:%M:%SZ")
return expiration < now
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("--dry-run", action="store_true")
return parser.parse_args()
def main():
args = parse_args()
certs = get_certificates()
expired_certs = [cert for cert in certs if is_too_old(cert)]
for cert in expired_certs:
delete_certificate(cert["ServerCertificateName"], args.dry_run)
if __name__ == "__main__":
main()