Skip to content

Commit eadcd9d

Browse files
[FR] Add Env Var DR_CLI_MAX_WIDTH and DaC Docs Updates (#4518)
* Add Env Var DR_CLI_MAX_WIDTH * Version Bump * Update limit from 120 to 240 * Clean references to reference main * Update Readme with DaC Info * Add DaC to Table of Contents * Bump Patch Version * Updated naming and add dac md * Organize Imports * Deprecate upload-rule * Update docs/detections-as-code.md Co-authored-by: Mika Ayenson, PhD <[email protected]> * move docs to docs-dev * Sort custom rules imports * Remove duplicate * Fix typo * Bump Patch Version --------- Co-authored-by: Mika Ayenson, PhD <[email protected]>
1 parent 3bdda09 commit eadcd9d

File tree

10 files changed

+182
-128
lines changed

10 files changed

+182
-128
lines changed

CLI.md

Lines changed: 100 additions & 119 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This repository was first announced on Elastic's blog post, [Elastic Security op
1616
- [Overview of this repository](#overview-of-this-repository)
1717
- [Getting started](#getting-started)
1818
- [How to contribute](#how-to-contribute)
19+
- [Detections as Code (DaC)](#detections-as-code-dac)
1920
- [RTAs](#rtas)
2021
- [Licensing](#licensing)
2122
- [Questions? Problems? Suggestions?](#questions-problems-suggestions)
@@ -133,11 +134,16 @@ For more advanced command line interface (CLI) usage, refer to the [CLI guide](C
133134

134135
We welcome your contributions to Detection Rules! Before contributing, please familiarize yourself with this repository, its [directory structure](#overview-of-this-repository), and our [philosophy](PHILOSOPHY.md) about rule creation. When you're ready to contribute, read the [contribution guide](CONTRIBUTING.md) to learn how we turn detection ideas into production rules and validate with testing.
135136

137+
## Detections as Code (DaC)
138+
139+
The Detection Rules repo includes a number of commands to help one manage rules with an "as code" philosophy. We recommend starting with our [DaC Specific Documentation](https://dac-reference.readthedocs.io/en/latest/) for strategies and recommended setup information. However, if you would prefer to jump right in, please see our [custom rules documentation](docs-dev/custom-rules-management.md) for information on how to configure this repo for use with custom rules followed by our [CLI documentation](CLI.md) for information on our commands to import and export rules.
140+
136141
## RTAs
137142

138143
Red Team Automations (RTAs) used to emulate attacker techniques and verify the rules can be found in dedicated
139144
repository - [Cortado](https://github.com/elastic/cortado).
140145

146+
141147
## Licensing
142148

143149
Everything in this repository — rules, code, etc. — is licensed under the [Elastic License v2](LICENSE.txt). These rules are designed to be used in the context of the Detection Engine within the Elastic Security application. If you’re using our [Elastic Cloud managed service](https://www.elastic.co/cloud/) or the default distribution of the Elastic Stack software that includes the [full set of free features](https://www.elastic.co/subscriptions), you’ll get the latest rules the first time you navigate to the detection engine.

detection_rules/custom_rules.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88

99
import click
1010
import yaml
11+
from semver import Version
1112

12-
from .main import root
13-
from .utils import get_etc_path, load_etc_dump, ROOT_DIR
1413
from .docs import REPO_DOCS_DIR
15-
16-
from semver import Version
14+
from .main import root
15+
from .utils import ROOT_DIR, get_etc_path, load_etc_dump
1716

1817
DEFAULT_CONFIG_PATH = Path(get_etc_path('_config.yaml'))
1918
CUSTOM_RULES_DOC_PATH = Path(ROOT_DIR).joinpath(REPO_DOCS_DIR, 'custom-rules.md')

detection_rules/kbwrap.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,15 @@ def kibana_group(ctx: click.Context, **kibana_kwargs):
5151
@click.option('--replace-id', '-r', is_flag=True, help='Replace rule IDs with new IDs before export')
5252
@click.pass_context
5353
def upload_rule(ctx, rules: RuleCollection, replace_id):
54-
"""Upload a list of rule .toml files to Kibana."""
54+
"""[Deprecated] Upload a list of rule .toml files to Kibana."""
5555
kibana = ctx.obj['kibana']
5656
api_payloads = []
5757

58+
click.secho(
59+
"WARNING: This command is deprecated as of Elastic Stack version 9.0. Please use `kibana import-rules`.",
60+
fg="yellow",
61+
)
62+
5863
for rule in rules:
5964
try:
6065
payload = downgrade_contents_from_rule(rule, kibana.version, replace_id=replace_id)

detection_rules/main.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,13 @@
4040
RULES_DIRS = RULES_CONFIG.rule_dirs
4141

4242

43-
@click.group('detection-rules', context_settings={'help_option_names': ['-h', '--help']})
43+
@click.group(
44+
'detection-rules',
45+
context_settings={
46+
'help_option_names': ['-h', '--help'],
47+
'max_content_width': int(os.getenv('DR_CLI_MAX_WIDTH', 240)),
48+
},
49+
)
4450
@click.option('--debug/--no-debug', '-D/-N', is_flag=True, default=None,
4551
help='Print full exception stacktrace on errors')
4652
@click.pass_context

detection_rules/ml.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ def setup_bundle(ctx, model_tag, repo, model_dir):
383383
click.echo(table)
384384

385385
click.echo('Associated rules and jobs can be found under ML-experimental-detections releases in the repo')
386-
click.echo('To upload rules, run: kibana upload-rule <ml-rule.toml>')
386+
click.echo('To upload rules, run: kibana import-rules -f <ml-rule.toml>')
387387
click.echo('To upload ML jobs, run: es experimental upload-ml-job <ml-job.json>')
388388

389389

File renamed without changes.

docs-dev/detections-as-code.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Detection as Code (DaC) Components in Detection-Rules Repo
2+
3+
The **detection-rules** repository contains features for **Detections as Code (DaC)**. These components, including CLI options and workflows, provide methods to help apply DaC principles in practice. The specific DaC architecture should be carefully considered before implementation, for more conceptual ideas and details about DaC, refer to the [DaC Documentation](https://dac-reference.readthedocs.io/en/latest/). Reference implementation is shared to facilitate experimentation and community contributions.
4+
5+
> [!NOTE]
6+
> This guidance outlines the support scope and best practices for using DaC components within the detection-rules repo. Users should take full responsibility for their usage of this repo, thoroughly test these tools in their environments, and verify functionality before using them.
7+
8+
---
9+
10+
## Support and Scope
11+
12+
Supported DaC components that interact with the Elastic Security Solution:
13+
- kibana export-rules ([link](https://github.com/elastic/detection-rules/blob/main/CLI.md#exporting-rules))
14+
- kibana import-rules ([link](https://github.com/elastic/detection-rules/blob/main/CLI.md#using-import-rules))
15+
- import-rules-to-repo ([link](https://github.com/elastic/detection-rules/blob/main/CLI.md#import-rules-to-repo))
16+
- export-rules-from-repo ([link](https://github.com/elastic/detection-rules/blob/main/CLI.md#uploading-rules-to-kibana))
17+
18+
We welcome general questions, feature requests, and bug reports through the following channels:
19+
- **GitHub Issues**: For raising general questions, bugs, and feature requests related to the detection-rules repo.
20+
[GitHub Issues](https://github.com/elastic/detection-rules/issues)
21+
- **Community Slack**: For informal discussions or questions (note that message history is limited to 30 days).
22+
[Elastic Security Community Slack](https://elasticstack.slack.com/archives/C06TE19EP09)
23+
24+
Support tickets related to this DaC reference implementation can be opened with Elastic, however since the logic is just a wrapper of the underlying product API's, we ask to resolve urgent DaC issues via direct interaction with the underlying [Kibana APIs](https://www.elastic.co/docs/api/doc/kibana/v8/group/endpoint-security-detections-api) or [Elastic Security Solution UI](https://www.elastic.co/guide/en/security/current/rules-ui-management.html), as we will not be able to treat DaC related support requests as a high severity (immediate time frame).
25+
26+
> [!TIP]
27+
> Questions about Kibana API usage should be directed to the Kibana repository:
28+
> [Kibana Issues](https://github.com/elastic/kibana/issues)
29+
30+
---
31+
32+
## Feature Requests
33+
34+
Feature requests for the DaC components that interact with the Elastic Security Solution (`kibana export-rules`, `kibana import-rules`, `import-rules-to-repo`, and `export-rules-from-repo`) in this repository will be handled similarly to the rest of the detection-rules repo:
35+
- **Prioritization**: Feature requests will be prioritized along with other development work in the repository.
36+
- **Schema Updates**: If there are breaking schema changes in Kibana that affect importing/exporting detection rules, those changes will be prioritized.
37+
- **Rule Engine API**: Current CLI tools leverage the rules engine API, and improvements to this will be treated as part of the ongoing development.
38+
---
39+
40+
## Reference Implementation of DaC Components
41+
42+
DaC is not a single tool. Detection as Code (DaC) is a modern security approach that applies software development best practices to the creation, management, and deployment of security rules. Here is a short summary of several components that extend upon Elastic's rule management capabilities (e.g. query validation, schema validation, unit tests, etc.) provided to help fast track users ability to implement custom DaC implementations in their private environments. If you are new to these concepts, please refer to the [DaC Documentation](https://dac-reference.readthedocs.io/en/latest/), which also provides a quickstart guide and example end-to-end CI/CD workflows. These components are configurable by using the [custom-rules](docs-dev/custom-rules.md) setup.
43+
44+
- Kibana's Rule Versioning Mechanism ([link](https://dac-reference.readthedocs.io/en/latest/internals_of_the_detection_rules_repo.html#option-2-defer-to-elastic-security))
45+
- Local rule management (e.g. autoschema generation, actions and exceptions) ([link](https://dac-reference.readthedocs.io/en/latest/internals_of_the_detection_rules_repo.html#option-1-using-the-built-in-configuration))
46+
47+
---
48+
49+
## Best Practices for Using DaC Components
50+
51+
When implementing DaC in your production environment, follow these best practices:
52+
53+
- **Design and Test Rigorously**: Since every DaC implementation will be user-specific, remember to diligently design, and thoroughly test the tools before deploying them in a production environment.
54+
- **Version Compatibility**: Before upgrading the detection-rules repo version, ensure that you test compatibility with your environment. For more information, see our [Versioning Documentation](https://github.com/elastic/ia-trade-team/issues/471%23issuecomment-2423259800).
55+
- **Limited Backward Compatibility**: We do not guarantee backward compatibility across versions for rule schemas. While we aim to make new fields optional where feasible, there may be minimum version requirements for Elastic Stack and are subject to Kibana's rule schema definitions.
56+
- **Schema Parity**: Not all fields in the schema defined in Kibana are fully supported. Some fields in the detection-rules repo are generalized (e.g., `field = dict()`), while others are more strictly defined. This is due to the complexity of the schemas and the prioritization of Elastic's internal use cases.
57+

docs-dev/experimental-machine-learning/experimental-detections.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Rules are now stored in ndjson format and can be imported into Kibana via the se
1414

1515
Earlier releases stored the rules in toml format. These can be uploaded using the
1616
[7.12 branch](https://github.com/elastic/detection-rules/tree/7.12) CLI using the
17-
[kibana upload-rule](../../CLI.md#uploading-rules-to-kibana) command
17+
[kibana import-rules](../../CLI.md#uploading-rules-to-kibana) command
1818

1919
### Uploading ML Jobs and Datafeeds
2020

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "detection_rules"
3-
version = "0.4.18"
3+
version = "0.4.19"
44
description = "Detection Rules is the home for rules used by Elastic Security. This repository is used for the development, maintenance, testing, validation, and release of rules for Elastic Security’s Detection Engine."
55
readme = "README.md"
66
requires-python = ">=3.12"

0 commit comments

Comments
 (0)