Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
is_auth, message = tools.ldap_authentification(SERVER_OPTS)
if not is_auth:
return tools.response_render(message, http_code='401 Unauthorized')
return tools.response_render('OK')
class MyApplication(web.application):
"""
Can change port or other stuff
"""
def run(self, port=int(SERVER_OPTS['port']), *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port))
if __name__ == "__main__":
if SERVER_OPTS['ssl']:
HTTPServer.ssl_adapter = BuiltinSSLAdapter(
certificate=SERVER_OPTS['ssl_public_key'],
private_key=SERVER_OPTS['ssl_private_key'])
if ARGS.verbose:
print('SSL: %s' % SERVER_OPTS['ssl'])
print('LDAP: %s' % SERVER_OPTS['ldap'])
print('Admin DB Failover: %s' % SERVER_OPTS['admin_db_failover'])
APP = MyApplication(constants.URLS, globals(), autoreload=False)
web.config.debug = SERVER_OPTS['debug']
if SERVER_OPTS['debug']:
print('Debug mode on')
APP.run()
except Exception:
pass
_thread.start_new_thread(timing_loop, ())
if gv.use_gpio_pins:
set_output()
app.notfound = lambda: web.seeother(u"/")
###########################
#### For HTTPS (SSL): ####
if gv.sd["htp"] == 443:
from cheroot.server import HTTPServer
from cheroot.ssl.builtin import BuiltinSSLAdapter
HTTPServer.ssl_adapter = BuiltinSSLAdapter(
certificate='/usr/lib/ssl/certs/SIP.crt',
private_key='/usr/lib/ssl/private/SIP.key'
)
app.run()
def __init__(
self, certificate, private_key, certificate_chain=None,
ciphers=None):
"""Set up context in addition to base class properties if available."""
if ssl is None:
raise ImportError('You must install the ssl module to use HTTPS.')
super(BuiltinSSLAdapter, self).__init__(
certificate, private_key, certificate_chain, ciphers)
self.context = ssl.create_default_context(
purpose=ssl.Purpose.CLIENT_AUTH,
cafile=certificate_chain
)
self.context.load_cert_chain(certificate, private_key)
if self.ciphers is not None:
self.context.set_ciphers(ciphers)
def bind(self, sock):
"""Wrap and return the given socket."""
return super(BuiltinSSLAdapter, self).bind(sock)
def run(self, handler): # pragma: no cover
from cheroot import wsgi
from cheroot.ssl import builtin
self.options['bind_addr'] = (self.host, self.port)
self.options['wsgi_app'] = handler
certfile = self.options.pop('certfile', None)
keyfile = self.options.pop('keyfile', None)
chainfile = self.options.pop('chainfile', None)
server = wsgi.Server(**self.options)
if certfile and keyfile:
server.ssl_adapter = builtin.BuiltinSSLAdapter(
certfile, keyfile, chainfile)
try:
server.start()
finally:
server.stop()
#############
def run(self, handler): # pragma: no cover
from cheroot import wsgi
from cheroot.ssl import builtin
self.options['bind_addr'] = (self.host, self.port)
self.options['wsgi_app'] = handler
certfile = self.options.pop('certfile', None)
keyfile = self.options.pop('keyfile', None)
chainfile = self.options.pop('chainfile', None)
server = wsgi.Server(**self.options)
if certfile and keyfile:
server.ssl_adapter = builtin.BuiltinSSLAdapter(
certfile, keyfile, chainfile)
try:
server.start()
finally:
server.stop()
#############
def run(self, server_handler):
"""
Overrides super to setup Cherry py with ssl and start the server.
:param server_handler: originating server type
:type server_handler:
"""
server = Server((self.host, self.port), server_handler)
# Uses the following github page's recommendation for setting up the cert:
# https://github.com/nickbabcock/bottle-ssl
server.ssl_adapter = BuiltinSSLAdapter(conf_obj['ssl']['cacert_pem_path'],
conf_obj['ssl']['privkey_pem_path'],
conf_obj['ssl'].get('chain_pem_path'))
try:
server.start()
finally:
server.stop()
def run(self, handler): # pragma: no cover
from cheroot import wsgi
from cheroot.ssl import builtin
self.options['bind_addr'] = (self.host, self.port)
self.options['wsgi_app'] = handler
certfile = self.options.pop('certfile', None)
keyfile = self.options.pop('keyfile', None)
chainfile = self.options.pop('chainfile', None)
self.server = wsgi.Server(**self.options)
if certfile and keyfile:
self.server.ssl_adapter = builtin.BuiltinSSLAdapter(
certfile, keyfile, chainfile)
self.server.start()