Description
Expected Behaviour
I have defined an apigateway rest resolver with swagger enabled and a security scheme with oauth.
I have two endpoints, one protected, and one unprotected. I explicitly define the unprotected endpoint with security empty, so I would expect it to override the global config of swagger and be unprotected.
Current Behaviour
Currently, the unprotected security of the route is not overwritten. Instead, the global config seems to be applied over the specific one of the router. This only happens if the specific config is an empty list.
However, if the global config of security is an empty list or not defined at all, defining a specific security config in a route overwrites it.
With the current behaviour, if I want to keep all resources protected except one, I need to remove the global config of security, and put it in each route that i want protected.
Code snippet
import json
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.models import OAuth2, OAuthFlows, OAuthFlowAuthorizationCode
app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(
path="/swagger",
security_schemes={
"oauth": OAuth2(
flows=OAuthFlows(
authorizationCode=OAuthFlowAuthorizationCode(
authorizationUrl="",
tokenUrl="",
scopes={
"email": "email scope",
"openid": "openid scope",
"profile": "profile scope",
},
),
),
)
},
security=[{"oauth": ["openid", "profile", "email"]}]
)
@app.get("/unprotected", security=[])
def unprotected() -> dict:
return {}
@app.get("/protected", security=[{"oauth": ["openid", "profile", "email"]}])
def protected() -> dict:
return {}
def lambda_handler(event, context):
return app.resolve(event, context)
if __name__ == "__main__":
test_event = {
"body": json.dumps({}),
"httpMethod": "GET",
"path": "/swagger",
"headers": {
"Content-Type": "application/json"
},
"requestContext": {
"requestId": "test-id",
"stage": "test-stage",
"path": "/swagger",
}
}
result = lambda_handler(test_event, {})
# store swagger in file to be able to check it
with open("swagger.html", "w") as f:
f.write(result["body"])
Possible Solution
Either clarify the docs of the enable_swagger() or make the specific router security options override the global security even if the security is empty.
Steps to Reproduce
- Copy the snippet provided.
- Run it and open the generated swagger.html
- The unprotected endpoint is protected (I expected it to not be protected)
- Remove the global security config from the enable_swagger()
- Rerun and reload the swagger.html
- The unprotected endpoint is now marked as unprotected as expected
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.12
Packaging format used
Lambda Layers
Debugging logs
Metadata
Metadata
Assignees
Type
Projects
Status
Activity
boring-cyborg commentedon Jan 21, 2025
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
leandrodamascena commentedon Jan 21, 2025
Hi @victorperezpiqueras! Thanks for opening this issue because I see room for improvement in our documentation. Actually this is not a bug, we already support it.
The OpenAPI v3 specification defines an optional security mechanism with
{}
, so this means that if you add a security scheme on a specific route like this@app.get("/unprotected", security=[{}])
it will make the authorization optional in that specific route.Reference: https://spec.openapis.org/oas/v3.1.0.html#fixed-fields
Please let me know if this works for you.
victorperezpiqueras commentedon Jan 21, 2025
Ah, my bad I missed that doc.
In that case, maybe the current field description could be extended with the complete description present in the OpenAPI specification:
def get_openapi_schema
:I see the rest of the params have a simplified description too, so idk if its a standard or documentation choice.
leandrodamascena commentedon Jan 21, 2025
I would prefer to add it here: https://docs.powertools.aws.dev/lambda/python/latest/core/event_handler/api_gateway/#security-schemes
github-actions commentedon Jan 22, 2025
This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
github-actions commentedon Jan 28, 2025
This is now released under 3.5.0 version!