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
| Idea | How it applies to detections |
|---|---|
| Source of truth | Rules live in git. The repository, not the SIEM, is where a rule is defined. |
| Code review | Every change goes through a pull request before it can merge. |
| Continuous integration | Automated checks run on every pull request. |
| Continuous delivery | Deploys are automated and staged: staging first, production only after staging succeeds. |
| Drift detection | A nightly job compares what's running in Kibana with what's in git. |
index=pipeline | transaction rule_id startswith="write" endswith="promote"
The workflow
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