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

[Management] Address UI searching bug with index pattern creation #14646

Merged

Conversation

chrisronline
Copy link
Contributor

Overview

On the index pattern creation wizard, if the user types in a character into the input field, we have code that will automatically append a * character to the end of what they typed and then move the cursor back to right after the typed letter. This experience came from discussions on the original PR.

Unfortunately, there is a bug with how we're transferring state between the angular directives where we weren't properly updating the state before the parent directive accessed it, leading to an initial query against the typed character, instead of the typed character plus the *.

For super users, this isn't a big deal because they aren't any security gates against searching for indices, but it becomes a problem for restricted users who aren't able to query for certain indices.

Take this sample of queries for a restricted user who can only access the logstash index:

POST l/_search -> 403
POST *l/_search -> 200
POST l*/_search -> 200
POST *l*/_search -> 200

The UI bug caused a search for just l instead of l* which resulted in 403 errors in the browser.

This PR addresses that and separates out the code into a separate directive. It also functions off keydown instead of an angular watch due to issues mutating that value post watch firing.

It's very possible this is a hacky and there is a better way, so please let me know!

TODO

  • Tests are hard for this kind of experience. Any recommendations?

}
}

$elem.on('keydown', (e) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a little concerned that this will not always work as intended (all work completing before watcher has a chance to fire). Maybe that is unfounded though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a good point and something I just assumed. I figured that the digest cycle doesn't kickoff until all interactions are "done", and I assumed that any listeners directly listening to UI interacts will be handled prior to anything angular does, but I could be mistaken there

Copy link
Contributor

Choose a reason for hiding this comment

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

Methods run to completion in JS, so this should be good, just being paranoid.

event.preventDefault();
$elem.val(val);
if (moveSelectionRange) {
$elem[0].setSelectionRange(1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

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

I had to think for a minute to grok why this line is doing what it does. Maybe parameter could be named better to make that clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

On it!

@bmcconaghy
Copy link
Contributor

LGTM

}

$elem.on('keydown', (e) => {
const charStr = String.fromCharCode(e.keyCode);
Copy link
Contributor

Choose a reason for hiding this comment

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

MDN warns against using KeyboardEvent.keyCode and recommends using KeyboardEvent.key. However, Chrome 51 was the first to introduce KeyboardEvent.key, so it'd be nice if this "gracefully degrades" in unsupported versions, perhaps by simply not intelligently adding the *.

Copy link
Contributor

Choose a reason for hiding this comment

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

This might be related to the following behavior, if I type Command + R (normally refreshes the page in Chrome on macOS) it's inserting a lower-case "r":

command-r-hijacking

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice call! Addressing the first one fixes the second one


if (indexPatternName && indexPatternName.length === 1) {
if (indexPatternName === '*') {
if ($scope.appendedWildcard) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This causes an interesting behavior, when the page first loads you're allowed to put * as the index-pattern, but if I create an index pattern where we append the * and $scope.appendedWildcard === true, if I try to type * again, it won't do anything, but if I press it again, it'll let me. This appears to be behavior that was carried over from the previous code, so if you'd prefer to create a separate PR/Issue to address this, I won't let it hold up my approval.

asterix

@chrisronline
Copy link
Contributor Author

@kobelb Thanks for the great thoughts! I fixed a couple things and refactored some of the code to hopefully avoid some of the confusion/issues. Lemme know

return;
}

// If the user is holdinng down ctrl/cmd, they are performing some shortcut
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Bah, typo in the comment


let indexPatternName = $elem.val() + e.key;
if (indexPatternName && indexPatternName.length === 1) {
if (indexPatternName !== '*') {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can be combined in the above if

Copy link
Contributor

@kobelb kobelb left a comment

Choose a reason for hiding this comment

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

LGTM

@chrisronline chrisronline force-pushed the fix/index_pattern_creation_wizard branch from 281d3ab to f84e4be Compare November 13, 2017 18:22
@chrisronline chrisronline merged commit a5bd0af into elastic:master Nov 13, 2017
chrisronline added a commit to chrisronline/kibana that referenced this pull request Nov 13, 2017
…astic#14646)

* Move this logic to a separate directive. Also fix a couple bugs with it around how it interacts with angular.

* Updated variable name

* Handle some edge cases and simplify code

* Fixing typo and further simplyfing code
chrisronline added a commit that referenced this pull request Nov 13, 2017
…4646) (#14922)

* Move this logic to a separate directive. Also fix a couple bugs with it around how it interacts with angular.

* Updated variable name

* Handle some edge cases and simplify code

* Fixing typo and further simplyfing code
@chrisronline
Copy link
Contributor Author

Backport

6.x: 076cea1

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.

3 participants