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

policy-engine/src/main: Set basic CORS headers #196

Merged
merged 2 commits into from Mar 9, 2021

Conversation

wking
Copy link
Member

@wking wking commented Jan 22, 2020

Allow simple requests by including the:

Access-Control-Allow-Origin: *

header, so folks can write cross-origin web applications that query our Cincinnati server. We're going to be building the response anyway, so we don't even get denial-of-service protection by blocking cross-origin requests for Cincy data.

Also set Access-Control-Allow-Methods for good measure, although we do not need to support preflight requests.

Implementation is based on these docs.

@openshift-ci-robot openshift-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Jan 22, 2020
@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 22, 2020
@wking wking force-pushed the cors branch 2 times, most recently from 906508b to 8c8067f Compare January 23, 2020 00:10
@openshift-ci-robot openshift-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Jan 23, 2020
@wking
Copy link
Member Author

wking commented Jan 23, 2020

cargo-test:

 41 | use actix_web::middleware::cors::Cors;
   |                            ^^^^ could not find `cors` in `middleware`

because CORS support moved out of actix-web into its own crate in actix-web 1.0.2. Pushed v906508b -> 8c8067f to pivot to actix-cors ^0.2.0.

@openshift-ci-robot openshift-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 23, 2020
@wking
Copy link
Member Author

wking commented Jan 23, 2020

cargo-test:

 error[E0277]: the trait bound `actix_cors::CorsFactory: actix_service::transform::IntoTransform<_, actix_web::app_service::AppRouting>` is not satisfied
  --> policy-engine/src/main.rs:85:17
   |
85 | /                 Cors::new()
86 | |                     .allowed_origin("*")
87 | |                     .allowed_methods(vec!["HEAD", "GET"])
88 | |                     .finish(),
   | |_____________________________^ the trait `actix_service::transform::IntoTransform<_, actix_web::app_service::AppRouting>` is not implemented for `actix_cors::CorsFactory`

That also turns up here, but I'm not clear enough on the Rust to understand what they're saying there yet. My example looks a lot like the actix-cors docs to me.

Copy link
Contributor

@steveej steveej left a comment

Choose a reason for hiding this comment

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

The used versions of actix-cors and actix-web are incompatible. Please see my suggestions which at least make it compile and run. In addition to that we should also have a test to verify these changes.

policy-engine/Cargo.toml Outdated Show resolved Hide resolved
policy-engine/src/main.rs Outdated Show resolved Hide resolved
@wking
Copy link
Member Author

wking commented Jan 30, 2020

Pushed a8733ab -> c471c49 with @steveej's suggested changes.

@wking
Copy link
Member Author

wking commented Jan 30, 2020

In addition to that we should also have a test to verify these changes.

Not sure how to set this up. Would I plug into here somehow? Looks like at the moment all that has access to is the body of the response, not the full HTTP response with headers and a body.

@steveej
Copy link
Contributor

steveej commented Jan 30, 2020

I think you forgot to push the changes to the Cargo.lock after my recent suggestions.

I played with the tests locally and would like to add a commit on top of your changes if that's fine with you. The tests will still be broken but we can continue to work on it together based on my commit.

@wking
Copy link
Member Author

wking commented Jan 31, 2020

I think you forgot to push the changes to the Cargo.lock...

openshift/release#6935 is in flight to make this easier to do, and impossible to forget (because the required images CI job would fail).

Copy link
Member

@LalatenduMohanty LalatenduMohanty left a comment

Choose a reason for hiding this comment

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

Why do not we have any tests with this PR?

@steveej
Copy link
Contributor

steveej commented Feb 3, 2020

I've been working on getting tests in for this as well as figuring out if this is working correctly in this branch: https://github.com/steveeJ-forks/openshift-cincinnati/tree/forks/wking/cors

However, I can't get the policy-engine to respond to or return any CORS related headers. I suggest we wait for #195 to land which bumps all actix crates to their latest versions. The latest versions have better documentation and we might have more luck with that.

On a side note, I don't fully understand the issue any client would be seeing without this PR. Cincinnati doesn't block any requests for me, no matter what Origin header I provide with the request.

@wking
Copy link
Member Author

wking commented Feb 3, 2020

On a side note, I don't fully understand the issue any client would be seeing without this PR.

CORS is a client-side mechanism. More details in the MDN page linked from my initial PR post, but basically this is Cincy telling browsers "it's ok if a page outside of api.openshift.com to make Cincy requests. That's neither a XSS attack nor malicious data exfiltration.".

