3 # create onionbalance config file
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.
16 # Copyright (c) 2016 Peter Palfrader
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
27 # The above copyright notice and this permission notice shall be
28 # included in all copies or substantial portions of the Software.
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.
45 j = '/etc/onionbalance/config-dsa-snippet.yaml'
46 outfile = '/etc/onionbalance/config.yaml'
48 relkeydir = 'private_keys'
49 keydir = os.path.join('/etc/onionbalance', relkeydir)
52 data = yaml.safe_load(conf)
54 service_instances = {}
57 if s not in service_instances:
58 service_instances[s] = []
61 'address': entry['address'],
62 'name' : entry['name'],
64 service_instances[s].append(instance)
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)
78 'instances': service_instances[s]
80 services.append(service)
84 config['services'] = services
86 with open(outfile, 'w') as f:
87 yaml.dump(config, f, indent=4)