Skip to content

Commit

Permalink
minor refactoring of project structure; generic proxy configuration a…
Browse files Browse the repository at this point in the history
…nd error handling (localstack#173)
  • Loading branch information
whummer committed Jul 17, 2017
1 parent b19e45b commit 8a55937
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ RUN make test
EXPOSE 4567-4582 8080

# install supervisor daemon & copy config file
ADD supervisord.conf /etc/supervisord.conf
ADD bin/supervisord.conf /etc/supervisord.conf

# define command at startup
ENTRYPOINT ["/usr/bin/supervisord"]
3 changes: 2 additions & 1 deletion bin/localstack
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ if __name__ == "__main__":
cmd = os.environ.get('CMD', '')
image_name = os.environ.get('IMAGE_NAME', constants.DOCKER_IMAGE_NAME)
service_ports = config.SERVICE_PORTS
max_port = max(4592, max(service_ports.values()))
port_mappings = '-p {min_port}-{max_port}:{min_port}-{max_port}'.format(
min_port=min(service_ports.values()), max_port=max(service_ports.values()))
min_port=min(service_ports.values()), max_port=max_port)

if services:
port_mappings = ''
Expand Down
File renamed without changes.
14 changes: 0 additions & 14 deletions bitbucket-pipelines.yml

This file was deleted.

23 changes: 12 additions & 11 deletions localstack/services/generic_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
# path for test certificate
SERVER_CERT_PEM_FILE = '%s/server.test.pem' % (TMP_FOLDER)

# CORS settings
CORS_ALLOWED_HEADERS = ('authorization', 'content-type', 'content-md5',
'x-amz-content-sha256', 'x-amz-date', 'x-amz-security-token', 'x-amz-user-agent')
CORS_ALLOWED_METHODS = ('HEAD', 'GET', 'PUT', 'POST', 'DELETE', 'OPTIONS', 'PATCH')

# set up logger
LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -149,6 +154,7 @@ def forward(self, method):
data=data, headers=forward_headers, response=response)
if isinstance(updated_response, Response):
response = updated_response

# copy headers and return response
self.send_response(response.status_code)

Expand All @@ -163,24 +169,19 @@ def forward(self, method):
if 'Access-Control-Allow-Origin' not in response.headers:
self.send_header('Access-Control-Allow-Origin', '*')
if 'Access-Control-Allow-Methods' not in response.headers:
self.send_header('Access-Control-Allow-Methods', 'HEAD,GET,PUT,POST,DELETE,OPTIONS,PATCH')
self.send_header('Access-Control-Allow-Methods', ','.join(CORS_ALLOWED_METHODS))
if 'Access-Control-Allow-Headers' not in response.headers:
self.send_header('Access-Control-Allow-Headers',
','.join(['authorization',
'content-type',
'content-md5',
'x-amz-content-sha256',
'x-amz-date',
'x-amz-security-token',
'x-amz-user-agent']))
self.send_header('Access-Control-Allow-Headers', ','.join(CORS_ALLOWED_HEADERS))

self.end_headers()
if len(response.content):
self.wfile.write(bytes_(response.content))
self.wfile.flush()
except Exception as e:
if not self.proxy.quiet or 'ConnectionRefusedError' not in str(traceback.format_exc()):
LOGGER.error("Error forwarding request: %s %s" % (e, traceback.format_exc()))
trace = str(traceback.format_exc())
conn_error = 'ConnectionRefusedError' in trace or 'NewConnectionError' in trace
if not self.proxy.quiet or not conn_error:
LOGGER.error("Error forwarding request: %s %s" % (e, trace))
self.send_response(502) # bad gateway
self.end_headers()

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ flask==0.10.1
flask-cors==3.0.3
flask_swagger==0.2.12
jsonpath-rw==1.4.0
localstack-ext==0.6.1.3
localstack-ext==0.6.1.4
moto-ext==1.0.1.4
nose==1.3.7
pep8==1.7.0
Expand Down

0 comments on commit 8a55937

Please sign in to comment.