In Part 1 I mentioned a CI pipeline that "only checked that the YAML was well-formed." That one line covers a lot of ground. Detection-as-code, CI/CD, linting and testing are where detection engineering borrows the most from software engineering, so this lesson goes a layer deeper: what each piece does, why it exists, and where it stops.

Same deal as before: one topic, quizzed until my explanation held up, then checked against real pipelines.

This is Part 3 of Becoming a Detection Engineer.

Why detections became code

Picture the old way. Rules live in the SIEM console. Someone edits one at 2 a.m. during an incident. A week later it stops firing. Who changed it? When? What did it look like before? Nobody knows, because the console doesn't really keep that story.

RunReveal describes that world as one where "changes to detection rules are often made directly in production without proper tracking."

Detection-as-code, or DaC, fixes that by borrowing from software engineering. Rules become files, usually YAML or Sigma, stored in source control. Magonia's five-year retrospective says DaC was "inspired by software engineering practices such as Continuous Integration (CI)."

Quick vocabulary. Git is the tool that tracks changes. Platforms like GitHub and GitLab host the repos and add merge requests, pipelines and permissions on top. The umbrella term is SCM, source code management.

Just having rules as files buys a lot, before any pipeline exists:

history     every change has a who, a when and a why
rollback    a bad tune is one revert away
review      an exclusion gets a second set of eyes before it goes live
rebuild     if the SIEM gets wiped, the repo is the detection library

That third one changed how I think about exclusions. An exclusion is a blind spot you add on purpose. It deserves a review just as much as a new rule does.

GitLab's own security team keeps it simple: "Maintaining a single source of truth for detections is critical to ensure detection consistency."

Why CI/CD

Files in Git are storage. CI/CD is what makes them safe to change. CI, continuous integration, means every proposed change gets checked automatically. CD has two meanings, and the difference matters.

A typical detection pipeline looks something like this:

validate -> plan -> apply -> drift

Validate checks every rule. Plan is a dry run that shows what would change in the SIEM. Apply pushes the change. Drift runs on a schedule and catches anyone who edited the console directly, the same job from Part 1.

Continuous deployment means every merge goes live by itself. Continuous delivery means merging gets a change ready, and a person still pushes the button. Most detection pipelines I read about use delivery.

The other thing that clicked was credentials. The dry run only needs to read production, so it gets read-only access. Only the apply job, on the main branch, can write. Even a broken or malicious merge request can't do damage before a human signs off.

Magonia points out that teams who adopt a rule format and stop there miss "one of the primary benefits of codifying your detection logic," which is the pipeline. The file format is the easy part.

What makes a pipeline effective

Every source I read described a slightly different pipeline, and the same traits kept showing up:

everything goes through it   new rules, tunes, exclusions, deletions
fails early                  local checks catch typos before CI does
checks against the SIEM      a dry run against the real API, beyond the file
blocks the merge             main only moves when every job is green
new rules start quiet        shadow mode first, alerts after review
metadata is enforced         owner, ATT&CK ID, runbook, or it fails
coverage falls out of it     the ATT&CK map updates on every merge

The first one sounds obvious, and it's the one that breaks. Exclusions and deletions change what you can see as much as new rules do, so they go through the same checks. GitLab's pipeline even confirms "the detection to be deleted is still active and can be deleted" before it removes anything.

Failing early is about where the feedback lands. Fortuna puts it plainly: "The pipeline should not be the first place a rule author learns their YAML is malformed." Pre-commit hooks run the same checks on the author's machine before the change is ever pushed.

Checking against the SIEM closes part of the gap linting leaves. GitLab's validation "queries SIEM API to ensure detection query is valid," and the dry run shows what would change before anything does.

Shadow mode was the idea I hadn't seen before. Fortuna says new rules "should enter shadow mode for at least seven days." The rule runs and "writes matches to a separate index, but creates no analyst alert." Then a human looks at the volume and the sample events and decides to "tune, promote, or kill the rule." A noisy rule gets caught before it pages anyone.

Metadata and coverage go together. If CI refuses a rule without an owner, an ATT&CK ID and a runbook link, the coverage map can be built straight from the rules. In Fortuna's words: "The pipeline produces the coverage artifact as a side effect of doing the work."

Magonia sums up why all of this is worth the effort: automating the basic checks "allows reviewers to concentrate on the actual detection logic."

Two things the sources mostly skip, which I'd add from watching a real pipeline run. First, check after the deploy: read back from the SIEM what actually landed and compare it to the repo. Second, be careful with drift. If the job that syncs console changes back into the repo runs while a merged change is still waiting to deploy, it can overwrite that change. A good drift job confirms main has been fully applied before it touches anything.

Why linting

