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