Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Zsolt Haraszti | accad4a | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 3 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import os |
| 19 | |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 20 | import grpc |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 21 | from klein import Klein |
| 22 | from simplejson import dumps, load |
| 23 | from structlog import get_logger |
| 24 | from twisted.internet import reactor, endpoints |
| 25 | from twisted.internet.defer import inlineCallbacks, returnValue |
| 26 | from twisted.internet.tcp import Port |
ubuntu | 3797558 | 2017-07-01 17:53:19 -0700 | [diff] [blame] | 27 | from twisted.internet.endpoints import SSL4ServerEndpoint |
| 28 | from twisted.internet.ssl import DefaultOpenSSLContextFactory |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 29 | from twisted.web.server import Site |
| 30 | from twisted.web.static import File |
ubuntu | 3797558 | 2017-07-01 17:53:19 -0700 | [diff] [blame] | 31 | from OpenSSL.SSL import TLSv1_2_METHOD |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 32 | from werkzeug.exceptions import BadRequest |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 33 | from grpc import StatusCode |
Matteo Scandolo | caecf42 | 2017-09-11 16:16:07 -0700 | [diff] [blame] | 34 | import json |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 35 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 36 | |
| 37 | log = get_logger() |
| 38 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 39 | |
| 40 | class WebServer(object): |
| 41 | |
| 42 | app = Klein() |
| 43 | |
ubuntu | 3797558 | 2017-07-01 17:53:19 -0700 | [diff] [blame] | 44 | def __init__(self, port, work_dir, swagger_url, grpc_client, key=None, cert=None): |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 45 | self.port = port |
| 46 | self.site = None |
| 47 | self.work_dir = work_dir |
Matteo Scandolo | 5435657 | 2017-04-24 18:52:28 -0700 | [diff] [blame] | 48 | self.swagger_url = swagger_url |
ubuntu | 3797558 | 2017-07-01 17:53:19 -0700 | [diff] [blame] | 49 | self.grpc_client = grpc_client |
| 50 | self.key = key |
| 51 | self.cert = cert |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 52 | |
| 53 | self.swagger_ui_root_dir = os.path.abspath( |
| 54 | os.path.join(os.path.dirname(__file__), '../swagger_ui')) |
| 55 | |
| 56 | self.tcp_port = None |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 57 | self.shutting_down = False |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 58 | |
Matteo Scandolo | 5435657 | 2017-04-24 18:52:28 -0700 | [diff] [blame] | 59 | self.add_swagger_routes(self.app, swagger_url) |
| 60 | |
| 61 | def add_swagger_routes(self, app, swagger_url): |
| 62 | log.info('Publishing swagger docs at %s' % swagger_url) |
| 63 | |
| 64 | @app.route(swagger_url + '/', branch=True) |
| 65 | def static(self, request): |
| 66 | try: |
| 67 | log.debug(request=request) |
| 68 | return File(self.swagger_ui_root_dir) |
| 69 | except Exception, e: |
| 70 | log.exception('file-not-found', request=request) |
| 71 | |
| 72 | @app.route(swagger_url + '/v1/swagger.json') |
| 73 | def swagger_json(self, request): |
| 74 | try: |
| 75 | return File(os.path.join(self.work_dir, 'swagger.json')) |
| 76 | except Exception, e: |
| 77 | log.exception('file-not-found', request=request) |
| 78 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 79 | @inlineCallbacks |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 80 | def start(self): |
| 81 | log.debug('starting') |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 82 | yield self._open_endpoint() |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 83 | log.info('started') |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 84 | returnValue(self) |
| 85 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 86 | @inlineCallbacks |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 87 | def stop(self): |
| 88 | log.debug('stopping') |
| 89 | self.shutting_down = True |
| 90 | if self.tcp_port is not None: |
| 91 | assert isinstance(self.tcp_port, Port) |
| 92 | yield self.tcp_port.socket.close() |
| 93 | log.info('stopped') |
| 94 | |
| 95 | @inlineCallbacks |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 96 | def _open_endpoint(self): |
ubuntu | 3797558 | 2017-07-01 17:53:19 -0700 | [diff] [blame] | 97 | try: |
| 98 | if self.key == None or self.cert == None: |
| 99 | endpoint = endpoints.TCP4ServerEndpoint(reactor, self.port) |
| 100 | else: |
| 101 | # Enforce TLSv1_2_METHOD |
| 102 | ctx = DefaultOpenSSLContextFactory(self.key, self.cert, TLSv1_2_METHOD) |
| 103 | endpoint = SSL4ServerEndpoint(reactor, self.port, ctx) |
| 104 | |
| 105 | self.site = Site(self.app.resource()) |
| 106 | self.tcp_port = yield endpoint.listen(self.site) |
| 107 | log.info('web-server-started', port=self.port) |
| 108 | self.endpoint = endpoint |
| 109 | except Exception, e: |
| 110 | self.log.exception('web-server-failed-to-start', e=e) |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 111 | |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 112 | def reload_generated_routes(self): |
| 113 | for fname in os.listdir(self.work_dir): |
| 114 | if fname.endswith('_gw.py'): |
| 115 | module_name = fname.replace('.py', '') |
| 116 | m = __import__(module_name) |
| 117 | assert hasattr(m, 'add_routes') |
| 118 | m.add_routes(self.app, self.grpc_client) |
| 119 | log.info('routes-loaded', module=module_name) |
| 120 | |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 121 | @app.handle_errors(grpc._channel._Rendezvous) |
| 122 | def grpc_exception(self, request, failure): |
| 123 | code = failure.value.code() |
| 124 | if code == StatusCode.NOT_FOUND: |
| 125 | request.setResponseCode(404) |
| 126 | return failure.value.details() |
| 127 | elif code == StatusCode.INVALID_ARGUMENT: |
| 128 | request.setResponseCode(400) |
| 129 | return failure.value.details() |
| 130 | elif code == StatusCode.ALREADY_EXISTS: |
| 131 | request.setResponseCode(409) |
| 132 | return failure.value.details() |
Matteo Scandolo | 92f7f1f | 2017-04-28 15:56:29 -0700 | [diff] [blame] | 133 | elif code == StatusCode.UNAUTHENTICATED: |
| 134 | request.setResponseCode(401) |
| 135 | return failure.value.details() |
| 136 | elif code == StatusCode.PERMISSION_DENIED: |
| 137 | request.setResponseCode(403) |
| 138 | return failure.value.details() |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 139 | else: |
Matteo Scandolo | caecf42 | 2017-09-11 16:16:07 -0700 | [diff] [blame] | 140 | request.setResponseCode(500) |
| 141 | return json.dumps({'error': 'Internal Server Error', 'specific_error': failure.value.details()}) |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 142 | |