Skip to content
Merged
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
27 changes: 18 additions & 9 deletions samtranslator/model/api/websocket_api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WebSocketApiGenerator(ApiV2Generator):
def __init__( # noqa: PLR0913
self,
logical_id: str,
stage_name: str | None,
stage_name: Intrinsicable[str] | None,
stage_variables: (
dict[str, Intrinsicable[str]] | None
), # I tried to keep presence of = None consistent with http
Expand Down Expand Up @@ -295,14 +295,23 @@ def _construct_permission(self, route_key: str, perms_id: str, route_spec: dict[
perms.Action = "lambda:InvokeFunction"
perms.FunctionName = route_spec["FunctionArn"]
perms.Principal = "apigateway.amazonaws.com"
perms.SourceArn = fnSub(
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${"
+ self.logical_id
+ ".ApiId}/"
+ self.stage_name
+ "/"
+ route_key
)
if isinstance(self.stage_name, str):
perms.SourceArn = fnSub(
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${"
+ self.logical_id
+ ".ApiId}/"
+ self.stage_name
+ "/"
+ route_key
)
else:
perms.SourceArn = fnSub(
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${"
+ self.logical_id
+ ".ApiId}/${__StageName__}/"
+ route_key,
{"__StageName__": self.stage_name},
)
return perms

def _construct_route_infr(self, route_key: str, route_spec: dict[str, Any]) -> tuple[
Expand Down
4 changes: 2 additions & 2 deletions samtranslator/model/sam_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -1910,7 +1910,7 @@ class SamWebSocketApi(SamResourceMacro):
"Routes": PropertyType(True, IS_DICT),
"RouteSettings": PropertyType(False, IS_DICT),
"RouteSelectionExpression": PropertyType(True, IS_STR),
"StageName": PropertyType(False, IS_STR),
"StageName": PropertyType(False, one_of(IS_STR, IS_DICT)),
"StageVariables": PropertyType(False, IS_DICT),
"Tags": PropertyType(False, IS_DICT),
}
Expand All @@ -1930,7 +1930,7 @@ class SamWebSocketApi(SamResourceMacro):
Routes: dict[str, dict[str, Any]]
RouteSettings: dict[str, Any] | None
RouteSelectionExpression: str
StageName: str | None
StageName: Intrinsicable[str] | None
StageVariables: dict[str, Intrinsicable[str]] | None
Tags: dict[str, Any] | None

Expand Down
10 changes: 10 additions & 0 deletions tests/model/api/test_websocket_api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def test_perms(self):
"arn:${AWS::Partition}:execute-api:${AWS::Region}:${AWS::AccountId}:${WebSocketApiId.ApiId}/default/$connect",
)

def test_perms_with_intrinsic_stage_name(self):
"""Test that _construct_permission handles intrinsic StageName without TypeError."""
kwargs = self.kwargs.copy()
kwargs["stage_name"] = {"Ref": "StageName"}
_, _, perm, _ = WebSocketApiGenerator(**kwargs)._construct_route_infr("$connect", kwargs["routes"]["$connect"])
fn_sub = perm.SourceArn["Fn::Sub"]
self.assertIsInstance(fn_sub, list)
self.assertIn("${__StageName__}", fn_sub[0])
self.assertEqual(fn_sub[1]["__StageName__"], {"Ref": "StageName"})

def test_none_auth_no_id(self):
kwargs = self.kwargs.copy()
kwargs["auth_config"] = {"AuthType": "NONE"}
Expand Down
Loading