00001 """
00002 XML utilities.
00003
00004 $Id: xml_utils.py 3449 2010-09-16 21:30:30Z sra $
00005
00006 Copyright (C) 2009 Internet Systems Consortium ("ISC")
00007
00008 Permission to use, copy, modify, and distribute this software for any
00009 purpose with or without fee is hereby granted, provided that the above
00010 copyright notice and this permission notice appear in all copies.
00011
00012 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00013 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00014 AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00015 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00016 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00017 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00018 PERFORMANCE OF THIS SOFTWARE.
00019
00020 Portions copyright (C) 2007--2008 American Registry for Internet Numbers ("ARIN")
00021
00022 Permission to use, copy, modify, and distribute this software for any
00023 purpose with or without fee is hereby granted, provided that the above
00024 copyright notice and this permission notice appear in all copies.
00025
00026 THE SOFTWARE IS PROVIDED "AS IS" AND ARIN DISCLAIMS ALL WARRANTIES WITH
00027 REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00028 AND FITNESS. IN NO EVENT SHALL ARIN BE LIABLE FOR ANY SPECIAL, DIRECT,
00029 INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00030 LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00031 OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00032 PERFORMANCE OF THIS SOFTWARE.
00033 """
00034
00035 import xml.sax, lxml.sax, lxml.etree, base64
00036 import rpki.exceptions
00037
00038 class sax_handler(xml.sax.handler.ContentHandler):
00039 """
00040 SAX handler for RPKI protocols.
00041
00042 This class provides some basic amenities for parsing protocol XML of
00043 the kind we use in the RPKI protocols, including whacking all the
00044 protocol element text into US-ASCII, simplifying accumulation of
00045 text fields, and hiding some of the fun relating to XML namespaces.
00046
00047 General assumption: by the time this parsing code gets invoked, the
00048 XML has already passed RelaxNG validation, so we only have to check
00049 for errors that the schema can't catch, and we don't have to play as
00050 many XML namespace games.
00051 """
00052
00053 def __init__(self):
00054 """
00055 Initialize SAX handler.
00056 """
00057 xml.sax.handler.ContentHandler.__init__(self)
00058 self.text = ""
00059 self.stack = []
00060
00061 def startElementNS(self, name, qname, attrs):
00062 """Redirect startElementNS() events to startElement()."""
00063 return self.startElement(name[1], attrs)
00064
00065 def endElementNS(self, name, qname):
00066 """Redirect endElementNS() events to endElement()."""
00067 return self.endElement(name[1])
00068
00069 def characters(self, content):
00070 """Accumulate a chuck of element content (text)."""
00071 self.text += content
00072
00073 def startElement(self, name, attrs):
00074 """
00075 Handle startElement() events.
00076
00077 We maintain a stack of nested elements under construction so that
00078 we can feed events directly to the current element rather than
00079 having to pass them through all the nesting elements.
00080
00081 If the stack is empty, this event is for the outermost element, so
00082 we call a virtual method to create the corresponding object and
00083 that's the object we'll be returning as our final result.
00084 """
00085
00086 a = dict()
00087 for k, v in attrs.items():
00088 if isinstance(k, tuple):
00089 if k == ("http://www.w3.org/XML/1998/namespace", "lang"):
00090 k = "xml:lang"
00091 else:
00092 assert k[0] is None
00093 k = k[1]
00094 a[k.encode("ascii")] = v.encode("ascii")
00095 if len(self.stack) == 0:
00096 assert not hasattr(self, "result")
00097 self.result = self.create_top_level(name, a)
00098 self.stack.append(self.result)
00099 self.stack[-1].startElement(self.stack, name, a)
00100
00101 def endElement(self, name):
00102 """
00103 Handle endElement() events. Mostly this means handling any
00104 accumulated element text.
00105 """
00106 text = self.text.encode("ascii").strip()
00107 self.text = ""
00108 self.stack[-1].endElement(self.stack, name, text)
00109
00110 @classmethod
00111 def saxify(cls, elt):
00112 """
00113 Create a one-off SAX parser, parse an ETree, return the result.
00114 """
00115 self = cls()
00116 lxml.sax.saxify(elt, self)
00117 return self.result
00118
00119 def create_top_level(self, name, attrs):
00120 """
00121 Handle top-level PDU for this protocol.
00122 """
00123 assert name == self.name and attrs["version"] == self.version
00124 return self.pdu()
00125
00126 class base_elt(object):
00127 """
00128 Virtual base class for XML message elements. The left-right and
00129 publication protocols use this. At least for now, the up-down
00130 protocol does not, due to different design assumptions.
00131 """
00132
00133
00134
00135 attributes = ()
00136
00137
00138
00139 elements = ()
00140
00141
00142
00143 booleans = ()
00144
00145 def startElement(self, stack, name, attrs):
00146 """
00147 Default startElement() handler: just process attributes.
00148 """
00149 if name not in self.elements:
00150 assert name == self.element_name, "Unexpected name %s, stack %s" % (name, stack)
00151 self.read_attrs(attrs)
00152
00153 def endElement(self, stack, name, text):
00154 """
00155 Default endElement() handler: just pop the stack.
00156 """
00157 assert name == self.element_name, "Unexpected name %s, stack %s" % (name, stack)
00158 stack.pop()
00159
00160 def toXML(self):
00161 """
00162 Default toXML() element generator.
00163 """
00164 return self.make_elt()
00165
00166 def read_attrs(self, attrs):
00167 """
00168 Template-driven attribute reader.
00169 """
00170 for key in self.attributes:
00171 val = attrs.get(key, None)
00172 if isinstance(val, str) and val.isdigit() and not key.endswith("_handle"):
00173 val = long(val)
00174 setattr(self, key, val)
00175 for key in self.booleans:
00176 setattr(self, key, attrs.get(key, False))
00177
00178 def make_elt(self):
00179 """
00180 XML element constructor.
00181 """
00182 elt = lxml.etree.Element("{%s}%s" % (self.xmlns, self.element_name), nsmap = self.nsmap)
00183 for key in self.attributes:
00184 val = getattr(self, key, None)
00185 if val is not None:
00186 elt.set(key, str(val))
00187 for key in self.booleans:
00188 if getattr(self, key, False):
00189 elt.set(key, "yes")
00190 return elt
00191
00192 def make_b64elt(self, elt, name, value):
00193 """
00194 Constructor for Base64-encoded subelement.
00195 """
00196 if value is not None and not value.empty():
00197 lxml.etree.SubElement(elt, "{%s}%s" % (self.xmlns, name), nsmap = self.nsmap).text = value.get_Base64()
00198
00199 def __str__(self):
00200 """
00201 Convert a base_elt object to string format.
00202 """
00203 lxml.etree.tostring(self.toXML(), pretty_print = True, encoding = "us-ascii")
00204
00205 @classmethod
00206 def make_pdu(cls, **kargs):
00207 """
00208 Generic PDU constructor.
00209 """
00210 self = cls()
00211 for k, v in kargs.items():
00212 if isinstance(v, bool):
00213 v = 1 if v else 0
00214 setattr(self, k, v)
00215 return self
00216
00217 class text_elt(base_elt):
00218 """
00219 Virtual base class for XML message elements that contain text.
00220 """
00221
00222
00223
00224 text_attribute = None
00225
00226 def endElement(self, stack, name, text):
00227 """
00228 Extract text from parsed XML.
00229 """
00230 base_elt.endElement(self, stack, name, text)
00231 setattr(self, self.text_attribute, text)
00232
00233 def toXML(self):
00234 """
00235 Insert text into generated XML.
00236 """
00237 elt = self.make_elt()
00238 elt.text = getattr(self, self.text_attribute) or None
00239 return elt
00240
00241 class data_elt(base_elt):
00242 """
00243 Virtual base class for PDUs that map to SQL objects. These objects
00244 all implement the create/set/get/list/destroy action attribute.
00245 """
00246
00247 def endElement(self, stack, name, text):
00248 """
00249 Default endElement handler for SQL-based objects. This assumes
00250 that sub-elements are Base64-encoded using the sql_template
00251 mechanism.
00252 """
00253 if name in self.elements:
00254 elt_type = self.sql_template.map.get(name)
00255 assert elt_type is not None, "Couldn't find element type for %s, stack %s" % (name, stack)
00256 setattr(self, name, elt_type(Base64 = text))
00257 else:
00258 assert name == self.element_name, "Unexpected name %s, stack %s" % (name, stack)
00259 stack.pop()
00260
00261 def toXML(self):
00262 """
00263 Default element generator for SQL-based objects. This assumes
00264 that sub-elements are Base64-encoded DER objects.
00265 """
00266 elt = self.make_elt()
00267 for i in self.elements:
00268 self.make_b64elt(elt, i, getattr(self, i, None))
00269 return elt
00270
00271 def make_reply(self, r_pdu = None):
00272 """
00273 Construct a reply PDU.
00274 """
00275 if r_pdu is None:
00276 r_pdu = self.__class__()
00277 self.make_reply_clone_hook(r_pdu)
00278 handle_name = self.element_name + "_handle"
00279 setattr(r_pdu, handle_name, getattr(self, handle_name, None))
00280 else:
00281 self.make_reply_clone_hook(r_pdu)
00282 for b in r_pdu.booleans:
00283 setattr(r_pdu, b, False)
00284 r_pdu.action = self.action
00285 r_pdu.tag = self.tag
00286 return r_pdu
00287
00288 def make_reply_clone_hook(self, r_pdu):
00289 """Overridable hook."""
00290 pass
00291
00292 def serve_fetch_one(self):
00293 """
00294 Find the object on which a get, set, or destroy method should
00295 operate.
00296 """
00297 r = self.serve_fetch_one_maybe()
00298 if r is None:
00299 raise rpki.exceptions.NotFound
00300 return r
00301
00302 def serve_pre_save_hook(self, q_pdu, r_pdu, cb, eb):
00303 """Overridable hook."""
00304 cb()
00305
00306 def serve_post_save_hook(self, q_pdu, r_pdu, cb, eb):
00307 """Overridable hook."""
00308 cb()
00309
00310 def serve_create(self, r_msg, cb, eb):
00311 """
00312 Handle a create action.
00313 """
00314
00315 r_pdu = self.make_reply()
00316
00317 def one():
00318 self.sql_store()
00319 setattr(r_pdu, self.sql_template.index, getattr(self, self.sql_template.index))
00320 self.serve_post_save_hook(self, r_pdu, two, eb)
00321
00322 def two():
00323 r_msg.append(r_pdu)
00324 cb()
00325
00326 oops = self.serve_fetch_one_maybe()
00327 if oops is not None:
00328 raise rpki.exceptions.DuplicateObject, "Object already exists: %r[%r] %r[%r]" % (self, getattr(self, self.element_name + "_handle"),
00329 oops, getattr(oops, oops.element_name + "_handle"))
00330
00331 self.serve_pre_save_hook(self, r_pdu, one, eb)
00332
00333 def serve_set(self, r_msg, cb, eb):
00334 """
00335 Handle a set action.
00336 """
00337
00338 db_pdu = self.serve_fetch_one()
00339 r_pdu = self.make_reply()
00340 for a in db_pdu.sql_template.columns[1:]:
00341 v = getattr(self, a, None)
00342 if v is not None:
00343 setattr(db_pdu, a, v)
00344 db_pdu.sql_mark_dirty()
00345
00346 def one():
00347 db_pdu.sql_store()
00348 db_pdu.serve_post_save_hook(self, r_pdu, two, eb)
00349
00350 def two():
00351 r_msg.append(r_pdu)
00352 cb()
00353
00354 db_pdu.serve_pre_save_hook(self, r_pdu, one, eb)
00355
00356 def serve_get(self, r_msg, cb, eb):
00357 """
00358 Handle a get action.
00359 """
00360 r_pdu = self.serve_fetch_one()
00361 self.make_reply(r_pdu)
00362 r_msg.append(r_pdu)
00363 cb()
00364
00365 def serve_list(self, r_msg, cb, eb):
00366 """
00367 Handle a list action for non-self objects.
00368 """
00369 for r_pdu in self.serve_fetch_all():
00370 self.make_reply(r_pdu)
00371 r_msg.append(r_pdu)
00372 cb()
00373
00374 def serve_destroy_hook(self, cb, eb):
00375 """
00376 Overridable hook.
00377 """
00378 cb()
00379
00380 def serve_destroy(self, r_msg, cb, eb):
00381 """
00382 Handle a destroy action.
00383 """
00384 def done():
00385 db_pdu.sql_delete()
00386 r_msg.append(self.make_reply())
00387 cb()
00388 db_pdu = self.serve_fetch_one()
00389 db_pdu.serve_destroy_hook(done, eb)
00390
00391 def serve_dispatch(self, r_msg, cb, eb):
00392 """
00393 Action dispatch handler.
00394 """
00395 dispatch = { "create" : self.serve_create,
00396 "set" : self.serve_set,
00397 "get" : self.serve_get,
00398 "list" : self.serve_list,
00399 "destroy" : self.serve_destroy }
00400 if self.action not in dispatch:
00401 raise rpki.exceptions.BadQuery, "Unexpected query: action %s" % self.action
00402 dispatch[self.action](r_msg, cb, eb)
00403
00404 def unimplemented_control(self, *controls):
00405 """
00406 Uniform handling for unimplemented control operations.
00407 """
00408 unimplemented = [x for x in controls if getattr(self, x, False)]
00409 if unimplemented:
00410 raise rpki.exceptions.NotImplementedYet, "Unimplemented control %s" % ", ".join(unimplemented)
00411
00412 class msg(list):
00413 """
00414 Generic top-level PDU.
00415 """
00416
00417 def startElement(self, stack, name, attrs):
00418 """
00419 Handle top-level PDU.
00420 """
00421 if name == "msg":
00422 assert self.version == int(attrs["version"])
00423 self.type = attrs["type"]
00424 else:
00425 elt = self.pdus[name]()
00426 self.append(elt)
00427 stack.append(elt)
00428 elt.startElement(stack, name, attrs)
00429
00430 def endElement(self, stack, name, text):
00431 """
00432 Handle top-level PDU.
00433 """
00434 assert name == "msg", "Unexpected name %s, stack %s" % (name, stack)
00435 assert len(stack) == 1
00436 stack.pop()
00437
00438 def __str__(self):
00439 """Convert msg object to string."""
00440 lxml.etree.tostring(self.toXML(), pretty_print = True, encoding = "us-ascii")
00441
00442 def toXML(self):
00443 """
00444 Generate top-level PDU.
00445 """
00446 elt = lxml.etree.Element("{%s}msg" % (self.xmlns), nsmap = self.nsmap, version = str(self.version), type = self.type)
00447 elt.extend([i.toXML() for i in self])
00448 return elt
00449
00450 @classmethod
00451 def query(cls, *args):
00452 """Create a query PDU."""
00453 self = cls(args)
00454 self.type = "query"
00455 return self
00456
00457 @classmethod
00458 def reply(cls, *args):
00459 """Create a reply PDU."""
00460 self = cls(args)
00461 self.type = "reply"
00462 return self
00463
00464 def is_query(self):
00465 """Is this msg a query?"""
00466 return self.type == "query"
00467
00468 def is_reply(self):
00469 """Is this msg a reply?"""
00470 return self.type == "reply"