Skip to content

Commit

Permalink
APIGW: Fix tests to run with non-default account ID (localstack#9690)
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni authored Dec 11, 2023
1 parent a4f0a22 commit 215fa1b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 45 deletions.
6 changes: 4 additions & 2 deletions localstack/services/apigateway/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,8 +585,10 @@ def get_stage_variables(context: ApiInvocationContext) -> Optional[Dict[str, str
if not context.stage:
return {}

_, region_name = get_api_account_id_and_region(context.api_id)
api_gateway_client = connect_to(region_name=region_name).apigateway
account_id, region_name = get_api_account_id_and_region(context.api_id)
api_gateway_client = connect_to(
aws_access_key_id=account_id, region_name=region_name
).apigateway
try:
response = api_gateway_client.get_stage(restApiId=context.api_id, stageName=context.stage)
return response.get("variables")
Expand Down
10 changes: 7 additions & 3 deletions localstack/services/apigateway/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ def get_source_arn(invocation_context: ApiInvocationContext):
def call_lambda(
function_arn: str, event: bytes, asynchronous: bool, invocation_context: ApiInvocationContext
) -> str:
region_name = extract_region_from_arn(function_arn)
clients = get_service_factory(
region_name=region_name, role_arn=invocation_context.integration.get("credentials")
region_name=extract_region_from_arn(function_arn),
role_arn=invocation_context.integration.get("credentials"),
)
inv_result = clients.lambda_.request_metadata(
service_principal=ServicePrincipal.apigateway, source_arn=get_source_arn(invocation_context)
Expand Down Expand Up @@ -754,7 +754,11 @@ def invoke(self, invocation_context: ApiInvocationContext):
else:
payload = json.loads(invocation_context.data)

client = connect_to().stepfunctions
client = get_service_factory(
region_name=invocation_context.region_name,
role_arn=invocation_context.integration.get("credentials"),
).stepfunctions

if isinstance(payload.get("input"), dict):
payload["input"] = json.dumps(payload["input"])

Expand Down
8 changes: 7 additions & 1 deletion localstack/utils/testutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def connect_api_gateway_to_http_with_lambda_proxy(
auth_creator_func=None,
http_method=None,
client=None,
role_arn: str = None,
):
if methods is None:
methods = []
Expand All @@ -304,12 +305,15 @@ def connect_api_gateway_to_http_with_lambda_proxy(

for method in methods:
int_meth = http_method or method
integration = {"type": "AWS_PROXY", "uri": target_uri, "httpMethod": int_meth}
if role_arn:
integration["credentials"] = role_arn
resources[resource_path].append(
{
"httpMethod": method,
"authorizationType": auth_type,
"authorizerId": None,
"integrations": [{"type": "AWS_PROXY", "uri": target_uri, "httpMethod": int_meth}],
"integrations": [integration],
}
)
return resource_utils.create_api_gateway(
Expand All @@ -332,6 +336,7 @@ def create_lambda_api_gateway_integration(
stage_name=None,
auth_type=None,
auth_creator_func=None,
role_arn: str = None,
):
if methods is None:
methods = []
Expand All @@ -355,6 +360,7 @@ def create_lambda_api_gateway_integration(
methods=methods,
auth_type=auth_type,
auth_creator_func=auth_creator_func,
role_arn=role_arn,
)
return result

Expand Down
5 changes: 5 additions & 0 deletions tests/aws/services/apigateway/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
"Statement": [{"Effect": "Allow", "Action": "lambda:*", "Resource": "*"}],
}

APIGATEWAY_S3_POLICY = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": "s3:*", "Resource": "*"}],
}

APIGATEWAY_DYNAMODB_POLICY = {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Action": "dynamodb:*", "Resource": "*"}],
Expand Down
Loading

0 comments on commit 215fa1b

Please sign in to comment.