Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement reverse proxy #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,30 @@ Configuration
log_config_file = /etc/eom/logging.conf
log_config_disable_existing = False

=====
Proxy
=====

EOM Proxy provides a Reverse Proxy WSGI App so that EOM can be used with
non-Python-based REST API stacks (f.e C++, Java, PHP, .NET).

EOM Proxy is easy to use and configure as shown in the following examples:

.. code-block:: ini

[eom:proxy]
upstream = http://localhost:8080
timeout = 30000

.. code-block:: python

from eom.proxy import ReverseProxy
from oslo.config import cfg

conf = cfg.CONF
conf(project='eom', args=[])
app = proxy.ReverseProxy()

====
RBAC
====
Expand Down
149 changes: 149 additions & 0 deletions eom/proxy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# Copyright (c) 2013 Rackspace, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import re
import uuid

from oslo.config import cfg
import requests


LOG = logging.getLogger(__name__)
_CONF = cfg.CONF

PROXY_GROUP_NAME = 'eom:proxy'
PROXY_OPTIONS = [
cfg.StrOpt('upstream'),
cfg.IntOpt('timeout')
]

_CONF.register_opts(PROXY_OPTIONS, group=PROXY_GROUP_NAME)


class ReverseProxyRequest(object):

def __init__(self, environ):
self.environ = environ
self.environ_controlled = {
k.upper(): v
for k, v in environ.items()
}
self.headers = {}

self.method = self.environ_controlled['REQUEST_METHOD']
self.path_virtual = self.environ_controlled['SCRIPT_NAME']
self.path = self.environ_controlled['PATH_INFO']
self.query_string = self.environ_controlled['QUERY_STRING']
self.server = {
'host': self.environ_controlled['HTTP_HOST']
if 'HTTP_HOST' in self.environ_controlled else None,
'name': self.environ_controlled['SERVER_NAME'],
'port': self.environ_controlled['SERVER_PORT']
}
self.wsgi = {
'version': environ['wsgi.version'],
'scheme': environ['wsgi.url_scheme'],
'input': environ['wsgi.input'],
'errors': environ['wsgi.errors'],
'multithread': environ['wsgi.multithread'],
'multiprocess': environ['wsgi.multiprocess'],
'run_once': environ['wsgi.run_once']
}

self.transaction_id = uuid.uuid4()
self.rebuild_headers()

def rebuild_headers(self):
self.headers = {}

for k, v in self.environ_controlled.items():

kp = k.lower().replace('_', '-')

if k.startswith('HTTP_'):
self.headers[kp[5:]] = v

elif k.startswith('CONTENT_'):
self.headers[kp] = v

self.headers['X-Reverse-Proxy-Transaction-Id'] = str(
self.transaction_id)

def url(self, upstream):
return '{0}{1}?{2}'.format(upstream,
self.path,
self.query_string)

@property
def body(self):
return self.wsgi['input']


class ReverseProxy(object):
"""WSGI Reverse Proxy Application

"""
STREAM_BLOCK_SIZE = 8 * 1024 # 8 Kilobytes

def __init__(self):
config_group = _CONF[PROXY_GROUP_NAME]

self.config = {
'upstream': config_group['upstream'],
'timeout': config_group['timeout']
}

if self.config['upstream'] in (None, ''):
LOG.error('upstream not valid')
LOG.warn('Configuration Error - upstream = {0}'
.format(self.config['upstream']))

url_regex = re.compile('(http|https)?(://)?[\w\d\.\-_]+:?(\d+)?/*')
if self.config['upstream']:
if not url_regex.match(self.config['upstream']):
LOG.error('Invalid URL - {0:}'.format(self.config['upstream']))

@staticmethod
def make_response_text(response):
return '{0} {1}'.format(response.status_code,
response.reason)

def handler(self, environ, start_response):

if self.config['upstream'] in (None, ''):
start_response('500 Internal Server Error',
[('Content-Type', 'plain/text')])
return [b'Please contact the administrator.']

req = ReverseProxyRequest(environ)

target_url = req.url(self.config['upstream'])

response = requests.request(req.method,
target_url,
headers=req.headers,
data=req.body,
timeout=self.config['timeout'],
allow_redirects=False,
stream=True)

start_response(ReverseProxy.make_response_text(response),
list(response.headers.items()))

return response.iter_content(decode_unicode=False)

def __call__(self, environ, start_response):
return self.handler(environ, start_response)
6 changes: 6 additions & 0 deletions examples/reverse_proxy_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from eom import proxy
from oslo.config import cfg

conf = cfg.CONF
conf(project='eom', args=[])
app = proxy.ReverseProxy()
4 changes: 3 additions & 1 deletion test-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ testtools
mock

# HTTP interface testing
stackinabox
stackinabox>=0.3
requests-mock

# Test runner
Expand All @@ -21,6 +21,8 @@ hacking

# Utils
fakeredis>=0.5.1
fixtures
httpretty
requests

# uwsgi-module testing
Expand Down
Loading