-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1189 from jertel/eql
add support for EQL queries
- Loading branch information
Showing
5 changed files
with
288 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# A dict containing EQL would resemble the following: | ||
# {'query': {'bool': {'filter': {'bool': {'must': [{'range': {'@timestamp': {'gt': 'yyyy...', 'lte': 'yyyy...'}}}, {'eql': 'process where process.name == "regsvr32.exe"'}]}}}}, 'sort': [{'@timestamp': {'order': 'asc'}}]} | ||
def format_request(body): | ||
query = body.get('query') | ||
if not query: | ||
return None | ||
|
||
query_bool = query.get('bool') | ||
if not query_bool: | ||
return None | ||
|
||
filter = query_bool.get('filter') | ||
if not filter: | ||
return None | ||
|
||
filter_bool = filter.get('bool') | ||
if not filter_bool: | ||
return None | ||
|
||
filter_bool_must = filter_bool.get('must') | ||
if not filter_bool_must: | ||
return None | ||
|
||
other_filters = [] | ||
eql = None | ||
for f in filter_bool_must: | ||
if f.get('eql'): | ||
eql = f['eql'] | ||
else: | ||
other_filters.append(f) | ||
|
||
if eql: | ||
new_body = {'filter': { 'bool': { 'must': other_filters }}, 'query': eql} | ||
return new_body | ||
|
||
return None | ||
|
||
def format_results(results): | ||
hits = results.get('hits') | ||
if not hits: | ||
return results | ||
|
||
events = hits.get('events') | ||
if events is None: | ||
return results | ||
|
||
# relabel events as hits, for consistency | ||
events = hits.pop('events') | ||
hits['hits'] = events | ||
|
||
return results |
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,127 @@ | ||
import elastalert.eql as eql | ||
from elastalert import ElasticSearchClient | ||
from unittest import mock | ||
|
||
|
||
def test_format_request_without_eql(): | ||
assert eql.format_request({}) is None | ||
assert eql.format_request({'query': {}}) is None | ||
assert eql.format_request({'query': {'bool': {}}}) is None | ||
assert eql.format_request({'query': {'bool': {'filter': {}}}}) is None | ||
assert eql.format_request({'query': {'bool': {'filter': {'bool': {}}}}}) is None | ||
assert eql.format_request({'query': {'bool': {'filter': {'bool': {'must': []}}}}}) is None | ||
assert eql.format_request({'query': {'bool': {'filter': {'bool': {'must': [{'foo': 'bar'}]}}}}}) is None | ||
|
||
|
||
def test_format_request_with_eql(): | ||
body = { | ||
'query': { | ||
'bool': { | ||
'filter': { | ||
'bool': { | ||
'must': [ | ||
{'eql': 'test query'}, | ||
{'other': 'other filter'}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
expected_body = {'filter': {'bool': {'must': [{'other': 'other filter'}]}}, 'query': 'test query'} | ||
assert eql.format_request(body) == expected_body | ||
|
||
|
||
def eql_body(): | ||
body = { | ||
'query': { | ||
'bool': { | ||
'filter': { | ||
'bool': { | ||
'must': [ | ||
{'eql': 'test query'}, | ||
{'other': 'other filter'}, | ||
{'eql': 'newer query'}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return body | ||
|
||
|
||
def test_format_request_with_excessive_eql(): | ||
body = eql_body() | ||
expected_body = {'filter': {'bool': {'must': [{'other': 'other filter'}]}}, 'query': 'newer query'} | ||
assert eql.format_request(body) == expected_body | ||
|
||
|
||
def test_format_results_without_events(): | ||
expected_results = {'hits': {'hits': []}} | ||
results = expected_results | ||
eql.format_results(results) == expected_results | ||
|
||
|
||
def test_format_results_with_events(): | ||
expected_results = {'hits': {'events': [{'foo': 'bar'}]}} | ||
results = {'hits': {'hits': [{'foo': 'bar'}]}} | ||
eql.format_results(results) == expected_results | ||
|
||
|
||
def init_client(): | ||
conn = { | ||
'es_host': '', | ||
'es_hosts': [], | ||
'es_port': 123, | ||
'es_url_prefix': '', | ||
'use_ssl': False, | ||
'verify_certs': False, | ||
'ca_certs': [], | ||
'ssl_show_warn': False, | ||
'http_auth': '', | ||
'headers': [], | ||
'es_conn_timeout': 0, | ||
'send_get_body_as': '', | ||
'client_cert': '', | ||
'client_key': '' | ||
} | ||
return ElasticSearchClient(conn) | ||
|
||
|
||
def test_search_without_eql(): | ||
es_client = init_client() | ||
|
||
expected_params = {'from': True, 'size': 12, 'scroll': True, '_source_includes': True} | ||
expected_headers = {} | ||
expected_body = {} | ||
results = {} | ||
es_client.transport = mock.Mock() | ||
es_client.transport.perform_request.return_value = results | ||
|
||
body = {} | ||
params = {'from_': True, 'size': 12, 'scroll': True, '_source_includes': True} | ||
es_client.search(body=body, index='test', params=params) | ||
es_client.transport.perform_request.assert_called_with('POST', '/test/_search', | ||
params=expected_params, | ||
headers=expected_headers, | ||
body=expected_body) | ||
|
||
|
||
def test_search_with_eql(): | ||
es_client = init_client() | ||
|
||
expected_params = {'from': True} | ||
expected_headers = {} | ||
expected_body = {'filter': {'bool': {'must': [{'other': 'other filter'}]}}, 'query': 'newer query', 'size': 12} | ||
results = {} | ||
es_client.transport = mock.Mock() | ||
es_client.transport.perform_request.return_value = results | ||
|
||
body = eql_body() | ||
params = {'from_': True, 'size': 12, 'scroll': True, '_source_includes': True} | ||
results = es_client.search(body=body, index='test', params=params) | ||
es_client.transport.perform_request.assert_called_with('POST', '/test/_eql/search', | ||
params=expected_params, | ||
headers=expected_headers, | ||
body=expected_body) |