This repository has been archived on 2021-09-01. You can view files and clone it, but cannot push or open issues or pull requests.
certbot_dns_corenetworks/cleanup.py

173 lines
4.6 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""API client to core-networks"""
import requests
import os
import json
import urlparse
import configparser
import sys
import logging
import tldextract
import codecs
def setup_logger():
# seup logging
logger = logging.getLogger("certbot_dns_corenetworks")
logging.basicConfig(level=logging.INFO)
# silence logging from requests module
logging.getLogger('requests').setLevel(logging.CRITICAL)
# create console handler
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s- %(levelname)s - %(message)s')
ch.setFormatter(formatter)
# add handler to logger
logger.addHandler(ch)
logger.propagate = False
return logger
def load_config(logger):
config_path = os.path.join(os.path.expanduser("~"), ".certbot_dns_corenetworks", "config.ini")
if os.path.isfile(config_path):
config = configparser.ConfigParser()
config.read(config_path)
else:
logger.error("Configfile " + config_path + " not present.")
sys.exit(1)
return config
def get_auth_token(config, logger):
data = {}
try:
data["login"] = config.get('API', 'USER')
data["password"] = config.get('API', 'PASSWORD')
json_data = json.dumps(data)
host = config.get('API', 'HOST')
except (configparser.NoSectionError, configparser.NoOptionError) as e:
logger.error("Config error: %s" % (e))
sys.exit(1)
url = urlparse.urljoin(host, os.path.join("auth", "token"))
try:
r = requests.post(url, data=json_data)
r.raise_for_status()
except requests.exceptions.HTTPError as e:
logger.error(e)
sys.exit(1)
return r
def get_record(config, logger):
filename = '/tmp/CERTBOT_%s.json' % (os.environ['CERTBOT_DOMAIN'])
if not os.path.isfile(filename):
logger.error("File '%s' does not exists." % (filename))
sys.exit(1)
with open(filename) as f:
data = json.load(f)
try:
host = config.get('API', 'HOST')
zone = config.get('DNS', 'ZONE')
token = config.get('API', 'TOKEN')
except (configparser.NoSectionError, configparser.NoOptionError) as e:
logger.error("Config error: %s" % (e))
sys.exit(1)
url = urlparse.urljoin(
host, os.path.join("dnszones", zone, "records", "?type=TXT&name=%s&data=%s") %
(data['name'], data['data'])
)
headers = {"Authorization": "Bearer %s" % (token)}
try:
r = requests.get(url, headers=headers)
r.raise_for_status()
except requests.exceptions.HTTPError, e:
logger.error(e)
sys.exit(1)
print url
print r.json()
sys.exit(1)
return r
def delete_record(config, logger):
try:
host = config.get('API', 'HOST')
zone = config.get('DNS', 'ZONE')
token = config.get('API', 'TOKEN')
except (configparser.NoSectionError, configparser.NoOptionError) as e:
logger.error("Config error: %s" % (e))
sys.exit(1)
url = urlparse.urljoin(host, os.path.join("dnszones", zone, "records/"))
headers = {"Authorization": "Bearer %s" % (token)}
try:
custom_cache_extract = tldextract.TLDExtract(
cache_file=os.path.join(os.path.expanduser("~"), "tld_set"))
subdomain = custom_cache_extract(os.environ['CERTBOT_DOMAIN']).subdomain
if subdomain:
certbot_domain = "_acme-challenge.%s" % (subdomain)
else:
certbot_domain = "_acme-challenge"
certbot_validation = os.environ['CERTBOT_VALIDATION']
except KeyError, e:
logger.error("Environment Variable %s not set" % (e))
sys.exit(1)
write_backup(os.environ['CERTBOT_DOMAIN'], certbot_validation, logger)
data = {}
data["name"] = certbot_domain
data["ttl"] = 120
data["type"] = "TXT"
data["data"] = certbot_validation
json_data = json.dumps(data)
try:
r = requests.post(url, headers=headers, data=json_data)
r.raise_for_status()
except requests.exceptions.HTTPError, e:
logger.error(e)
sys.exit(1)
return r
def main():
"""Main logic entrypoint"""
logger = setup_logger()
config = load_config(logger)
# get auth token
logger.info("Try to get auth token")
r_token = get_auth_token(config, logger)
config.set('API', 'TOKEN', r_token.json()["token"])
logger.info("Authentication successful")
# get txt record
logger.info("Verify TXT record")
z_record = get_record(config, logger)
if __name__ == "__main__":
main()