new onionbalance config generation
[mirror/dsa-puppet.git] / modules / onion / files / create-onionbalance-config
1 #!/usr/bin/python3
2
3 # create onionbalance config file
4 #
5 # create an onionbalance config file from a pre-cursor yaml
6 # file that puppet puts together.
7 # the input file looks like this:
8 #  - service: www.debian.org
9 #    address: jmri7yqqjpdxob4s
10 #    name: busoni-www.debian.org
11 #  - service: www.debian.org
12 #    address: ufhzy7r7qfy2tmy3
13 #    name: klecker-www.debian.org
14 #  - service: www.ports.debian.org
15 #    address: g32eridc6ocxni5w
16 #    name: busoni-www.ports.debian.org
17 # and so on.  This script collect together instances for the same
18 # service name, creates a new key if none is present already, and
19 # writes a new config.
20
21
22 # Copyright (c) 2016 Peter Palfrader
23 #
24 # Permission is hereby granted, free of charge, to any person
25 # obtaining a copy of this software and associated documentation
26 # files (the "Software"), to deal in the Software without
27 # restriction, including without limitation the rights to use,
28 # copy, modify, merge, publish, distribute, sublicense, and/or sell
29 # copies of the Software, and to permit persons to whom the
30 # Software is furnished to do so, subject to the following
31 # conditions:
32 #
33 # The above copyright notice and this permission notice shall be
34 # included in all copies or substantial portions of the Software.
35 #
36 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
38 # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
40 # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
41 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
42 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43 # OTHER DEALINGS IN THE SOFTWARE.
44
45 import os.path
46 import subprocess
47 import yaml
48
49 j = '/etc/onionbalance/config-dsa-snippet.yaml'
50 outfile = '/etc/onionbalance/config.yaml-NEW'
51
52 relkeydir = 'private_keys'
53 keydir = os.path.join('/etc/onionbalance', relkeydir)
54
55 data = yaml.safe_load(open(j))
56
57 service_instances = {}
58 for entry in data:
59   s = entry['service']
60   if s not in service_instances:
61     service_instances[s] = []
62
63   instance = {
64     'address': entry['address'],
65     'name'   : entry['name'],
66   }
67   service_instances[s].append(instance)
68
69 services = []
70 for s in service_instances:
71   keyfile = os.path.join(keydir, s+'.key')
72   relkeyfile = os.path.join(relkeydir, s+'.key')
73   if (not os.path.exists(keyfile)):
74     subprocess.check_call('umask 0027 && openssl genrsa -out %s 1024 && chgrp onionbalance %s'%(keyfile, keyfile), shell=True)
75
76   service = {
77     'key': relkeyfile,
78     'instances': service_instances[s]
79   }
80   services.append(service)
81
82
83 config = {}
84 config['service'] = services
85
86 with open(outfile, 'w') as f:
87   yaml.dump(config, f, indent=4)