forked from nategraf/Naumachia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure.py
executable file
·223 lines (183 loc) · 8.48 KB
/
configure.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python3
from os import path, makedirs, chmod
from lazycert import LazyCert
import io
import jinja2
import sys
import yaml
import argparse
import subprocess
import requests
import tarfile
import logging
import tempfile
logger = logging.getLogger(__name__)
script_dir = path.dirname(path.realpath(__file__))
wildcard = '*'
defaults = {
'eve': False,
'domain': None,
'challenges_directory': './challenges',
'challenges': {
'*': {
'port': 1194,
'files': [],
'openvpn_management_port': None
}
},
'registrar': {
'port': 3960,
'network': 'default',
'tls_enabled': False,
'tls_verify_client': False,
'tls_clients': []
}
}
EASYRSA_URL='https://github.com/OpenVPN/easy-rsa/releases/download/v3.0.4/EasyRSA-3.0.4.tgz'
EASYRSA_DIR='EasyRSA-3.0.4'
EASYRSA_DEFAULT=path.abspath(path.join(script_dir, 'tools', EASYRSA_DIR, 'easyrsa'))
REGISTRAR_CERT_DIR=path
def install_easyrsa():
install_dir = path.abspath(path.join(script_dir, 'tools'))
if not path.isdir(install_dir):
makedirs(install_dir)
with requests.get(EASYRSA_URL, stream=True) as resp:
tarball = tarfile.open(fileobj=io.BytesIO(resp.content), mode='r:gz')
tarball.extractall(path=install_dir)
logger.info("Installed easyrsa to '{}' from '{}'".format(install_dir, EASYRSA_URL))
def apply_defaults(config, defaults):
# Expand the wildcard
# Wildcard only makes sense when the value is a dict
if wildcard in defaults:
default = defaults[wildcard]
defaults.update({k: default for k in config if k not in defaults})
defaults.pop(wildcard)
for key, default in defaults.items():
# Handle the case where the key is not in config
if key not in config:
config[key] = default
# Recurisly apply defaults to found dicts if the default is a dict
elif isinstance(default, dict) and isinstance(config[key], dict):
apply_defaults(config[key], default)
def read_config(filename):
with open(filename, 'r') as config_file:
config = yaml.load(config_file)
logger.debug("Read from file: %s", config)
apply_defaults(config, defaults)
for chal_name, chal_settings in config['challenges'].items():
if 'commonname' not in chal_settings:
chal_settings['commonname'] = append_domain(chal_name, config['domain'])
logger.debug("Modified: %s", config)
return config
def parse_args():
parser = argparse.ArgumentParser(
description='Parse the Naumachia config file and set up the environment',
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--verbosity', '-v', metavar="LEVEL", default="info", choices=('critical', 'error', 'warning', 'info', 'debug'), help="logging level to use")
parser.add_argument('--config', metavar="PATH", default=path.join(script_dir, 'config.yml'), help='path to Naumachia config file')
parser.add_argument('--templates', metavar="PATH", default=path.join(script_dir, 'templates'), help='path to the configuration templates')
parser.add_argument('--registrar_certs', metavar="PATH", default=path.join(script_dir, 'registrar/certs'), help='path to the configuration templates')
parser.add_argument('--compose', metavar="PATH", default=path.join(script_dir, 'docker-compose.yml'), help='path to the rendered docker-compose output')
parser.add_argument('--ovpn_configs', metavar="PATH", default=path.join(script_dir, 'openvpn', 'config'), help='path to openvpn configurations')
parser.add_argument('--easyrsa', metavar="PATH", default=EASYRSA_DEFAULT, help='location of easyrsa executable. If the path does not exist, easyrsa will be installed')
return parser.parse_args()
def init_pki(easyrsa, directory, cn):
easyrsa = path.abspath(easyrsa)
debug = logger.isEnabledFor(logging.DEBUG)
common_args = {
'check': True,
'cwd': directory,
'stdout': subprocess.PIPE if not debug else None,
'stderr': subprocess.PIPE if not debug else None,
'universal_newlines': True
}
try:
logger.info("Initializing public key infrastructure (PKI)")
subprocess.run([easyrsa, 'init-pki'], **common_args)
logger.info("Building certificiate authority (CA)")
subprocess.run([easyrsa, 'build-ca', 'nopass'], input="{}.{}\n".format('ca', cn), **common_args)
logger.info("Generating Diffie-Hellman (DH) parameters")
subprocess.run([easyrsa, 'gen-dh'], **common_args)
logger.info("Building server certificiate")
subprocess.run([easyrsa, 'build-server-full', cn, 'nopass'], **common_args)
logger.info("Generating certificate revocation list (CRL)")
subprocess.run([easyrsa, 'gen-crl'], **common_args)
except subprocess.CalledProcessError as e:
logger.error("Command '{}' failed with exit code {}".format(e.cmd, e.returncode))
if e.output:
logger.error(e.output)
def _render(tpl_path, context):
dirname, filename = path.split(tpl_path)
return jinja2.Environment(
loader=jinja2.FileSystemLoader(dirname or './')
).get_template(filename).render(context)
def render(tpl_path, dst_path, context):
with open(dst_path, 'w') as f:
f.write(_render(tpl_path, context))
logger.info("Rendered {} from {} ".format(dst_path, tpl_path))
def rendertmp(tpl_path, context):
f = tempfile.NamedTemporaryFile(mode='w+')
f.write(_render(tpl_path, context))
f.flush()
return f
def append_domain(name, domain):
if domain:
return '.'.join((name, domain))
else:
return name
if __name__ == "__main__":
args = parse_args()
# Configure logging
levelnum = getattr(logging, args.verbosity.upper(), None)
if not isinstance(levelnum, int):
raise ValueError('Invalid log level: {}'.format(args.verbosity))
logging.basicConfig(level=levelnum, format="[%(levelname)s] %(message)s")
# Load the config from disk
logger.info("Using config from {}".format(args.config))
config = read_config(args.config)
# Ensure easyrsa is installed
if not path.exists(args.easyrsa):
if args.easyrsa == EASYRSA_DEFAULT:
install_easyrsa()
else:
raise FileNotFoundError(args.easyrsa)
# Render the docker-compose file
template_path = path.join(args.templates, 'docker-compose.yml.j2')
render(template_path, args.compose, config)
# Create and missing openvpn config directories
for name, chal in config['challenges'].items():
config_dirname = path.join(args.ovpn_configs, name)
logger.info("Configuring '{}'".format(name))
if not path.isdir(config_dirname):
makedirs(config_dirname)
logger.info("Created new openvpn config directory {}".format(config_dirname))
init_pki(args.easyrsa, config_dirname, chal['commonname'])
else:
logger.info("Using existing openvpn config directory {}".format(config_dirname))
context = {'chal': chal}
context.update(config)
render(path.join(args.templates, 'ovpn_env.sh.j2'), path.join(config_dirname, 'ovpn_env.sh'), context)
render(path.join(args.templates, 'openvpn.conf.j2'), path.join(config_dirname, 'openvpn.conf'), context)
# Create certificates for the registrar if needed
if config['registrar'] and config['registrar']['tls_enabled']:
logger.info("Setting up certificates for registrar in {}".format(args.registrar_certs))
if not path.isdir(args.registrar_certs):
makedirs(args.registrar_certs)
generator = LazyCert(args.registrar_certs)
config_template = path.join(args.templates, 'openssl.conf.j2')
# Create the gencert function here to have config and args in closure
def gencert(name, ca=None):
cn = append_domain(name, config['domain'])
if ca is not None:
ca = append_domain(ca, config['domain'])
if not path.isfile(path.join(args.registrar_certs, cn + '.crt')):
with rendertmp(config_template, {'cn': cn, 'ca': ca is None}) as certconfig:
generator.create(cn, ca=ca, config=certconfig.name)
logger.info("Created new certificate for {}".format(cn))
else:
logger.info("Using existing certificate for {}".format(cn))
gencert('ca')
gencert('registrar', 'ca')
for client in config['registrar']['tls_clients']:
gencert(client, 'ca')