Skip to content

Commit 013ad5f

Browse files
Update Error Types
1 parent f23d839 commit 013ad5f

File tree

4 files changed

+35
-33
lines changed

4 files changed

+35
-33
lines changed

detection_rules/esql_errors.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""ESQL exceptions."""
22

3-
from marshmallow.exceptions import ValidationError
4-
53
__all__ = (
64
"EsqlSchemaError",
75
"EsqlSemanticError",
@@ -10,21 +8,29 @@
108
)
119

1210

13-
class EsqlSchemaError(ValidationError):
14-
"""Error for missing fields in ESQL."""
11+
class EsqlSchemaError(Exception):
12+
"""Error in ESQL schema. Validated via Kibana until AST is available."""
13+
14+
def __init__(self, message: str):
15+
super().__init__(message)
1516

1617

17-
class EsqlSyntaxError(ValidationError):
18-
"""Error with ESQL syntax."""
18+
class EsqlSyntaxError(Exception):
19+
"""Error with ESQL syntax. Validated via Kibana until AST is available."""
1920

20-
# TODO: Update this to a Kibana Error extension? Perhaps?
21+
def __init__(self, message: str):
22+
super().__init__(message)
2123

2224

23-
class EsqlSemanticError(ValidationError):
24-
"""Error with ESQL semantics."""
25+
class EsqlSemanticError(Exception):
26+
"""Error with ESQL semantics. Validated via Kibana until AST is available."""
2527

26-
# TODO: Update this to a Kibana Error extension? Perhaps?
28+
def __init__(self, message: str):
29+
super().__init__(message)
2730

2831

29-
class EsqlTypeMismatchError(ValidationError):
32+
class EsqlTypeMismatchError(Exception):
3033
"""Error when validating types in ESQL."""
34+
35+
def __init__(self, message: str):
36+
super().__init__(message)

detection_rules/index_mappings.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def prepare_integration_mappings(
5858

5959
for integration in rule_integrations:
6060
package = integration
61-
# TODO check should be latest or least?
6261
package_version, _ = integrations.find_latest_compatible_version(
6362
package,
6463
"",

detection_rules/misc.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
import requests
1717
from elastic_transport import ObjectApiResponse
1818
from elasticsearch import AuthenticationException, Elasticsearch
19+
from elasticsearch.exceptions import BadRequestError
1920
from kibana import Kibana # type: ignore[reportMissingTypeStubs]
2021

22+
from .esql_errors import EsqlSchemaError
2123
from .utils import add_params, cached, combine_dicts, load_etc_dump
2224

2325
LICENSE_HEADER = """
@@ -427,17 +429,21 @@ def get_simulated_index_template_mappings(elastic_client: Elasticsearch, name: s
427429

428430
def create_index_with_index_mapping(
429431
elastic_client: Elasticsearch, index_name: str, mappings: dict[str, Any]
430-
) -> ObjectApiResponse[Any]:
432+
) -> ObjectApiResponse[Any] | None:
431433
"""Create an index with the specified mappings and settings to support large number of fields and nested objects."""
432-
return elastic_client.indices.create(
433-
index=index_name,
434-
mappings={"properties": mappings},
435-
settings={
436-
"index.mapping.total_fields.limit": 10000,
437-
"index.mapping.nested_fields.limit": 500,
438-
"index.mapping.nested_objects.limit": 10000,
439-
},
440-
)
434+
try:
435+
return elastic_client.indices.create(
436+
index=index_name,
437+
mappings={"properties": mappings},
438+
settings={
439+
"index.mapping.total_fields.limit": 10000,
440+
"index.mapping.nested_fields.limit": 500,
441+
"index.mapping.nested_objects.limit": 10000,
442+
},
443+
)
444+
except BadRequestError as e:
445+
if e.status_code == 400 and "validation_exception" in str(e):
446+
raise EsqlSchemaError(str(e)) from e
441447

442448

443449
def get_existing_mappings(elastic_client: Elasticsearch, indices: list[str]) -> tuple[dict[str, Any], dict[str, Any]]:

detection_rules/rule_validators.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .beats import get_datasets_and_modules, parse_beats_from_index
3333
from .config import CUSTOM_RULES_DIR, load_current_package_version, parse_rules_config
3434
from .custom_schemas import update_auto_generated_schema
35+
from .esql_errors import EsqlTypeMismatchError
3536
from .index_mappings import (
3637
create_remote_indices,
3738
execute_query_against_indices,
@@ -57,15 +58,6 @@
5758
)
5859
KQL_ERROR_TYPES = kql.KqlCompileError | kql.KqlParseError
5960
RULES_CONFIG = parse_rules_config()
60-
# TODO ESQL specific error message to catch Kibana Bad Request Errors
61-
# TODO ESQL.py file to hold ESQL specific logic for Errors, subclass exceptions
62-
# Expect to support the following as ESQL (middle 2 from Kibana)
63-
"""
64-
EsqlSchemaError
65-
EsqlSemanticError
66-
EsqlSyntaxError
67-
EsqlTypeMismatchError
68-
"""
6961

7062

7163
@dataclass(frozen=True)
@@ -786,8 +778,7 @@ def validate_columns_index_mapping(
786778
)
787779

788780
if mismatched_columns:
789-
# TODO this should be an ESQL type Error (check to match EQL error structure)
790-
raise ValueError("Column validation errors:\n" + "\n".join(mismatched_columns))
781+
raise EsqlTypeMismatchError("Column validation errors:\n" + "\n".join(mismatched_columns))
791782

792783
return True
793784

0 commit comments

Comments
 (0)