summaryrefslogtreecommitdiff
path: root/tools/sendsth.py
blob: 8583bd72194697c6831f44b76e3fbcefc55f6791 (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2014, NORDUnet A/S.
# See LICENSE for licensing information.

import argparse
import json
import urllib2
import sys
import yaml
from certtools import  http_request, create_ssl_context

parser = argparse.ArgumentParser(description="")
parser.add_argument('--config', help="System configuration", required=True)
parser.add_argument('--localconfig', help="Local configuration", required=True)
parser.add_argument('--frontendnode', help="Destination node", required=True)
parser.add_argument('--sthfile', help="File containing STH", required=True)
args = parser.parse_args()

config = yaml.load(open(args.config))
localconfig = yaml.load(open(args.localconfig))

frontendnodes = config["frontendnodes"]
frontendnode = [node for node in frontendnodes if node["name"] == args.frontendnode][0]
paths = localconfig["paths"]

create_ssl_context(cafile=paths["https_cacertfile"])

own_key = (localconfig["nodename"], "%s/%s-private.pem" % (paths["privatekeys"], localconfig["nodename"]))

hashed_dir = True

def sendsth(node, baseurl, submission):
    try:
        result = http_request(baseurl + "plop/v1/frontend/sendsth",
            json.dumps(submission), key=own_key, verifynode=node, publickeydir=paths["publickeys"])
        return json.loads(result)
    except urllib2.HTTPError, e:
        print >>sys.stderr, "ERROR: sendsth", e.read()
        sys.exit(1)
    except ValueError, e:
        print >>sys.stderr, "==== FAILED REQUEST ===="
        print >>sys.stderr, submission
        print >>sys.stderr, "======= RESPONSE ======="
        print >>sys.stderr, result
        print >>sys.stderr, "========================"
        sys.stderr.flush()
        raise e

sth = json.load(open(args.sthfile))

nodeaddress = "https://%s/" % frontendnode["address"]
nodename = frontendnode["name"]
sendsthresult = sendsth(nodename, nodeaddress, sth)
if sendsthresult["result"] != "ok":
    print >>sys.stderr, "send sth:", sendsthresult
    sys.exit(1)