-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathipa-dns-hook.py
More file actions
executable file
·123 lines (99 loc) · 3.85 KB
/
Copy pathipa-dns-hook.py
File metadata and controls
executable file
·123 lines (99 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#! /usr/bin/env python
from requests_kerberos import HTTPKerberosAuth, REQUIRED
from time import sleep
import logging
import os
import requests
import sys
# Create logger
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
# Check for server
if "IPA_SERVER" not in os.environ:
raise Exception("Missing IPA_SERVER in environment")
# Check for domain
if "IPA_DOMAIN" not in os.environ:
raise Exception("Missing IPA_DOMAIN in environment")
# If use and password is set, use that
if ("IPA_USER" in os.environ) and ("IPA_PASSWORD" in os.environ):
IPA_USER = os.environ["IPA_USER"]
IPA_PASSWORD = os.environ["IPA_PASSWORD"]
else:
IPA_USER = None
IPA_PASSWORD = None
IPA_SERVER = os.environ["IPA_SERVER"]
IPA_DOMAIN = os.environ["IPA_DOMAIN"]
TTL = 5
def _call_freeipa(json_operation):
headers = {'content-type': 'application/json',
'referer': 'https://%s/ipa' % IPA_SERVER}
if IPA_USER:
# Login and keep a cookie
login_result = requests.post("https://%s/ipa/session/login_password" % IPA_SERVER,
data="user=%s&password=%s" % (IPA_USER, IPA_PASSWORD),
headers={'content-Type':'application/x-www-form-urlencoded',
'referer': 'https://%s/ipa' % IPA_SERVER},
verify='/etc/ipa/ca.crt')
# No auth
auth = None
# Use cookies
cookies=login_result.cookies
else:
# Use kerberos authentication
auth = HTTPKerberosAuth(mutual_authentication=REQUIRED,
sanitize_mutual_error_response=False)
# No cookies
cookies = None
result = requests.post("https://%s/ipa/session/json" % IPA_SERVER,
data=json_operation,
headers=headers,
auth=auth,
cookies=cookies,
verify='/etc/ipa/ca.crt')
retval = result.json()
if retval['error']:
return retval
else:
return None
# Create DNS-record
def create_txt_record(args):
entry, token = args[0], args[2]
if entry.endswith(IPA_DOMAIN):
entry = entry[:-(len(IPA_DOMAIN)+1)]
add_dns_entry = """{ "id": 0,
"method": "dnsrecord_add/1",
"params": [ [ "%s", { "__dns_name__": "_acme-challenge.%s" } ],
{ "txtrecord": [ "%s" ],
"dnsttl": %s,
"version": "2.229" } ] }""" % (IPA_DOMAIN, entry, token, TTL)
ret = _call_freeipa(add_dns_entry)
if ret:
logger.error(ret['error']['message'])
sleep(25)
def delete_txt_record(args):
entry, token = args[0], args[2]
if entry.endswith(IPA_DOMAIN):
entry = entry[:-(len(IPA_DOMAIN)+1)]
remove_dns_entry = """{ "id": 0,
"method": "dnsrecord_del/1",
"params": [ [ "%s", { "__dns_name__": "_acme-challenge.%s" } ],
{ "txtrecord": [ "%s" ],
"version": "2.229" } ] }""" % (IPA_DOMAIN, entry, token)
ret = _call_freeipa(remove_dns_entry)
if ret:
logger.error(ret['error']['message'])
def deploy_cert(args):
pass
def main(argv):
hook_name, args = argv[0], argv[1:]
ops = {'deploy_challenge': create_txt_record,
'clean_challenge': delete_txt_record,
'deploy_cert': deploy_cert, }
if hook_name in ops.keys():
logger.info(' + freeipa hook executing: %s', hook_name)
ops[hook_name](args)
else:
logger.debug(' + freeipa hook not executing: %s', hook_name)
if __name__ == '__main__':
main(sys.argv[1:])