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

Allow UI to query client directly for task logs/state #6669

Merged
merged 1 commit into from
Nov 20, 2019

Conversation

notnoop
Copy link
Contributor

@notnoop notnoop commented Nov 11, 2019

Nomad web UI currently fails when querying client nodes for allocation
state end endpoints, due to CORS policy.

The issue is that CORS requests that are marked withCredentials need
the http server to include a Access-Control-Allow-Credentials [1].

But Nomad Task Logs and filesystem requests include authenticating
information and thus marked with credentials=true[2][3].

It's worth noting that the browser currently sends credentials and
authentication token to servers anyway; it's just that the response is
not made available to caller nomad ui javascript. For task logs
specifically, nomad ui retries again by querying the web ui address
(typically pointing to a nomad server) which will forward the request
to the nomad client agent appropriately.

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
[2]

RSVP.race([this.token.authorizedRequest(url), timeout(timing)]).then(

[3]
// All non Ember Data requests should go through authorizedRequest.
// However, the request that gets regions falls into that category.
// This authorizedRawRequest is necessary in order to fetch data
// with the guarantee of a token but without the automatic region
// param since the region cannot be known at this point.
authorizedRawRequest(url, options = { credentials: 'include' }) {
const headers = {};
const token = this.secret;
if (token) {
headers['X-Nomad-Token'] = token;
}
return fetch(url, assign(options, { headers }));
},

Nomad web UI currently fails when querying client nodes for allocation
state end endpoints, due to CORS policy.

The issue is that CORS requests that are marked `withCredentials` need
the http server to include a `Access-Control-Allow-Credentials` [1].

But Nomad Task Logs and filesystem requests include authenticating
information and thus marked with `credentials=true`[2][3].

It's worth noting that the browser currently sends credentials and
authentication token to servers anyway; it's just that the response is
not made available to caller nomad ui javascript.  For task logs
specifically, nomad ui retries again by querying the web ui address
(typically pointing to a nomad server) which will forward the request
to the nomad client agent appropriately.

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Credentials
[2] https://github.com/hashicorp/nomad/blob/101d0373eec5d58761d05e67e03f38916997a6d2/ui/app/components/task-log.js#L50
[3] https://github.com/hashicorp/nomad/blob/101d0373eec5d58761d05e67e03f38916997a6d2/ui/app/services/token.js#L25-L39
@notnoop notnoop added this to the 0.10.2 milestone Nov 11, 2019
@DingoEatingFuzz
Copy link
Contributor

I'm sure this is a good patch and it works as described, but it's puzzling to me. I swear @schmichael and I worked through this when we put CORS in place originally. Alas I don't remember any of that pairing session.

Maybe @schmichael does?

@notnoop
Copy link
Contributor Author

notnoop commented Nov 12, 2019

Looks like there was a regression. CORS code was added in October 2017 without setting credentials flag in #3414 and #3416. I suspect we broke it incidentally when we added the credentials="include" flag got set later in #3728 in January 2018.

@backspace
Copy link
Contributor

Is it possible to have a test assertion that this header exists, to prevent a regression happening again?

@notnoop
Copy link
Contributor Author

notnoop commented Nov 12, 2019

@backspace What would you suggest? Is there a way to have mirage or frontend tests assert that browsers load the page given those headers?

I feel like adding a backend test simply asserting which headers are set isn't quite meaningful and wouldn't have caught the regression here considering its due to ui changing the request parameters? IMO, an effective test would be probably an integration or an e2e test, but we lack this class of tests now.

Copy link
Contributor

@DingoEatingFuzz DingoEatingFuzz left a comment

Choose a reason for hiding this comment

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

I think this is safe to merge. It would be super cool to do a UI e2e test, but that's not realistic at the moment.

@notnoop notnoop merged commit bbc7b6b into master Nov 20, 2019
@notnoop notnoop deleted the b-cors-allow-credentials branch November 20, 2019 20:14
@github-actions
Copy link

I'm going to lock this pull request because it has been closed for 120 days ⏳. This helps our maintainers find and focus on the active contributions.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants