# elastic/detection-rules architecture

> Architecture diagram of the GitHub repository elastic/detection-rules, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/elastic/detection-rules
- Repository: https://github.com/elastic/detection-rules

## Overview

This project is a Python‐based “Detection Rules” repository designed to build, validate, test, package, and integrate detection rules into Elastic Security’s Detection Engine (Kibana). The project is not a typical full-stack web application but a highly modular library and CLI tool that supports “detections as code” with multiple supporting libraries (e.g. for Kibana API interactions and KQL parsing) and extensive testing and workflow automation.

##### Main Components
- CLI Interface & Entry Points: There is a “detection_rules” command line interface (see the “CLI.md” and the __main__.py files in relevant modules) that developers and automation use to create, validate, import, search, and test rules.
- Rule Core Module: The detection_rules/ directory contains the core Python module. It is responsible for parsing, validating, formatting, and packaging rules. It also integrates with configuration files in the “etc” folder (including ECS and Beats schemas) and rule locking/versioning.
- Rule Storage: The “rules/” directory holds the production detection rules in .toml format. In addition, building block rules are in “rules_building_block/”. These are the artifacts that ultimately get imported into Elastic Security.
- Threat Hunting Component: The “hunting/” directory contains an additional module for threat hunting queries and associated scripts. It acts as a complementary component to the detection rules, and it is independently testable.
- Integration Libraries: The project includes two Python libraries in “lib/kibana” and “lib/kql”. The kibana library encapsulates API calls and functions to interface with Kibana’s Detection Engine, while the kql library handles the parsing, transformation, and validation of Kibana Query Language expressions.
- Automated Testing & CI/CD Workflows: The “tests/” directory contains unit tests and integration tests for the rules and helper utilities. Additionally, the .github folder with its workflows and issue/PR templates integrates automated code quality and release management.
- Dependency & Environment Management: The Makefile and pip configuration (pyproject.toml, pre-commit config, etc.) specify toolchain dependencies. Additionally, instructions are provided for installing this project into a Python 3.12+ virtual environment.

##### Relationships and Interactions
- User Interaction Flow: Developers interact with the repository via the CLI (and possibly via integrated IDE tools) to create and update rule files. Their changes are version controlled and then pushed to GitHub.
- CI/CD Pipeline: Upon commit, GitHub Actions (shown by numerous workflow files inside .github/workflows) kick off unit tests and validation commands (using “test”, “validate-rule” and “toml-lint”) that ensure rule correctness.
- Integration Points: The detection_rules core module communicates with the Kibana integration library to push or synchronize packaged rules with Elastic Security’s Detection Engine. Simultaneously, the kql library is used to convert or validate query language expressions.
- Testing & Validation: The “tests/” module runs unit and integration tests against each component (CLI, rule parser, packaging, transformation utilities, and even remote rule validation). This ensures high-quality output before deployment.
- External Dependencies: The system depends on external Python libraries (such as requests, Click, jsonschema) and on external services like Kibana and Elasticsearch. These can be shown as external systems that interact with the core modules during rule ingestion or update.

##### Architectural Patterns & Design Principles
- Modular Design: Each piece (CLI, core rule handling, hunting queries, external API integrations, and testing routines) is separated into its own directory and Python package to maintain clean separation of concerns.
- Extensibility & Reusability: The libraries (kibana and kql) are built as reusable Python packages. Similarly, rule definitions are stored in a declarative format (.toml), facilitating easy updates and reuse.
- Automation & Integration: The repository leverages GitHub workflows for CI/CD (automated testing, linting, release management) and uses a Makefile to standardize environment setup.
- “Detections as Code” Paradigm: The design supports turning rule creation into a codified process, making it clear how each part—from rule creation, validation, packaging, to integration—is handled.

