Skip to content
Snippets Groups Projects
infoblox_hook.py 1.30 KiB
#!/usr/bin/env python3
""" Add Acme challenges to Infoblox """
import os
import time
import configparser
import requests


def create_acme(iblox_domain, acme_token, iblox_user, iblox_pw):
    """ upload txt record """
    post_req = requests.post(
        'https://infoblox.geant.org/wapi/v2.6.1/record:txt',
        auth=(iblox_user, iblox_pw),
        data={
            'name': '_acme-challenge.{}'.format(iblox_domain),
            'text': acme_token,
            'ttl': '60',
            "view": "External"
        }
    )
    return post_req.status_code


# Here we Go.
if __name__ == "__main__":

    CONFIG = configparser.RawConfigParser()
    CONFIG.read_file(open('/root/.geant_acme.ini'))
    IBLOX_PASS = CONFIG.get('geant_acme', 'iblox_pass')
    IBLOX_USER = CONFIG.get('geant_acme', 'iblox_user')

    ARGS = os.sys.argv
    _ = ARGS.pop(0)

    DOMAIN = os.environ.get('CERTBOT_DOMAIN')
    VALIDATION = os.environ.get('CERTBOT_VALIDATION')

    os.sys.stdout = os.sys.stderr = open('/var/log/acme/acme.log', 'a', 1)

    HTTP_CODE = create_acme(DOMAIN, VALIDATION, IBLOX_USER, IBLOX_PASS)
    if HTTP_CODE != 201:
        print('could not create {} for {}'.format(VALIDATION, DOMAIN))
        os.sys.exit(1)

    print('+' + 72*'-' + '+')
    print('sleep 5 seconds to wait for DNS to settle down')
    time.sleep(5)