kibana/x-pack/test
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
..
accessibility skip flaky suite (#99006) 2021-05-10 14:40:56 +01:00
alerting_api_integration [actions] adds config allowing per-host networking options (#96630) 2021-04-28 15:26:47 -04:00
api_integration Move functional tests off of legacy es client. (#99801) 2021-05-12 15:21:22 -04:00
api_integration_basic Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
apm_api_integration [RAC] Decouple registry from alerts-as-data client (#98935) 2021-05-13 17:12:47 +02:00
banners_functional add per space configuration to custom header banner (#94449) 2021-03-31 10:57:06 +02:00
case_api_integration [SECURITY SOLUTION] Get case ids from alert ids (#98702) 2021-05-05 07:55:13 +02:00
common Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
detection_engine_api_integration Add ML rule API integration tests and test for removing action (#98100) 2021-05-04 17:42:41 -04:00
encrypted_saved_objects_api_integration [ftr/esArchiver] disable geo_shape field of maps saved objects in archives (#99119) 2021-05-04 10:36:28 -07:00
endpoint_api_integration_no_ingest Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
examples Add partial results to search examples demo (#96366) 2021-05-11 12:41:21 -07:00
fleet_api_integration [Fleet] Fix error when searching for keys whose names have spaces (#100056) 2021-05-13 17:32:14 -04:00
fleet_functional [Fleet] Bootstrap functional test suite (#91898) 2021-02-18 16:34:50 -05:00
functional [RAC] Decouple registry from alerts-as-data client (#98935) 2021-05-13 17:12:47 +02:00
functional_basic [ftr] validate that suites are not in multiple ciGroups (#99398) 2021-05-05 14:50:07 -07:00
functional_cors improve type safety for integration test helpers (#98731) 2021-04-30 05:31:17 -04:00
functional_embedded [Plugins Discovery] Enforce camelCase plugin IDs (#90752) 2021-02-11 14:36:17 +00:00
functional_enterprise_search [App Search] Add delete action to EnginesTable component (#92844) 2021-03-09 09:49:52 -05:00
functional_vis_wizard Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
functional_with_es_ssl 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
licensing_plugin skip flaky suite (#53575) 2021-04-21 13:38:14 +01:00
lists_api_integration ES client : use the new type definitions (#83808) 2021-03-25 04:47:16 -04:00
load [loa testing] wait 60 sec b/w simulations (#96612) 2021-04-08 23:07:08 +02:00
observability_api_integration ES client : use the new type definitions (#83808) 2021-03-25 04:47:16 -04:00
plugin_api_integration [Event Log] Extend ECS event schema with fields needed for Detection Engine (#95067) 2021-03-29 14:59:36 +02:00
plugin_api_perf [Plugins Discovery] Enforce camelCase plugin IDs (#90752) 2021-02-11 14:36:17 +00:00
plugin_functional [ftr/esArchiver] disable geo_shape field of maps saved objects in archives (#99119) 2021-05-04 10:36:28 -07:00
reporting_api_integration [Reporting] Kibana Application Privileges for Reporting (#94966) 2021-04-20 20:44:24 -07:00
reporting_functional [Reporting] Kibana Application Privileges for Reporting (#94966) 2021-04-20 20:44:24 -07:00
saved_object_api_integration Migrate away from legacyEs service in tests. (#95402) 2021-03-26 18:49:19 +01:00
saved_object_tagging skip flaky suite (#89958) 2021-02-17 06:55:11 -08:00
saved_objects_field_count Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
search_sessions_integration [Search Sessions] Fix display of expired session state in management (#98915) 2021-05-04 12:02:17 +02:00
security_api_integration Handle session timeout and user activity (#98461) 2021-05-12 17:33:10 +01:00
security_functional skip flaky suite (#98562) 2021-05-04 13:58:31 +01:00
security_solution_cypress [Security Solution] adds 'Alert details with unmapped fields' test (#98800) 2021-05-04 08:00:36 +02:00
security_solution_endpoint [ftr] auto assign ciGroupDocker to suites with dockerServers (#99393) 2021-05-06 12:42:29 -07:00
security_solution_endpoint_api_int [ftr] auto assign ciGroupDocker to suites with dockerServers (#99393) 2021-05-06 12:42:29 -07:00
security_solution_ftr/page_objects/detections Add a11y test coverage to Rule Creation Flow for Detections tab (#94377) 2021-03-25 15:05:23 -04:00
spaces_api_integration skip flaky suite (#92358) 2021-03-29 13:11:33 +01:00
stack_functional_integration Add CCS integration test for security rules (#99042) 2021-05-11 16:19:32 +02:00
ui_capabilities [Plugins Discovery] Enforce camelCase plugin IDs (#90752) 2021-02-11 14:36:17 +00:00
upgrade Fix reporting test (#95586) 2021-03-26 16:33:29 -06:00
upgrade_assistant_integration Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
usage_collection [Plugins Discovery] Enforce camelCase plugin IDs (#90752) 2021-02-11 14:36:17 +00:00
visual_regression Elastic License 2.0 (#90099) 2021-02-03 18:12:39 -08:00
tsconfig.json [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