Skip to content

Commit 6456cbc

Browse files
Add index_replacement option
1 parent b6aac59 commit 6456cbc

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

detection_rules/remote_validation.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,39 @@ def request(c: TOMLRuleContents) -> None:
178178

179179
return responses # type: ignore[reportUnknownVariableType]
180180

181-
def validate_esql(self, contents: TOMLRuleContents) -> dict[str, Any]:
181+
def validate_esql(self, contents: TOMLRuleContents, index_replacement: bool = False) -> dict[str, Any]:
182182
query = contents.data.query # type: ignore[reportAttributeAccessIssue]
183183
rule_id = contents.data.rule_id
184184
if not self.es_client:
185185
raise ValueError("No ES client found")
186186

187187
if not self.kibana_client:
188188
raise ValueError("No Kibana client found")
189-
try:
190-
validator = ESQLValidator(contents.data.query) # type: ignore[reportIncompatibleMethodOverride]
191-
response = validator.remote_validate_rule_contents(self.kibana_client, self.es_client, contents)
192-
except Exception as exc:
193-
if isinstance(exc, elasticsearch.BadRequestError):
194-
raise ValidationError(f"ES|QL query failed: {exc} for rule: {rule_id}, query: \n{query}") from exc
195-
raise Exception(f"ES|QL query failed for rule: {rule_id}, query: \n{query}") from exc # noqa: TRY002
189+
if index_replacement:
190+
try:
191+
validator = ESQLValidator(contents.data.query) # type: ignore[reportIncompatibleMethodOverride]
192+
response = validator.remote_validate_rule_contents(self.kibana_client, self.es_client, contents)
193+
except Exception as exc:
194+
if isinstance(exc, elasticsearch.BadRequestError):
195+
raise ValidationError(f"ES|QL query failed: {exc} for rule: {rule_id}, query: \n{query}") from exc
196+
raise Exception(f"ES|QL query failed for rule: {rule_id}, query: \n{query}") from exc # noqa: TRY002
197+
else:
198+
headers = {"accept": "application/json", "content-type": "application/json"}
199+
body = {"query": f"{query} | LIMIT 0"}
200+
if not self.es_client:
201+
raise ValueError("No ES client found")
202+
try:
203+
response = self.es_client.perform_request(
204+
"POST",
205+
"/_query",
206+
headers=headers,
207+
params={"pretty": True},
208+
body=body,
209+
)
210+
except Exception as exc:
211+
if isinstance(exc, elasticsearch.BadRequestError):
212+
raise ValidationError(f"ES|QL query failed: {exc} for rule: {rule_id}, query: \n{query}") from exc
213+
raise Exception(f"ES|QL query failed for rule: {rule_id}, query: \n{query}") from exc # noqa: TRY002
196214

197215
return response.body
198216

0 commit comments

Comments
 (0)