forked from gsathya/centinel-server
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.py
77 lines (63 loc) · 2.31 KB
/
run.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
import os
import sys
import argparse
import centinel
import centinel.models
import centinel.views
import config
import logging
from logging.handlers import RotatingFileHandler
if (2, 7, 9) > sys.version_info:
print ("WARNING: Python is older than 2.7.9, "
"using older SSL version. This is "
"incompatible with Werkzeug 0.10.x and "
"will break if you use Werkzeug >= 0.10.0")
from OpenSSL import SSL
py_279 = False
else:
import ssl
py_279 = True
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--adhoc', help='Use adhoc SSL key and certificate.',
action='store_true')
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# create the centinel directory if it doesn't exist
if not os.path.exists(config.centinel_home):
os.makedirs(config.centinel_home)
print "Created centinel home directory at {}".format(config.centinel_home)
db = centinel.db
app = centinel.app
# setup logging first
log_handler = RotatingFileHandler(config.LOG_FILE)
log_handler.setLevel(config.LOG_LEVEL)
logger = logging.getLogger('werkzeug')
logger.addHandler(log_handler)
app.logger.addHandler(log_handler)
db.create_all()
Role = centinel.models.Role
admin_role = db.session.query(Role).filter(Role.name == 'admin').all()
if len(admin_role) == 0:
db.session.add(Role('admin'))
client_role = db.session.query(Role).filter(Role.name == 'client').all()
if len(client_role) == 0:
db.session.add(Role('client'))
db.session.commit()
if args.adhoc:
context = 'adhoc'
else:
# default method should be TLS
if py_279:
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
context.load_cert_chain(config.ssl_cert, config.ssl_key)
else:
context = SSL.Context(SSL.TLSv1_METHOD)
context.use_privatekey_file(config.ssl_key)
context.use_certificate_file(config.ssl_cert)
context.load_verify_locations(config.ssl_chain)
# Also, I shouldn't have to say this, but *DO NOT COMMIT THE
# KEY*. Under no circumstances should the key be committed
app.run(host="0.0.0.0", port=8082, use_reloader=True,
ssl_context=context, threaded=True)