Created
May 28, 2019 07:14
-
-
Save xvzf/e13e1120cec717b19dbf028feb10c58b to your computer and use it in GitHub Desktop.
Traefik certificate extract
This file contains 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
import sys | |
import json | |
import base64 | |
def extract(acme_path, extract_domain, out_name): | |
with open(acme_path, "r") as f: | |
acme = json.loads(f.read()) | |
for certificate in acme["Certificates"]: | |
domain = certificate["Domain"]["Main"] | |
cert = certificate["Certificate"] | |
key = certificate["Key"] | |
if extract_domain == domain: | |
with open(f"{out_name}.crt", "wb") as c: | |
c.write(base64.b64decode(cert)) | |
with open(f"{out_name}.key", "wb") as k: | |
k.write(base64.b64decode(key)) | |
return True | |
return False | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print(f"Usage:\npython {sys.argv[0]} <acme-json-path> <domain-cert-to-extract> <output-prefix>") | |
sys.exit(1) | |
if not extract(*sys.argv[1:]): # pylint: disable=no-value-for-parameter | |
print(f"Domain {sys.argv[2]} not found in {sys.argv[1]}") | |
print(f"Cert and Key for {sys.argv[2]} are found in {sys.argv[3]}.{{key,crt}}") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment