aboutsummaryrefslogtreecommitdiff
path: root/scripts/rpkid.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/rpkid.py')
-rwxr-xr-xscripts/rpkid.py57
1 files changed, 28 insertions, 29 deletions
diff --git a/scripts/rpkid.py b/scripts/rpkid.py
index 11edb1aa..cdac0c60 100755
--- a/scripts/rpkid.py
+++ b/scripts/rpkid.py
@@ -8,21 +8,15 @@ framework onto which I'm bolting various parts for testing.
import tlslite.api, MySQLdb, xml.sax, lxml.etree, lxml.sax, POW, POW.pkix, traceback
import rpki.https, rpki.config, rpki.resource_set, rpki.up_down, rpki.left_right, rpki.relaxng, rpki.cms, rpki.exceptions
-def decode(msg, cms_ta):
- return lxml.etree.fromstring(rpki.cms.decode(msg, cms_ta))
-
-def encode(msg, cms_key, cms_certs):
- return rpki.cms.encode(lxml.etree.tostring(msg, pretty_print=True, encoding="us-ascii", xml_declaration=True), cms_key, cms_certs)
-
def left_right_handler(query, path):
try:
- q_elt = decode(query, cms_ta_irbe)
+ q_elt = rpki.cms.xml_decode(query, gctx.cms_ta_irbe)
rpki.relaxng.left_right.assertValid(q_elt)
q_msg = rpki.left_right.sax_handler.saxify(q_elt)
- r_msg = q_msg.serve_top_level(db, cur)
+ r_msg = q_msg.serve_top_level(gctx)
r_elt = r_msg.toXML()
rpki.relaxng.left_right.assertValid(r_elt)
- return 200, encode(r_elt, cms_key, cms_certs)
+ return 200, rpki.cms.xml_encode(r_elt, gctx.cms_key, gctx.cms_certs)
except Exception, data:
traceback.print_exc()
return 500, "Unhandled exception %s" % data
@@ -32,18 +26,17 @@ def up_down_handler(query, path):
child_id = path.partition("/up-down/")[2]
if not child_id.isdigit():
raise rpki.exceptions.BadContactURL, "Bad path: %s" % path
- child = rpki.left_right.child_elt.sql_fetch(db, cur, long(child_id))
+ child = rpki.left_right.child_elt.sql_fetch(gctx.db, gctx.cur, long(child_id))
if child is None:
raise rpki.exceptions.NotFound, "Could not find CMS TA to verify request"
- bsc = rpki.left_right.bsc_elt.sql_fetch(db, cur, child.bsc_id)
-
- q_elt = decode(query, child.peer_ta)
+ bsc = rpki.left_right.bsc_elt.sql_fetch(gctx.db, gctx.cur, child.bsc_id)
+ q_elt = rpki.cms.xml_decode(query, child.peer_ta)
rpki.relaxng.up_down.assertValid(q_elt)
q_msg = rpki.up_down.sax_handler.saxify(q_elt)
- r_msg = q_msg.serve_top_level(db, cur)
+ r_msg = q_msg.serve_top_level(gctx)
r_elt = r_msg.toXML()
rpki.relaxng.up_down.assertValid(r_elt)
- return 200, encode(r_elt, bsc.private_key_id, bsc.signing_cert)
+ return 200, rpki.cms.xml_encode(r_elt, bsc.private_key_id, bsc.signing_cert)
except Exception, data:
traceback.print_exc()
return 500, "Unhandled exception %s" % data
@@ -51,26 +44,32 @@ def up_down_handler(query, path):
def cronjob_handler(query, path):
raise NotImplementedError
-cfg = rpki.config.parser("re.conf")
-section = "rpki"
+class global_context(object):
+ """A place to stash various global parameters."""
+ pass
+
+gctx = global_context()
+
+gctx.cfg = rpki.config.parser("re.conf")
+gctx.cfg_section = "rpki"
-db = MySQLdb.connect(user = cfg.get(section, "sql-username"),
- db = cfg.get(section, "sql-database"),
- passwd = cfg.get(section, "sql-password"))
+gctx.db = MySQLdb.connect(user = gctx.cfg.get(gctx.cfg_section, "sql-username"),
+ db = gctx.cfg.get(gctx.cfg_section, "sql-database"),
+ passwd = gctx.cfg.get(gctx.cfg_section, "sql-password"))
-cur = db.cursor()
+gctx.cur = gctx.db.cursor()
-cms_ta_irdb = cfg.get(section, "cms-ta-irdb")
-cms_ta_irbe = cfg.get(section, "cms-ta-irbe")
-cms_key = cfg.get(section, "cms-key")
-cms_certs = cfg.multiget(section, "cms-cert")
+gctx.cms_ta_irdb = gctx.cfg.get(gctx.cfg_section, "cms-ta-irdb")
+gctx.cms_ta_irbe = gctx.cfg.get(gctx.cfg_section, "cms-ta-irbe")
+gctx.cms_key = gctx.cfg.get(gctx.cfg_section, "cms-key")
+gctx.cms_certs = gctx.cfg.multiget(gctx.cfg_section, "cms-cert")
-https_key = rpki.x509.RSA_Keypair(PEM_file = cfg.get(section, "https-key"))
-https_certs = certChain = rpki.x509.X509_chain()
+gctx.https_key = rpki.x509.RSA_Keypair(PEM_file = gctx.cfg.get(gctx.cfg_section, "https-key"))
+gctx.https_certs = certChain = rpki.x509.X509_chain()
-https_certs.load_from_PEM(cfg.multiget(section, "https-cert"))
+gctx.https_certs.load_from_PEM(gctx.cfg.multiget(gctx.cfg_section, "https-cert"))
-rpki.https.server(privateKey=https_key, certChain=https_certs,
+rpki.https.server(privateKey=gctx.https_key, certChain=gctx.https_certs,
handlers=(("/left-right", left_right_handler),
("/up-down/", up_down_handler),
("/cronjob", cronjob_handler)))