kibana/x-pack/plugins
John Schulz 0364e8f5aa
[Fleet] Fix error when searching for keys whose names have spaces (#100056)
## Summary
fixes #99895

Can reproduce #99895 with something like
```shell
curl 'http://localhost:5601/api/fleet/enrollment-api-keys' \
  -H 'content-type: application/json' \
  -H 'kbn-version: 8.0.0' \
  -u elastic:changeme \
  --data-raw '{"name":"with spaces","policy_id":"d6a93200-b1bd-11eb-90ac-052b474d74cd"}'
```

Kibana logs this stack trace

```
server    log   [10:57:07.863] [error][fleet][plugins] KQLSyntaxError: Expected AND, OR, end of input but "\" found.
policy_id:"d6a93200-b1bd-11eb-90ac-052b474d74cd" AND name:with\ spaces*
--------------------------------------------------------------^
    at Object.fromKueryExpression (/Users/jfsiii/work/kibana/src/plugins/data/common/es_query/kuery/ast/ast.ts:52:13)
    at listEnrollmentApiKeys (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts:37:69)
    at Object.generateEnrollmentAPIKey (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts:160:31)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at postEnrollmentApiKeyHandler (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts:53:20)
    at Router.handle (/Users/jfsiii/work/kibana/src/core/server/http/router/router.ts:273:30)
    at handler (/Users/jfsiii/work/kibana/src/core/server/http/router/router.ts:228:11)
    at exports.Manager.execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
    at Object.internals.handler (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/handler.js:46:20)
    at exports.execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/handler.js:31:20)
    at Request._lifecycle (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/request.js:370:32)
    at Request._execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/request.js:279:9) {
  shortMessage: 'Expected AND, OR, end of input but "\\" found.'
```

the `kuery` value which causes the `KQLSyntaxError` is
```
policy_id:\"d6a93200-b1bd-11eb-90ac-052b474d74cd\" AND name:with\\ spaces*
``` 

a value without spaces, e.g. `no_spaces` 

```
policy_id:\"d6a93200-b1bd-11eb-90ac-052b474d74cd\" AND name:no_spaces*
```

is converted to this query object

```
{
  "bool": {
    "filter": [
      {
        "bool": {
          "should": [
            {
              "match_phrase": {
                "policy_id": "d6a93200-b1bd-11eb-90ac-052b474d74cd"
              }
            }
          ],
          "minimum_should_match": 1
        }
      },
      {
        "bool": {
          "should": [
            {
              "query_string": {
                "fields": [
                  "name"
                ],
                "query": "no_spaces*"
              }
            }
          ],
          "minimum_should_match": 1
        }
      }
    ]
  }
```

I tried some other approaches for handling the spaces based on what I saw in the docs like `name:"\"with spaces\"` and `name:(with spaces)*`but they all failed as well, like

```
KQLSyntaxError: Expected AND, OR, end of input but "*" found.
policy_id:"d6a93200-b1bd-11eb-90ac-052b474d74cd" AND name:(with spaces)*
-----------------------------------------------------------------------^
    at Object.fromKueryExpression (/Users/jfsiii/work/kibana/src/plugins/data/common/es_query/kuery/ast/ast.ts:52:13)
    at listEnrollmentApiKeys (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts:37:69)
    at Object.generateEnrollmentAPIKey (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/services/api_keys/enrollment_api_key.ts:166:31)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at postEnrollmentApiKeyHandler (/Users/jfsiii/work/kibana/x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts:53:20)
    at Router.handle (/Users/jfsiii/work/kibana/src/core/server/http/router/router.ts:273:30)
    at handler (/Users/jfsiii/work/kibana/src/core/server/http/router/router.ts:228:11)
    at exports.Manager.execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/toolkit.js:60:28)
    at Object.internals.handler (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/handler.js:46:20)
    at exports.execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/handler.js:31:20)
    at Request._lifecycle (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/request.js:370:32)
    at Request._execute (/Users/jfsiii/work/kibana/node_modules/@hapi/hapi/lib/request.js:279:9) {
  shortMessage: 'Expected AND, OR, end of input but "*" found.'
```

So I logged out the query object for a successful request, and put that in a function

```
{
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "match_phrase": {
                  "policy_id": "d6a93200-b1bd-11eb-90ac-052b474d74cd"
                }
              }
            ],
            "minimum_should_match": 1
          }
        },
        {
          "bool": {
            "should": [
              {
                "query_string": {
                  "fields": [
                    "name"
                  ],
                  "query": "(with spaces) *"
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  }
}
```


### Checklist
- [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios
2021-05-13 17:32:14 -04:00
..
actions [sec/actions] move constants to a separate file to avoid circular deps (#99439) 2021-05-06 06:54:20 -07:00
alerting Rename alert status OK to Recovered and fix some UX issues around disabling a rule while being in an error state (#98135) 2021-05-13 14:16:36 -04:00
apm [RAC] Decouple registry from alerts-as-data client (#98935) 2021-05-13 17:12:47 +02:00
banners Do not mutate config in place during deprecations (#99629) 2021-05-11 05:41:48 -04:00
beats_management Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
canvas [Canvas] Remove unused legacy autocomplete component (#99215) 2021-05-13 15:24:40 -05:00
cases [Security Solution][Endpoint][Host Isolation] Send case ids from UI to isolate api (#99484) 2021-05-13 09:37:56 -04:00
cloud Don't query for the current user on anonymous pages (#99511) 2021-05-06 17:04:56 -04:00
console_extensions Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
cross_cluster_replication [CCR] Unskip component integration tests (#99385) 2021-05-11 08:55:46 -04:00
dashboard_enhanced feat: 🎸 close drilldown manager on view mode switch (#99309) 2021-05-06 00:26:13 +02:00
dashboard_mode Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
data_enhanced [Search Sessions] Fix display of expired session state in management (#98915) 2021-05-04 12:02:17 +02:00
discover_enhanced Disable context menu "Explore underlying data" by default (#98039) 2021-04-26 13:20:43 +02:00
drilldowns Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
embeddable_enhanced feat: 🎸 enable drilldown actions in "edit" mode (#96023) 2021-04-12 16:44:48 +02:00
encrypted_saved_objects [SavedObjects] Add aggregations support (#96292) 2021-04-16 10:40:30 +02:00
enterprise_search [Workplace Search] Fix bug when transitioning to personal dashboard (#100061) 2021-05-13 16:16:28 -05:00
event_log [RAC] Rule registry plugin (#95903) 2021-04-09 10:35:44 +02:00
features [Alerting] Export rules and connectors (#98802) 2021-05-05 11:33:01 -04:00
file_data_visualizer [ML] Data vizualizer: add choropleth map for index and file (#99434) 2021-05-11 13:40:06 -04:00
file_upload [file upload] use index exists API for /internal/file_upload/index_exists route (#98471) 2021-05-05 07:59:29 -06:00
fleet [Fleet] Fix error when searching for keys whose names have spaces (#100056) 2021-05-13 17:32:14 -04:00
global_search Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
global_search_bar Clearing the global search bar will reset suggestions (#88637) 2021-05-13 11:35:03 -04:00
global_search_providers Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
graph Unifying converting listing pages to new layout (#98651) 2021-05-06 18:31:20 -04:00
grokdebugger Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
index_lifecycle_management Reintroduce 96111: Provide guidance of "Custom" allocation behavior in ILM (#99007) 2021-05-03 12:29:57 -07:00
index_management [Index management] Add selected index count (#99652) 2021-05-11 14:10:34 +01:00
infra [Metrics UI] Increase groupBy pagination size on Metric Threshold alerts (#99683) 2021-05-11 17:29:07 -05:00
ingest_pipelines Added CIT test for the fail processor. (#95261) 2021-05-11 16:41:56 -04:00
lens [Lens] visualize in maps button (#98677) 2021-05-12 09:54:34 -06:00
license_api_guard Fix typo in license_api_guard README name and import http server mocks from public interface (#97334) 2021-04-18 20:28:13 -07:00
license_management initial migration (#97795) 2021-04-23 17:14:54 +02:00
licensing Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
lists [Security Solutions][Lists] Trims down list plugin size by breaking out the exception builder into chunks by using react lazy loading (#99989) 2021-05-13 14:05:02 -06:00
logstash migrate logstash plugin to new ES client (#98064) 2021-04-27 10:25:50 +02:00
maps [Maps][Vega] Tidy up mapbox-gl imports (#99733) 2021-05-12 09:33:48 -04:00
metrics_entities [Security Solutions] (Phase 1) Adds an application cache called metrics entities and integrates it within Security Solutions behind a feature flag (#96446) 2021-04-30 12:36:06 -06:00
ml [ML] fix schema definition for job type (#99954) 2021-05-13 00:02:28 +02:00
monitoring [Monitoring] Added missing cgroup memory (#99602) 2021-05-12 11:24:05 -04:00
observability [RAC] Decouple registry from alerts-as-data client (#98935) 2021-05-13 17:12:47 +02:00
osquery [Osquery] Fix Osquery plugin initialization (#99591) 2021-05-10 19:44:01 +02:00
painless_lab Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
remote_clusters [Remote clusters] Fixed skipped test for search bar (#97472) 2021-05-05 13:05:35 +02:00
reporting [Reporting] Relax save requirement for CSV reports (#99313) 2021-05-12 10:32:51 +02:00
rollup Use doc link services in rollups (#99137) 2021-05-10 10:01:19 -07:00
rule_registry [RAC] Decouple registry from alerts-as-data client (#98935) 2021-05-13 17:12:47 +02:00
runtime_fields Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
saved_objects_tagging Add description as title on tag badge (#97109) 2021-04-18 20:49:35 +02:00
searchprofiler Revert "TS Incremental build exclude test files (#95610)" (#96223) 2021-04-05 11:59:26 -07:00
security Gracefully handle malformed index patterns on role management pages (#99918) 2021-05-13 12:25:29 -04:00
security_solution [Security Solutions][Lists] Trims down list plugin size by breaking out the exception builder into chunks by using react lazy loading (#99989) 2021-05-13 14:05:02 -06:00
snapshot_restore [Snapshot and Restore] Remove box shadow from panels (#98292) 2021-04-30 12:30:36 -04:00
spaces Don't retrieve the active space on anonymous pages (#99514) 2021-05-11 12:00:23 -04:00
stack_alerts [Alerting UI] Fixing behavior when trying to render an Index Threshold visualization with invalid data (#99518) 2021-05-10 14:46:01 -04:00
task_manager Do not mutate config in place during deprecations (#99629) 2021-05-11 05:41:48 -04:00
telemetry_collection_xpack [Rollup] Fixes telemetry (#98639) 2021-05-06 17:41:54 +03:00
timelines [plugin-generator] don't generate .eslintrc.js files for internal plugins (#96921) 2021-04-13 07:43:03 -07:00
transform [ML] Transforms: Fix handling of fields with keyword mapping available (#98882) 2021-05-04 16:21:27 +02:00
translations [App Search] Schema view (#99868) 2021-05-12 19:44:28 -04:00
triggers_actions_ui Rename alert status OK to Recovered and fix some UX issues around disabling a rule while being in an error state (#98135) 2021-05-13 14:16:36 -04:00
ui_actions_enhanced Enable custom time ranges for saved searches (#99180) 2021-05-05 16:35:39 +02:00
upgrade_assistant Document plugin API for spaces and spacesOss (#98966) 2021-05-05 10:50:59 -04:00
uptime [Uptime] [Synthetics Integration] ensure that proxy url is not overwritten (#99944) 2021-05-13 16:15:59 -04:00
watcher [Watcher] Migrate to new ES client (#97260) 2021-04-30 10:44:08 +02:00
xpack_legacy Remove legacy ES client usages in home and xpack_legacy (#97359) 2021-04-18 20:42:07 +02:00