blob: 54e788b1ad1398ea2152fe25414dfaeb25010421 [file] [log] [blame]
Brad Bishop7bc6d8d2016-08-29 22:19:51 -04001#!/usr/bin/env python
2
3# Contributors Listed Below - COPYRIGHT 2016
4# [+] International Business Machines Corp.
5#
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
16# implied. See the License for the specific language governing
17# permissions and limitations under the License.
18
19
20import sys
21import os
22import gevent
23from gevent.pywsgi import WSGIServer
Deepak Kodihalli0fe213f2017-10-11 00:08:48 -050024have_wsock = True
25try:
26 from geventwebsocket.handler import WebSocketHandler
27except ImportError:
28 have_wsock = False
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040029
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070030# Parameters
31# <wsgi application> REQUIRED Application to import and run (e.g. rest_dbus)
32# <--no-ssl> OPTIONAL Don't use SSL
33#
34# NOTE: If not activated via a systemd socket then this server will bind
35# by default to all address's at port 443 or 80(--no-ssl)
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040036if __name__ == '__main__':
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070037
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040038 if len(sys.argv) < 2:
39 sys.stderr.write('WSGI application required!')
40 sys.exit(1)
41
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070042 if (len(sys.argv) > 2) and (sys.argv[2] == "--no-ssl"):
43 use_ssl = False
44 else:
45 use_ssl = True
46
CamVan Nguyen249d1322018-03-05 10:08:33 -060047 exec('from obmc.wsgi.apps.%s import App' % sys.argv[1])
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040048
49 default_cert = os.path.join(
50 sys.prefix, 'share', os.path.basename(__file__), 'cert.pem')
51
Deepak Kodihalli0fe213f2017-10-11 00:08:48 -050052 kw = {}
53 if have_wsock:
54 kw['have_wsock'] = True
55 app = App(**kw)
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040056
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070057 # repurpose for WSGIServer usage below
58 kw = {}
Ratan Gupta91ff1102018-01-14 12:57:41 +053059
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070060 if use_ssl:
61 # ECDH - Allow Elliptic Curve Diffie Hellman
62 # kDH - Allow Key Exchange algorithm as Diffie Hellman
63 # kEDH - Allow Key Exchange algorithm as Ephemeral Diffie Hellman
64 # kRSA - Allow Key Exchange algorithm as RSA
65 # !SSLv3 - Disallows any ciphers specific to SSLv3
66 # !SSLv2 - Disallows any ciphers specific to SSLv2 protocol
67 # !aNULL - Disallows anonymous authentication or no authentication
68 # !eNULL - Disallows connection with NULL encryption
69 # !LOW - Disallows any low strength ciphers
70 # !MEDIUM- Disallows medium strength ciphers
71
72 kw['ciphers'] = (
73 'ECDH:kDH:kEDH:kRSA:!SSLv3:!SSLv2:!aNULL:!eNULL:!LOW:!MEDIUM:@STRENGTH'
74 )
75
76 kw['keyfile'] = default_cert
77 kw['certfile'] = default_cert
Ratan Gupta91ff1102018-01-14 12:57:41 +053078
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040079 if os.environ.get('LISTEN_PID', None) == str(os.getpid()):
80 FIRST_SYSTEMD_SOCKET_FD = 3
81 bind = gevent.socket.fromfd(FIRST_SYSTEMD_SOCKET_FD,
Deepak Kodihalli48c76412017-10-11 00:10:54 -050082 gevent.socket.AF_INET,
83 gevent.socket.SOCK_STREAM)
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040084 else:
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070085 if use_ssl:
86 bind = ('', 443)
87 else:
88 bind = ('', 80)
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040089
Deepak Kodihalli0fe213f2017-10-11 00:08:48 -050090 if have_wsock:
91 kw['handler_class'] = WebSocketHandler
Andrew Geisslerfe3a0992018-04-05 09:45:45 -070092
93 server = WSGIServer( bind, app, **kw )
94
Brad Bishop7bc6d8d2016-08-29 22:19:51 -040095 server.serve_forever()