2018-06-04 22:32:16 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
2018-06-06 19:38:20 +00:00
|
|
|
|
|
|
|
"""API client to core-networks"""
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import urlparse
|
2018-06-07 20:03:27 +00:00
|
|
|
import configparser
|
2018-06-09 21:54:08 +00:00
|
|
|
import sys
|
|
|
|
import logging
|
2018-06-06 19:38:20 +00:00
|
|
|
|
|
|
|
|
2018-06-09 21:54:08 +00:00
|
|
|
def setup_logger():
|
|
|
|
# seup logging
|
|
|
|
logger = logging.getLogger("certbot_dns_corenetworks")
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
# create console handler
|
|
|
|
ch = logging.StreamHandler()
|
|
|
|
ch.setLevel(logging.INFO)
|
|
|
|
|
2018-06-11 20:39:56 +00:00
|
|
|
formatter = logging.Formatter('%(asctime)s - %(name)s- %(levelname)s - %(message)s')
|
2018-06-09 21:54:08 +00:00
|
|
|
ch.setFormatter(formatter)
|
|
|
|
# add handler to logger
|
|
|
|
logger.addHandler(ch)
|
|
|
|
logger.propagate = False
|
|
|
|
return logger
|
2018-06-06 19:38:20 +00:00
|
|
|
|
|
|
|
|
2018-06-10 00:26:08 +00:00
|
|
|
def load_config(logger):
|
2018-06-09 20:46:31 +00:00
|
|
|
try:
|
2018-06-09 21:54:08 +00:00
|
|
|
config_path = os.path.join(
|
2018-06-11 20:39:56 +00:00
|
|
|
os.path.expanduser("~"), ".certbot_dns_corenetworks", "config.ini")
|
2018-06-09 20:46:31 +00:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read(config_path)
|
|
|
|
|
2018-06-10 00:26:08 +00:00
|
|
|
data = {}
|
|
|
|
data["API_HOST"] = config['API']['HOST']
|
|
|
|
data["API_USER"] = config['API']['USER']
|
|
|
|
data["PASSWORD"] = config['API']['PASSWORD']
|
|
|
|
|
|
|
|
data["ZONE"] = config['DNS']['ZONE']
|
2018-06-09 21:54:08 +00:00
|
|
|
except KeyError, e:
|
|
|
|
logger.error("Key %s not found in config" % (e))
|
2018-06-11 20:39:56 +00:00
|
|
|
sys.exit(1)
|
2018-06-07 20:03:27 +00:00
|
|
|
|
2018-06-10 00:26:08 +00:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def get_auth_token(user, passwd, host, logger):
|
|
|
|
data = {}
|
|
|
|
data["login"] = user
|
|
|
|
data["password"] = passwd
|
|
|
|
json_data = json.dumps(data)
|
|
|
|
|
|
|
|
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, e:
|
|
|
|
logger.error(e)
|
2018-06-11 20:39:56 +00:00
|
|
|
sys.exit(1)
|
2018-06-10 00:26:08 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def get_zone(token, host, zone, logger):
|
|
|
|
url = urlparse.urljoin(host, os.path.join("dnszones", zone))
|
|
|
|
headers = {"Authorization": "Bearer %s" % (token)}
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.get(url, headers=headers)
|
|
|
|
r.raise_for_status()
|
|
|
|
except requests.exceptions.HTTPError, e:
|
|
|
|
logger.error(e)
|
2018-06-11 20:39:56 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def set_record(token, host, zone, logger):
|
|
|
|
url = urlparse.urljoin(host, os.path.join("dnszones", zone, "records/"))
|
|
|
|
headers = {"Authorization": "Bearer %s" % (token)}
|
|
|
|
|
|
|
|
try:
|
|
|
|
certbot_domain = "_acme-challenge.%s" % (os.environ['CERTBOT_DOMAIN'])
|
|
|
|
certbot_validation = os.environ['CERTBOT_VALIDATION']
|
|
|
|
except KeyError, e:
|
|
|
|
logger.error("Environment Variable %s not set" % (e))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
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)
|
2018-06-10 00:26:08 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Main logic entrypoint"""
|
|
|
|
|
|
|
|
logger = setup_logger()
|
|
|
|
config = load_config(logger)
|
|
|
|
|
|
|
|
# get auth token
|
|
|
|
logger.info("Try to get auth token")
|
2018-06-11 20:39:56 +00:00
|
|
|
r_token = get_auth_token(config["API_USER"], config["PASSWORD"], config["API_HOST"], logger)
|
2018-06-10 00:26:08 +00:00
|
|
|
auth_token = r_token.json()["token"]
|
|
|
|
logger.info("Authentication successful")
|
|
|
|
|
|
|
|
# verify dns zone
|
|
|
|
r_zone = get_zone(auth_token, config["API_HOST"], config["ZONE"], logger)
|
|
|
|
zone_name = r_zone.json()["name"]
|
|
|
|
zone_active = r_zone.json()["active"]
|
|
|
|
|
|
|
|
if zone_active:
|
|
|
|
logger.info("Zone '%s' verified (active)" % (zone_name))
|
|
|
|
else:
|
2018-06-11 20:39:56 +00:00
|
|
|
logger.error("Zone '%s' verified (not active)" % (zone_name))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# add txt record
|
|
|
|
z_record = set_record(auth_token, config["API_HOST"], zone_name, logger)
|
|
|
|
print z_record
|
2018-06-06 19:38:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|