– Start with end-user or developer initiating a CLI command.
– Break the system into layers:
- Input Layer: CLI and rule creation interfaces.
- Processing Layer: Core detection_rules module along with libraries (kibana and kql) and configuration (etc).
- Storage Layer: File system folders hosting rule definitions.
- Testing & Automation Layer: CI/CD pipelines (GitHub workflows), unit tests in the tests/ directory.
- External Integration: Kibana and Elastic Security Detection Engine as the endpoint for rule deployment.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Developer Interaction
    Developer["Developer"]:::input

    %% Input Layer
    subgraph "Input Layer"
        CLI_Doc["CLI Documentation"]:::input
        DRules_CLI["Detection Rules CLI Entry"]:::input
        Hunting_CLI["Hunting CLI Entry"]:::input
    end

    %% Processing Layer
    subgraph "Processing Layer"
        Core_Module["Rule Core Module"]:::processing
        Config_Files["Configuration Files"]:::processing
        Kibana_API["Kibana API Library"]:::processing
        KQL_Parser["KQL Parser"]:::processing
        Hunting_Module["Threat Hunting Module"]:::processing
    end

    %% Storage Layer
    subgraph "Storage Layer"
        Prod_Rules["Production Rules"]:::storage
        Building_Block["Building Block Rules"]:::storage
    end

    %% Testing & Automation Layer
    subgraph "Testing & Automation Layer"
        Automated_Test["Automated Testing"]:::automation
        CICD_Workflows["CI/CD Workflows"]:::automation
        Makefile_Node["Makefile"]:::automation
        Pip_Config["Pip Configuration"]:::automation
        PreCommit["Pre-commit Config"]:::automation
    end

    %% External Integration
    subgraph "External Integration"
        Kibana_DE["Kibana's Detection Engine"]:::external
        Elasticsearch["Elasticsearch"]:::external
    end

    %% Developer to Input Layer
    Developer -->|"initiates"| CLI_Doc
    Developer -->|"initiates"| DRules_CLI
    Developer -->|"initiates"| Hunting_CLI

    %% Input Layer to Processing Layer
    DRules_CLI -->|"create/validate"| Core_Module
    Hunting_CLI -->|"execute_hunting"| Hunting_Module
    CLI_Doc ---|"reference"| DRules_CLI

    %% Processing Internal Connections
    Core_Module -->|"load_configs"| Config_Files
    Core_Module -->|"API_call"| Kibana_API
    Core_Module -->|"query_parse"| KQL_Parser

    %% Processing to Storage
    Core_Module -->|"package_rules"| Prod_Rules

    %% Testing & Automation Interactions
    CICD_Workflows -->|"trigger_tests"| Automated_Test
    Automated_Test -->|"test_execution"| Core_Module
    Automated_Test -->|"test_execution"| Hunting_Module
    Makefile_Node -->|"env_setup"| Core_Module
    Pip_Config -->|"env_setup"| Core_Module
    PreCommit -->|"env_setup"| Core_Module

    %% Integration with External Systems
    Kibana_API -->|"sync_rules"| Kibana_DE
    Core_Module -->|"deploy_rules"| Kibana_DE
    Kibana_DE -->|"store_data"| Elasticsearch

    %% Click Events for Input Layer
    click CLI_Doc "https://github.com/elastic/detection-rules/blob/main/CLI.md"
    click DRules_CLI "https://github.com/elastic/detection-rules/blob/main/detection_rules/__main__.py"
    click Hunting_CLI "https://github.com/elastic/detection-rules/blob/main/hunting/__main__.py"

    %% Click Events for Processing Layer
    click Core_Module "https://github.com/elastic/detection-rules/tree/main/detection_rules/"
    click Config_Files "https://github.com/elastic/detection-rules/tree/main/detection_rules/etc/"
    click Kibana_API "https://github.com/elastic/detection-rules/tree/main/lib/kibana/"
    click KQL_Parser "https://github.com/elastic/detection-rules/tree/main/lib/kql/"
    click Hunting_Module "https://github.com/elastic/detection-rules/tree/main/hunting/"

    %% Click Events for Storage Layer
    click Prod_Rules "https://github.com/elastic/detection-rules/tree/main/rules/"
    click Building_Block "https://github.com/elastic/detection-rules/tree/main/rules_building_block/"

    %% Click Events for Testing & Automation Layer
    click Automated_Test "https://github.com/elastic/detection-rules/tree/main/tests/"
    click CICD_Workflows "https://github.com/elastic/detection-rules/tree/main/.github/"
    click Makefile_Node "https://github.com/elastic/detection-rules/tree/main/Makefile"
    click Pip_Config "https://github.com/elastic/detection-rules/blob/main/pyproject.toml"
    click PreCommit "https://github.com/elastic/detection-rules/blob/main/.pre-commit-config.yaml"

    %% Styles
    classDef input fill:#AED6F1,stroke:#1F618D,stroke-width:2px;
    classDef processing fill:#ABEBC6,stroke:#229954,stroke-width:2px;
    classDef storage fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef automation fill:#F5CBA7,stroke:#B03A2E,stroke-width:2px;
    classDef external fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
