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: add CORS support for API Gateway and clean up configuration #24

Merged
merged 1 commit into from
Nov 25, 2024

Conversation

italopessoa
Copy link
Member

No description provided.

Copy link

coderabbitai bot commented Nov 25, 2024

Walkthrough

The changes in the pull request involve modifications to the Terraform configuration for an AWS API Gateway, specifically to implement CORS (Cross-Origin Resource Sharing) support. New resources are added to handle OPTIONS requests, including a method for CORS, an integration with a MOCK type, and corresponding method and integration responses. Minor formatting adjustments are also made, along with comments on the deployment stage and resource count, indicating potential management changes.

Changes

File Path Change Summary
modules/rest_api/main.tf - Added aws_api_gateway_method "cors_options" for handling OPTIONS requests.
- Added aws_api_gateway_integration "cors_integration" with MOCK integration type.
- Added aws_api_gateway_method_response "cors_method_response" to define CORS response.
- Added aws_api_gateway_integration_response "cors_integration_response" for CORS integration response.
- Minor formatting changes and comments on deployment stage and resource count.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
3 Security Hotspots

See analysis details on SonarQube Cloud

Copy link

Terraform Cloud Plan Output

```
Plan: 40 to add, 0 to change, 0 to destroy.
```
[Terraform Cloud Plan](https://app.terraform.io/app/FiapPostech-SOAT/workspaces/bmb-authenticator/runs/run-rrjiygPkXte8RogW)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
modules/rest_api/main.tf (2)

86-86: Verify stage management approach

The stage will now be created unconditionally (count = 0 removed), and the stage name is hardcoded to "dev". Consider using variables for the stage name to support multiple environments.

- stage_name    = "dev"#aws_api_gateway_deployment.dev.stage_name
+ stage_name    = var.environment

Also applies to: 88-88


121-177: CORS implementation structure looks good

The CORS implementation includes all necessary components (method, integration, responses) and follows AWS best practices. Consider moving the CORS configuration to a separate file for better organization.

Consider creating a separate cors.tf file in the same module to improve code organization.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 858f240 and 2873a88.

📒 Files selected for processing (1)
  • modules/rest_api/main.tf (3 hunks)
🔇 Additional comments (2)
modules/rest_api/main.tf (2)

46-47: LGTM: Formatting changes are consistent

The spacing adjustments follow Terraform style conventions and improve readability.

Also applies to: 51-51, 54-54


162-166: ⚠️ Potential issue

Remove hard-coded dependencies

The integration response has hard-coded dependencies on specific method responses ("production", "orders", "payment"). This makes the module less reusable and maintainable.

-  depends_on = [
-    aws_api_gateway_method_response.cors_method_response["production"],
-    aws_api_gateway_method_response.cors_method_response["orders"],
-    aws_api_gateway_method_response.cors_method_response["payment"]
-  ]
+  depends_on = [aws_api_gateway_method_response.cors_method_response]

Likely invalid or redundant comment.

@@ -72,20 +72,20 @@ resource "aws_api_gateway_vpc_link" "vpc_link" {
resource "aws_api_gateway_deployment" "dev" {
depends_on = [aws_api_gateway_integration.integrations]
rest_api_id = aws_api_gateway_rest_api.api_gtw.id
stage_name = "dev"
# stage_name = "dev"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Stage configuration needs attention - potential deployment issues

The verification reveals inconsistencies in the stage configuration:

  • The stage resource has a commented-out count = 0 which could prevent stage creation
  • The stage_name reference contains a mix of hardcoded "dev" and a commented reference to the deployment's stage_name
  • The deployment resource has its stage_name commented out, but there's no clear indication of whether this is intentional
🔗 Analysis chain

Verify deployment strategy changes

The stage name has been commented out in the deployment resource and moved to the stage resource. The triggers configuration ensures proper redeployment when the API changes.

Also applies to: 80-82

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for any other deployment resources or stage configurations
rg -A 5 "aws_api_gateway_deployment|aws_api_gateway_stage" --type tf

Length of output: 1133

Comment on lines +172 to +176
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent,Cpf'"
"method.response.header.Access-Control-Allow-Methods" = "'GET,PUT,POST,PATCH,OPTIONS'"
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
Copy link

@coderabbitai coderabbitai bot Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Review CORS security configuration

Several security considerations in the CORS configuration:

  1. Using '*' for Allow-Origin is overly permissive. Consider restricting to specific domains.
  2. The Allow-Methods header should match your actual API methods.
  3. Consider adding security headers like CORS-Safelisted-Request-Headers.
   response_parameters = {
     "method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent,Cpf'"
-    "method.response.header.Access-Control-Allow-Methods" = "'GET,PUT,POST,PATCH,OPTIONS'"
-    "method.response.header.Access-Control-Allow-Origin"  = "'*'"
+    "method.response.header.Access-Control-Allow-Methods" = "'GET,POST,OPTIONS'"
+    "method.response.header.Access-Control-Allow-Origin"  = "var.allowed_origins"
+    "method.response.header.Access-Control-Max-Age" = "'3600'"
   }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodeRabbit create a issue for it

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@italopessoa Issue criada para acompanhar essa melhoria: #25

🔗 Analysis chain

@italopessoa Entendido, vou criar uma issue para acompanhar essa melhoria.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
gh issue create \
  --title "Rever configuração de segurança CORS" \
  --body $'Conforme discutido no PR #24 (https://github.com/soat-fiap/bmb.authenticator/pull/24), no comentário (https://github.com/soat-fiap/bmb.authenticator/pull/24#discussion_r1857103500), há considerações de segurança na configuração do CORS que precisam ser abordadas.\n\n- Uso de \'*\' em Allow-Origin é excessivamente permissivo. Considere restringir a domínios específicos.\n- O header Allow-Methods deve corresponder aos métodos reais da API.\n- Considere adicionar headers de segurança como CORS-Safelisted-Request-Headers.\n\nSolicitado por @italopessoa.' \
  --assignee "italopessoa"

Length of output: 707

@italopessoa italopessoa merged commit d483191 into main Nov 25, 2024
5 of 6 checks passed
@italopessoa italopessoa deleted the add_cors branch December 2, 2024 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant