Skip to content

Commit

Permalink
minor fixes for CloudFormation and handling AWS auth headers
Browse files Browse the repository at this point in the history
  • Loading branch information
whummer committed Jan 17, 2021
1 parent cf9ee73 commit 6926d47
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
13 changes: 9 additions & 4 deletions localstack/services/cloudformation/service_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ def __init__(self, resource_json, region_name=None, **params):
# ABSTRACT BASE METHODS
# ----------------------

def set_resource_state(self, state):
""" Return the deployment state of this resource. """
self.state = state or {}

def get_resource_name(self):
""" Return the name of this resource, based on its properties (to be overwritten by subclasses) """
return None
Expand All @@ -78,6 +74,11 @@ def update_resource(self, new_resource, stack_name, resources):
""" Update the deployment of this resource, using the updated properties (implemented by subclasses). """
pass

@classmethod
def cloudformation_type(cls):
""" Return the CloudFormation resource type name, e.g., "AWS::S3::Bucket" (implemented by subclasses). """
return super(GenericBaseModel, cls).cloudformation_type()

@staticmethod
def get_deploy_templates():
""" Return template configurations used to create the final API requests (implemented by subclasses). """
Expand Down Expand Up @@ -105,6 +106,10 @@ def get_cfn_attribute(self, attribute_name):
# GENERIC UTIL METHODS
# ----------------------

def set_resource_state(self, state):
""" Set the deployment state of this resource. """
self.state = state or {}

def update_state(self, details):
""" Update the deployment state of this resource (existing attributes will be overwritten). """
details = details or {}
Expand Down
9 changes: 5 additions & 4 deletions localstack/services/edge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from localstack.utils import persistence
import re
import os
import sys
Expand All @@ -9,6 +8,7 @@
import threading
from requests.models import Response
from localstack import config
from localstack.utils import persistence
from localstack.services import plugins
from localstack.dashboard import infra as dashboard_infra
from localstack.utils.aws import aws_stack
Expand Down Expand Up @@ -73,9 +73,10 @@ def forward_request(self, method, path, data, headers):

if api in ['', None, '_unknown_']:
truncated = truncate(data)
LOG.info(('Unable to find forwarding rule for host "%s", path "%s %s", '
'target header "%s", auth header "%s", data "%s"') % (
host, method, path, target, auth_header, truncated))
if auth_header or target or data or path != '/':
LOG.info(('Unable to find forwarding rule for host "%s", path "%s %s", '
'target header "%s", auth header "%s", data "%s"') % (
host, method, path, target, auth_header, truncated))
else:
LOG.info(('Unable to determine forwarding port for API "%s" - please '
'make sure this API is enabled via the SERVICES configuration') % api)
Expand Down
6 changes: 5 additions & 1 deletion localstack/services/generic_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ def get(cls, region=None):
# maps region name to region backend instance
cls.REGIONS = {}

region = region or aws_stack.get_region()
region = region or cls.get_current_request_region()
cls.REGIONS[region] = cls.REGIONS.get(region) or cls()
return cls.REGIONS[region]

@classmethod
def get_current_request_region(cls):
return aws_stack.get_region()


# ---------------------
# PROXY LISTENER UTILS
Expand Down
7 changes: 5 additions & 2 deletions localstack/utils/aws/aws_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,14 @@ def check_valid_region(headers):
raise Exception('Invalid region specified in "Authorization" header: "%s"' % region)


def set_default_region_in_headers(headers):
def set_default_region_in_headers(headers, service=None, region=None):
auth_header = headers.get('Authorization')
region = region or get_region()
if not auth_header:
if service:
headers['Authorization'] = mock_aws_request_headers(service, region_name=region)['Authorization']
return
replaced = re.sub(r'(.*Credential=[^/]+/[^/]+/)([^/])+/', r'\1%s/' % get_region(), auth_header)
replaced = re.sub(r'(.*Credential=[^/]+/[^/]+/)([^/])+/', r'\1%s/' % region, auth_header)
headers['Authorization'] = replaced


Expand Down
2 changes: 1 addition & 1 deletion localstack/utils/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def start_infra_in_docker():

mkdir(config.TMP_FOLDER)
try:
run('chmod -R 777 "%s"' % config.TMP_FOLDER)
run('chmod -R 777 "%s"' % config.TMP_FOLDER, print_error=False)
except Exception:
pass

Expand Down

0 comments on commit 6926d47

Please sign in to comment.