Skip to content

Commit e8a342b

Browse files
author
Michael Brewer
committed
refactor: some improvements
1 parent 738f356 commit e8a342b

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

aws_lambda_powertools/utilities/data_classes/api_gateway_authorizer_event.py

+12-18
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ def _add_method(self, effect: str, verb: str, resource: str, conditions: List[Di
393393
the internal list contains a resource ARN and a condition statement. The condition
394394
statement can be null."""
395395
if verb != "*" and not hasattr(HttpVerb, verb):
396-
raise NameError(f"Invalid HTTP verb {verb}. Allowed verbs in HttpVerb class")
396+
raise ValueError(f"Invalid HTTP verb {verb}. Allowed verbs in HttpVerb class")
397397

398398
resource_pattern = re.compile(self.path_regex)
399399
if not resource_pattern.match(resource):
400-
raise NameError(f"Invalid resource path: {resource}. Path should match {self.path_regex}")
400+
raise ValueError(f"Invalid resource path: {resource}. Path should match {self.path_regex}")
401401
if resource[:1] == "/":
402402
resource = resource[1:]
403403

@@ -445,35 +445,29 @@ def deny_all_methods(self):
445445
"""Adds a '*' allow to the policy to deny access to all methods of an API"""
446446
self._add_method("Deny", HttpVerb.ALL, "*", [])
447447

448-
def allow_method(self, verb, resource: str):
448+
def allow_method(self, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
449449
"""Adds an API Gateway method (Http verb + Resource path) to the list of allowed
450-
methods for the policy"""
451-
self._add_method("Allow", verb, resource, [])
450+
methods for the policy.
452451
453-
def deny_method(self, verb: str, resource: str):
454-
"""Adds an API Gateway method (Http verb + Resource path) to the list of denied
455-
methods for the policy"""
456-
self._add_method("Deny", verb, resource, [])
457-
458-
def allow_method_with_conditions(self, verb: str, resource: str, conditions: List[Dict]):
459-
"""Adds an API Gateway method (Http verb + Resource path) to the list of allowed
460-
methods and includes a condition for the policy statement. More on AWS policy
452+
Optionally includes a condition for the policy statement. More on AWS policy
461453
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
462-
self._add_method("Allow", verb, resource, conditions)
454+
self._add_method("Allow", http_method, resource, conditions or [])
463455

464-
def deny_method_with_conditions(self, verb: str, resource: str, conditions: List[Dict]):
456+
def deny_method(self, http_method: str, resource: str, conditions: Optional[List[Dict]] = None):
465457
"""Adds an API Gateway method (Http verb + Resource path) to the list of denied
466-
methods and includes a condition for the policy statement. More on AWS policy
458+
methods for the policy.
459+
460+
Optionally includes a condition for the policy statement. More on AWS policy
467461
conditions here: https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements.html#Condition"""
468-
self._add_method("Deny", verb, resource, conditions)
462+
self._add_method("Deny", http_method, resource, conditions or [])
469463

470464
def asdict(self) -> Dict[str, Any]:
471465
"""Generates the policy document based on the internal lists of allowed and denied
472466
conditions. This will generate a policy with two main statements for the effect:
473467
one statement for Allow and one statement for Deny.
474468
Methods that includes conditions will have their own statement in the policy."""
475469
if len(self._allow_methods) == 0 and len(self._deny_methods) == 0:
476-
raise NameError("No statements defined for the policy")
470+
raise ValueError("No statements defined for the policy")
477471

478472
response: Dict[str, Any] = {
479473
"principalId": self.principal_id,

tests/functional/data_classes/test_api_gateway_authorizer.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def builder():
1313

1414
def test_authorizer_response_no_statement(builder: APIGatewayAuthorizerResponse):
1515
# GIVEN a builder with no statements
16-
with pytest.raises(NameError) as ex:
16+
with pytest.raises(ValueError) as ex:
1717
# WHEN calling build
1818
builder.asdict()
1919

@@ -22,7 +22,7 @@ def test_authorizer_response_no_statement(builder: APIGatewayAuthorizerResponse)
2222

2323

2424
def test_authorizer_response_invalid_verb(builder: APIGatewayAuthorizerResponse):
25-
with pytest.raises(NameError) as ex:
25+
with pytest.raises(ValueError) as ex:
2626
# GIVEN a invalid http_method
2727
# WHEN calling deny_method
2828
builder.deny_method("INVALID", "foo")
@@ -32,7 +32,7 @@ def test_authorizer_response_invalid_verb(builder: APIGatewayAuthorizerResponse)
3232

3333

3434
def test_authorizer_response_invalid_resource(builder: APIGatewayAuthorizerResponse):
35-
with pytest.raises(NameError) as ex:
35+
with pytest.raises(ValueError) as ex:
3636
# GIVEN a invalid resource path "$"
3737
# WHEN calling deny_method
3838
builder.deny_method(HttpVerb.GET, "$")
@@ -112,7 +112,7 @@ def test_authorizer_response_deny_method(builder: APIGatewayAuthorizerResponse):
112112

113113

114114
def test_authorizer_response_allow_method_with_conditions(builder: APIGatewayAuthorizerResponse):
115-
builder.allow_method_with_conditions(
115+
builder.allow_method(
116116
HttpVerb.POST,
117117
"/foo",
118118
[
@@ -136,7 +136,7 @@ def test_authorizer_response_allow_method_with_conditions(builder: APIGatewayAut
136136

137137

138138
def test_authorizer_response_deny_method_with_conditions(builder: APIGatewayAuthorizerResponse):
139-
builder.deny_method_with_conditions(
139+
builder.deny_method(
140140
HttpVerb.POST,
141141
"/foo",
142142
[

0 commit comments

Comments
 (0)