File tree 3 files changed +16
-14
lines changed
aws_lambda_powertools/event_handler
tests/functional/event_handler
3 files changed +16
-14
lines changed Original file line number Diff line number Diff line change 46
46
from aws_lambda_powertools .event_handler .util import (
47
47
_FrozenDict ,
48
48
_FrozenListDict ,
49
+ _validate_openapi_security_parameters ,
49
50
extract_origin_header ,
50
- validate_openapi_security_parameters ,
51
51
)
52
52
from aws_lambda_powertools .shared .cookies import Cookie
53
53
from aws_lambda_powertools .shared .functions import powertools_dev_is_set
@@ -1595,7 +1595,7 @@ def get_openapi_schema(
1595
1595
# Add routes to the OpenAPI schema
1596
1596
for route in all_routes :
1597
1597
1598
- if route .security and not validate_openapi_security_parameters (
1598
+ if route .security and not _validate_openapi_security_parameters (
1599
1599
security = route .security ,
1600
1600
security_schemes = security_schemes ,
1601
1601
):
@@ -1649,7 +1649,7 @@ def _get_openapi_security(
1649
1649
if not security :
1650
1650
return None
1651
1651
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 ):
1653
1653
raise SchemaValidationError (
1654
1654
"Security configuration was not found in security_schemas or security_schema was not defined." ,
1655
1655
)
Original file line number Diff line number Diff line change @@ -71,12 +71,14 @@ def extract_origin_header(resolver_headers: Dict[str, Any]):
71
71
return resolved_header
72
72
73
73
74
- def validate_openapi_security_parameters (
74
+ def _validate_openapi_security_parameters (
75
75
security : List [Dict [str , List [str ]]],
76
76
security_schemes : Optional [Dict [str , "SecurityScheme" ]],
77
77
) -> bool :
78
78
"""
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.
80
82
81
83
Parameters
82
84
----------
@@ -88,11 +90,11 @@ def validate_openapi_security_parameters(
88
90
Returns
89
91
-------
90
92
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.
94
94
"""
95
95
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 )
Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ def handler():
49
49
raise NotImplementedError ()
50
50
51
51
# 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
53
53
# THEN a SchemaValidationError should be raised
54
54
with pytest .raises (SchemaValidationError ):
55
55
app .get_openapi_schema (
@@ -80,7 +80,7 @@ def test_openapi_operation_level_security_missing():
80
80
# GIVEN an APIGatewayRestResolver instance
81
81
app = APIGatewayRestResolver ()
82
82
83
- # WHEN we define a security in operation
83
+ # AND a route with a security scheme defined
84
84
@app .get ("/" , security = [{"apiKey" : []}])
85
85
def handler ():
86
86
raise NotImplementedError ()
@@ -95,7 +95,7 @@ def test_openapi_operation_level_security_mismatch(security_scheme):
95
95
# GIVEN an APIGatewayRestResolver instance
96
96
app = APIGatewayRestResolver ()
97
97
98
- # WHEN we define a security in operation with value HTTPBearer
98
+ # AND a route with a security scheme using HTTPBearer
99
99
@app .get ("/" , security = [{"HTTPBearer" : []}])
100
100
def handler ():
101
101
raise NotImplementedError ()
You can’t perform that action at this time.
0 commit comments