diff options
author | Rob Austein <sra@hactrn.net> | 2007-09-20 22:36:30 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2007-09-20 22:36:30 +0000 |
commit | 7ee009baa2652d79a88765300ffed0cdd4e6d3ea (patch) | |
tree | a15048fe805d87f788a5f956efb3399aac6a217a | |
parent | 140ba2754c27db351ab6981a416d042b91e667cf (diff) |
Merge PDU-type-specific left-right command line code into
rpki.left_right so that we can use ordinary method processing to
handle responses.
svn path=/scripts/irbe-cli.py; revision=999
-rwxr-xr-x | scripts/irbe-cli.py | 111 | ||||
-rw-r--r-- | scripts/rpki/left_right.py | 69 |
2 files changed, 80 insertions, 100 deletions
diff --git a/scripts/irbe-cli.py b/scripts/irbe-cli.py index f9b3c1c0..413eb068 100755 --- a/scripts/irbe-cli.py +++ b/scripts/irbe-cli.py @@ -1,106 +1,21 @@ # $Id$ -"""Command line program to simulate behavior of the IR back-end. +""" +Command line IR back-end control program. -This only handles the control channel. The query back-channel will be -a separate program. +The query back-channel is handled by a separate program. """ -import glob, getopt, sys, lxml.etree, lxml.sax +import sys, lxml.etree, lxml.sax import rpki.left_right, rpki.relaxng, rpki.cms, rpki.https, rpki.x509, rpki.config -class command(object): - """Command processor mixin class for left-right protocol objects. - - This class and its derived classes probably should be merged into - the left-right protocol classes, once this stuff is stable. - """ - - elements = () - - def getopt(self, argv): - """Parse options for this class.""" - opts, args = getopt.getopt(argv, "", [x + "=" for x in self.attributes + self.elements] + list(self.booleans)) - for o, a in opts: - o = o[2:] - handler = getattr(self, "client_query_" + o, None) - if handler is not None: - handler(a) - elif o in self.booleans: - setattr(self, o, True) - else: - assert o in self.attributes - setattr(self, o, a) - return args - - def client_query_action(self, arg): - """Special handler for --action option.""" - self.action = arg - self.type = "query" - - def client_query_peer_ta(self, arg): - """Special handler for --peer_ta option.""" - self.peer_ta = rpki.x509.X509(Auto_file=arg) - - def client_reply_decode(self): - pass - - def client_reply_show(self): - self.client_reply_decode() - print self.element_name - for i in self.attributes + self.elements: - print " " + i + ": " + getattr(self, i) - -class self(command, rpki.left_right.self_elt): - '''"self" command.''' - - elements = ("extension_preference",) - - def client_query_extension_preference(self, arg): - """--extension_preferences option.""" - k,v = arg.split("=", 1) - pref = rpki.left_right.extension_preference_elt() - pref.name = k - pref.value = v - self.prefs.append(pref) - -class bsc(command, rpki.left_right.bsc_elt): - '''"bsc" command.''' - - elements = ('signing_cert',) - - def client_query_signing_cert(self, arg): - """--signing_cert option.""" - self.signing_cert.append(rpki.x509.X509(Auto_file=arg)) - -class parent(command, rpki.left_right.parent_elt): - '''"parent" command.''' - elements = ("peer_ta",) - -class child(command, rpki.left_right.child_elt): - '''"child" command.''' - elements = ("peer_ta",) - -class repository(command, rpki.left_right.repository_elt): - '''"repository" command.''' - elements = ("peer_ta",) - -class route_origin(command, rpki.left_right.route_origin_elt): - '''"route_origin" command.''' - - def client_query_as_number(self, arg): - """Handle autonomous sequence numbers.""" - self.as_number = long(arg) - - def client_query_ipv4(self, arg): - """Handle IPv4 addresses.""" - self.ipv4 = resource_set.resource_set_ipv4(arg) - - def client_query_ipv6(self, arg): - """Handle IPv6 addresses.""" - self.ipv6 = resource_set.resource_set_ipv6(arg) - -dispatch = dict((x.element_name, x) for x in (self, bsc, parent, child, repository, route_origin)) +dispatch = dict((x.element_name, x) + for x in (rpki.left_right.self_elt, + rpki.left_right.bsc_elt, + rpki.left_right.parent_elt, + rpki.left_right.child_elt, + rpki.left_right.repository_elt, + rpki.left_right.route_origin_elt)) def usage(): print "Usage:", sys.argv[0] @@ -142,7 +57,7 @@ def main(): q_pdu = dispatch[argv[0]]() except KeyError: usage() - argv = q_pdu.getopt(argv[1:]) + argv = q_pdu.client_getopt(argv[1:]) q_msg.append(q_pdu) q_elt = q_msg.toXML() @@ -180,7 +95,7 @@ def main(): r_msg = handler.result # Can't enable this until our reply handler methods are merged into rpki.left_right. - if False: + if True: for r_pdu in r_msg: r_pdu.client_reply_show() diff --git a/scripts/rpki/left_right.py b/scripts/rpki/left_right.py index 7c6c8ea9..34eab6f1 100644 --- a/scripts/rpki/left_right.py +++ b/scripts/rpki/left_right.py @@ -2,7 +2,8 @@ """RPKI "left-right" protocol.""" -import base64, rpki.sax_utils, rpki.resource_set, lxml.etree, rpki.x509, rpki.sql, rpki.exceptions, rpki.pkcs10 +import getopt, base64, lxml.etree +import rpki.sax_utils, rpki.resource_set, rpki.x509, rpki.sql, rpki.exceptions, rpki.pkcs10 xmlns = "http://www.hactrn.net/uris/rpki/left-right-spec/" @@ -12,6 +13,7 @@ class base_elt(object): """Virtual base type for left-right message elements.""" attributes = () + elements = () booleans = () def startElement(self, stack, name, attrs): @@ -55,7 +57,7 @@ class base_elt(object): lxml.etree.tostring(self.toXML(), pretty_print=True, encoding="us-ascii") class data_elt(base_elt, rpki.sql.sql_persistant): - """Virtual class for top-level left-right protocol data elements.""" + """Virtual class for top-level left-right protocol data elements.""" def sql_decode(self, vals): rpki.sql.sql_persistant.sql_decode(self, vals) @@ -138,6 +140,40 @@ class data_elt(base_elt, rpki.sql.sql_persistant): raise rpki.exceptions.BadQuery, "Unexpected query: type %s, action %s" % (self.type, self.action) dispatch[self.action](db, cur, r_msg) + def client_getopt(self, argv): + """Parse options for this class.""" + opts, args = getopt.getopt(argv, "", [x + "=" for x in self.attributes + self.elements] + list(self.booleans)) + for o, a in opts: + o = o[2:] + handler = getattr(self, "client_query_" + o, None) + if handler is not None: + handler(a) + elif o in self.booleans: + setattr(self, o, True) + else: + assert o in self.attributes + setattr(self, o, a) + return args + + def client_query_action(self, arg): + """Special handler for --action option.""" + self.action = arg + self.type = "query" + + def client_query_peer_ta(self, arg): + """Special handler for --peer_ta option.""" + self.peer_ta = rpki.x509.X509(Auto_file=arg) + + def client_reply_decode(self): + pass + + def client_reply_show(self): + self.client_reply_decode() + print self.element_name + for i in self.attributes + self.elements: + if getattr(self, i) is not None: + print " %s: %s" % (i, getattr(self, i)) + class extension_preference_elt(base_elt): """Container for extension preferences.""" @@ -165,6 +201,7 @@ class bsc_elt(data_elt): element_name = "bsc" attributes = ("action", "type", "self_id", "bsc_id", "key_type", "hash_alg", "key_length") + elements = ('signing_cert',) booleans = ("generate_keypair", "clear_signing_certs") sql_template = rpki.sql.template("bsc", "bsc_id", "self_id", "public_key", "private_key_id") @@ -231,11 +268,16 @@ class bsc_elt(data_elt): self.make_b64elt(elt, "public_key") return elt + def client_query_signing_cert(self, arg): + """--signing_cert option.""" + self.signing_cert.append(rpki.x509.X509(Auto_file=arg)) + class parent_elt(data_elt): """<parent/> element.""" element_name = "parent" attributes = ("action", "type", "self_id", "parent_id", "bsc_id", "repository_id", "peer_contact_uri", "sia_base") + elements = ("peer_ta",) booleans = ("rekey", "reissue", "revoke") sql_template = rpki.sql.template("parent", "parent_id", "self_id", "bsc_id", "repository_id", "peer_ta", "peer_contact_uri", "sia_base") @@ -272,6 +314,7 @@ class child_elt(data_elt): element_name = "child" attributes = ("action", "type", "self_id", "child_id", "bsc_id") + elements = ("peer_ta",) booleans = ("reissue", ) sql_template = rpki.sql.template("child", "child_id", "self_id", "bsc_id", "peer_ta") @@ -325,6 +368,7 @@ class repository_elt(data_elt): element_name = "repository" attributes = ("action", "type", "self_id", "repository_id", "bsc_id", "peer_contact_uri") + elements = ("peer_ta",) sql_template = rpki.sql.template("repository", "repository_id", "self_id", "bsc_id", "peer_ta", "peer_contact_uri") @@ -412,11 +456,24 @@ class route_origin_elt(data_elt): """Generate <route_origin/> element.""" return self.make_elt() + def client_query_as_number(self, arg): + """Handle autonomous sequence numbers.""" + self.as_number = long(arg) + + def client_query_ipv4(self, arg): + """Handle IPv4 addresses.""" + self.ipv4 = resource_set.resource_set_ipv4(arg) + + def client_query_ipv6(self, arg): + """Handle IPv6 addresses.""" + self.ipv6 = resource_set.resource_set_ipv6(arg) + class self_elt(data_elt): """<self/> element.""" element_name = "self" attributes = ("action", "type", "self_id") + elements = ("extension_preference",) booleans = ("rekey", "reissue", "revoke", "run_now", "publish_world_now", "clear_extension_preferences") sql_template = rpki.sql.template("self", "self_id", "use_hsm") @@ -475,6 +532,14 @@ class self_elt(data_elt): elt.extend([i.toXML() for i in self.prefs]) return elt + def client_query_extension_preference(self, arg): + """--extension_preferences option.""" + k,v = arg.split("=", 1) + pref = rpki.left_right.extension_preference_elt() + pref.name = k + pref.value = v + self.prefs.append(pref) + class resource_class_elt(base_elt): """<resource_class/> element.""" |