aboutsummaryrefslogtreecommitdiff
path: root/rp/utils/hashdir
blob: 21619ce488ee0cb2a49b5724fd39634a1fd71baf (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
#!/usr/bin/env python
#
# $Id$
#
# Copyright (C) 2014 Dragon Research Labs ("DRL")
#
# Permission to use, copy, modify, and/or 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.

"""
Copies an authenticated result tree from an rcynic run into the format
expected by most OpenSSL-based programs: a collection of "PEM" format
files with names in the form that OpenSSL's -CApath lookup routines
expect.  This can be useful for validating RPKI objects which are not
distributed as part of the repository system.
"""

import os
import sys
import argparse

from rpki.rcynicdb.iterator import authenticated_objects

def check_dir(s):
    if os.path.isdir(s):
        return os.path.abspath(s)
    else:
        raise argparse.ArgumentTypeError("{!r} is not a directory".format(s))

parser = argparse.ArgumentParser(description = __doc__)
parser.add_argument("-v", "--verbose", action = "store_true", help = "whistle while you work")
parser.add_argument("rcynic_dir", nargs = "?", type = check_dir, help = "rcynic authenticated output directory")
parser.add_argument("output_dir", help = "name of output directory to create")
args = parser.parse_args()

if not os.path.isdir(args.output_dir):
    os.makedirs(args.output_dir)

def store(uri, obj, fmt):
    for i in xrange(1000000):
        fn = os.path.join(args.output_dir, fmt.format(i))
        if os.path.exists(fn):
            continue
        with open(fn, "w") as f:
            f.write(obj.pemWrite())
            if args.verbose:
                print fn, "<=", uri
            return
    else:
        sys.exit("No path name available for {} ({})".format(uri, fn))

for uri, cer in authenticated_objects(uri_suffix = ".cer"):
    store(uri, cer, "{:08x}.{{:d}}".format(cer.getSubjectHash()))

for uri, crl in authenticated_objects(uri_suffix = ".crl"):
    store(uri, crl, "{:08x}.r{{:d}}".format(crl.getIssuerHash()))