aboutsummaryrefslogtreecommitdiff
path: root/potpourri/rrdp-test-tool
blob: fa2de0235c7f9df98d9c7f76f7f83daed38b00a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# $Id$
#
# Copyright (C) 2013  Dragon Research Labs ("DRL")
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND DRL DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS.  IN NO EVENT SHALL DRL BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.

"""
Test tool for prototype RRDP implementation.  Eventually some of this
code will likely be refactored into more user-friendly form, but for
the moment this just does whatever insane thing I need to do this week
for testing.
"""

import rpki.relaxng
import lxml.etree
import argparse
import os

class Tags(object):
  def __init__(self, *tags):
    for tag in tags:
      setattr(self, tag, rpki.relaxng.rrdp.xmlns + tag)

tags = Tags("notification", "deltas", "delta", "snapshot", "publish", "withdraw")

class main(object):

  def __init__(self):

    parser = argparse.ArgumentParser(description = __doc__)
    parser.add_argument("--rcynic-tree", default = "rcynic-data/unauthenticated",
                        help = "directory tree in which to write extracted RPKI objects")
    parser.add_argument("rrdp_file", nargs = "+",
                        help = "RRDP snapshot or deltas file")
    self.args = parser.parse_args()

    if not os.path.isdir(self.args.rcynic_tree):
      os.makedirs(self.args.rcynic_tree)

    for rrdp_file in self.args.rrdp_file:
      xml = lxml.etree.ElementTree(file = rrdp_file).getroot()
      rpki.relaxng.rrdp.assertValid(xml)
      getattr(self, xml.tag[len(rpki.relaxng.rrdp.xmlns):])(xml)


  def snapshot(self, xml):
    assert xml.tag == tags.snapshot

    print "Unpacking version %s session %s serial %s" % (
      xml.get("version"), xml.get("session_id"), xml.get("serial"))

    for elt in xml:
      assert elt.tag == tags.publish
      uri = elt.get("uri")
      print " ", uri
      assert uri.startswith("rsync://")
      fn = os.path.join(self.args.rcynic_tree, uri[len("rsync://"):])
      dn = os.path.dirname(fn)
      if not os.path.isdir(dn):
        os.makedirs(dn)
      with open(fn, "wb") as f:
        f.write(elt.text.decode("base64"))


if __name__ == "__main__":
  main()