Skip to content

Commit ce070f3

Browse files
Addressing Heitor's feedback
1 parent 0756711 commit ce070f3

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
from aws_lambda_powertools.event_handler.util import (
4747
_FrozenDict,
4848
_FrozenListDict,
49+
_validate_openapi_security_parameters,
4950
extract_origin_header,
50-
validate_openapi_security_parameters,
5151
)
5252
from aws_lambda_powertools.shared.cookies import Cookie
5353
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
@@ -1595,7 +1595,7 @@ def get_openapi_schema(
15951595
# Add routes to the OpenAPI schema
15961596
for route in all_routes:
15971597

1598-
if route.security and not validate_openapi_security_parameters(
1598+
if route.security and not _validate_openapi_security_parameters(
15991599
security=route.security,
16001600
security_schemes=security_schemes,
16011601
):
@@ -1649,7 +1649,7 @@ def _get_openapi_security(
16491649
if not security:
16501650
return None
16511651

1652-
if not validate_openapi_security_parameters(security=security, security_schemes=security_schemes):
1652+
if not _validate_openapi_security_parameters(security=security, security_schemes=security_schemes):
16531653
raise SchemaValidationError(
16541654
"Security configuration was not found in security_schemas or security_schema was not defined.",
16551655
)

aws_lambda_powertools/event_handler/util.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,14 @@ def extract_origin_header(resolver_headers: Dict[str, Any]):
7171
return resolved_header
7272

7373

74-
def validate_openapi_security_parameters(
74+
def _validate_openapi_security_parameters(
7575
security: List[Dict[str, List[str]]],
7676
security_schemes: Optional[Dict[str, "SecurityScheme"]],
7777
) -> bool:
7878
"""
79-
Validates the security parameters based on the provided security schemes.
79+
This function checks if all security requirements listed in the 'security'
80+
parameter are defined in the 'security_schemes' dictionary, as specified
81+
in the OpenAPI schema.
8082
8183
Parameters
8284
----------
@@ -88,11 +90,11 @@ def validate_openapi_security_parameters(
8890
Returns
8991
-------
9092
bool
91-
True if all security scheme names in the `security` parameter are present in the `security_schemes` parameter,
92-
False otherwise.
93-
93+
Whether list of security schemes match allowed security_schemes.
9494
"""
9595

96-
return bool(
97-
security_schemes and all(key in security_schemes for sec in security for key in sec),
98-
)
96+
security_schemes = security_schemes or {}
97+
98+
security_schema_match = all(key in security_schemes for sec in security for key in sec)
99+
100+
return bool(security_schema_match and security_schemes)

tests/functional/event_handler/test_openapi_security.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def handler():
4949
raise NotImplementedError()
5050

5151
# WHEN the get_openapi_schema method is called with security defined security schemes as APIKey
52-
# WHEN top level security is defined as HTTPBearer
52+
# AND top level security is defined as HTTPBearer
5353
# THEN a SchemaValidationError should be raised
5454
with pytest.raises(SchemaValidationError):
5555
app.get_openapi_schema(
@@ -80,7 +80,7 @@ def test_openapi_operation_level_security_missing():
8080
# GIVEN an APIGatewayRestResolver instance
8181
app = APIGatewayRestResolver()
8282

83-
# WHEN we define a security in operation
83+
# AND a route with a security scheme defined
8484
@app.get("/", security=[{"apiKey": []}])
8585
def handler():
8686
raise NotImplementedError()
@@ -95,7 +95,7 @@ def test_openapi_operation_level_security_mismatch(security_scheme):
9595
# GIVEN an APIGatewayRestResolver instance
9696
app = APIGatewayRestResolver()
9797

98-
# WHEN we define a security in operation with value HTTPBearer
98+
# AND a route with a security scheme using HTTPBearer
9999
@app.get("/", security=[{"HTTPBearer": []}])
100100
def handler():
101101
raise NotImplementedError()

0 commit comments

Comments
 (0)