diff options
Diffstat (limited to 'scripts/rpki')
-rw-r--r-- | scripts/rpki/left_right.py | 130 |
1 files changed, 125 insertions, 5 deletions
diff --git a/scripts/rpki/left_right.py b/scripts/rpki/left_right.py index 02d20b22..7c1c50c5 100644 --- a/scripts/rpki/left_right.py +++ b/scripts/rpki/left_right.py @@ -16,6 +16,13 @@ class base_elt(object): def endElement(self, stack, name, text): stack.pop() + def attr_maybe(self, key): + val = getattr(self, key, None) + if val is None: + return '' + else: + return ' %s="%s"' % (key, val) + class extension_preference_elt(base_elt): """ Container for extension preferences. @@ -26,9 +33,13 @@ class extension_preference_elt(base_elt): self.name = attrs["name"] def endElement(self, stack, name, text): - self.value = text + self.value = text.strip() stack.pop() + def __str__(self): + return (' <extension_preference name="%s">%s</extension_preference>\n' + % (self.name, self.value)) + class self_elt(base_elt): booleans = ("rekey", "reissue", "revoke", "run_now", "publish_world_now") @@ -60,6 +71,15 @@ class self_elt(base_elt): assert name == "self", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = ' <self action="%s"%s>\n' % (self.action, self.attr_maybe("self_id")) + for i in self.prefs: + xml += str(i) + for i in self.booleans: + if getattr(self, i): + xml += ' <%s/>\n' % i + return xml + ' </self>\n' + class bsc_elt(base_elt): generate_keypair = False @@ -90,6 +110,19 @@ class bsc_elt(base_elt): assert name == "bsc", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = (' <bsc action="%s" self_id="%s"%s>\n' + % (self.action, self.self_id, self.attr_maybe("bsc_id"))) + for i in self.signing_cert: + xml += ' <signing_cert>' + base64.b64encode(i) + '</signing_cert>\n' + i = getattr(self, "pkcs10_cert_request", None) + if i is not None: + xml += ' <pkcs10_cert_request>' + base64.b64encode(i) + '</pkcs10_cert_request>\n' + i = getattr(self, "public_key", None) + if i is not None: + xml += ' <public_key>' + base64.b64encode(i) + '</public_key>\n' + return xml + ' </bsc>\n' + class parent_elt(base_elt): ids = ("bsc_link", "repository_link") @@ -120,14 +153,35 @@ class parent_elt(base_elt): assert name == "parent", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = (' <parent action="%s" self_id="%s"%s>\n' + % (self.action, self.self_id, self.attr_maybe("parent_id"))) + i = getattr(self, "peer_ta", None) + if i is not None: + xml += ' <peer_ta>' + base64.b64encode(i) + '</peer_ta>\n' + i = getattr(self, "peer_contact", None) + if i is not None: + xml += ' <peer_contact uri="%s"/>\n' % i + i = getattr(self, "sia_base", None) + if i is not None: + xml += ' <sia_base uri="%s"/>\n' % i + i = getattr(self, "bsc_link", None) + if i is not None: + xml += ' <bsc_link id="%s"/>\n' % i + i = getattr(self, "repository_link", None) + if i is not None: + xml += ' <repository_link id="%s"/>\n' % i + for i in self.booleans: + if getattr(self, i): + xml += ' <%s/>\n' % i + return xml + ' </parent>\n' + class child_elt(base_elt): ids = ("bsc_link", "child_db_id") booleans = ("reissue", ) - rekey = False reissue = False - revoke = False def startElement(self, stack, name, attrs): if name in self.ids: @@ -147,6 +201,23 @@ class child_elt(base_elt): assert name == "child", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = (' <child action="%s" self_id="%s"%s>\n' + % (self.action, self.self_id, self.attr_maybe("child_id"))) + i = getattr(self, "peer_ta", None) + if i is not None: + xml += ' <peer_ta>' + base64.b64encode(i) + '</peer_ta>\n' + i = getattr(self, "bsc_link", None) + if i is not None: + xml += ' <bsc_link id="%s"/>\n' % i + i = getattr(self, "child_db_id", None) + if i is not None: + xml += ' <child_db_id id="%s"/>\n' % i + for i in self.booleans: + if getattr(self, i): + xml += ' <%s/>\n' % i + return xml + ' </child>\n' + class repository_elt(base_elt): def startElement(self, stack, name, attrs): @@ -167,9 +238,24 @@ class repository_elt(base_elt): assert name == "repository", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = (' <repository action="%s" self_id="%s"%s>\n' + % (self.action, self.self_id, self.attr_maybe("repository_id"))) + i = getattr(self, "peer_ta", None) + if i is not None: + xml += ' <peer_ta>' + base64.b64encode(i) + '</peer_ta>\n' + i = getattr(self, "peer_contact", None) + if i is not None: + xml += ' <peer_contact uri="%s"/>\n' % i + i = getattr(self, "bsc_link", None) + if i is not None: + xml += ' <bsc_link id="%s"/>\n' % i + return xml + ' </repository>\n' + class route_origin_elt(base_elt): suppress_publication = False + ipv4 = None ipv6 = None @@ -177,7 +263,7 @@ class route_origin_elt(base_elt): if name == "suppress_publication": self.suppress_publication = True elif name == "resources": - self.asn = attrs["asn"] + self.asn = long(attrs["asn"]) if "ipv4" in attrs: self.ipv4 = resource_set.resource_set_ipv4(attrs["ipv4"]) if "ipv6" in attrs: @@ -193,6 +279,19 @@ class route_origin_elt(base_elt): assert name == "route_origin", "Unexpected name %s, stack %s" % (name, stack) stack.pop() + def __str__(self): + xml = (' <route_origin action="%s" self_id="%s"%s>\n' + % (self.action, self.self_id, self.attr_maybe("route_origin_id"))) + asn = getattr(self, "asn", None) + if asn is not None: + xml += ' <resources asn="%d"' % asn + if self.ipv4 is not None: + xml += ' ipv4="%s"' % str(self.ipv4) + if self.ipv6 is not None: + xml += ' ipv6="%s"' % str(self.ipv6) + xml += '/>\n' + return xml + ' </route_origin>\n' + class resource_class_elt(base_elt): def startElement(self, stack, name, attrs): @@ -210,6 +309,14 @@ class resource_class_elt(base_elt): if "req_ipv6" in attrs: self.req_ipv6 = resource_set.resource_set_ipv6(attrs["req_ipv6"]) + def __str__(self): + xml = ' <resource_class' + for k in ("as", "req_as", "ipv4", "req_ipv4", "ipv6", "req_ipv6"): + v = getattr(self, k, None) + if v is not None: + xml += ' %s="%s"' % (k, v) + return xml + '/>\n' + class list_resources_elt(base_elt): def __init__(self): @@ -225,6 +332,14 @@ class list_resources_elt(base_elt): assert name == "list_resources", "Unexpected name %s, stack %s" % (name, stack) self.self_id = attrs["self_id"] self.child_id = attrs.get("child_id") + self.valid_until = attrs.get("valid_until") + + def __str__(self): + xml = (' <list_resources self_id="%s"%s%s>\n' + % (self.self_id, self.attr_maybe("child_id"), self.attr_maybe("valid_until"))) + for i in self.resources: + xml += str(i) + return xml + ' </list_resources>\n' class report_error_elt(base_elt): @@ -233,6 +348,9 @@ class report_error_elt(base_elt): self.self_id = attrs["self_id"] self.error_code = attrs["error_code"] + def __str__(self): + return ' <report_error self_id="%s" error_code="%s"/>\n' % (self.self_id, self.error_code) + class msg(list): """ Left-right PDU. @@ -268,7 +386,9 @@ class msg(list): def __str__(self): return ('<?xml version="1.0" encoding="US-ASCII" ?>\n' - '<msg xmlns="%s" version="%d" type="%s">\n' + '<msg xmlns="%s"\n' + ' version="%d"\n' + ' type="%s">\n' '%s</msg>\n' % (self.spec_uri, self.version, self.type, "".join(map(str, self)))) |