blob: 9f4e9d60b7a492fb285b27cff7597a5252c90541 [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
Zack Williamsaf5bd032020-12-14 20:55:26 -0700245 irouter = []
Zack Williamsc0347202020-12-09 12:59:09 -0700246 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":
Zack Williamsaf5bd032020-12-14 20:55:26 -0700258 ir = {"ip": value["ip4"]}
259 if (
260 "rfc3442routes" in value["custom_fields"]
261 and value["custom_fields"]["rfc3442routes"]
262 ):
263 ir["rfc3442routes"] = value["custom_fields"]["rfc3442routes"].split(
264 ","
265 )
266
267 irouter.append(ir)
Zack Williamsc0347202020-12-09 12:59:09 -0700268 continue
Zack Williams70ae8272020-12-03 09:54:59 -0700269
Zack Williamsc0347202020-12-09 12:59:09 -0700270 # has a MAC address, and it's not null
271 if "macaddr" in value and value["macaddr"]:
Zack Williamscaf05662020-10-09 19:52:40 -0700272
Zack Williamsc0347202020-12-09 12:59:09 -0700273 ihosts.append(
274 {
275 "name": name,
276 "ip_addr": value["ip4"],
277 "mac_addr": value["macaddr"].lower(),
278 }
279 )
Zack Williamscaf05662020-10-09 19:52:40 -0700280
Zack Williamsc0347202020-12-09 12:59:09 -0700281 # Add dns based on service entries
282 if "services" in value:
283 for svc in value["services"]:
Zack Williamscaf05662020-10-09 19:52:40 -0700284
Zack Williamsc0347202020-12-09 12:59:09 -0700285 # add DNS server
286 if svc["port"] == 53 and svc["protocol"] == "udp":
287 idns_servers.append(value["ip4"])
Zack Williamscaf05662020-10-09 19:52:40 -0700288
Zack Williamsc0347202020-12-09 12:59:09 -0700289 # add tftp server
290 if svc["port"] == 69 and svc["protocol"] == "udp":
291 itftpd_server = value["ip4"]
Zack Williamscaf05662020-10-09 19:52:40 -0700292
Zack Williamsc0347202020-12-09 12:59:09 -0700293 return (ihosts, idyn_range, irouter, idns_servers, itftpd_server)
294
295 # run inner function and build
296 hosts, dyn_range, router, dns_servers, tftpd_server = dhcp_iterate(devs)
297
298 # assign only hosts, dynamic range, based on the prefix
Zack Williamscaf05662020-10-09 19:52:40 -0700299 subnet["hosts"] = hosts
Zack Williamsc0347202020-12-09 12:59:09 -0700300 subnet["range"] = dyn_range
301
302 # only assign router if specified
303 if router:
304 subnet["routers"] = router
305
306 # find parent prefix devices, to fill in where needed
307 phosts, pdyn_range, prouter, pdns_servers, ptftpd_server = dhcp_iterate(parent_devs)
308
309 # use parent prefix devices if dns/tftp services needed aren't found within prefix
310 if dns_servers:
311 subnet["dns_servers"] = dns_servers
312 else:
313 subnet["dns_servers"] = pdns_servers
314
315 if tftpd_server:
316 subnet["tftpd_server"] = tftpd_server
317 else:
318 subnet["tftpd_server"] = ptftpd_server
Zack Williamscaf05662020-10-09 19:52:40 -0700319
320 return subnet
321
322
323def find_dhcpd_interface(prefix, devs):
324 # DHCPd interface is first usable IP in range
325
326 first_ip = str(netaddr.IPAddress(netaddr.IPNetwork(prefix).first + 1))
327
Zack Williamsc0347202020-12-09 12:59:09 -0700328 # look for interface corresponding to first IP address in range
Zack Williamscaf05662020-10-09 19:52:40 -0700329 for name, value in devs.items():
Zack Williamsc0347202020-12-09 12:59:09 -0700330 if "ip4" in value:
331 if value["ip4"] == first_ip:
332 return value["iface"]
333
334 # if interface not found, return None and ignore
335 return None
Zack Williamscaf05662020-10-09 19:52:40 -0700336
337
338def get_device_services(device_id, filters=""):
339
340 if device_id in device_services_cache:
341 return device_services_cache[device_id]
342
343 # get services info
344 url = "%s%s" % (
345 settings["api_endpoint"],
346 "api/ipam/services/?device_id=%s%s" % (device_id, filters),
347 )
348
349 raw_svcs = json_api_get(url, headers, validate_certs=settings["validate_certs"])
350
351 services = []
352
353 for rsvc in raw_svcs["results"]:
354
355 svc = {}
356
357 svc["name"] = rsvc["name"]
358 svc["description"] = rsvc["description"]
359 svc["port"] = rsvc["port"]
360 svc["protocol"] = rsvc["protocol"]["value"]
361 svc["ip4s"] = []
362
363 for ip in rsvc["ipaddresses"]:
364 svc["ip4s"].append(str(netaddr.IPNetwork(ip["address"]).ip))
365
366 services.append(svc)
367
368 device_services_cache[device_id] = services
369 return services
370
371
372def get_interface_mac_addr(interface_id):
373 # return a mac addres, or None if undefined
374 if interface_id in interface_mac_cache:
375 return interface_mac_cache[interface_id]
376
377 # get the interface info
378 url = "%s%s" % (settings["api_endpoint"], "api/dcim/interfaces/%s/" % interface_id)
379
380 iface = json_api_get(url, headers, validate_certs=settings["validate_certs"])
381
382 if iface["mac_address"]:
383 interface_mac_cache[interface_id] = iface["mac_address"]
384 return iface["mac_address"]
385
386 interface_mac_cache[interface_id] = None
387 return None
388
389
390def get_device_interfaces(device_id, filters=""):
391
392 if device_id in device_interface_cache:
393 return device_interface_cache[device_id]
394
395 url = "%s%s" % (
396 settings["api_endpoint"],
397 "api/dcim/interfaces/?device_id=%s%s&mgmt_only=true" % (device_id, filters),
398 )
399
400 logger.debug("raw_ifaces_url: %s", url)
401
402 raw_ifaces = json_api_get(url, headers, validate_certs=settings["validate_certs"])
403
404 logger.debug("raw_ifaces: %s", raw_ifaces)
405
406 ifaces = []
407
408 for raw_iface in raw_ifaces["results"]:
409
410 iface = {}
411
412 iface["name"] = raw_iface["name"]
413 iface["macaddr"] = raw_iface["mac_address"]
414 iface["mgmt_only"] = raw_iface["mgmt_only"]
415 iface["description"] = raw_iface["description"]
416
417 if raw_iface["count_ipaddresses"]:
418 url = "%s%s" % (
419 settings["api_endpoint"],
420 "api/ipam/ip-addresses/?interface_id=%s" % raw_iface["id"],
421 )
422
423 raw_ip = json_api_get(
424 url, headers, validate_certs=settings["validate_certs"]
425 )
426
427 iface["ip4"] = str(netaddr.IPNetwork(raw_ip["results"][0]["address"]).ip)
428
429 ifaces.append(iface)
430
431 device_interface_cache[device_id] = ifaces
432 return ifaces
433
434
435def get_prefix_devices(prefix, filters=""):
436
437 # get all devices in a prefix
438 url = "%s%s" % (
439 settings["api_endpoint"],
440 "api/ipam/ip-addresses/?parent=%s%s" % (prefix, filters),
441 )
442
443 raw_ips = json_api_get(url, headers, validate_certs=settings["validate_certs"])
444
445 logger.debug("raw_ips: %s", raw_ips)
446
447 devs = {}
448
449 # iterate by IP, sorted
450 for ip in sorted(raw_ips["results"], key=lambda k: k["address"]):
451
452 logger.debug("ip: %s", ip)
453
454 # if it's a DHCP range, add that range to the dev list as prefix_dhcp
455 if ip["status"]["value"] == "dhcp":
456 devs["prefix_dhcp"] = {"dhcp_range": ip["address"]}
457 continue
458
Zack Williams70ae8272020-12-03 09:54:59 -0700459 # if it's a reserved IP
460 if ip["status"]["value"] == "reserved":
461 res = {}
462
463 res["type"] = "reserved"
464 res["description"] = ip["description"]
465 res["ip4"] = str(netaddr.IPNetwork(ip["address"]).ip)
466 res["dns_name"] = ip["dns_name"] if "dns_name" in ip else "None"
467 res["services"] = {}
Zack Williamsaf5bd032020-12-14 20:55:26 -0700468 res["custom_fields"] = ip["custom_fields"]
Zack Williams70ae8272020-12-03 09:54:59 -0700469
470 resname = res["description"].lower().split(" ")[0]
471
472 devs[resname] = res
473 continue
474
Zack Williamsaf5bd032020-12-14 20:55:26 -0700475 aotype = ip["assigned_object_type"]
476
477 # don't handle VM's yet
478 if aotype == "virtualization.vminterface":
479 continue
480
Zack Williamscaf05662020-10-09 19:52:40 -0700481 dev = {}
482
Zack Williams70ae8272020-12-03 09:54:59 -0700483 dev["type"] = "device"
Zack Williamscaf05662020-10-09 19:52:40 -0700484 dev["ip4"] = str(netaddr.IPNetwork(ip["address"]).ip)
485 dev["macaddr"] = get_interface_mac_addr(ip["assigned_object"]["id"])
486
487 ifaces = get_device_interfaces(ip["assigned_object"]["device"]["id"])
488
489 if ifaces and dev["ip4"] == ifaces[0]["ip4"]: # this is a mgmt IP
490 devname = "%s-%s" % (
491 ip["assigned_object"]["device"]["name"].lower().split(".")[0],
492 ifaces[0]["name"],
493 )
494 dev["iface"] = ip["assigned_object"]["name"]
495 dev["dns_name"] = ""
496 dev["services"] = []
497
498 else: # this is a primary IP
499
500 name = ip["assigned_object"]["device"]["name"]
501 devname = name.lower().split(".")[0]
502
503 dev["iface"] = ip["assigned_object"]["name"]
504 dev["dns_name"] = ip["dns_name"] if "dns_name" in ip else "None"
505 dev["services"] = get_device_services(ip["assigned_object"]["device"]["id"])
506
507 # fix multihomed devices in same IP range
508 # FIXME: Does not handle > 2 connections properly
509 if devname in devs:
510 devs["%s-1" % devname] = devs.pop(devname)
511 devs["%s-2" % devname] = dev
512 else:
513 devs[devname] = dev
514
515 return devs
516
517
Zack Williamsc0347202020-12-09 12:59:09 -0700518def get_parent_prefix(child_prefix):
519 # returns a parent prefix given a child prefix
520 # FIXME: only returns the first found prefix, so doesn't handle more than 2 layers of hierarchy
521
522 # get all devices in a prefix
523 url = "%s%s" % (
524 settings["api_endpoint"],
525 "api/ipam/prefixes/?contains=%s" % child_prefix,
526 )
527
528 raw_prefixes = json_api_get(url, headers, validate_certs=settings["validate_certs"])
529
530 logger.debug(raw_prefixes)
531
532 for prefix in raw_prefixes["results"]:
533 if prefix["prefix"] != child_prefix:
534 return prefix["prefix"]
535
536 return None
537
538
Zack Williamscaf05662020-10-09 19:52:40 -0700539def get_prefix_data(prefix):
540
541 # get all devices in a prefix
542 url = "%s%s" % (settings["api_endpoint"], "api/ipam/prefixes/?prefix=%s" % prefix)
543
544 raw_prefix = json_api_get(url, headers, validate_certs=settings["validate_certs"])
545
546 logger.debug("raw_prefix: %s", raw_prefix)
547
548 return raw_prefix["results"][0]
549
550
551# main function that calls other functions
552if __name__ == "__main__":
553
554 args = parse_nb_args()
555
556 # only print log messages if debugging
557 if args.debug:
558 logger.setLevel(logging.DEBUG)
559 else:
560 logger.setLevel(logging.INFO)
561
562 # load settings from yaml file
563 settings = yaml.safe_load(args.settings.read())
564
565 yaml_out = {}
566
567 # load default config
568 with open(
569 os.path.join(
570 os.path.dirname(os.path.realpath(__file__)), "base_edgeconfig.yaml"
571 )
572 ) as defconfig:
573 yaml_out = yaml.safe_load(defconfig)
574
575 logger.debug("settings: %s" % settings)
576
577 # global, so this isn't run multiple times
578 headers = [
579 ("Authorization", "Token %s" % settings["token"]),
580 ]
581
582 # create structure from extracted data
583 dns_zones = {}
584 dns_rev_zones = {}
585 dhcpd_subnets = []
586 dhcpd_interfaces = []
587 devs_per_prefix = {}
Zack Williamsc0347202020-12-09 12:59:09 -0700588 prefixes = {}
589 parent_prefixes = {}
Zack Williamscaf05662020-10-09 19:52:40 -0700590
591 for prefix in settings["ip_prefixes"]:
592
593 prefix_data = get_prefix_data(prefix)
594
Zack Williamsc0347202020-12-09 12:59:09 -0700595 parent_prefix = get_parent_prefix(prefix)
596 prefix_data["parent"] = parent_prefix
597
598 pdevs = {}
599 if parent_prefix:
600 if parent_prefix in parent_prefixes:
601 pdevs = devs_per_prefix[parent_prefix]
602 else:
603 pdevs = get_prefix_devices(parent_prefix)
604 devs_per_prefix[parent_prefix] = pdevs
605
606 prefix_data["parent_devs"] = pdevs
607
608 prefixes[prefix] = prefix_data
609
Zack Williamscaf05662020-10-09 19:52:40 -0700610 prefix_domain_extension = prefix_data["description"]
611
612 devs = get_prefix_devices(prefix)
613
614 devs_per_prefix[prefix] = devs
615
616 dns_zones[prefix_domain_extension] = create_dns_zone(
Zack Williamsc0347202020-12-09 12:59:09 -0700617 prefix_domain_extension, devs, pdevs
Zack Williamscaf05662020-10-09 19:52:40 -0700618 )
619
620 dns_zones[prefix_domain_extension]["ip_range"] = prefix
621
Zack Williamsc0347202020-12-09 12:59:09 -0700622 dhcpd_subnets.append(
623 create_dhcp_subnet(prefix, prefix_domain_extension, devs, pdevs)
624 )
Zack Williamscaf05662020-10-09 19:52:40 -0700625
626 dhcpd_if = find_dhcpd_interface(prefix, devs)
627
Zack Williamsc0347202020-12-09 12:59:09 -0700628 if dhcpd_if and dhcpd_if not in dhcpd_interfaces:
Zack Williamscaf05662020-10-09 19:52:40 -0700629 dhcpd_interfaces.append(dhcpd_if)
630
631 yaml_out.update(
632 {
633 "dns_zones": dns_zones,
634 "dns_rev_zones": dns_rev_zones,
635 "dhcpd_subnets": dhcpd_subnets,
636 "dhcpd_interfaces": dhcpd_interfaces,
Zack Williamsc0347202020-12-09 12:59:09 -0700637 # the below are useful when debugging
638 # "devs_per_prefix": devs_per_prefix,
639 # "prefixes": prefixes,
Zack Williamscaf05662020-10-09 19:52:40 -0700640 }
641 )
642
643 print(yaml.safe_dump(yaml_out, indent=2))