This repository has been archived by the owner on Jun 7, 2021. It is now read-only.
/
ripe_ds_updater.py
161 lines (126 loc) · 4.58 KB
/
ripe_ds_updater.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
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
import datetime
from copy import deepcopy
import requests
from glom import glom
import dns.resolver
import dns.flags
import config as c
def get_maintained_domains(mntner):
params = {
"query-string": mntner,
"inverse-attribute": "mnt-by",
"type-filter": "domain",
"flags": ["no-referenced", "no-irt", "no-filtering", ],
"source": "RIPE",
}
r = requests.get(c.RIPE_DB_API_URL + "/search.json", params=params).json()
yield from glom(r, "objects.object")
def _iterate_attrs(obj, callback, **kwargs):
for a in glom(obj, "attributes.attribute"):
r = callback(a["name"], a["value"], **kwargs)
if r is not None:
yield r
def _get_rpsl(key, value):
return "{:19} {}\n".format(key + ":", value)
def _get_attr(key, value, attr):
if key == attr:
return value.lower()
def get_attrs(obj, attr):
return _iterate_attrs(obj, _get_attr, attr=attr)
def get_single_attr(obj, attr):
return next(get_attrs(obj, attr))
def ripe_obj_to_rpsl(obj):
yield from _iterate_attrs(obj, _get_rpsl)
def delete_ds_rdata(obj):
attrs = glom(obj, "attributes.attribute")
for a in list(attrs):
if a["name"] == "ds-rdata":
attrs.remove(a)
return obj
def append_ds_rdata(obj, ds_rdataset):
attrs = glom(obj, "attributes.attribute")
index = -1
for i, a in enumerate(attrs):
if a["name"] == "nserver":
index = i + 1
attrs[index:index] = [
{"name": "ds-rdata", "value": d, }
for d in ds_rdataset
]
return obj
def print_rpsl_object(obj):
print("".join(ripe_obj_to_rpsl(obj)))
def _clear_object(obj):
""" Remove unneeded keys before submission. Return deep copy. """
obj = deepcopy(obj)
attrs = glom(obj, "attributes.attribute")
for a in list(attrs):
if a["name"] in ["created", "last-modified"]:
attrs.remove(a)
return obj
def put_object_to_ripe_db(obj, password, dry_run=True):
domain = get_single_attr(obj, "domain")
uri = f"{c.RIPE_DB_API_URL}/ripe/domain/{domain}"
obj = _clear_object(obj)
json = {"objects": {"object": [obj, ], }, }
params = {"password": password, }
if dry_run:
params["dry-run"] = True
headers = {"Accept": "application/json", }
r = requests.put(uri, json=json, params=params, headers=headers).json()
if "errormessages" in r:
for e in glom(r, "errormessages.errormessage"):
print(f"{e['severity']}: {e['text']}")
return glom(r, "objects.object")[0]
def process_cds_records(obj, dry_run=True):
domain = get_single_attr(obj, "domain")
print(f"Domain: {domain}")
lm = get_single_attr(obj, "last-modified")
lm = datetime.datetime.strptime(lm, "%Y-%m-%dT%H:%M:%SZ")
lm = lm.replace(tzinfo=datetime.timezone.utc)
ripe_ds_rdataset = set(get_attrs(obj, "ds-rdata"))
print(f"RIPE rdataset: {ripe_ds_rdataset}")
resolver = dns.resolver.Resolver()
resolver.set_flags(dns.flags.RD | dns.flags.AD)
resolver.use_edns(0, dns.flags.DO, 512)
try:
a = resolver.query(domain + ".", "CDS")
assert a.response.rcode() == 0, "DNS response failure"
assert a.response.flags & dns.flags.AD, "Unauthenticated DNS response"
asig = a.response.find_rrset(
a.response.answer,
a.qname,
a.rdclass,
dns.rdatatype.RRSIG,
a.rdtype,
)
inception = datetime.datetime.fromtimestamp(
asig[0].inception,
datetime.timezone.utc,
)
dns_ds_rdataset = {rd.to_text() for rd in a}
print(f"DNS rdataset: {dns_ds_rdataset}")
print(f"Inception: {inception}, last modified: {lm}")
assert inception > lm, "Signature inception too early"
if dns_ds_rdataset and dns_ds_rdataset != ripe_ds_rdataset:
delete_ds_rdata(obj)
if not (
len(a) == 1 and # Special Delete DS record
a[0].key_tag == 0 and
a[0].algorithm == 0 and
a[0].digest_type == 0 and
a[0].digest == b'\x00'
):
append_ds_rdata(obj, dns_ds_rdataset)
print("updating DB record")
o = put_object_to_ripe_db(obj, c.UPDATER_PW, dry_run=dry_run)
print_rpsl_object(o)
except dns.exception.DNSException as e:
print(f"DNS exception: {e}")
except AssertionError as e:
print(f"Assertion error: {e}")
def main(dry_run=True):
for d in get_maintained_domains(c.UPDATER_MNT):
process_cds_records(d, dry_run)
if __name__ == "__main__":
main()