-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbase.py
More file actions
324 lines (266 loc) · 9.51 KB
/
Copy pathbase.py
File metadata and controls
324 lines (266 loc) · 9.51 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
import os
import sys
from abc import ABC, abstractmethod
from typing import Dict, List, Optional, Any
from dataclasses import dataclass
from enum import Enum
class RecordType(Enum):
A = "A"
AAAA = "AAAA"
CNAME = "CNAME"
TXT = "TXT"
MX = "MX"
NS = "NS"
CAA = "CAA"
SRV = "SRV"
PTR = "PTR"
@dataclass
class DNSRecord:
"""Represents a DNS record."""
id: Optional[str]
name: str
type: RecordType
content: str
ttl: int = 60
proxied: bool = False
priority: Optional[int] = None
data: Optional[Dict[str, Any]] = None
@dataclass
class CAARecord:
"""Represents a CAA record with specific fields."""
name: str
flags: int
tag: str
value: str
ttl: int = 60
class DNSProvider(ABC):
"""Abstract base class for DNS providers."""
DETECT_ENV = ""
# Certbot configuration - override in subclasses
CERTBOT_PLUGIN = ""
CERTBOT_PLUGIN_MODULE = ""
CERTBOT_PACKAGE = ""
CERTBOT_PROPAGATION_SECONDS = 120
CERTBOT_CREDENTIALS_FILE = "" # Path to credentials file
def __init__(self):
"""Initialize the DNS provider."""
pass
def setup_certbot_credentials(self) -> bool:
"""Setup credentials file for certbot. Override in subclasses if needed."""
return True # Default: no setup needed
def validate_credentials(self) -> bool:
"""Validate provider credentials. Override in subclasses if needed."""
return True # Default: no validation needed
@classmethod
def suitable(cls) -> bool:
"""Check if the current environment is suitable for this DNS provider."""
return os.environ.get(cls.DETECT_ENV) is not None
@abstractmethod
def get_dns_records(
self, name: str, record_type: Optional[RecordType] = None
) -> List[DNSRecord]:
"""Get DNS records for a domain.
Args:
name: The record name
record_type: Optional record type filter
Returns:
List of DNS records
"""
pass
@abstractmethod
def create_dns_record(self, record: DNSRecord) -> bool:
"""Create a DNS record.
Args:
record: The DNS record to create
Returns:
True if successful, False otherwise
"""
pass
@abstractmethod
def delete_dns_record(self, record_id: str, domain: str) -> bool:
"""Delete a DNS record.
Args:
record_id: The record ID to delete
domain: The domain name (for zone lookup)
Returns:
True if successful, False otherwise
"""
pass
@abstractmethod
def create_caa_record(self, caa_record: CAARecord) -> bool:
"""Create a CAA record.
Args:
caa_record: The CAA record to create
Returns:
True if successful, False otherwise
"""
pass
def set_a_record(
self, name: str, ip_address: str, ttl: int = 60, proxied: bool = False
) -> bool:
"""Set an A record (delete existing and create new).
Args:
name: The record name
ip_address: The IP address
ttl: Time to live
proxied: Whether to proxy through provider (if supported)
Returns:
True if successful, False otherwise
"""
existing_records = self.get_dns_records(name, RecordType.A)
for record in existing_records:
# Check if record already exists with same IP
if record.content == ip_address:
print("A record with the same IP already exists")
return True
if record.id:
self.delete_dns_record(record.id, name)
new_record = DNSRecord(
id=None,
name=name,
type=RecordType.A,
content=ip_address,
ttl=ttl,
proxied=proxied,
)
return self.create_dns_record(new_record)
def set_alias_record(
self,
name: str,
content: str,
ttl: int = 60,
proxied: bool = False,
) -> bool:
"""Set an alias record (delete existing and create new).
Creates a CNAME record by default. Some providers may override this
to use A records instead (e.g., Linode to avoid CAA conflicts).
Args:
name: The record name
content: The alias target (domain name)
ttl: Time to live
proxied: Whether to proxy through provider (if supported)
Returns:
True if successful, False otherwise
"""
return self.set_cname_record(name, content, ttl, proxied)
def set_cname_record(
self,
name: str,
content: str,
ttl: int = 60,
proxied: bool = False,
) -> bool:
"""Set an alias record (delete existing and create new).
Creates a CNAME record by default. Some providers may override this
to use A records instead (e.g., Linode to avoid CAA conflicts).
Args:
name: The record name
content: The alias target (domain name)
ttl: Time to live
proxied: Whether to proxy through provider (if supported)
Returns:
True if successful, False otherwise
"""
existing_records = self.get_dns_records(name, RecordType.CNAME)
for record in existing_records:
# Check if record already exists with same content
if record.content == content:
print("CNAME record with the same content already exists")
return True
if record.id:
self.delete_dns_record(record.id, name)
new_record = DNSRecord(
id=None,
name=name,
type=RecordType.CNAME,
content=content,
ttl=ttl,
proxied=proxied,
)
return self.create_dns_record(new_record)
def set_txt_record(self, name: str, content: str, ttl: int = 60) -> bool:
"""Set a TXT record (delete existing and create new).
Args:
name: The record name
content: The TXT content
ttl: Time to live
Returns:
True if successful, False otherwise
"""
existing_records = self.get_dns_records(name, RecordType.TXT)
for record in existing_records:
# Check if record already exists with same content
if record.content == content or record.content == f'"{content}"':
print("TXT record with the same content already exists")
return True
if record.id:
self.delete_dns_record(record.id, name)
new_record = DNSRecord(
id=None, name=name, type=RecordType.TXT, content=content, ttl=ttl
)
return self.create_dns_record(new_record)
def zone_is_resolvable(self, name: str) -> bool:
"""Whether the provider can find a zone that owns this name.
Lets callers tell "nothing there" apart from "could not look". The base
answer is optimistic so providers that do not model zones keep their
current behaviour; override where the distinction is knowable.
"""
return True
def unset_txt_record(self, name: str) -> bool:
"""Delete all TXT records for a name.
Used to clean up ACME DNS-01 challenge records (e.g. from a certbot
--manual cleanup hook). Missing records are treated as success.
Args:
name: The record name
Returns:
True if all matching records were removed (or none existed).
"""
# get_dns_records returns [] both for "this name has no TXT records" and
# for "the zone could not be resolved", so an empty list on its own would
# report a failed cleanup as a success. Ask whether the zone resolves
# before believing the emptiness.
records = self.get_dns_records(name, RecordType.TXT)
if not records and not self.zone_is_resolvable(name):
print(
f"Error: cannot delete TXT records for {name}: zone lookup failed",
file=sys.stderr,
)
return False
ok = True
for record in records:
if not record.id:
print(f"Warning: TXT record for {name} has no id; cannot delete")
ok = False
continue
if not self.delete_dns_record(record.id, name):
ok = False
return ok
def set_caa_record(
self,
name: str,
tag: str,
value: str,
flags: int = 0,
ttl: int = 60,
) -> bool:
"""Set a CAA record (delete existing with same tag and create new).
Args:
name: The record name
tag: The CAA tag (issue, issuewild, iodef)
value: The CAA value
flags: The CAA flags
ttl: Time to live
Returns:
True if successful, False otherwise
"""
existing_records = self.get_dns_records(name, RecordType.CAA)
for record in existing_records:
if record.data and record.data.get("tag") == tag:
if record.data.get("value") == value:
print("CAA record with the same content already exists")
return True
if record.id:
self.delete_dns_record(record.id, name)
caa_record = CAARecord(name=name, flags=flags, tag=tag, value=value, ttl=ttl)
return self.create_caa_record(caa_record)