index=detections sourcetype=git | where reviewed=true AND tested=true

Detection-as-Code: shipping detections like software.

I built an end-to-end pipeline in my homelab where every detection rule is versioned, reviewed, tested, deployed to staging, proven with a real attack, and only then promoted to production.

index=siem action=rule_edit | where isnull(review) AND isnull(tests)

Why it matters

In most SIEMs, a rule change is a live edit in a GUI. There's no review, no tests and no history. Over time, what's actually running drifts away from what the team thinks is running.

I wanted my detections handled the way good software is: written in files, reviewed before they merge, tested automatically, and shipped through a pipeline that refuses to go further when something is wrong.

index=practices | table idea, how_it_applies

The five ideas

The five software practices applied to detection rules
IdeaHow it applies to detections
Source of truthRules live in git. The repository, not the SIEM, is where a rule is defined.
Code reviewEvery change goes through a pull request before it can merge.
Continuous integrationAutomated checks run on every pull request.
Continuous deliveryDeploys are automated and staged: staging first, production only after staging succeeds.
Drift detectionA nightly job compares what's running in Kibana with what's in git.

index=pipeline | transaction rule_id startswith="write" endswith="promote"

The workflow

Detection-as-Code pipeline flowchart A rule is written on a branch, then a pull request runs three CI checks, then it is reviewed and squash merged, then deployed to staging. A decision follows: does the Atomic Red Team test raise the rule's alert? If no, the pipeline stops and production is unchanged. If yes, the rule is promoted to production. A nightly drift check compares production with git. Branch + rule rule · tests · ADS PR: 3 CI checks GitHub-hosted Review + merge squash merge Deploy to staging self-hosted runner Atomic test alerts? no yes Stop production unchanged Promote production, by rule ID Nightly drift check Kibana vs git Detection-as-Code pipeline flowchart A rule is written on a branch, then a pull request runs three CI checks, then it is reviewed and squash merged, then deployed to staging. A decision follows: does the Atomic Red Team test raise the rule's alert? If no, the pipeline stops and production is unchanged. If yes, the rule is promoted to production. A nightly drift check compares production with git. Branch + rule rule · tests · ADS PR: 3 CI checks GitHub-hosted Review + merge squash merge Deploy to staging self-hosted runner Atomic test alerts? no yes Stop production unchanged Promote production, by rule ID Nightly drift check Kibana vs git
Figure: how a rule moves from a branch to production. If the Atomic Red Team test doesn't raise the rule's alert in staging, the pipeline stops and production stays unchanged.

Step 1

Write the rule

Each detection starts as a TOML rule file with its ATT&CK mapping, known false positives and an investigation guide.

Alongside it I commit recorded test data (a real attack sample and a benign look-alike) and a short Alerting & Detection Strategy (ADS) doc.

Step 2

Three CI checks on every pull request

These run on GitHub-hosted runners:

  • Rule validation: schema, query syntax, ECS field names and ATT&CK IDs, plus a strict build using the same loader as the deploy.
  • House standards: my own pytest tests.
  • Replay: a throwaway Elasticsearch loads the recorded data. The rule must fire on the attack and stay silent on the benign data.

Step 3

Review and merge

Every pull request has a checklist covering field behavior, false positives, triage context and severity.

The main branch is protected, so merging is blocked until the checks pass. Changes land as squash merges.

Step 4

Deploy to staging

Merging triggers a deploy from a self-hosted runner inside the lab. The runner only makes outbound connections.

It deploys into a separate Kibana staging space using a least-privilege service account.

Step 5

Attack and verify

For each changed rule with a mapped test, the pipeline runs the matching Atomic Red Team test on a dedicated Windows target, then waits for that rule's alert in staging.

No alert means the deploy stops.

Step 6

Promote to production

Rules reach production only if staging succeeded. They update in place by rule ID.

After it ships

Drift detection

A nightly job compares Kibana with git, looking for GUI edits and for disabled, missing or untracked rules. Anything that differs becomes a GitHub issue.

index=pipeline | table decision, reason

Design decisions

  • CI runs on GitHub's runners. Unreviewed code from a pull request never runs near lab credentials. The lab runner only gets involved after a reviewed merge.
  • A separate staging space. Test alerts from attack verification stay apart from real ones.
  • A least-privilege deploy identity. The deploy uses a service account that has only the access it needs.
  • Standards enforced as code. My house standards are pytest tests, so they're checked on every pull request instead of being remembered.
  • Every rule has an ADS doc. The reasoning behind a detection ships with the detection.

index=pipeline | stats values(tool)

Stack

  • Elastic Security 9.5 (self-managed)
  • Elastic's detection-rules CLI
  • GitHub Actions
  • Atomic Red Team
  • Elastic Defend
  • Python