forked from elastic/detection-rules
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FR] Add support to decouple actions and exceptions #5
Merged
brokensound77
merged 1 commit into
add-configurable-testing-and-validation
from
decoupled-exceptions-and-actions
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# 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; you may not use this file except in compliance with the Elastic License | ||
# 2.0. | ||
|
||
"""Dataclasses for Action.""" | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import List, Optional | ||
|
||
from .mixins import MarshmallowDataclassMixin | ||
from .schemas import definitions | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ActionMeta(MarshmallowDataclassMixin): | ||
"""Data stored in an exception's [metadata] section of TOML.""" | ||
creation_date: definitions.Date | ||
rule_id: definitions.UUIDString | ||
rule_name: str | ||
updated_date: definitions.Date | ||
|
||
# Optional fields | ||
deprecation_date: Optional[definitions.Date] | ||
comments: Optional[str] | ||
maturity: Optional[definitions.Maturity] | ||
|
||
|
||
@dataclass | ||
class Action(MarshmallowDataclassMixin): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
"""Data object for rule Action.""" | ||
@dataclass | ||
class ActionParams: | ||
body: str | ||
|
||
action_type_id: str | ||
group: str | ||
params: ActionParams | ||
id: Optional[str] | ||
frequency: Optional[dict] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TOMLActionContents(MarshmallowDataclassMixin): | ||
"""Object for action from TOML file.""" | ||
metadata: ActionMeta | ||
actions: List[Action] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TOMLAction: | ||
"""Object for action from TOML file.""" | ||
contents: TOMLActionContents | ||
path: Path | ||
|
||
@property | ||
def name(self): | ||
return self.contents.metadata.rule_name | ||
|
||
@property | ||
def id(self): | ||
return self.contents.metadata.rule_id |
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,143 @@ | ||
# 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; you may not use this file except in compliance with the Elastic License | ||
# 2.0. | ||
"""Rule exceptions data.""" | ||
from dataclasses import dataclass | ||
from pathlib import Path | ||
from typing import List, Literal, Optional, Union | ||
|
||
from marshmallow import validates_schema, ValidationError | ||
|
||
from .mixins import MarshmallowDataclassMixin | ||
from .schemas import definitions | ||
|
||
|
||
# https://www.elastic.co/guide/en/security/current/exceptions-api-overview.html | ||
|
||
@dataclass(frozen=True) | ||
class ExceptionMeta(MarshmallowDataclassMixin): | ||
"""Data stored in an exception's [metadata] section of TOML.""" | ||
creation_date: definitions.Date | ||
rule_id: definitions.UUIDString | ||
rule_name: str | ||
updated_date: definitions.Date | ||
|
||
# Optional fields | ||
deprecation_date: Optional[definitions.Date] | ||
comments: Optional[str] | ||
maturity: Optional[definitions.Maturity] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class BaseExceptionItemEntry(MarshmallowDataclassMixin): | ||
field: str | ||
type: definitions.ExceptionEntryType | ||
|
||
|
||
@dataclass(frozen=True) | ||
class NestedExceptionItemEntry(BaseExceptionItemEntry, MarshmallowDataclassMixin): | ||
entries: List['ExceptionItemEntry'] | ||
|
||
@validates_schema | ||
def validate_nested_entry(self, data: dict, **kwargs): | ||
if data.get('list') is not None: | ||
raise ValidationError('Nested entries cannot define a list') | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExceptionItemEntry(BaseExceptionItemEntry, MarshmallowDataclassMixin): | ||
@dataclass(frozen=True) | ||
class ListObject: | ||
id: definitions.UUIDString | ||
type: definitions.EsDataTypes | ||
|
||
list: Optional[ListObject] | ||
operator: definitions.ExceptionEntryOperator | ||
value: Optional[Union[str, List[str]]] | ||
|
||
@validates_schema | ||
def validate_entry(self, data: dict, **kwargs): | ||
value = data.get('value', '') | ||
if data['type'] in ('exists', 'list') and value is not None: | ||
raise ValidationError(f'Entry of type {data["type"]} cannot have a value') | ||
elif data['type'] in ('match', 'wildcard') and not isinstance(value, str): | ||
raise ValidationError(f'Entry of type {data["type"]} must have a string value') | ||
elif data['type'] == 'match_any' and not isinstance(value, list): | ||
raise ValidationError(f'Entry of type {data["type"]} must have a list of strings as a value') | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExceptionItem(MarshmallowDataclassMixin): | ||
@dataclass(frozen=True) | ||
class Comment: | ||
comment: str | ||
|
||
comments: List[Optional[Comment]] | ||
description: str | ||
entries: List[Union[ExceptionItemEntry, NestedExceptionItemEntry]] | ||
list_id: str | ||
item_id: Optional[str] # api sets field when not provided | ||
meta: Optional[dict] | ||
name: str | ||
namespace_type: Optional[definitions.ExceptionNamespaceType] # defaults to "single" if not provided | ||
tags: Optional[List[str]] | ||
type: Literal['simple'] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class EndpointException(ExceptionItem, MarshmallowDataclassMixin): | ||
_tags: List[definitions.ExceptionItemEndpointTags] | ||
|
||
@validates_schema | ||
def validate_endpoint(self, data: dict, **kwargs): | ||
for entry in data['entries']: | ||
if entry['operator'] == "excluded": | ||
raise ValidationError("Endpoint exceptions cannot have an `excluded` operator") | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DetectionException(ExceptionItem, MarshmallowDataclassMixin): | ||
expire_time: Optional[str] # fields.DateTime] # maybe this is isoformat? | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ExceptionContainer(MarshmallowDataclassMixin): | ||
description: str | ||
list_id: Optional[str] | ||
meta: Optional[dict] | ||
name: str | ||
namespace_type: Optional[definitions.ExceptionNamespaceType] | ||
tags: Optional[List[str]] | ||
type: definitions.ExceptionContainerType | ||
|
||
def to_rule_entry(self) -> dict: | ||
"""Returns a dict of the format required in rule.exception_list.""" | ||
# requires KSO id to be consider valid structure | ||
return dict(namespace_type=self.namespace_type, type=self.type, list_id=self.list_id) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class Data(MarshmallowDataclassMixin): | ||
container: ExceptionContainer | ||
items: List[DetectionException] # Union[DetectionException, EndpointException]] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TOMLExceptionContents(MarshmallowDataclassMixin): | ||
metadata: ExceptionMeta | ||
exceptions: List[Data] | ||
|
||
|
||
@dataclass(frozen=True) | ||
class TOMLException: | ||
contents: TOMLExceptionContents | ||
path: Optional[Path] = None | ||
|
||
@property | ||
def name(self): | ||
return self.contents.metadata.rule_name | ||
|
||
@property | ||
def id(self): | ||
return self.contents.metadata.rule_id |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should action list be associated to only one rule or multiple rules?