|
1 | | -import requests |
2 | 1 | import logging |
3 | 2 |
|
4 | 3 | from .utils import CustomExceptionServerError |
5 | 4 | from datetime import datetime |
6 | 5 | from django.conf import settings |
7 | 6 | from typing import List |
| 7 | +from cloudflare import Cloudflare |
8 | 8 |
|
9 | 9 |
|
10 | | -PDNS_API_BASE_URL = f"http://{settings.LOCALCERT_PDNS_SERVER_IP}:{settings.LOCALCERT_PDNS_API_PORT}/api/v1" |
11 | | -PDNS_HEADERS = { |
12 | | - "X-API-Key": settings.LOCALCERT_PDNS_API_KEY, |
13 | | - "accept": "application/json", |
| 10 | +ZONE_IDS = { |
| 11 | + "localcert.net.": "ab2d04b0ccf31906dd87900f0db11f73", |
| 12 | + "localhostcert.net.": "ac1335db9f052915b076c0de09e06443", |
14 | 13 | } |
15 | 14 |
|
16 | 15 |
|
17 | | -def pdns_create_zone(zone: str): |
18 | | - assert zone.endswith(".") |
| 16 | +client = Cloudflare(api_token=os.environ.get("CLOUDFLARE_TOKEN")) |
19 | 17 |
|
20 | | - logging.debug(f"[PDNS] Create {zone}") |
21 | 18 |
|
22 | | - # Create zone in pdns |
23 | | - resp = requests.post( |
24 | | - PDNS_API_BASE_URL + "/servers/localhost/zones", |
25 | | - headers=PDNS_HEADERS, |
26 | | - json={ |
27 | | - "name": zone, |
28 | | - "kind": "Native", |
29 | | - }, |
30 | | - ) |
31 | | - json_resp = resp.json() |
| 19 | +# TODO: Some records are set by wildcard, hardcode these |
| 20 | +def pdns_describe_domain(domain: str) -> dict: |
| 21 | + assert domain.endswith(".") |
| 22 | + logging.debug(f"[PDNS] Describe {domain}") |
32 | 23 |
|
33 | | - if "error" in json_resp.keys(): |
34 | | - raise CustomExceptionServerError(json_resp["error"]) # pragma: no cover |
35 | | - |
36 | | - # success |
37 | | - return |
38 | | - |
39 | | - |
40 | | -# TODO use the targeted name/type |
41 | | -def pdns_describe_domain(zone_name: str) -> dict: |
42 | | - assert zone_name.endswith(".") |
43 | | - |
44 | | - logging.debug(f"[PDNS] Describe {zone_name}") |
45 | | - |
46 | | - # TODO: newer pdns versions can filter by name/type |
47 | | - resp = requests.get( |
48 | | - f"{PDNS_API_BASE_URL}/servers/localhost/zones/{zone_name}", |
49 | | - headers=PDNS_HEADERS, |
50 | | - ) |
51 | | - if resp.status_code != requests.codes.ok: |
52 | | - raise CustomExceptionServerError( |
53 | | - f"Unable to describe domain, PDNS error code: {resp.status_code}" |
54 | | - ) # pragma: no cover |
55 | | - |
56 | | - return resp.json() |
57 | | - |
58 | | - |
59 | | -def pdns_delete_rrset(zone_name: str, rr_name: str, rrtype: str): |
60 | | - assert zone_name.endswith(".") |
61 | | - assert rr_name.endswith(zone_name) |
62 | | - assert rrtype == "TXT" |
63 | | - |
64 | | - logging.debug(f"[PDNS] Delete {zone_name} {rr_name} {rrtype}") |
65 | | - |
66 | | - resp = requests.patch( |
67 | | - f"{PDNS_API_BASE_URL}/servers/localhost/zones/{zone_name}", |
68 | | - headers=PDNS_HEADERS, |
69 | | - json={ |
70 | | - "rrsets": [ |
71 | | - { |
72 | | - "name": rr_name, |
73 | | - "type": "TXT", |
74 | | - "changetype": "DELETE", |
75 | | - }, |
76 | | - ], |
77 | | - }, |
78 | | - ) |
79 | | - |
80 | | - if resp.status_code != requests.codes.no_content: |
81 | | - raise CustomExceptionServerError(f"{resp.status_code}") # pragma: no cover |
| 24 | + for k, v in ZONE_IDS.items(): |
| 25 | + if domain.endswith(f".{k}") |
| 26 | + zone_id = v |
| 27 | + break |
| 28 | + else: |
| 29 | + # Ooops |
| 30 | + return {} |
82 | 31 |
|
83 | | - # success |
84 | | - return |
| 32 | + # CF doesn't use trailing dot |
| 33 | + domain = domain[:-1] |
| 34 | + |
| 35 | + # Two lookups: |
| 36 | + # <domain>.<zone> (exact) |
| 37 | + # *.<domain>.<zone> (endswith) |
| 38 | + results = client.dns.records.list( |
| 39 | + zone_id=zone_id, |
| 40 | + name={"endswith": f".{domain}"}, |
| 41 | + type="TXT", |
| 42 | + ).result |
| 43 | + r2 = client.dns.records.list( |
| 44 | + zone_id=zone_id, |
| 45 | + name={"exact": domain}, |
| 46 | + type="TXT", |
| 47 | + ).result |
| 48 | + results.extend(r2) |
| 49 | + |
| 50 | + rrsets = [] |
| 51 | + for result in results: |
| 52 | + rrset.append({ |
| 53 | + "type": "TXT", |
| 54 | + "name": result.name, |
| 55 | + "content": result.content, |
| 56 | + "ttl": result.ttl, |
| 57 | + }) |
| 58 | + return { "rrsets": rrsets } |
85 | 59 |
|
86 | 60 |
|
87 | 61 | def pdns_replace_rrset( |
88 | 62 | zone_name: str, rr_name: str, rr_type: str, ttl: int, record_contents: List[str] |
89 | 63 | ): |
90 | 64 | """ |
91 | | -
|
92 | 65 | record_contents - Records from least recently added |
93 | 66 | """ |
94 | 67 | assert rr_name.endswith(".") |
95 | 68 | assert rr_name.endswith(zone_name) |
96 | | - assert rr_type in ["TXT", "A", "MX", "NS", "SOA"] |
97 | | - |
98 | | - logging.debug( |
99 | | - f"[PDNS] Replace {zone_name} {rr_name} {rr_type} {ttl} {record_contents}" |
100 | | - ) |
101 | | - |
102 | | - records = [ |
103 | | - { |
104 | | - "content": content, |
105 | | - "disabled": False, |
106 | | - } |
107 | | - for content in record_contents |
108 | | - ] |
109 | | - comments = [ |
110 | | - { |
111 | | - "content": f"{record_contents[idx]} : {idx}", |
112 | | - "account": "", |
113 | | - "modified_at": int(datetime.now().timestamp()), |
114 | | - } |
115 | | - for idx in range(len(record_contents)) |
116 | | - ] |
117 | | - |
118 | | - resp = requests.patch( |
119 | | - f"{PDNS_API_BASE_URL}/servers/localhost/zones/{zone_name}", |
120 | | - headers=PDNS_HEADERS, |
121 | | - json={ |
122 | | - "rrsets": [ |
123 | | - { |
124 | | - "name": rr_name, |
125 | | - "type": rr_type, |
126 | | - "changetype": "REPLACE", |
127 | | - "ttl": ttl, |
128 | | - "records": records, |
129 | | - "comments": comments, |
130 | | - }, |
131 | | - ], |
132 | | - }, |
133 | | - ) |
134 | | - |
135 | | - if resp.status_code != requests.codes.no_content: |
136 | | - raise CustomExceptionServerError( |
137 | | - f"{resp.status_code}: {resp.content.decode('utf-8')}" |
138 | | - ) # pragma: no cover |
| 69 | + assert rr_type == "TXT" |
| 70 | + |
| 71 | + # CF doesn't use trailing dot |
| 72 | + rr_name = rr_name[:-1] |
| 73 | + |
| 74 | + # Collect the existing content |
| 75 | + zone_id = ZONE_IDS[zone_name] |
| 76 | + results = client.dns.records.list( |
| 77 | + zone_id=zone_id, |
| 78 | + name=rr_name, |
| 79 | + type=rr_type, |
| 80 | + ).result |
| 81 | + |
| 82 | + for record in results: |
| 83 | + if record.content not in record_contents: |
| 84 | + # Delete records that are no longer needed |
| 85 | + client.dns.records.delete( |
| 86 | + zone_id=zone_id, |
| 87 | + dns_record_id=record.id, |
| 88 | + ) |
| 89 | + else: |
| 90 | + # Don't alter records that already exist |
| 91 | + record_contents.remove(record.content) |
| 92 | + |
| 93 | + for content in record_contents: |
| 94 | + # Create anything that's new |
| 95 | + client.dns.records.create( |
| 96 | + zone_id=zone_id, |
| 97 | + name=rr_name, |
| 98 | + type=rr_type, |
| 99 | + content=content, |
| 100 | + ) |
139 | 101 |
|
140 | 102 | # success |
141 | 103 | return |
142 | 104 |
|
143 | | - |
144 | | -def pdns_get_stats(): |
145 | | - resp = requests.get( |
146 | | - f"{PDNS_API_BASE_URL}/servers/localhost/statistics", |
147 | | - headers=PDNS_HEADERS, |
148 | | - ) |
149 | | - |
150 | | - if resp.status_code != 200: # pragma: no cover |
151 | | - logging.error(f"{resp.status_code}: {resp.content.decode('utf-8')}") |
152 | | - return {} |
153 | | - |
154 | | - # success |
155 | | - return resp.json() |
0 commit comments