Linting reads a file and compares it against a set of house rules, without ever running it. Usually it's a script that loads a schema saying what a valid rule looks like. These fields are required. Severity has to be one of a few values. A MITRE technique has to look like T1059. On top of that, teams add their own checks: the lookback isn't zero, the log source is approved, no two rules share an ID. If anything fails, the pipeline goes red and the change can't merge.

It's cheap, fast and catches a whole class of dumb mistakes before they reach production. That's why it's always the first step.

A linter checks the rule file against the house rules, and that's where it stops. A rule asking for OriginalFileName when the real field is OriginalFilename passes every lint check and matches nothing.

Andrea Fortuna, writing about Sigma pipelines, puts it exactly: a bad rule "produces a syntactically valid query that matches nothing, and that failure is silent."

That silence is why teams add tests on top of linting.

Why testing

If linting asks "is this file well-formed?", testing asks "does this rule actually catch the thing?"

The idea comes from test-driven development in software. You write the test first, watch it fail, then write the code until it passes. For a detection, that means starting with a sample of the attack before writing the rule.

Magonia explains why the order matters. Tests are sometimes "retroactively built to match the detection logic rather than the source intelligence." If you misread the intel, the rule and the test share the mistake, and both pass.

Fortuna's rule of thumb is the clearest I found: "The minimum viable test is two fixtures: a malicious event the rule must match, and a benign event it must not match." Rapid7 says the same thing: "Positive tests prove it catches the threat; negative tests prove it doesn't fire on legitimate activity."

The negative test is the one that's easy to skip. A rule that fires on everything passes the positive test too. The benign sample is what proves it can tell the difference.

Those sample events live in the repo next to the rule, and CI runs them on every change. Then there's end-to-end testing. Datadog's Threatest post calls sample-log testing "a first step." The real proof is to "reproduce the attacks we expect to detect and verify on the other end that an expected alert is produced." That catches failures a unit test never sees. One on Datadog's list: "Detection rules may be unexpectedly disabled."

Put together, it's a ladder:

lint          is the file well-formed?           cheap, every change
unit test     does the logic match a sample?     cheap, every change
end-to-end    does a real attack get caught?     expensive, some rules

Each rung catches what the one below can't. Unit tests come with a catch, though. A test is only as honest as its sample. If you write the sample by hand and make the same mistake as the rule, both pass and both are wrong. The samples have to come from real data.

Why it matters more with AI

AI catches some of these mistakes, some of the time. A model only knows your field names if it checks your data. Otherwise it guesses, and a wrong guess doesn't throw an error, so nothing tells it to look again. A pipeline check gives the same answer every time, and that consistency is the point.

That makes the pipeline more important as AI joins the workflow. Teams are starting to use AI agents that read closed alerts, spot false positives and draft exclusions. The safe version is the "agents propose, humans approve" pattern from Part 2: the agent can open a merge request, but it can't change production.

The way I think about why: a tuning agent reads attacker-controlled data. Command lines, filenames and user agents all land in logs, and an attacker can put text there on purpose. If an agent could be talked into "exclude this host" and had write access, the attacker would have switched off the detection for themselves. With a human gate, someone sees a strange exclusion and asks why.

What it costs

None of this is free. A merge request is slower than clicking in the console when something's on fire. It only works if everyone uses it, which is why drift detection has to exist at all. And end-to-end testing is, in Datadog's words, "time-consuming, challenging to scale, and hard to automate." That's a big reason so many teams stop at linting.

A green pipeline can also make review feel optional. Fortuna argues "the pipeline is the gatekeeper, not the analyst who happens to review the pull request." That works once the pipeline tests logic as well as format. Until then, the reviewer is the fact check. Elastic's take fits better for most teams: "with this power comes an increased responsibility on rule reviewers."

What I'd tell someone starting out

Put your rules in Git, even before you have a pipeline. History and review alone are worth it. Give the dry run read-only credentials and keep the deploy behind a human. Then open your linter and read what it actually checks. You might be surprised how little it knows about your data. Add a positive and a negative test to the next rule you write, and let it run quietly in shadow mode before it alerts anyone. If AI is part of your workflow, make it go through the same merge requests as everyone else.

A green lint check tells you the file is well-formed. Tests tell you whether the rule fires on the right thing. Or, as Fortuna puts it, "version-controlling garbage rules is still garbage, just with a better commit history."

Which leads to the next question: how do you know which fields are real in the first place? That starts with how the data gets in. Datadog lists "The parsing pipeline may incorrectly parse CloudTrail logs" among the ways a detection breaks, and Fortuna suggests confirming "that your log sources are complete" before putting engineering discipline on the rules at all.

Next up, Part 4: data ingestion, and how a log goes from a source to something a rule can actually query.

Readings that helped

Most of these come from vendors with a product to sell, so I used them to explain how things work.