aboutsummaryrefslogtreecommitdiff
path: root/docker/auto-certbot/get-cert-domains.py
blob: 9bd28c860603149fca97bf6d500592166f9053f6 (plain)
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
#!/usr/bin/env python3

import sys
import os
from os.path import *
from cryptography.x509 import *
from cryptography.x509.oid import ExtensionOID

# Check only one argument
if len(sys.argv) != 2:
    print("You should only specify one argument, aka, the path of cert.",
          file=sys.stderr)
    exit(1)

cert_path = sys.argv[1]

if not exists(cert_path):
    print("Cert file does not exist.", file=sys.stderr)
    exit(2)

if not isfile(cert_path):
    print("Cert path is not a file.")
    exit(3)

if not 'CRUPEST_DOMAIN' in os.environ:
    print("Please set CRUPEST_DOMAIN environment variable to root domain.", file=sys.stderr)
    exit(4)

root_domain = os.environ['CRUPEST_DOMAIN']

with open(cert_path, 'rb') as f:
    cert = load_pem_x509_certificate(f.read())
    ext = cert.extensions.get_extension_for_oid(
        ExtensionOID.SUBJECT_ALTERNATIVE_NAME)
    domains: list = ext.value.get_values_for_type(DNSName)
    domains.remove(root_domain)
    domains = [root_domain, *domains]
    print('\n'.join(domains))