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

fix cors stategy for Java/NodeJs/Python #2001

Merged
merged 11 commits into from
May 2, 2023

Conversation

vhvb1989
Copy link
Member

@vhvb1989 vhvb1989 commented Apr 20, 2023

fix: #1057

This PR re-designs the CORS strategy for the api service.

There are 2 layers where CORS (allowed-orings) can be configured:

  • Infra: The Azure Service takes care of validating the incoming http request and lets only allowed-origins to get to the api server.
  • runtime Server. Once the request passes the infra layer, it is received by the server running on the Azure Service and it can also check allowed-origins.

Not every Azure service supports the CORS configuration on infra layer. For example, Azure Container Apps does not support the CORS configuration on infra layer.

Old stategy

  • Use CORS on infra layer if supported (web-apps) and do not add CORS on runtime server
  • Use CORS on runtime server when it is not supported on infra layer, like Azure container apps.
    • Use web-client url as value for REACT_APP_WEB_BASE_URL which is used by the api server as a valid origin.
  • Allow all origins when the application is running locally (not in Azure services)

Old strategy issues

  • Wrong detection for running api service locally v/s running on Azure Service.

New Strategy

  • Make api service to configure CORS by default and expose an env var to allow customers to opt-out and allow all origins on demand:
    • Using API_ENVIRONMENT=develop makes application to allow all origins on runtime server layer.
    • Update tasks.json and lauch.json to set this env var when lauching the app locally, so CORS is disabled.
    • Document env var in the app Readme for customers who are not using VSCode task to start the api, so they know how to allow all origins.
  • Expose env var: API_ALLOW_ORIGINS as a way for customers to define a list of allowed origins for the runtime service layer.
    • Set API_ALLOW_ORIGINS to include the web client host url when deploying the infrastructure, this values is then used by the runtime server to allow the web client as origin.

Notes

  • If the Azure service supports CORS, the validation will be done by the infra layer and also by the application layer.
  • The application can run locally with CORS enabled and customer can define specific list of allowed origins.
  • For templates using Azure Functions:
    • CORS is only configured on the infra layer
    • As the solution is serverless, there is not a runtime server configured and serving the api backend.
    • On every request to Azure Functions, the api-backend is started to dispatch the request without using a server like tomcat for java or uvicorn in python. Hence, there's no layer to configure cors.

@ghost ghost assigned vhvb1989 Apr 20, 2023
@vhvb1989 vhvb1989 changed the title fix cors set for nodejs server fix cors stategy for Java/NodeJs/Python Apr 21, 2023
@azure-sdk
Copy link
Collaborator

Repoman Generation Results

Repoman pushed changes to remotes for the following projects:

Project: todo-csharp-cosmos-sql

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-csharp-cosmos-sql -b pr/2001

View Changes | Compare Changes


Project: todo-csharp-sql-swa-func

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-csharp-sql-swa-func -b pr/2001

View Changes | Compare Changes


Project: todo-csharp-sql

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-csharp-sql -b pr/2001

View Changes | Compare Changes


Project: todo-java-mongo-aca

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-java-mongo-aca -b pr/2001

View Changes | Compare Changes


Project: todo-java-mongo

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-java-mongo -b pr/2001

View Changes | Compare Changes


Project: todo-nodejs-mongo-aca

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-nodejs-mongo-aca -b pr/2001

View Changes | Compare Changes


Project: todo-nodejs-mongo-aks

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-nodejs-mongo-aks -b pr/2001

View Changes | Compare Changes


Project: todo-nodejs-mongo-swa-func

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-nodejs-mongo-swa-func -b pr/2001

View Changes | Compare Changes


Project: todo-nodejs-mongo

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-nodejs-mongo -b pr/2001

View Changes | Compare Changes


Project: todo-nodejs-mongo-terraform

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-nodejs-mongo-terraform -b pr/2001

View Changes | Compare Changes


Project: todo-python-mongo-aca

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-python-mongo-aca -b pr/2001

View Changes | Compare Changes


Project: todo-python-mongo-swa-func

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-python-mongo-swa-func -b pr/2001

View Changes | Compare Changes


Project: todo-python-mongo

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-python-mongo -b pr/2001

View Changes | Compare Changes


Project: todo-python-mongo-terraform

Remote: azure-samples-staging

Branch: pr/2001

You can initialize this project with:

azd init -t Azure-Samples/todo-python-mongo-terraform -b pr/2001

View Changes | Compare Changes


Copy link
Contributor

@hemarina hemarina left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the great work and the fix! I just have a few comments.

templates/todo/api/java/README.md Show resolved Hide resolved
templates/todo/api/js/src/app.ts Show resolved Hide resolved
templates/todo/api/js/src/app.ts Show resolved Hide resolved
@vhvb1989 vhvb1989 merged commit c323f7e into Azure:main May 2, 2023
@vhvb1989 vhvb1989 deleted the fix-cors-local-run-server branch May 2, 2023 01:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Start api in vscode failed when run vscode tests in todo-python-mongo-swa-func
4 participants