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

[console] Replace text/plain fallback with application/json #12294

Merged
merged 3 commits into from
Aug 18, 2017

Conversation

jbudz
Copy link
Member

@jbudz jbudz commented Jun 12, 2017

This ensures that all elasticsearch queries sent from console that have a content-type set it to either application/json or application/x-ndjson.

Closes #10504

@jbudz
Copy link
Member Author

jbudz commented Jun 13, 2017

jenkins, test it

@jbudz jbudz added the v5.6.0 label Jun 13, 2017
Copy link
Contributor

@epixa epixa left a comment

Choose a reason for hiding this comment

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

This all seems really hacky to me, and it is brittle enough that I worry about us introducing regressions in the future.

Line 20 will split valid JSON if a string in the JSON includes a valid newline character. The only reason this works out OK is that a future line (line 22) will throw an exception when it tries to parse each individual "line", which happens to now be broken, and the error handling for line 22 happens to fallback to application/json.

I'm trying not to get too hung up on it since the existing implementation isn't immune to this flakiness either, but I'd feel more comfortable if there were some unit tests around this behavior.

@jbudz
Copy link
Member Author

jbudz commented Jun 13, 2017

Understandable, I will say that case was intentional but is hidden. In the case of multiline json, each line must be a valid json object. I'll get some tests together and see if I can make it more clear.

@jbudz
Copy link
Member Author

jbudz commented Jun 19, 2017

Added tests, reminder note that console tests are available at http://localhost:5601/app/sense-tests

@jbudz jbudz changed the title [console] Always set content-type for requests to es [console] Replace text/plain fallback with application/json Jul 17, 2017
@jbudz
Copy link
Member Author

jbudz commented Jul 17, 2017

I updated this, it only addresses #10504 now.

foo: 'bar'
}, null, 2));

equal(contentType, APPLICATION_JSON);
Copy link

Choose a reason for hiding this comment

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

I don't get it. This case adds newlines and spaces and yet the contenttype should be the one without _NEWLINE_?

Copy link
Member Author

Choose a reason for hiding this comment

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

x-ndjson requires each line to be a valid json object. if there's lines that aren't parseable then we use application/json

Copy link

Choose a reason for hiding this comment

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

Ah, thanks 👍

contentType = 'application/x-ndjson';
}
catch (e) {
contentType = 'application/json';
Copy link

@rhoboat rhoboat Jul 21, 2017

Choose a reason for hiding this comment

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

Are we absolutely 110% sure that it should always be this contentType when we fail for any reason?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, elasticsearch won't accept content types outside of x-ndjson and json. it was text/plain before to prevent proxies and load balancers from parsing it, but in practice this also prevents us from getting detailed error messages back from es

Copy link

Choose a reason for hiding this comment

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

this also prevents us from getting detailed error messages back from es

Is this desired?

Copy link
Member Author

Choose a reason for hiding this comment

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

imo, #13423 has an example of this case

try {
// there is more than one line
const lines = body.split('\n').filter(Boolean);
if (!lines.length) throw new Error('not multiline json')
Copy link
Contributor

Choose a reason for hiding this comment

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

Calling split with a substring that doesn't have any matches results in an array of length 1, so this multiline error check will never fire. As a result, even a simple:

POST logstash-*/_search
{}

results in an error

Copy link
Contributor

@epixa epixa left a comment

Choose a reason for hiding this comment

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

The logic here has some issues to sort out, I left a comment inline.

@jbudz
Copy link
Member Author

jbudz commented Aug 9, 2017

This is good for review again.

@tylersmalley tylersmalley self-requested a review August 10, 2017 19:27
Copy link
Contributor

@tylersmalley tylersmalley left a comment

Choose a reason for hiding this comment

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

LGTM: 437 tests of 437 passed, 0 failed.
Fixes #13423

@epixa
Copy link
Contributor

epixa commented Aug 11, 2017

@jbudz You mentioned wanting to just send application/json always. Do you still want to do that? If so, then we probably shouldn't bother moving forward with this PR

Copy link
Contributor

@tylersmalley tylersmalley left a comment

Choose a reason for hiding this comment

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

Most recent changes LGTM.

@tylersmalley
Copy link
Contributor

@epixa, mind taking another pass at this?

@epixa
Copy link
Contributor

epixa commented Aug 17, 2017

LGTM

@jbudz jbudz merged commit 61c3f38 into elastic:master Aug 18, 2017
jbudz added a commit that referenced this pull request Aug 18, 2017
* [console] Always set content-type for requests to es

* [console] always set content type to application/json if there is a body

* remove extra space
jbudz added a commit that referenced this pull request Aug 18, 2017
* [console] Always set content-type for requests to es

* [console] always set content type to application/json if there is a body

* remove extra space
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants