blob: 1ba9dc5dc7cfe14b0aa130f57024b37c520ee41e [file] [log] [blame]
Zack Williamscaf05662020-10-09 19:52:40 -07001#!/usr/bin/env python3
2
3# SPDX-FileCopyrightText: © 2020 Open Networking Foundation <support@opennetworking.org>
4# SPDX-License-Identifier: Apache-2.0
5
6# netbox_edgeconfig.py
7# given a s
8
9from __future__ import absolute_import
10
11import argparse
12import json
13import logging
14import netaddr
15import os
16import re
17import ssl
18import urllib.parse
19import urllib.request
20from ruamel import yaml
21
22# create shared logger
23logging.basicConfig()
24logger = logging.getLogger("nbec")
25
26# global dict of jsonpath expressions -> compiled jsonpath parsers, as
27# reparsing expressions in each loop results in 100x longer execution time
28jpathexpr = {}
29
30# headers to pass, set globally
31headers = []
32
33# settings
34settings = {}
35
36# cached data from API
37device_interface_cache = {}
38device_services_cache = {}
39interface_mac_cache = {}
40
Zack Williamsc0347202020-12-09 12:59:09 -070041# parent prefixes
42parent_prefixes = {}
43
Zack Williamscaf05662020-10-09 19:52:40 -070044
45def parse_nb_args():
46 """
47 parse CLI arguments
48 """
49
50 parser = argparse.ArgumentParser(description="NetBox Edge Config")
51
52 # Positional args
53 parser.add_argument(
54 "settings",
55 type=argparse.FileType("r"),
56 help="YAML ansible inventory file w/NetBox API token",
57 )
58
59 parser.add_argument(
60 "--debug", action="store_true", help="Print additional debugging information"
61 )
62
63 return parser.parse_args()
64
65
66def json_api_get(
67 url,
68 headers,
69 data=None,
70 trim_prefix=False,
71 allow_failure=False,
72 validate_certs=False,
73):
74 """
75 Call JSON API endpoint, return data as a dict
76 """
77
78 logger.debug("json_api_get url: %s", url)
79
80 # if data included, encode it as JSON
81 if data:
82 data_enc = str(json.dumps(data)).encode("utf-8")
83
84 request = urllib.request.Request(url, data=data_enc, method="POST")
85 request.add_header("Content-Type", "application/json; charset=UTF-8")
86 else:
87 request = urllib.request.Request(url)
88
89 # add headers tuples
90 for header in headers:
91 request.add_header(*header)
92
93 try:
94
95 if validate_certs:
96 response = urllib.request.urlopen(request)
97
98 else:
99 ctx = ssl.create_default_context()
100 ctx.check_hostname = False
101 ctx.verify_mode = ssl.CERT_NONE
102
103 response = urllib.request.urlopen(request, context=ctx)
104
105 except urllib.error.HTTPError:
106 # asking for data that doesn't exist results in a 404, just return nothing
107 if allow_failure:
108 return None
109 logger.exception("Server encountered an HTTPError at URL: '%s'", url)
110 except urllib.error.URLError:
111 logger.exception("An URLError occurred at URL: '%s'", url)
112 else:
113 # docs: https://docs.python.org/3/library/json.html
114 jsondata = response.read()
115 logger.debug("API response: %s", jsondata)
116
117 try:
118 data = json.loads(jsondata)
119 except json.decoder.JSONDecodeError:
120 # allow return of no data
121 if allow_failure:
122 return None
123 logger.exception("Unable to decode JSON")
124 else:
125 logger.debug("JSON decoded: %s", data)
126
127 return data
128
129
Zack Williamsc0347202020-12-09 12:59:09 -0700130def create_dns_zone(extension, devs, parent_devs={}):
Zack Williamscaf05662020-10-09 19:52:40 -0700131 # Checks for dns entries
132
133 a_recs = {} # PTR records created by inverting this
134 cname_recs = {}
135 srv_recs = {}
136 ns_recs = []
137 txt_recs = {}
138
139 # scan through devs and look for dns_name, if not, make from name and
140 # extension
141 for name, value in devs.items():
142
143 # add DNS entries for every DHCP host if there's a DHCP range
144 # DHCP addresses are of the form dhcp###.extension
145 if name == "prefix_dhcp":
146 for ip in netaddr.IPNetwork(value["dhcp_range"]).iter_hosts():
147 a_recs["dhcp%03d" % (ip.words[3])] = str(ip)
148
149 continue
150
151 # require DNS names to only use ASCII characters (alphanumeric, lowercase, with dash/period)
152 # _'s are used in SRV/TXT records, but in general use aren't recommended
153 dns_name = re.sub("[^a-z0-9.-]", "-", name, 0, re.ASCII)
154
155 # Add as an A record (and inverse, PTR record), only if it's a new name
156 if dns_name not in a_recs:
157 a_recs[dns_name] = value["ip4"]
158 else:
159 # most likely a data entry error
160 logger.warning(
161 "Duplicate DNS name '%s' for devices at IP: '%s' and '%s', ignoring",
162 dns_name,
163 a_recs[dns_name],
164 value["ip4"],
165 )
166 continue
167
168 # if a DNS name is given as a part of the IP address, it's viewed as a CNAME
169 if value["dns_name"]:
170
171 if re.search("%s$" % extension, value["dns_name"]):
172
173 # strip off the extension, and add as a CNAME
174 dns_cname = value["dns_name"].split(".%s" % extension)[0]
175
176 elif "." in value["dns_name"]:
177 logger.warning(
178 "Device '%s' has a IP assigned DNS name '%s' outside the prefix extension: '%s', ignoring",
179 name,
180 value["dns_name"],
181 extension,
182 )
183 continue
184
185 else:
186 dns_cname = value["dns_name"]
187
188 if dns_cname == dns_name:
189 logger.warning(
190 "DNS Name field '%s' is identical to device name '%s', ignoring",
191 value["dns_name"],
192 dns_name,
193 )
194 else:
195 cname_recs[dns_cname] = "%s.%s." % (dns_name, extension)
196
197 # Add services as cnames, and possibly ns records
198 for svc in value["services"]:
199
200 # only add service if it uses the IP of the host
201 if value["ip4"] in svc["ip4s"]:
202 cname_recs[svc["name"]] = "%s.%s." % (dns_name, extension)
203
204 if svc["port"] == 53 and svc["protocol"] == "udp":
205 ns_recs.append("%s.%s." % (dns_name, extension))
206
Zack Williamsc0347202020-12-09 12:59:09 -0700207 # iterate over the parent devs to add additional nameservers
208 for pname, pval in parent_devs.items():
209 if "services" in pval:
210 for svc in pval["services"]:
211 # look for DNS servers
212 if svc["port"] == 53 and svc["protocol"] == "udp":
213 # make name
214 dns_name = re.sub("[^a-z0-9.-]", "-", pname, 0, re.ASCII)
215
216 # add an a record for this nameserver if IP is outside of subnet
217 a_recs[dns_name] = pval["ip4"]
218
219 # add a NS record if it doesn't already exist
220 ns_name = "%s.%s." % (dns_name, extension)
221 if ns_name not in ns_recs:
222 ns_recs.append(ns_name)
223
Zack Williamscaf05662020-10-09 19:52:40 -0700224 return {
225 "a": a_recs,
226 "cname": cname_recs,
227 "ns": ns_recs,
228 "srv": srv_recs,
229 "txt": txt_recs,
230 }
231
232
Zack Williamsc0347202020-12-09 12:59:09 -0700233def create_dhcp_subnet(prefix, prefix_search, devs, parent_devs={}):
Zack Williamscaf05662020-10-09 19:52:40 -0700234 # makes DHCP subnet information
235
236 subnet = {}
237
238 subnet["subnet"] = prefix
239 subnet["dns_search"] = [prefix_search]
240
Zack Williamsc0347202020-12-09 12:59:09 -0700241 def dhcp_iterate(devs):
242 # inner function to iterate over a dev list
243 ihosts = []
244 idyn_range = None
245 irouter = None
246 idns_servers = []
247 itftpd_server = None
Zack Williamscaf05662020-10-09 19:52:40 -0700248
Zack Williamsc0347202020-12-09 12:59:09 -0700249 for name, value in devs.items():
Zack Williamscaf05662020-10-09 19:52:40 -0700250
Zack Williamsc0347202020-12-09 12:59:09 -0700251 # handle a DHCP range
252 if name == "prefix_dhcp":
253 idyn_range = value["dhcp_range"]
254 continue
Zack Williamscaf05662020-10-09 19:52:40 -0700255
Zack Williamsc0347202020-12-09 12:59:09 -0700256 # handle a router reservation
257 if name == "router":
258 irouter = value["ip4"]
259 continue
Zack Williams70ae8272020-12-03 09:54:59 -0700260
Zack Williamsc0347202020-12-09 12:59:09 -0700261 # has a MAC address, and it's not null
262 if "macaddr" in value and value["macaddr"]:
Zack Williamscaf05662020-10-09 19:52:40 -0700263
Zack Williamsc0347202020-12-09 12:59:09 -0700264 ihosts.append(
265 {
266 "name": name,
267 "ip_addr": value["ip4"],
268 "mac_addr": value["macaddr"].lower(),
269 }
270 )
Zack Williamscaf05662020-10-09 19:52:40 -0700271
Zack Williamsc0347202020-12-09 12:59:09 -0700272 # Add dns based on service entries
273 if "services" in value:
274 for svc in value["services"]:
Zack Williamscaf05662020-10-09 19:52:40 -0700275
Zack Williamsc0347202020-12-09 12:59:09 -0700276 # add DNS server
277 if svc["port"] == 53 and svc["protocol"] == "udp":
278 idns_servers.append(value["ip4"])
Zack Williamscaf05662020-10-09 19:52:40 -0700279
Zack Williamsc0347202020-12-09 12:59:09 -0700280 # add tftp server
281 if svc["port"] == 69 and svc["protocol"] == "udp":
282 itftpd_server = value["ip4"]
Zack Williamscaf05662020-10-09 19:52:40 -0700283
Zack Williamsc0347202020-12-09 12:59:09 -0700284 return (ihosts, idyn_range, irouter, idns_servers, itftpd_server)
285
286 # run inner function and build
287 hosts, dyn_range, router, dns_servers, tftpd_server = dhcp_iterate(devs)
288
289 # assign only hosts, dynamic range, based on the prefix
Zack Williamscaf05662020-10-09 19:52:40 -0700290 subnet["hosts"] = hosts
Zack Williamsc0347202020-12-09 12:59:09 -0700291 subnet["range"] = dyn_range
292
293 # only assign router if specified
294 if router:
295 subnet["routers"] = router
296
297 # find parent prefix devices, to fill in where needed
298 phosts, pdyn_range, prouter, pdns_servers, ptftpd_server = dhcp_iterate(parent_devs)
299
300 # use parent prefix devices if dns/tftp services needed aren't found within prefix
301 if dns_servers:
302 subnet["dns_servers"] = dns_servers
303 else:
304 subnet["dns_servers"] = pdns_servers
305
306 if tftpd_server:
307 subnet["tftpd_server"] = tftpd_server
308 else:
309 subnet["tftpd_server"] = ptftpd_server
Zack Williamscaf05662020-10-09 19:52:40 -0700310
311 return subnet
312
313
314def find_dhcpd_interface(prefix, devs):
315 # DHCPd interface is first usable IP in range
316
317 first_ip = str(netaddr.IPAddress(netaddr.IPNetwork(prefix).first + 1))
318
Zack Williamsc0347202020-12-09 12:59:09 -0700319 # look for interface corresponding to first IP address in range
Zack Williamscaf05662020-10-09 19:52:40 -0700320 for name, value in devs.items():
Zack Williamsc0347202020-12-09 12:59:09 -0700321 if "ip4" in value:
322 if value["ip4"] == first_ip:
323 return value["iface"]
324
325 # if interface not found, return None and ignore
326 return None
Zack Williamscaf05662020-10-09 19:52:40 -0700327
328
329def get_device_services(device_id, filters=""):
330
331 if device_id in device_services_cache:
332 return device_services_cache[device_id]
333
334 # get services info
335 url = "%s%s" % (
336 settings["api_endpoint"],
337 "api/ipam/services/?device_id=%s%s" % (device_id, filters),
338 )
339
340 raw_svcs = json_api_get(url, headers, validate_certs=settings["validate_certs"])
341
342 services = []
343
344 for rsvc in raw_svcs["results"]:
345
346 svc = {}
347
348 svc["name"] = rsvc["name"]
349 svc["description"] = rsvc["description"]
350 svc["port"] = rsvc["port"]
351 svc["protocol"] = rsvc["protocol"]["value"]
352 svc["ip4s"] = []
353
354 for ip in rsvc["ipaddresses"]:
355 svc["ip4s"].append(str(netaddr.IPNetwork(ip["address"]).ip))
356
357 services.append(svc)
358
359 device_services_cache[device_id] = services
360 return services
361
362
363def get_interface_mac_addr(interface_id):
364 # return a mac addres, or None if undefined
365 if interface_id in interface_mac_cache:
366 return interface_mac_cache[interface_id]
367
368 # get the interface info
369 url = "%s%s" % (settings["api_endpoint"], "api/dcim/interfaces/%s/" % interface_id)
370
371 iface = json_api_get(url, headers, validate_certs=settings["validate_certs"])
372
373 if iface["mac_address"]:
374 interface_mac_cache[interface_id] = iface["mac_address"]
375 return iface["mac_address"]
376
377 interface_mac_cache[interface_id] = None
378 return None
379
380
381def get_device_interfaces(device_id, filters=""):
382
383 if device_id in device_interface_cache:
384 return device_interface_cache[device_id]
385
386 url = "%s%s" % (
387 settings["api_endpoint"],
388 "api/dcim/interfaces/?device_id=%s%s&mgmt_only=true" % (device_id, filters),
389 )
390
391 logger.debug("raw_ifaces_url: %s", url)
392
393 raw_ifaces = json_api_get(url, headers, validate_certs=settings["validate_certs"])
394
395 logger.debug("raw_ifaces: %s", raw_ifaces)
396
397 ifaces = []
398
399 for raw_iface in raw_ifaces["results"]:
400
401 iface = {}
402
403 iface["name"] = raw_iface["name"]
404 iface["macaddr"] = raw_iface["mac_address"]
405 iface["mgmt_only"] = raw_iface["mgmt_only"]
406 iface["description"] = raw_iface["description"]
407
408 if raw_iface["count_ipaddresses"]:
409 url = "%s%s" % (
410 settings["api_endpoint"],
411 "api/ipam/ip-addresses/?interface_id=%s" % raw_iface["id"],
412 )
413
414 raw_ip = json_api_get(
415 url, headers, validate_certs=settings["validate_certs"]
416 )
417
418 iface["ip4"] = str(netaddr.IPNetwork(raw_ip["results"][0]["address"]).ip)
419
420 ifaces.append(iface)
421
422 device_interface_cache[device_id] = ifaces
423 return ifaces
424
425
426def get_prefix_devices(prefix, filters=""):
427
428 # get all devices in a prefix
429 url = "%s%s" % (
430 settings["api_endpoint"],
431 "api/ipam/ip-addresses/?parent=%s%s" % (prefix, filters),
432 )
433
434 raw_ips = json_api_get(url, headers, validate_certs=settings["validate_certs"])
435
436 logger.debug("raw_ips: %s", raw_ips)
437
438 devs = {}
439
440 # iterate by IP, sorted
441 for ip in sorted(raw_ips["results"], key=lambda k: k["address"]):
442
443 logger.debug("ip: %s", ip)
444
445 # if it's a DHCP range, add that range to the dev list as prefix_dhcp
446 if ip["status"]["value"] == "dhcp":
447 devs["prefix_dhcp"] = {"dhcp_range": ip["address"]}
448 continue
449
Zack Williams70ae8272020-12-03 09:54:59 -0700450 # if it's a reserved IP
451 if ip["status"]["value"] == "reserved":
452 res = {}
453
454 res["type"] = "reserved"
455 res["description"] = ip["description"]
456 res["ip4"] = str(netaddr.IPNetwork(ip["address"]).ip)
457 res["dns_name"] = ip["dns_name"] if "dns_name" in ip else "None"
458 res["services"] = {}
459
460 resname = res["description"].lower().split(" ")[0]
461
462 devs[resname] = res
463 continue
464
Zack Williamscaf05662020-10-09 19:52:40 -0700465 dev = {}
466
Zack Williams70ae8272020-12-03 09:54:59 -0700467 dev["type"] = "device"
Zack Williamscaf05662020-10-09 19:52:40 -0700468 dev["ip4"] = str(netaddr.IPNetwork(ip["address"]).ip)
469 dev["macaddr"] = get_interface_mac_addr(ip["assigned_object"]["id"])
470
471 ifaces = get_device_interfaces(ip["assigned_object"]["device"]["id"])
472
473 if ifaces and dev["ip4"] == ifaces[0]["ip4"]: # this is a mgmt IP
474 devname = "%s-%s" % (
475 ip["assigned_object"]["device"]["name"].lower().split(".")[0],
476 ifaces[0]["name"],
477 )
478 dev["iface"] = ip["assigned_object"]["name"]
479 dev["dns_name"] = ""
480 dev["services"] = []
481
482 else: # this is a primary IP
483
484 name = ip["assigned_object"]["device"]["name"]
485 devname = name.lower().split(".")[0]
486
487 dev["iface"] = ip["assigned_object"]["name"]
488 dev["dns_name"] = ip["dns_name"] if "dns_name" in ip else "None"
489 dev["services"] = get_device_services(ip["assigned_object"]["device"]["id"])
490
491 # fix multihomed devices in same IP range
492 # FIXME: Does not handle > 2 connections properly
493 if devname in devs:
494 devs["%s-1" % devname] = devs.pop(devname)
495 devs["%s-2" % devname] = dev
496 else:
497 devs[devname] = dev
498
499 return devs
500
501
Zack Williamsc0347202020-12-09 12:59:09 -0700502def get_parent_prefix(child_prefix):
503 # returns a parent prefix given a child prefix
504 # FIXME: only returns the first found prefix, so doesn't handle more than 2 layers of hierarchy
505
506 # get all devices in a prefix
507 url = "%s%s" % (
508 settings["api_endpoint"],
509 "api/ipam/prefixes/?contains=%s" % child_prefix,
510 )
511
512 raw_prefixes = json_api_get(url, headers, validate_certs=settings["validate_certs"])
513
514 logger.debug(raw_prefixes)
515
516 for prefix in raw_prefixes["results"]:
517 if prefix["prefix"] != child_prefix:
518 return prefix["prefix"]
519
520 return None
521
522
Zack Williamscaf05662020-10-09 19:52:40 -0700523def get_prefix_data(prefix):
524
525 # get all devices in a prefix
526 url = "%s%s" % (settings["api_endpoint"], "api/ipam/prefixes/?prefix=%s" % prefix)
527
528 raw_prefix = json_api_get(url, headers, validate_certs=settings["validate_certs"])
529
530 logger.debug("raw_prefix: %s", raw_prefix)
531
532 return raw_prefix["results"][0]
533
534
535# main function that calls other functions
536if __name__ == "__main__":
537
538 args = parse_nb_args()
539
540 # only print log messages if debugging
541 if args.debug:
542 logger.setLevel(logging.DEBUG)
543 else:
544 logger.setLevel(logging.INFO)
545
546 # load settings from yaml file
547 settings = yaml.safe_load(args.settings.read())
548
549 yaml_out = {}
550
551 # load default config
552 with open(
553 os.path.join(
554 os.path.dirname(os.path.realpath(__file__)), "base_edgeconfig.yaml"
555 )
556 ) as defconfig:
557 yaml_out = yaml.safe_load(defconfig)
558
559 logger.debug("settings: %s" % settings)
560
561 # global, so this isn't run multiple times
562 headers = [
563 ("Authorization", "Token %s" % settings["token"]),
564 ]
565
566 # create structure from extracted data
567 dns_zones = {}
568 dns_rev_zones = {}
569 dhcpd_subnets = []
570 dhcpd_interfaces = []
571 devs_per_prefix = {}
Zack Williamsc0347202020-12-09 12:59:09 -0700572 prefixes = {}
573 parent_prefixes = {}
Zack Williamscaf05662020-10-09 19:52:40 -0700574
575 for prefix in settings["ip_prefixes"]:
576
577 prefix_data = get_prefix_data(prefix)
578
Zack Williamsc0347202020-12-09 12:59:09 -0700579 parent_prefix = get_parent_prefix(prefix)
580 prefix_data["parent"] = parent_prefix
581
582 pdevs = {}
583 if parent_prefix:
584 if parent_prefix in parent_prefixes:
585 pdevs = devs_per_prefix[parent_prefix]
586 else:
587 pdevs = get_prefix_devices(parent_prefix)
588 devs_per_prefix[parent_prefix] = pdevs
589
590 prefix_data["parent_devs"] = pdevs
591
592 prefixes[prefix] = prefix_data
593
Zack Williamscaf05662020-10-09 19:52:40 -0700594 prefix_domain_extension = prefix_data["description"]
595
596 devs = get_prefix_devices(prefix)
597
598 devs_per_prefix[prefix] = devs
599
600 dns_zones[prefix_domain_extension] = create_dns_zone(
Zack Williamsc0347202020-12-09 12:59:09 -0700601 prefix_domain_extension, devs, pdevs
Zack Williamscaf05662020-10-09 19:52:40 -0700602 )
603
604 dns_zones[prefix_domain_extension]["ip_range"] = prefix
605
Zack Williamsc0347202020-12-09 12:59:09 -0700606 dhcpd_subnets.append(
607 create_dhcp_subnet(prefix, prefix_domain_extension, devs, pdevs)
608 )
Zack Williamscaf05662020-10-09 19:52:40 -0700609
610 dhcpd_if = find_dhcpd_interface(prefix, devs)
611
Zack Williamsc0347202020-12-09 12:59:09 -0700612 if dhcpd_if and dhcpd_if not in dhcpd_interfaces:
Zack Williamscaf05662020-10-09 19:52:40 -0700613 dhcpd_interfaces.append(dhcpd_if)
614
615 yaml_out.update(
616 {
617 "dns_zones": dns_zones,
618 "dns_rev_zones": dns_rev_zones,
619 "dhcpd_subnets": dhcpd_subnets,
620 "dhcpd_interfaces": dhcpd_interfaces,
Zack Williamsc0347202020-12-09 12:59:09 -0700621 # the below are useful when debugging
622 # "devs_per_prefix": devs_per_prefix,
623 # "prefixes": prefixes,
Zack Williamscaf05662020-10-09 19:52:40 -0700624 }
625 )
626
627 print(yaml.safe_dump(yaml_out, indent=2))