aboutsummaryrefslogtreecommitdiff
path: root/rpki/publication.py
diff options
context:
space:
mode:
Diffstat (limited to 'rpki/publication.py')
-rw-r--r--rpki/publication.py167
1 files changed, 25 insertions, 142 deletions
diff --git a/rpki/publication.py b/rpki/publication.py
index ca7f7792..9b5bfcbd 100644
--- a/rpki/publication.py
+++ b/rpki/publication.py
@@ -38,161 +38,44 @@ import rpki.log
logger = logging.getLogger(__name__)
-class publication_namespace(object):
- xmlns = rpki.relaxng.publication.xmlns
- nsmap = rpki.relaxng.publication.nsmap
+nsmap = rpki.relaxng.publication.nsmap
+version = rpki.relaxng.publication.version
+tag_msg = rpki.relaxng.publication.xmlns + "msg"
+tag_list = rpki.relaxng.publication.xmlns + "list"
+tag_publish = rpki.relaxng.publication.xmlns + "publish"
+tag_withdraw = rpki.relaxng.publication.xmlns + "withdraw"
+tag_report_error = rpki.relaxng.publication.xmlns + "report_error"
-class base_publication_elt(rpki.xml_utils.base_elt, publication_namespace):
- """
- Base element for publication protocol. Publish and withdraw PDUs subclass this.
- """
-
- attributes = ("tag", "uri", "hash")
-
- tag = None
- uri = None
- der = None
- hash = None
-
- _payload = None
-
- def __repr__(self):
- return rpki.log.log_repr(self, self.tag, self.uri, self.hash, self.payload)
-
- @property
- def payload(self):
- if self._payload is None and self.der is not None:
- self._payload = rpki.x509.uri_dispatch(self.uri)(DER = self.der)
- return self._payload
- def raise_if_error(self):
- """
- No-op unless this is a <report_error/> PDU.
- """
-
- pass
-
-
-class publish_elt(base_publication_elt):
+def raise_if_error(pdu):
"""
- <publish/> element.
- """
-
- element_name = "publish"
-
- def endElement(self, stack, name, text):
- """
- Handle reading of the object to be published
- """
+ Raise an appropriate error if this is a <report_error/> PDU.
- assert name == self.element_name, "Unexpected name %s, stack %s" % (name, stack)
- if text:
- self.der = text.decode("base64")
- stack.pop()
-
- def toXML(self):
- """
- Generate XML element for publishable object.
- """
-
- elt = self.make_elt()
- if self.der is not None:
- elt.text = self.der.encode("base64")
- return elt
-
-
-class withdraw_elt(base_publication_elt):
- """
- <withdraw/> element.
+ As a convience, this will also accept a <msg/> PDU and raise an
+ appropriate error if it contains any <report_error/> PDUs.
"""
- element_name = "withdraw"
-
+ if pdu.tag == tag_report_error:
+ code = pdu.get("error_code")
+ logger.debug("<report_error/> code %r", code)
+ e = getattr(rpki.exceptions, code, None)
+ if e is not None and issubclass(e, rpki.exceptions.RPKI_Exception):
+ raise e(pdu.text)
+ else:
+ raise rpki.exceptions.BadPublicationReply("Unexpected response from pubd: %r, %r" % (code, pdu))
-class list_elt(base_publication_elt):
- """
- <list/> element.
- """
-
- element_name = "list"
+ if pdu.tag == tag_msg:
+ for p in pdu:
+ raise_if_error(p)
-class report_error_elt(rpki.xml_utils.text_elt, publication_namespace):
- """
- <report_error/> element.
- """
-
- element_name = "report_error"
- attributes = ("tag", "error_code")
- text_attribute = "error_text"
-
- error_code = None
- error_text = None
-
- def __repr__(self):
- return rpki.log.log_repr(self, self.error_code, self.error_text)
-
- def __str__(self):
- s = ""
- if getattr(self, "tag", None) is not None:
- s += "[%s] " % self.tag
- s += self.error_code
- if getattr(self, "error_text", None) is not None:
- s += ": " + self.error_text
- return s
-
- def raise_if_error(self):
- """
- Raise exception associated with this <report_error/> PDU.
- """
-
- try:
- e = getattr(rpki.exceptions, self.error_code)
- if issubclass(e, rpki.exceptions.RPKI_Exception):
- raise e(getattr(self, "text", None))
- except (TypeError, AttributeError):
- pass
- raise rpki.exceptions.BadPublicationReply("Unexpected response from pubd: %s" % self)
-
-
-class msg(rpki.xml_utils.msg, publication_namespace):
- """
- Publication PDU.
- """
-
- ## @var version
- # Protocol version
- version = int(rpki.relaxng.publication.version)
-
- ## @var pdus
- # Dispatch table of PDUs for this protocol.
- pdus = dict((x.element_name, x) for x in (publish_elt, withdraw_elt, list_elt, report_error_elt))
-
-
-class sax_handler(rpki.xml_utils.sax_handler):
- """
- SAX handler for publication protocol.
- """
-
- pdu = msg
- name = "msg"
- version = rpki.relaxng.publication.version
-
-
-class cms_msg(rpki.x509.XML_CMS_object):
+class cms_msg_no_sax(rpki.x509.XML_CMS_object):
"""
Class to hold a CMS-signed publication PDU.
+
+ Name is a transition kludge: once we ditch SAX, this will become cms_msg.
"""
encoding = "us-ascii"
schema = rpki.relaxng.publication
- saxify = sax_handler.saxify
-
-class cms_msg_no_sax(cms_msg):
- """
- Transition kludge: varient of cms_msg (q.v.) with SAX parsing disabled.
- If and when we ditch SAX entirely, this will become cms_msg.
- """
-
- saxify = None