forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Move ES|QL parsing functionality into `@kbn/secur…
…itysolution-utils` package (elastic#202772) ## Summary With this PR we move existing `parseEsqlQuery` method into a shared security solution utils package. We need to the same functionality in "SIEM migrations" feature. Previously we duplicated the code in [this PR](https://github.com/elastic/kibana/pull/202331/files#diff-b5f1a952a5e5a9685a4fef5d1f5a4c3b53ce338333e569bb6f92ccf2681100b7R54) and these are the follow-up changes to make parsing functionality shared for easier re-use within security solution. ### Checklist Check the PR satisfies following conditions. Reviewers should verify this PR satisfies this list as well. - [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 --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
6f3ebbc
commit ff57bf3
Showing
7 changed files
with
150 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
packages/kbn-securitysolution-utils/src/esql/parse_esql_query.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { parseEsqlQuery } from './parse_esql_query'; | ||
|
||
describe('parseEsqlQuery', () => { | ||
describe('ES|QL query syntax', () => { | ||
it.each([['incorrect syntax'], ['from test* metadata']])( | ||
'detects incorrect syntax in "%s"', | ||
(esqlQuery) => { | ||
const result = parseEsqlQuery(esqlQuery); | ||
expect(result.errors.length).toEqual(1); | ||
expect(result.errors[0].message.startsWith('SyntaxError:')).toBeTruthy(); | ||
expect(parseEsqlQuery(esqlQuery)).toMatchObject({ | ||
hasMetadataOperator: false, | ||
isEsqlQueryAggregating: false, | ||
}); | ||
} | ||
); | ||
|
||
it.each([ | ||
['from test* metadata _id'], | ||
[ | ||
'FROM kibana_sample_data_logs | STATS total_bytes = SUM(bytes) BY host | WHERE total_bytes > 200000 | SORT total_bytes DESC | LIMIT 10', | ||
], | ||
[ | ||
`from packetbeat* metadata | ||
_id | ||
| limit 100`, | ||
], | ||
[ | ||
`FROM kibana_sample_data_logs | | ||
STATS total_bytes = SUM(bytes) BY host | | ||
WHERE total_bytes > 200000 | | ||
SORT total_bytes DESC | | ||
LIMIT 10`, | ||
], | ||
])('parses correctly valid syntax in "%s"', (esqlQuery) => { | ||
const result = parseEsqlQuery(esqlQuery); | ||
expect(result.errors.length).toEqual(0); | ||
expect(result).toMatchObject({ errors: [] }); | ||
}); | ||
}); | ||
|
||
describe('METADATA operator', () => { | ||
it.each([ | ||
['from test*'], | ||
['from metadata*'], | ||
['from test* | keep metadata'], | ||
['from test* | eval x="metadata _id"'], | ||
])('detects when METADATA operator is missing in a NON aggregating query "%s"', (esqlQuery) => { | ||
expect(parseEsqlQuery(esqlQuery)).toEqual({ | ||
errors: [], | ||
hasMetadataOperator: false, | ||
isEsqlQueryAggregating: false, | ||
}); | ||
}); | ||
|
||
it.each([ | ||
['from test* metadata _id'], | ||
['from test* metadata _id, _index'], | ||
['from test* metadata _index, _id'], | ||
['from test* metadata _id '], | ||
['from test* metadata _id '], | ||
['from test* metadata _id | limit 10'], | ||
[ | ||
`from packetbeat* metadata | ||
_id | ||
| limit 100`, | ||
], | ||
])('detects existin METADATA operator in a NON aggregating query "%s"', (esqlQuery) => | ||
expect(parseEsqlQuery(esqlQuery)).toEqual({ | ||
errors: [], | ||
hasMetadataOperator: true, | ||
isEsqlQueryAggregating: false, | ||
}) | ||
); | ||
|
||
it('detects missing METADATA operator in an aggregating query "from test* | stats c = count(*) by fieldA"', () => | ||
expect(parseEsqlQuery('from test* | stats c = count(*) by fieldA')).toEqual({ | ||
errors: [], | ||
hasMetadataOperator: false, | ||
isEsqlQueryAggregating: true, | ||
})); | ||
}); | ||
|
||
describe('METADATA _id field for NON aggregating queries', () => { | ||
it('detects missing METADATA "_id" field', () => { | ||
expect(parseEsqlQuery('from test*')).toEqual({ | ||
errors: [], | ||
hasMetadataOperator: false, | ||
isEsqlQueryAggregating: false, | ||
}); | ||
}); | ||
|
||
it('detects existing METADATA "_id" field', async () => { | ||
expect(parseEsqlQuery('from test* metadata _id')).toEqual({ | ||
errors: [], | ||
hasMetadataOperator: true, | ||
isEsqlQueryAggregating: false, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('METADATA _id field for aggregating queries', () => { | ||
it('detects existing METADATA operator with missing "_id" field', () => { | ||
expect( | ||
parseEsqlQuery('from test* metadata someField | stats c = count(*) by fieldA') | ||
).toEqual({ errors: [], hasMetadataOperator: false, isEsqlQueryAggregating: true }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters