diff options
Diffstat (limited to 'portal-gui/scripts')
-rwxr-xr-x | portal-gui/scripts/helper (renamed from portal-gui/scripts/list_resources) | 3 | ||||
-rwxr-xr-x | portal-gui/scripts/list_resources.py | 162 | ||||
l--------- | portal-gui/scripts/load_csv | 1 | ||||
-rwxr-xr-x | portal-gui/scripts/load_csv.py | 61 |
4 files changed, 143 insertions, 84 deletions
diff --git a/portal-gui/scripts/list_resources b/portal-gui/scripts/helper index 07df3541..7fd9e8c0 100755 --- a/portal-gui/scripts/list_resources +++ b/portal-gui/scripts/helper @@ -1,5 +1,6 @@ #!/bin/sh +NAME=`basename $0` BASE_PATH=`dirname $0`/../.. export PYTHONPATH=$BASE_PATH/rpkid:$BASE_PATH/portal-gui export DJANGO_SETTINGS_MODULE=rpkigui.settings -python `dirname $0`/list_resources.py +python `dirname $0`/${NAME}.py $* diff --git a/portal-gui/scripts/list_resources.py b/portal-gui/scripts/list_resources.py index ae95228b..acd97847 100755 --- a/portal-gui/scripts/list_resources.py +++ b/portal-gui/scripts/list_resources.py @@ -1,6 +1,9 @@ #!/usr/bin/env python +import sys import os +from datetime import datetime + from rpki.myrpki import EntityDB, CA import rpki.config import rpki.x509 @@ -12,20 +15,6 @@ import rpki.ipaddrs from rpkigui.myrpki import models -class ReceivedResources(object): - def __init__(self, self_handle, parent_handle, asn, ipv4, ipv6, uri, not_before, not_after): - self.self_handle = self_handle - self.parent_handle = parent_handle - self.asn = asn - self.ipv4 = ipv4 - self.ipv6 = ipv6 - self.uri = uri - self.not_before = not_before - self.not_after = not_after - - def __str__(self): - return "%s's received resources from parent %s" % (self.self_handle, self.parent_handle, ) - def query_rpkid(handle=None): """Fetch our received resources from the local rpkid using the myrpki.conf in the current directory.""" cfg_file = os.getenv("MYRPKI_CONF", "myrpki.conf") @@ -46,87 +35,94 @@ def query_rpkid(handle=None): url = rpkid_base + "left-right", debug = True)) - print 'calling rpkid...' + print 'calling rpkid... for self_handle=', handle rpkid_reply = call_rpkid( #rpki.left_right.parent_elt.make_pdu(action="list", tag="parents", self_handle=handle), #rpki.left_right.list_roa_requests_elt.make_pdu(tag='roas', self_handle=handle), + rpki.left_right.child_elt.make_pdu(action="list", tag="children", + self_handle = handle), rpki.left_right.list_received_resources_elt.make_pdu(tag = "resources", self_handle = handle)) print 'done' - resources = [] - for x in rpkid_reply: - if isinstance(x, rpki.left_right.parent_elt): - print x.parent_handle, x.sia_base, x.sender_name, x.recipient_name, \ - x.peer_contact_uri - #elif isinstance(x, rpki.left_right.list_roa_requests_elt): - # print x.asn, x.ipv4, x.ipv6 - if isinstance(x, rpki.left_right.list_received_resources_elt): - resources.append(ReceivedResources(self_handle=handle, - parent_handle=x.parent_handle, - asn=rpki.resource_set.resource_set_as(x.asn), - ipv4=rpki.resource_set.resource_set_ipv4(x.ipv4), - ipv6=rpki.resource_set.resource_set_ipv6(x.ipv6), - uri=x.uri, - not_after=x.notAfter, - not_before=x.notBefore)) - return resources - -x = query_rpkid() -for y in x: - conf = models.Conf.objects.filter(handle=y.self_handle)[0] + return rpkid_reply - parent_set = conf.parents.filter(handle=y.parent_handle) - if not parent_set: - print 'have not yet seen parent %s, creating...' % (y.parent_handle, ) - # have not seen this parent before - parent = models.Parent(conf=conf, handle=y.parent_handle) - parent.save() +for pdu in query_rpkid(None if len(sys.argv) == 1 else sys.argv[1]): + conf_set = models.Conf.objects.filter(handle=pdu.self_handle) + if conf_set.count(): + conf = conf_set[0] else: - parent = parent_set[0] + print 'creating new conf for %s' % (pdu.self_handle,) + conf = models.Conf.objects.create(handle=pdu.self_handle) - # have we seen this resource cert before? - cert_set = conf.resources.filter(uri=y.uri) - if cert_set.count() == 0: - # no - cert = models.ResourceCert(uri=uri, parent=parent, not_before=x.not_before, - not_after=x.not_after) - else: - # yes - cert = cert_set[0] + #if isinstance(pdu, rpki.left_right.parent_elt): +# print x.parent_handle, x.sia_base, x.sender_name, x.recipient_name, \ +# x.peer_contact_uri + if isinstance(pdu, rpki.left_right.child_elt): + # have we seen this parent before? + child_set = conf.children.filter(handle=pdu.child_handle) + if not child_set: + print 'creating new child %s' % (pdu.child_handle,) + child = models.Child(conf=conf, handle=pdu.child_handle) + child.save() + #elif isinstance(x, rpki.left_right.list_roa_requests_elt): + # print x.asn, x.ipv4, x.ipv6 + elif isinstance(pdu, rpki.left_right.list_received_resources_elt): + # have we seen this parent before? + parent_set = conf.parents.filter(handle=pdu.parent_handle) + if not parent_set: + parent = models.Parent(conf=conf, handle=pdu.parent_handle) + parent.save() + else: + parent = parent_set[0] + + not_before = datetime.strptime(pdu.notBefore, "%Y-%m-%dT%H:%M:%SZ") + not_after = datetime.strptime(pdu.notAfter, "%Y-%m-%dT%H:%M:%SZ") + + # have we seen this resource cert before? + cert_set = parent.resources.filter(uri=pdu.uri) + if cert_set.count() == 0: + cert = models.ResourceCert(uri=pdu.uri, parent=parent, + not_before=not_before, not_after=not_after) + else: + cert = cert_set[0] + # update timestamps since it could have been modified + cert.not_before = not_before + cert.not_after = not_after + cert.save() - for asn in y.asn: - # see if this resource is already part of the cert - if cert.asn.get(lo=asn.min, hi=asn.max) is None: - # ensure that this range wasn't previously seen from another of our parents - for v in models.Asn.objects.filter(lo=asn.min, hi=asn.max): - # determine if this resource is delegated from another parent as well - if v.from_cert.filter(parent__in=conf.parents.all()).count(): - cert.asn.add(v) - break - else: - print 'could not find ASN %s in known set' % ( asn, ) - cert.asn.create(lo=asn.min, hi=asn.max) - cert.save() + for asn in rpki.resource_set.resource_set_as(pdu.asn): + # see if this resource is already part of the cert + if cert.asn.filter(lo=asn.min, hi=asn.max).count() == 0: + # ensure this range wasn't seen from another of our parents + for v in models.Asn.objects.filter(lo=asn.min, hi=asn.max): + # determine if resource is delegated from another parent + if v.from_cert.filter(parent__in=conf.parents.all()).count(): + cert.asn.add(v) + break + else: + print 'could not find ASN %s in known set' % ( asn, ) + cert.asn.create(lo=asn.min, hi=asn.max) + cert.save() - # IPv4/6 - not separated in the django db - def add_missing_address(addr_set): - for ip in addr_set: - lo=str(ip.min) - hi=str(ip.max) - if cert.address_range.get(lo=lo, hi=hi) is None: - # ensure that this range wasn't previously seen from another of our parents - for v in models.AddressRange.objects.filter(lo=lo, hi=hi): - # determine if this resource is delegated from another parent as well - if v.from_cert.filter(parent__in=conf.parents.all()).count(): - cert.address_range.add(v) - break - else: - print 'could not find address range %s in known set' % ( ip, ) - cert.address_range.create(lo=lo, hi=hi) - cert.save() + # IPv4/6 - not separated in the django db + def add_missing_address(addr_set): + for ip in addr_set: + lo=str(ip.min) + hi=str(ip.max) + if cert.address_range.filter(lo=lo, hi=hi).count() == 0: + # ensure that this range wasn't previously seen from another of our parents + for v in models.AddressRange.objects.filter(lo=lo, hi=hi): + # determine if this resource is delegated from another parent as well + if v.from_cert.filter(parent__in=conf.parents.all()).count(): + cert.address_range.add(v) + break + else: + print 'could not find address range %s in known set' % (ip,) + cert.address_range.create(lo=lo, hi=hi) + cert.save() - add_missing_address(y.ipv4) - add_missing_address(y.ipv6) + add_missing_address(rpki.resource_set.resource_set_ipv4(pdu.ipv4)) + add_missing_address(rpki.resource_set.resource_set_ipv6(pdu.ipv6)) # vim:sw=4 expandtab ts=4 diff --git a/portal-gui/scripts/load_csv b/portal-gui/scripts/load_csv new file mode 120000 index 00000000..f0521c79 --- /dev/null +++ b/portal-gui/scripts/load_csv @@ -0,0 +1 @@ +helper
\ No newline at end of file diff --git a/portal-gui/scripts/load_csv.py b/portal-gui/scripts/load_csv.py new file mode 100755 index 00000000..e34039db --- /dev/null +++ b/portal-gui/scripts/load_csv.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# +# Helper script to load existing data from csv into the Django DB. +# Primarly useful for the initial load, as the GUI does not sync changes +# made directly to the csv files back into the database. +# +# This script should be run from the directory containing the myrpki.conf +# for the handle you are loading data +# + +import os +import csv + +import rpki +import rpki.resource_set +import rpki.ipaddrs +from rpki.myrpki import csv_reader + +from rpkigui.myrpki import models +from rpkigui.myrpki.views import update_roas +from rpkigui.myrpki.asnset import asnset + +cfg_file = os.getenv("MYRPKI_CONF", "myrpki.conf") +cfg = rpki.config.parser(cfg_file, "myrpki") +handle = cfg.get('handle') +asn_csv = cfg.get('asn_csv') +prefix_csv = cfg.get('prefix_csv') +roa_csv = cfg.get('roa_csv') + +conf = models.Conf.objects.get(handle=handle) + +for asn, child_handle in csv_reader(asn_csv, columns=2): + child = conf.children.get(conf=conf, handle=child_handle) + asn = models.Asn.objects.get(lo=asn, hi=asn, + from_cert__parent__in=conf.parents.all()) + child.asn.add(asn) + +def prefix_to_range(s): + """returns a tuple of (lo,hi) of the address range specified by a prefix""" + net, bits = prefix.split('/') + addr = rpki.resource_set.resource_range_ipv4.make_prefix(rpki.ipaddrs.v4addr(net), int(bits)) + return str(addr.min), str(addr.max) + +for prefix, child_handle in csv_reader(prefix_csv, columns=2): + child = conf.children.get(conf=conf, handle=child_handle) + addr = prefix_to_range(prefix) + obj = models.AddressRange.objects.get(lo=addr[0], hi=addr[1], + from_cert__parent__in=conf.parents.all()) + child.address_range.add(obj) + +for prefix, asn, group in csv_reader(roa_csv, columns=3): + addr = prefix_to_range(prefix) + obj = models.AddressRange.objects.get(lo=addr[0], hi=addr[1], + from_cert__parent__in=conf.parents.all()) + roa_asns = asnset(obj.asns) + asid = int(asn) + if asid not in roa_asns: + roa_asns.add(asid) + obj.asns = str(roa_asns) + obj.save() + update_roas(conf, obj) |