Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event_handler): add support for OpenAPI security schemes #4103

Merged
merged 22 commits into from
Apr 18, 2024

Conversation

rubenfonseca
Copy link
Contributor

@rubenfonseca rubenfonseca commented Apr 10, 2024

Issue number: #4036

Summary

This PR adds support for OpenAPI security schemes to OpenAPI generation and the Swagger UI.

Changes

Please provide a summary of what's being changed

  • Added support for declaring security schemes when generating OpenAPI schemas and when enabling the Swagger UI
  • Added support for using those schemas globally or per operation
  • Added support for configuring an OAuth2 client when enabling the Swagger UI, and implemented the logic for the OAuth redirect.
  • Updated the documentation

User experience

Please share what the user experience looks like before and after this change

For instance, here's how to declare an OAuth2 security scheme globally and test it on Swagger UI:

from aws_lambda_powertools.event_handler import (
    APIGatewayRestResolver,
)
from aws_lambda_powertools.event_handler.openapi.models import (
    OAuth2,
    OAuthFlowAuthorizationCode,
    OAuthFlows,
)
from aws_lambda_powertools.event_handler.openapi.swagger_ui import OAuth2Config

oauth2 = OAuth2Config(
    client_id="xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    app_name="OAuth2 app",
)

app = APIGatewayRestResolver(enable_validation=True)
app.enable_swagger(
    oauth2_config=oauth2,
    security_schemes={
        "oauth": OAuth2(
            flows=OAuthFlows(
                authorizationCode=OAuthFlowAuthorizationCode(
                    authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
                    tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
                ),
            ),
        ),
    },
    security=[{"oauth": []}],
)
image

TODO

  • add tests for the new code
  • add support for security schemes in the Swagger UI
  • update documentation and examples
  • aws_lambda_powertools/event_handler/api_gateway.py:1429:9: C901 get_openapi_schema is too complex (16 > 15)
  • aws_lambda_powertools/event_handler/api_gateway.py:1429:9: PLR0912 Too many branches (16 > 15)
  • ERROR:xenon:block "aws_lambda_powertools/event_handler/api_gateway.py:1430 get_openapi_schema" has a rank of D
  • Fix pydantic v2 tests
  • Create a sample to test OAuth2 via Swagger

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Apr 10, 2024
@github-actions github-actions bot added the feature New feature or functionality label Apr 10, 2024
@boring-cyborg boring-cyborg bot added the tests label Apr 10, 2024
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 10, 2024
@pull-request-size pull-request-size bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@rubenfonseca rubenfonseca marked this pull request as ready for review April 15, 2024 15:21
@rubenfonseca rubenfonseca requested a review from a team as a code owner April 15, 2024 15:21
@rubenfonseca rubenfonseca linked an issue Apr 15, 2024 that may be closed by this pull request
2 tasks
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 15, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 15, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 17, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 17, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 17, 2024
Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
Signed-off-by: Ruben Fonseca <fonseka@gmail.com>
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 17, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 17, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 18, 2024
@rubenfonseca
Copy link
Contributor Author

@leandrodamascena ready for your review

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 18, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 18, 2024
@rubenfonseca
Copy link
Contributor Author

@leandrodamascena sorry one last time, there was a logic error and we were always printing the warning

@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 18, 2024
Copy link

Quality Gate Passed Quality Gate passed

Issues
2 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
1.8% Duplication on New Code

See analysis details on SonarCloud

@rubenfonseca rubenfonseca changed the title feat(event_handler): add support for OpenAPI security feat(event_handler): add support for OpenAPI security schemes Apr 18, 2024
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Apr 18, 2024
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Apr 18, 2024
@rubenfonseca rubenfonseca merged commit 55713ce into develop Apr 18, 2024
19 checks passed
@rubenfonseca rubenfonseca deleted the rf/4036 branch April 18, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event_handlers feature New feature or functionality openapi-schema size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. tests
Projects
None yet
2 participants