@openshift-ci-robot openshift-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 3, 2020
@LalatenduMohanty
Copy link
Member

LalatenduMohanty commented Feb 4, 2020

CORS is a client-side mechanism. More details in the MDN page linked from my initial PR post, but basically this is Cincy telling browsers "it's ok if a page outside of api.openshift.com to make Cincy requests. That's neither a XSS attack nor malicious data exfiltration.".

+1 , basically with this PR we are open to CORS requests from clients which would fail otherwise. AFAIK the typical example is our web browsers which does not allow CORS requests by default.

@wking
Copy link
Member Author

wking commented Feb 4, 2020

Setting this header will avoid the:

CORS header ‘Access-Control-Allow-Origin’ missing

error mentioned in 066d1dc, when Cincinnati is fetched from a page outside of api.openshift.com.

@steveej
Copy link
Contributor

steveej commented Feb 4, 2020

And should Access-Control-Allow-Origin be included in the headers in every GET or HEAD request made to any endpoint?

@wking
Copy link
Member Author

wking commented Feb 11, 2020

And should Access-Control-Allow-Origin be included in the headers in every GET or HEAD request made to any endpoint?

s/request/response/, but sure, why not. Seems academic until we have other endpoints to talk about? I don't care one way or the other about having the header on metrics responses, and don't know if the global setup I have here will affect them or not, since they're on a different port.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

8 similar comments
@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-bot
Copy link

/retest

Please review the full test history for this PR and help us cut down flakes.

@vrutkovs
Copy link
Member

/retest

probe timeout was increased

@vrutkovs
Copy link
Member

Lets

/hold

it for > 4hrs to make it recreate a namespace

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Feb 23, 2021
@vrutkovs
Copy link
Member

Needs rebase due to Cargo.toml changes

@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Feb 26, 2021
@wking
Copy link
Member Author

wking commented Feb 26, 2021

Rebased around #371 and #360, but still overshooting the SLOs. I think I'm probably holding the CORS library wrong...

failures:
    check_slo::_clamp_min_histogram_quantile_0_90_sum_cincinnati_pe_v1_graph_serve_duration_seconds_bucket_by_le_1_1_
    check_slo::_max_over_time_cincinnati_pe_http_upstream_errors_total_1h_0_
    check_slo::_r_min_over_time_up_job_cincinnati_policy_engine_1h_1_

@vrutkovs
Copy link
Member

vrutkovs commented Mar 1, 2021

Still seeing readiness probe failures

/retest

@vrutkovs
Copy link
Member

vrutkovs commented Mar 1, 2021

Timeline:

10:29:03 Successfully assigned cincinnati-e2e/cincinnati-1-ddw22 to ip-10-0-166-203.ec2.internal"

14 seconds for process in the container to start:

10:29:17 DEBUG graph_builder] application settings:
10:29:48 Readiness probe failed: HTTP probe failed with statuscode: 503"
10:29:51 DEBUG graph_builder::graph] graph update completed

So readiness probe doesn't account for the time for binary in the container to start. Its weird that it takes that to start though

policy-engine/src/main.rs Outdated Show resolved Hide resolved
Allow simple requests [1] by including the:

  Access-Control-Allow-Origin: *

header, so folks can write cross-origin web applications that query
our Cincinnati server.  We're going to be building the response
anyway, so we don't even get denial-of-service protection by blocking
cross-origin requests for Cincy data.

Also set Access-Control-Allow-Methods for good measure [2], although
we do not need to support preflight requests.

Implementation is based on [3] (CORS support moved out of actix-web
into its own crate in actix-web 1.0.2 [4]).

[1]: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#Simple_requests
[2]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
[3]: https://docs.rs/actix-cors/0.5.4/actix_cors/
[4]: https://docs.rs/crate/actix-web/1.0.2/source/CHANGES.md
Generated with:

  $ cargo update

using:

  $ cargo --version
  cargo 1.47.0
Copy link
Member

@vrutkovs vrutkovs left a comment

Choose a reason for hiding this comment

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

/lgtm

@vrutkovs
Copy link
Member

vrutkovs commented Mar 9, 2021

/hold cancel

@openshift-ci-robot openshift-ci-robot added lgtm Indicates that a PR is ready to be merged. and removed do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. labels Mar 9, 2021
@openshift-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: PratikMahajan, vrutkovs, wking

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:
  • OWNERS [PratikMahajan,vrutkovs,wking]

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-merge-robot openshift-merge-robot merged commit 35c5f8c into openshift:master Mar 9, 2021
@wking wking deleted the cors branch March 9, 2021 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants