70 lines
1.5 KiB
Python
70 lines
1.5 KiB
Python
#!/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
|
|
|
|
|
|
def api_auth(user, passwd, host):
|
|
data = {}
|
|
data["login"] = user
|
|
data["password"] = passwd
|
|
json_data = json.dumps(data)
|
|
|
|
url = urlparse.urljoin(host, os.path.join("auth", "token"))
|
|
r = requests.post(url, data=json_data)
|
|
return r.json()
|
|
|
|
|
|
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)
|
|
|
|
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 main():
|
|
"""Main logic entrypoint"""
|
|
|
|
logger = setup_logger()
|
|
|
|
try:
|
|
config_path = os.path.join(
|
|
os.path.expanduser("~"),
|
|
".certbot_dns_corenetworks",
|
|
"config.ini"
|
|
)
|
|
config = configparser.ConfigParser()
|
|
config.read(config_path)
|
|
|
|
API_HOST = config['API']['HOST']
|
|
API_USER = config['API']['USER']
|
|
PASSWORD = config['API']['PASSWORD']
|
|
except KeyError, e:
|
|
logger.error("Key %s not found in config" % (e))
|
|
sys.exit(0)
|
|
|
|
auth = api_auth(API_USER, PASSWORD, API_HOST)
|
|
print auth["token"]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|