Skip to content

Commit 47d7a3a

Browse files
eric-forte-elasticbrokensound77Mikaayenson
authored
[DaC] Beta Release (#3889)
Co-authored-by: Justin Ibarra <[email protected]> Co-authored-by: brokensound77 <[email protected]> Co-authored-by: Mika Ayenson <[email protected]> Co-authored-by: Mika Ayenson <[email protected]>
1 parent f9717e7 commit 47d7a3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2962
-364
lines changed

CLI.md

+77-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ the [README](README.md). Basic use of the CLI such as [creating a rule](CONTRIBU
55
[testing](CONTRIBUTING.md#testing-a-rule-with-the-cli) are referenced in the [contribution guide](CONTRIBUTING.md).
66

77

8-
## Using a config file or environment variables
8+
## Using a user config file or environment variables
99

1010
CLI commands which are tied to Kibana and Elasticsearch are capable of parsing auth-related keyword args from a config
1111
file or environment variables.
@@ -17,9 +17,9 @@ follows:
1717
* config values
1818
* prompt (this only applies to certain values)
1919

20-
#### Setup a config file
20+
#### Setup a user config file
2121

22-
In the root directory of this repo, create the file `.detection-rules-cfg.json` and add relevant values
22+
In the root directory of this repo, create the file `.detection-rules-cfg.json` (or `.yaml`) and add relevant values
2323

2424
Currently supported arguments:
2525
* elasticsearch_url
@@ -42,6 +42,8 @@ on the building block rules.
4242

4343
Using the environment variable `DR_BYPASS_TAGS_VALIDATION` will bypass the Detection Rules Unit Tests on the `tags` field in toml files.
4444

45+
Using the environment variable `DR_BYPASS_TIMELINE_TEMPLATE_VALIDATION` will bypass the timeline template id and title validation for rules.
46+
4547
## Importing rules into the repo
4648

4749
You can import rules into the repo using the `create-rule` or `import-rules-to-repo` commands. Both of these commands will
@@ -85,9 +87,19 @@ Usage: detection_rules import-rules-to-repo [OPTIONS] [INPUT_FILE]...
8587
Import rules from json, toml, yaml, or Kibana exported rule file(s).
8688

8789
Options:
88-
--required-only Only prompt for required fields
89-
-d, --directory DIRECTORY Load files from a directory
90-
-h, --help Show this message and exit.
90+
-ac, --action-connector-import Include action connectors in export
91+
-e, --exceptions-import Include exceptions in export
92+
--required-only Only prompt for required fields
93+
-d, --directory DIRECTORY Load files from a directory
94+
-s, --save-directory DIRECTORY Save imported rules to a directory
95+
-se, --exceptions-directory DIRECTORY
96+
Save imported exceptions to a directory
97+
-sa, --action-connectors-directory DIRECTORY
98+
Save imported actions to a directory
99+
-ske, --skip-errors Skip rule import errors
100+
-da, --default-author TEXT Default author for rules missing one
101+
-snv, --strip-none-values Strip None values from the rule
102+
-h, --help Show this message and exit.
91103
```
92104

93105
The primary advantage of using this command is the ability to import multiple rules at once. Multiple rule paths can be
@@ -97,10 +109,14 @@ a combination of both.
97109
In addition to the formats mentioned using `create-rule`, this will also accept an `.ndjson`/`jsonl` file
98110
containing multiple rules (as would be the case with a bulk export).
99111

112+
The `-s/--save-directory` is an optional parameter to specify a non default directory to place imported rules. If it is not specified, the first directory specified in the rules config will be used.
113+
100114
This will also strip additional fields and prompt for missing required fields.
101115

102116
<a id="note-3">\* Note</a>: This will attempt to parse ALL files recursively within a specified directory.
103117

118+
Additionally, the `-e` flag can be used to import exceptions in addition to rules from the export file.
119+
104120

105121
## Commands using Elasticsearch and Kibana clients
106122

@@ -165,6 +181,8 @@ Options:
165181
-h, --help Show this message and exit.
166182

167183
Commands:
184+
export-rules Export custom rules from Kibana.
185+
import-rules Import custom rules into Kibana.
168186
search-alerts Search detection engine alerts with KQL.
169187
upload-rule Upload a list of rule .toml files to Kibana.
170188
```
@@ -272,21 +290,24 @@ directly.
272290
```console
273291
Usage: detection_rules export-rules-from-repo [OPTIONS]
274292

275-
Export rule(s) into an importable ndjson file.
293+
Export rule(s) and exception(s) into an importable ndjson file.
276294

277295
Options:
278296
-f, --rule-file FILE
279297
-d, --directory DIRECTORY Recursively load rules from a directory
280298
-id, --rule-id TEXT
281299
-o, --outfile PATH Name of file for exported rules
282300
-r, --replace-id Replace rule IDs with new IDs before export
283-
--stack-version [7.10|7.11|7.12|7.13|7.14|7.15|7.16|7.8|7.9|8.0|8.1|8.10|8.11|8.12|8.13|8.2|8.3|8.4|8.5|8.6|8.7|8.8|8.9]
301+
--stack-version [7.8|7.9|7.10|7.11|7.12|7.13|7.14|7.15|7.16|8.0|8.1|8.2|8.3|8.4|8.5|8.6|8.7|8.8|8.9|8.10|8.11|8.12|8.13|8.14]
284302
Downgrade a rule version to be compatible
285303
with older instances of Kibana
286304
-s, --skip-unsupported If `--stack-version` is passed, skip rule
287305
types which are unsupported (an error will
288306
be raised otherwise)
289307
--include-metadata Add metadata to the exported rules
308+
-ac, --include-action-connectors
309+
Include Action Connectors in export
310+
-e, --include-exceptions Include Exceptions Lists in export
290311
-h, --help Show this message and exit.
291312
```
292313

@@ -317,6 +338,7 @@ Options:
317338
--kibana-url TEXT
318339
-kp, --kibana-password TEXT
319340
-kc, --kibana-cookie TEXT Cookie from an authed session
341+
--api-key TEXT
320342
--cloud-id TEXT ID of the cloud instance.
321343
322344
Usage: detection_rules kibana import-rules [OPTIONS]
@@ -329,7 +351,7 @@ Options:
329351
-id, --rule-id TEXT
330352
-o, --overwrite Overwrite existing rules
331353
-e, --overwrite-exceptions Overwrite exceptions in existing rules
332-
-a, --overwrite-action-connectors
354+
-ac, --overwrite-action-connectors
333355
Overwrite action connectors in existing
334356
rules
335357
-h, --help Show this message and exit.
@@ -476,6 +498,51 @@ python -m detection_rules kibana import-rules -d test-export-rules -o
476498

477499
### Exporting rules
478500

501+
This command should be run with the `CUSTOM_RULES_DIR` envvar set, that way proper validation is applied to versioning when the rules are downloaded. See the [custom rules docs](docs/custom-rules.md) for more information.
502+
503+
```
504+
python -m detection_rules kibana export-rules -h
505+
506+
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
507+
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
508+
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
509+
510+
Kibana client:
511+
Options:
512+
--ignore-ssl-errors TEXT
513+
--space TEXT Kibana space
514+
--provider-name TEXT Elastic Cloud providers: cloud-basic and cloud-
515+
saml (for SSO)
516+
--provider-type TEXT Elastic Cloud providers: basic and saml (for
517+
SSO)
518+
-ku, --kibana-user TEXT
519+
--kibana-url TEXT
520+
-kp, --kibana-password TEXT
521+
-kc, --kibana-cookie TEXT Cookie from an authed session
522+
--api-key TEXT
523+
--cloud-id TEXT ID of the cloud instance.
524+
525+
Usage: detection_rules kibana export-rules [OPTIONS]
526+
527+
Export custom rules from Kibana.
528+
529+
Options:
530+
-d, --directory PATH Directory to export rules to [required]
531+
-acd, --action-connectors-directory PATH
532+
Directory to export action connectors to
533+
-ed, --exceptions-directory PATH
534+
Directory to export exceptions to
535+
-da, --default-author TEXT Default author for rules missing one
536+
-r, --rule-id TEXT Optional Rule IDs to restrict export to
537+
-ac, --export-action-connectors
538+
Include action connectors in export
539+
-e, --export-exceptions Include exceptions in export
540+
-s, --skip-errors Skip errors when exporting rules
541+
-sv, --strip-version Strip the version fields from all rules
542+
-h, --help Show this message and exit.
543+
544+
```
545+
479546
Example of a rule exporting, with errors skipped
480547

481548
```
@@ -648,4 +715,4 @@ value = "fast"
648715
```
649716

650717
The easiest way to _update_ a rule with existing transform entries is to use `guide-plugin-convert` and manually add it
651-
to the rule.
718+
to the rule.

detection_rules/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
assert (3, 12) <= sys.version_info < (4, 0), "Only Python 3.12+ supported"
1212

1313
from . import ( # noqa: E402
14+
custom_schemas,
15+
custom_rules,
1416
devtools,
1517
docs,
1618
eswrap,
@@ -28,6 +30,8 @@
2830
)
2931

3032
__all__ = (
33+
'custom_rules',
34+
'custom_schemas',
3135
'devtools',
3236
'docs',
3337
'eswrap',

detection_rules/action.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
# or more contributor license agreements. Licensed under the Elastic License
3+
# 2.0; you may not use this file except in compliance with the Elastic License
4+
# 2.0.
5+
6+
"""Dataclasses for Action."""
7+
from dataclasses import dataclass
8+
from pathlib import Path
9+
from typing import List, Optional
10+
11+
from .mixins import MarshmallowDataclassMixin
12+
from .schemas import definitions
13+
14+
15+
@dataclass(frozen=True)
16+
class ActionMeta(MarshmallowDataclassMixin):
17+
"""Data stored in an exception's [metadata] section of TOML."""
18+
creation_date: definitions.Date
19+
rule_id: List[definitions.UUIDString]
20+
rule_name: str
21+
updated_date: definitions.Date
22+
23+
# Optional fields
24+
deprecation_date: Optional[definitions.Date]
25+
comments: Optional[str]
26+
maturity: Optional[definitions.Maturity]
27+
28+
29+
@dataclass
30+
class Action(MarshmallowDataclassMixin):
31+
"""Data object for rule Action."""
32+
@dataclass
33+
class ActionParams:
34+
"""Data object for rule Action params."""
35+
body: str
36+
37+
action_type_id: definitions.ActionTypeId
38+
group: str
39+
params: ActionParams
40+
id: Optional[str]
41+
frequency: Optional[dict]
42+
alerts_filter: Optional[dict]
43+
44+
45+
@dataclass(frozen=True)
46+
class TOMLActionContents(MarshmallowDataclassMixin):
47+
"""Object for action from TOML file."""
48+
metadata: ActionMeta
49+
actions: List[Action]
50+
51+
52+
@dataclass(frozen=True)
53+
class TOMLAction:
54+
"""Object for action from TOML file."""
55+
contents: TOMLActionContents
56+
path: Path
57+
58+
@property
59+
def name(self):
60+
return self.contents.metadata.rule_name
61+
62+
@property
63+
def id(self):
64+
return self.contents.metadata.rule_id

0 commit comments

Comments
 (0)