Skip to content

Commit fd306fa

Browse files
author
Michael Brewer
committed
feat(data-classes): add from_route_arn helper
Add `from_route_arn` to build APIGatewayAuthorizerResponse Add a relaxed pylintrc
1 parent 2654d73 commit fd306fa

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

.pylintrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[MESSAGES CONTROL]
2+
disable=
3+
too-many-arguments,
4+
too-many-instance-attributes,
5+
too-few-public-methods,
6+
anomalous-backslash-in-string,
7+
missing-class-docstring,
8+
missing-module-docstring,
9+
missing-function-docstring,
10+
11+
[FORMAT]
12+
max-line-length=120

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,12 @@ def raw_query_string(self) -> str:
234234

235235
@property
236236
def cookies(self) -> List[str]:
237+
"""Cookies"""
237238
return self["cookies"]
238239

239240
@property
240241
def headers(self) -> Dict[str, str]:
242+
"""Http headers"""
241243
return self["headers"]
242244

243245
@property
@@ -314,6 +316,8 @@ def asdict(self) -> dict:
314316

315317

316318
class HttpVerb(enum.Enum):
319+
"""Enum of http methods / verbs"""
320+
317321
GET = "GET"
318322
POST = "POST"
319323
PUT = "PUT"
@@ -405,7 +409,25 @@ def __init__(
405409
self.usage_identifier_key = usage_identifier_key
406410
self._allow_routes: List[Dict] = []
407411
self._deny_routes: List[Dict] = []
408-
self.resource_pattern = re.compile(self.path_regex)
412+
self._resource_pattern = re.compile(self.path_regex)
413+
414+
@staticmethod
415+
def from_route_arn(
416+
arn: str,
417+
principal_id: str,
418+
context: Optional[Dict] = None,
419+
usage_identifier_key: Optional[str] = None,
420+
) -> "APIGatewayAuthorizerResponse":
421+
parsed_arn = parse_api_gateway_arn(arn)
422+
return APIGatewayAuthorizerResponse(
423+
principal_id,
424+
parsed_arn.region,
425+
parsed_arn.aws_account_id,
426+
parsed_arn.api_id,
427+
parsed_arn.stage,
428+
context,
429+
usage_identifier_key,
430+
)
409431

410432
def _add_route(self, effect: str, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
411433
"""Adds a route to the internal lists of allowed or denied routes. Each object in
@@ -415,7 +437,7 @@ def _add_route(self, effect: str, http_method: str, resource: str, conditions: O
415437
allowed_values = [verb.value for verb in HttpVerb]
416438
raise ValueError(f"Invalid HTTP verb: '{http_method}'. Use either '{allowed_values}'")
417439

418-
if not self.resource_pattern.match(resource):
440+
if not self._resource_pattern.match(resource):
419441
raise ValueError(f"Invalid resource path: {resource}. Path should match {self.path_regex}")
420442

421443
if resource[:1] == "/":

tests/functional/data_classes/test_api_gateway_authorizer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def test_authorizer_response_invalid_resource(builder: APIGatewayAuthorizerRespo
3737

3838

3939
def test_authorizer_response_allow_all_routes_with_context():
40-
builder = APIGatewayAuthorizerResponse("foo", "us-west-1", "123456789", "fantom", "dev", context={"name": "Foo"})
40+
arn = "arn:aws:execute-api:us-west-1:123456789:fantom/dev/GET/foo"
41+
builder = APIGatewayAuthorizerResponse.from_route_arn(arn, principal_id="foo", context={"name": "Foo"})
4142
builder.allow_all_routes()
4243
assert builder.asdict() == {
4344
"principalId": "foo",
@@ -56,7 +57,8 @@ def test_authorizer_response_allow_all_routes_with_context():
5657

5758

5859
def test_authorizer_response_allow_all_routes_with_usage_identifier_key():
59-
builder = APIGatewayAuthorizerResponse("cow", "us-east-1", "1111111111", "api", "dev", usage_identifier_key="key")
60+
arn = "arn:aws:execute-api:us-east-1:1111111111:api/dev/ANY/y"
61+
builder = APIGatewayAuthorizerResponse.from_route_arn(arn, principal_id="cow", usage_identifier_key="key")
6062
builder.allow_all_routes()
6163
assert builder.asdict() == {
6264
"principalId": "cow",

0 commit comments

Comments
 (0)