Skip to content

Commit

Permalink
Merge pull request #731 from ferozsalam/es8-compatibility
Browse files Browse the repository at this point in the history
Enable loading ElastAlert2 against a fresh ES8 instance
  • Loading branch information
jertel authored Feb 20, 2022
2 parents ed04343 + eaa1047 commit af11e9b
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [Docs] Update of RuleType Configuration Cheat Sheet - [#707](https://github.com/jertel/elastalert2/pull/707) - @nsano-rururu
- Pytest 7.0.0 to 7.0.1 - [#710](https://github.com/jertel/elastalert2/pull/710) - @nsano-rururu
- Fixing jira_transition_to schema bug. Change property type from boolean to string [#721](https://github.com/jertel/elastalert2/pull/721) - @toxisch
- Begin Elasticsearch 8 support - ElastAlert 2 now supports setup with fresh ES 8 instances, and works with some alert types [#731](https://github.com/jertel/elastalert2/pull/731) - @ferozsalam

# 2.3.0

Expand Down
7 changes: 7 additions & 0 deletions elastalert/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ def is_atleastseven(self):
"""
return int(self.es_version.split(".")[0]) >= 7

def is_atleasteight(self):
"""
Returns True when the Elasticsearch server version >= 8
"""
return int(self.es_version.split(".")[0]) >= 8


def resolve_writeback_index(self, writeback_index, doc_type):
""" In ES6, you cannot have multiple _types per index,
therefore we use self.writeback_index as the prefix for the actual
Expand Down
26 changes: 22 additions & 4 deletions elastalert/create_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
else:
esversion = esinfo['number']

es_index_mappings = read_es_index_mappings() if is_atleastsix(esversion) else read_es_index_mappings(5)
es_index_mappings = {}
if is_atleasteight(esversion):
es_index_mappings = read_es_index_mappings()
elif is_atleastsix(esversion):
es_index_mappings = read_es_index_mappings(6)
else:
es_index_mappings = read_es_index_mappings(5)

es_index = IndicesClient(es_client)
if not recreate:
Expand Down Expand Up @@ -61,8 +67,18 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None

# To avoid a race condition. TODO: replace this with a real check
time.sleep(2)

if is_atleastseven(esversion):
if is_atleasteight(esversion):
es_client.indices.put_mapping(index=ea_index,
body=es_index_mappings['elastalert'])
es_client.indices.put_mapping(index=ea_index + '_status',
body=es_index_mappings['elastalert_status'])
es_client.indices.put_mapping(index=ea_index + '_silence',
body=es_index_mappings['silence'])
es_client.indices.put_mapping(index=ea_index + '_error',
body=es_index_mappings['elastalert_error'])
es_client.indices.put_mapping(index=ea_index + '_past',
body=es_index_mappings['past_elastalert'])
elif is_atleastseven(esversion):
# TODO remove doc_type completely when elasicsearch client allows doc_type=None
# doc_type is a deprecated feature and will be completely removed in Elasicsearch 8
es_client.indices.put_mapping(index=ea_index, doc_type='_doc',
Expand Down Expand Up @@ -118,7 +134,7 @@ def create_index_mappings(es_client, ea_index, recreate=False, old_ea_index=None
print('Done!')


def read_es_index_mappings(es_version=6):
def read_es_index_mappings(es_version=8):
print('Reading Elastic {0} index mappings:'.format(es_version))
return {
'silence': read_es_index_mapping('silence', es_version),
Expand Down Expand Up @@ -150,6 +166,8 @@ def is_atleastsixtwo(es_version):
def is_atleastseven(es_version):
return int(es_version.split(".")[0]) >= 7

def is_atleasteight(es_version):
return int(es_version.split(".")[0]) >= 8

def main():
parser = argparse.ArgumentParser()
Expand Down
39 changes: 39 additions & 0 deletions elastalert/es_mappings/8/elastalert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"numeric_detection": true,
"date_detection": false,
"dynamic_templates": [
{
"strings_as_keyword": {
"mapping": {
"ignore_above": 1024,
"type": "keyword"
},
"match_mapping_type": "string"
}
}
],
"properties": {
"rule_name": {
"type": "keyword"
},
"@timestamp": {
"type": "date",
"format": "date_optional_time"
},
"alert_time": {
"type": "date",
"format": "date_optional_time"
},
"match_time": {
"type": "date",
"format": "date_optional_time"
},
"match_body": {
"enabled": "false",
"type": "object"
},
"aggregate_id": {
"type": "keyword"
}
}
}
12 changes: 12 additions & 0 deletions elastalert/es_mappings/8/elastalert_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"properties": {
"data": {
"type": "object",
"enabled": "false"
},
"@timestamp": {
"type": "date",
"format": "date_optional_time"
}
}
}
11 changes: 11 additions & 0 deletions elastalert/es_mappings/8/elastalert_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"properties": {
"rule_name": {
"type": "keyword"
},
"@timestamp": {
"type": "date",
"format": "date_optional_time"
}
}
}
18 changes: 18 additions & 0 deletions elastalert/es_mappings/8/past_elastalert.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"properties": {
"rule_name": {
"type": "keyword"
},
"match_body": {
"type": "object",
"enabled": "false"
},
"@timestamp": {
"type": "date",
"format": "date_optional_time"
},
"aggregate_id": {
"type": "keyword"
}
}
}
15 changes: 15 additions & 0 deletions elastalert/es_mappings/8/silence.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"properties": {
"rule_name": {
"type": "keyword"
},
"until": {
"type": "date",
"format": "date_optional_time"
},
"@timestamp": {
"type": "date",
"format": "date_optional_time"
}
}
}
20 changes: 20 additions & 0 deletions tests/create_index_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def test_read_es_6_index_mappings():
print((json.dumps(mappings, indent=2)))


def test_read_es_8_index_mappings():
mappings = elastalert.create_index.read_es_index_mappings(8)
assert len(mappings) == len(es_mappings)
print((json.dumps(mappings, indent=2)))


@pytest.mark.parametrize('es_version, expected', [
('5.6.0', False),
('6.0.0', True),
Expand Down Expand Up @@ -144,3 +150,17 @@ def test_is_atleastsixtwo(es_version, expected):
def test_is_atleastseven(es_version, expected):
result = elastalert.create_index.is_atleastseven(es_version)
assert result == expected


@pytest.mark.parametrize('es_version, expected', [
('5.6.0', False),
('6.0.0', False),
('6.1.0', False),
('7.0.0', False),
('7.1.0', False),
('7.17.0', False),
('8.0.0', True)
])
def test_is_atleasteight(es_version, expected):
result = elastalert.create_index.is_atleasteight(es_version)
assert result == expected

0 comments on commit af11e9b

Please sign in to comment.