|
| 1 | +# coding: utf-8 |
| 2 | +# Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | +# This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license. |
| 4 | + |
| 5 | +# This script checks all load balancer certificates in the tenancy if they will remain valid |
| 6 | +# after N (user supplied) number of days. It traverses the compartment tree, locating |
| 7 | +# load balancer ctificates in each compartment. It extracts the "Not Valid After" attribute |
| 8 | +# and checks if the certificate will expire after N (user supplied) days. |
| 9 | +# |
| 10 | +# Usage: |
| 11 | +# python check_lb_cert_validity.py -d 'expiry-in-the-next-days' -c 'compartment-ocid' |
| 12 | +# |
| 13 | +# The script uses the default OCI SDK/CLI configuration file ~/.oci/config . |
| 14 | + |
| 15 | + |
| 16 | +import argparse |
| 17 | +import oci |
| 18 | +from cryptography import x509 |
| 19 | +from cryptography.hazmat.backends import default_backend |
| 20 | +from datetime import datetime, timedelta |
| 21 | + |
| 22 | + |
| 23 | +def verify_cert_expiry(compartment_id, load_balancer_id, cert_bundle): |
| 24 | + |
| 25 | + pem_cert = cert_bundle.public_certificate |
| 26 | + cert = x509.load_pem_x509_certificate(bytes(str(pem_cert), 'utf-8'), default_backend()) |
| 27 | + cert_not_valid_after = cert.not_valid_after.date() |
| 28 | + |
| 29 | + validity_date = datetime.utcnow().date().today() + timedelta(days=int(cert_validity_days)) |
| 30 | + |
| 31 | + if cert_not_valid_after <= validity_date: |
| 32 | + print("========================================================") |
| 33 | + print("Compartment Id: ", compartment_id) |
| 34 | + print("Load Balancer ID: ", load_balancer_id) |
| 35 | + print("Certificate Name: ", cert_bundle.certificate_name) |
| 36 | + print("Certificate Validity: ", cert.not_valid_after) |
| 37 | + print("Certificate expiring within {0} days.".format(cert_validity_days)) |
| 38 | + print("========================================================\n") |
| 39 | + |
| 40 | + |
| 41 | +# Check certificates in a load balancer. |
| 42 | +def verify_certificates(compartment_id, load_balancer_id): |
| 43 | + |
| 44 | + response = lb_client.list_certificates(load_balancer_id) |
| 45 | + |
| 46 | + cert_list = response.data |
| 47 | + for i in cert_list: |
| 48 | + verify_cert_expiry(compartment_id, load_balancer_id, i) |
| 49 | + |
| 50 | + |
| 51 | +# Check certificates in all load balancers in a compartment. |
| 52 | +def inpect_lb_certs(compartment_id): |
| 53 | + |
| 54 | + response = lb_client.list_load_balancers(compartment_id) |
| 55 | + lb_list = response.data |
| 56 | + |
| 57 | + for i in lb_list: |
| 58 | + lb_id = i.id |
| 59 | + verify_certificates(compartment_id, lb_id) |
| 60 | + |
| 61 | + |
| 62 | +# Traverse the compartment tree and identify any expired certificates. |
| 63 | +def traverse_compartment_tree(compartment_id): |
| 64 | + |
| 65 | + response = identity_client.list_compartments(compartment_id) |
| 66 | + child_compartment_list = response.data |
| 67 | + |
| 68 | + length = len(child_compartment_list) |
| 69 | + |
| 70 | + if length > 0: |
| 71 | + for i in child_compartment_list: |
| 72 | + child_compartment_id = i.id |
| 73 | + traverse_compartment_tree(child_compartment_id) |
| 74 | + |
| 75 | + # Look for expiring certificates in load balancers belonging to this compartment. |
| 76 | + inpect_lb_certs(compartment_id) |
| 77 | + |
| 78 | + |
| 79 | +def main(): |
| 80 | + |
| 81 | + # Traverse the compartment heirarchy and identify the expiring certificates. |
| 82 | + traverse_compartment_tree(top_compartment) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + parser = argparse.ArgumentParser() |
| 87 | + parser.add_argument("-d", |
| 88 | + "--expiry-in-the-next-days", |
| 89 | + action="store", |
| 90 | + help="check certificates which will expire in the coming days", |
| 91 | + required=True) |
| 92 | + parser.add_argument("-c", |
| 93 | + "--compartment-id", |
| 94 | + action="store", |
| 95 | + default="root", |
| 96 | + help="Compartment and its child compartments to be searched for expired load balancer certs ") |
| 97 | + |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + # Get hold of the arguments. |
| 101 | + cert_validity_days = args.expiry_in_the_next_days |
| 102 | + top_compartment = args.compartment_id |
| 103 | + |
| 104 | + try: |
| 105 | + config = oci.config.from_file() |
| 106 | + except oci.exceptions.ConfigFileNotFound as e: |
| 107 | + print("") |
| 108 | + print("ConfigFileNotFound: {}".format(e)) |
| 109 | + print("") |
| 110 | + exit(-1) |
| 111 | + |
| 112 | + # If compartment was not provided, default value is root. |
| 113 | + if "root" == top_compartment: |
| 114 | + top_compartment = config["tenancy"] |
| 115 | + |
| 116 | + # Service clients |
| 117 | + identity_client = oci.identity.IdentityClient(config) |
| 118 | + lb_client = oci.load_balancer.LoadBalancerClient(config) |
| 119 | + |
| 120 | + main() |
0 commit comments