forked from sblaisot/certbot-dns-01-authenticators
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
executable file
·132 lines (106 loc) · 3.75 KB
/
auth.py
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
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import pprint
import ovh
import dns.resolver
import os
import time
wait_timeout = 180 # timeour in seconds
wait_loop_step = 2 # seconds between 2 checks
pp = pprint.PrettyPrinter(indent=4)
certbot_domain = os.environ.get("CERTBOT_DOMAIN")
try:
certbot_domain
except NameError:
print("***ERROR: CERTBOT_DOMAIN environment variable is missing, exiting")
exit(1)
certbot_validation = os.environ.get("CERTBOT_VALIDATION")
try:
certbot_validation
except NameError:
print("***ERROR: CERTBOT_VALIDATION environment variable is missing, exiting")
exit(1)
script_dir = os.path.dirname(os.path.realpath(__file__))
config_file = script_dir + "/ovh.conf"
if not os.path.exists(config_file):
print("***ERROR: config file not found")
exit(1)
# Instanciate an OVH Client.
# Credentials are in ovh.conf file
client = ovh.Client(config_file=config_file)
# Look for requested domain
result = client.get('/domain/zone/')
if certbot_domain not in result:
print("***ERROR: The requested domain " + certbot_domain + " was not found in this ovh account")
exit(1)
result = client.get('/domain/zone/'+ certbot_domain)
domain_nameservers = result['nameServers']
# Get IPs of zone DNS servers
local_resolver = dns.resolver.Resolver()
IP_nameservers = []
for nameserver in domain_nameservers:
myAnswers = local_resolver.query(nameserver, "A")
for rdata in myAnswers:
IP_nameservers.append(str(rdata))
myAnswers = local_resolver.query(nameserver, "AAAA")
for rdata in myAnswers:
IP_nameservers.append(str(rdata))
# Look for existing _acme-challenge record
result = client.get('/domain/zone/' + certbot_domain + '/record',
fieldType='TXT',
subDomain='_acme-challenge',
)
if len(result) > 0:
print("***ERROR: Existing _acme-challenge record found, exiting")
exit(1)
result = client.post('/domain/zone/' + certbot_domain + '/record',
fieldType='TXT',
subDomain='_acme-challenge',
target=certbot_validation,
ttl=300,
)
try:
result['id']
except:
print("***ERROR: something went wrong when creating DNS record")
pp.pprint(result)
exit(1)
result=client.post('/domain/zone/' + certbot_domain + '/refresh')
if result is None:
print("All good, DNS record created")
else:
print("***ERROR: something went wrong when refreshing DNS zone")
pp.pprint(result)
exit(1)
# Now we loop requesting the zone DNS servers waiting for the record to be available
myResolver = dns.resolver.Resolver(configure=False)
myResolver.nameservers = IP_nameservers
elapsed = 0;
print("Waiting for record to be available on DNS servers")
while elapsed < wait_timeout :
try:
myAnswers = myResolver.query("_acme-challenge.mycozycloud.com", "TXT")
break
except:
pass
time.sleep(wait_loop_step)
elapsed += wait_loop_step
if not elapsed % 10:
print(str(elapsed) + " seconds elapsed, still waiting...")
if elapsed >= wait_timeout:
print("***ERROR: DNS record still not available on DNS servers after waiting for " + str(elapsed) + " seconds")
exit(1)
else:
print("DNS record available on DNS server")