Dev.to Security 🔐 Cybersecurity 👁 0 📖 10 min read

Invariants as Code: A Third Paradigm for Continuous Security Reasoning

✓ Human-authored analysis; AI used for formatting and proofreading. Security tools fit into a recognized category: CSPM, SAST, scanner, policy engine, IaC. The product names compete inside each box. The boxes haven't m

✓ Human-authored analysis; AI used for formatting and proofreading.

Security tools fit into a recognized category: CSPM, SAST, scanner, policy engine, IaC. The product names compete inside each box. The boxes haven't moved in a decade.

A security engine we'd built for AWS configuration evaluation turned out to evaluate Azure, GCP, Kubernetes, GitHub branch protection rules, Cloudflare DNS records, and Microsoft 365 Conditional Access policies without a single line of engine code changing. Six provider types. Five different operating models. Zero engine modifications.

That doesn't fit the CSPM box. CSPM tools are cloud-specific. It doesn't fit the policy-engine box either, policy engines gate admissions; this evaluates continuously. It doesn't fit SAST, which scans source code, or vulnerability scanners, which check resources against benchmarks.

This is System Invariant as Code.

The paradigm comparison

Paradigm Primitive Example tools Answers
Infrastructure as Code Resource declaration Terraform, CloudFormation, Pulumi "What should exist?" → create it
Policy as Code Rule evaluation OPA/Rego, Sentinel, Conftest "Is this change allowed?" → gate it
** System Invariant as Code** Predicate over observations across time Stave "What must always be true — and what changed, what's missing, and what combines into something worse?"

The visible difference is the answer. The structural difference is the primitive.

Infrastructure as Code declares what should exist. The code is prescriptive:

resource "aws_s3_bucket" "data" {
  bucket = "prod-data"
  versioning { enabled = true }
}

A provisioning engine reads the declaration and makes reality match. The engine is per-cloud (different Terraform provider modules for AWS, Azure, GCP) and the declaration is per-resource.

Policy as Code evaluates rules against input:

deny[msg] {
  input.kind == "Deployment"
  not input.spec.securityContext.runAsNonRoot
  msg := "Deployments must run as non-root"
}

It runs at admission time, can this change proceed?. It evaluates a single resource against a rule. It doesn't hold the full inventory; it doesn't compare across time; it doesn't reason about absence.

System Invariant as Code declares what must always be true:

- id: CTL.S3.ENCRYPTION.001
  unsafe_predicate:
    any:
      - field: properties.storage.encryption.algorithm
        op: equals
        value: none
  severity: critical

An evaluation engine reads the predicate, evaluates it against observed state, and emits findings. The engine is content-independent. It doesn't know the predicate is about S3, encryption, or AWS. It evaluates property paths against operators.

That last difference makes the paradigm work.

The compiler analogy

A compiler doesn't know whether the program is a game, a web server, or a database. It knows syntax, semantics, and execution. The program is content. The compiler is infrastructure.

Compiler concept System Invariant as Code equivalent
Source code Control definitions (invariants in YAML)
Compiler Engine (parser + predicate evaluator)
Input data Observations (captured infrastructure state)
Program output Findings (violations, evidence, deltas)
Standard library Built-in predicates, triage templates, chain vocabulary
Linking Chain composition (linking findings across controls)
Runtime Time-series evaluation (multi-snapshot execution)

If a control catalog is a set of programs in this paradigm, the engine is the compiler. New controls don't require compiler changes. New providers don't require compiler changes. The compiler doesn't care.

This is the SQLite property applied to security: the engine is infrastructure, the controls are applications. SQLite evaluates SQL against tables without knowing whether the schema is a CMS, a checkout cart, or a flight scheduler. An Invariant-as-Code engine evaluates predicates against observations without knowing whether the observation is an S3 bucket or an M365 group.

Why this is different from IaC

IaC and Invariant as Code look similar (both are declarative, both are "as Code") but they solve different problems:

IaC is per-resource. Invariants are cross-resource. A Terraform module defines one resource. An invariant can reason across the entire inventory. No IAM policy may reference a resource that doesn't exist is a cross-inventory assertion. IaC can't express it because IaC doesn't hold the full inventory.

IaC is creation-time. Invariants are continuous. Terraform runs when you deploy. Invariants evaluate whenever observations are captured continuously, including after manual changes, drift, and out-of-band deletions that IaC doesn't know about.

IaC doesn't survive deletion. When a resource is deleted outside Terraform, the declaration becomes stale. Reality diverges from code. Invariants don't have this problem. They evaluate the actual state, not the declared state. Ghost references (a policy pointing at a deleted resource) appear in invariants because invariants see the post-deletion world.

IaC is provider-specific. Invariants are provider-agnostic. The Terraform AWS provider doesn't work for Azure. The invariant encryption must be enabled on storage works across S3, Azure Blob, GCP Cloud Storage, and any future storage provider. Because the invariant is a predicate over a property path.

The proof: six providers, zero engine changes

The engine was built for AWS configuration evaluation. To validate that the architecture was content-independent, we tried other provider domains one at a time, treating each as an attempt to find the engine's hidden assumptions.

Provider type What it manages Engine code changes
Cloud infrastructure AWS, Azure, GCP 0
Container orchestration Kubernetes 0
Developer platform GitHub 0
Edge security Cloudflare DNS / WAF 0
SaaS productivity Microsoft 365 0

Five categories of security target. One evaluation engine. Zero modifications.

Adding each provider was new YAML files in the control catalog and a new namespace of observations. The engine looked the same on day one and day six. The triage output, drift detection, ghost reference pattern and the compound-chain composition were all the same.

What the engine evaluated:

  • AWS: "S3 bucket policy allows public read AND the public-access-block is disabled"
  • M365: "Conditional Access policy requires MFA AND legacy authentication is enabled" (the MFA bypass path)
  • GitHub: "branch protection requires reviewers AND CODEOWNERS references a deleted team" (the review-bypass via ghost owner)
  • Cloudflare: "DNS record points at a CNAME target AND the target resource no longer exists" (subdomain takeover)

Each of those is the same evaluation primitive: predicate over properties, optionally compared across snapshots, optionally composed with other findings into a compound chain. The domain knowledge lives in the control YAML. The engine just executes.

Three architectural properties made this work and define the new category.

The three defining properties

These are invariants of the paradigm.

1. Evaluation reasons about absence, not just presence

Every other security tool evaluates what exists. CSPM tools list buckets and check each one. SAST tools list functions and analyze each one. Vulnerability scanners list packages and look up each one.

Invariant as Code also evaluates what's missing. The predicate "every IAM policy must reference a resource that exists" requires holding two complete inventories, what's referenced AND what exists and computing the difference.

Cross-inventory reasoning is different from per-resource scanning. A ghost reference is a relationship between the reference site and the absent target. You can't detect it by iterating over resources.

We have 33 ghost-reference controls across 5 providers today. None of them would be expressible in the scanner model. Each one detects a specific deletion-survives-pointer pattern (IAM policy → deleted role; CODEOWNERS → deleted team; Cloudflare CNAME → deleted CDN endpoint; Conditional Access → deleted group; CloudWatch alarm → deleted Lambda).

2. Time is structural

Every other tool takes a snapshot. Some record when findings first appeared. Stave evaluates across a time-series. Drift, duration, and temporal confirmation are evaluation primitives.

The engine was designed around multiple snapshots from day one. A control YAML can say "this property must not change between observations." That's a predicate evaluated across two snapshots, not one. The same primitive that detects an S3 bucket flipping from private to public detects an M365 Conditional Access policy weakening from MFA-required to MFA-optional. The engine doesn't know the difference. It sees a property that changed between snapshots.

3. The engine is content-independent

Six providers, zero engine changes. The engine doesn't know the domain. The controls encode domain knowledge. The engine evaluates predicates.

This is the property that makes the other two scalable. If the engine knew about S3, then adding GitHub support means modifying engine code, writing GitHub-specific handlers, and risking regressions on the AWS path. The engine doesn't know about S3 or any specific AWS service. So adding GitHub is writing new YAML files.

What Invariant as Code extends in Policy as Code

OPA / Rego / Sentinel are the closest existing paradigm. Policy as Code evaluates rules against input. The two differ on three axes that require architectural support:

Cross-inventory reasoning. OPA evaluates one input at a time. The input might be a JSON document representing a single resource change. To express "this policy references a resource that doesn't exist" in OPA, you'd need to fetch the full inventory at evaluation time and join across it. That works for a 50-resource deployment. It doesn't work for an inventory of 50,000 resources where the policy must hold across all relationships. Invariant as Code holds both inventories and computes the difference.

Temporal evaluation. OPA evaluates at a single point in time. The invariant "this property must not change between observations" requires comparing snapshots. Two inputs to compute the delta.

Compound composition. OPA produces individual pass/fail results. The invariant "if finding A and finding B and finding C all exist on related assets, the compound severity is critical" requires composing individual findings into higher-order findings. A second evaluation pass over the first pass's output. Policy engines don't have this loop.

Policy as Code is gate-based: should this change proceed? Invariant as Code is continuous: what's true right now, what changed, what's missing, and what combines into something worse?

The historical pattern

Each time an abstraction layer appears, a new category emerges:

Before New abstraction Category created
Manual server provisioning Terraform Infrastructure as Code
Manual testing JUnit / pytest Test as Code
Manual CI builds Jenkins / GitHub Actions Pipeline as Code
Manual security scanning An invariants engine Invariant as Code

Each prior abstraction made the previous approach look manual. Provisioning servers using Terraform. Using test runner to run tests. Using pipeline to build. The new abstraction names the primitive, and that creates the category.

Manual security scanning today looks like: list resources, iterate over them, check each one, report violations. That's per-resource, point-in-time and presence-only. It misses ghost references because it can't see absence. It misses drift because it sees one snapshot. It misses compound paths because it doesn't compose.

An invariants engine fixes all three by giving you a primitive: predicate + observation + time → finding. Everything else is content.

The implications

Any system that can be described as "assets with properties" is evaluable. SaaS applications, on-prem Active Directory, network devices, CI/CD pipelines, container registries, DNS providers, identity providers, database configurations, API gateways or anything with a configuration state capturable as key-value properties on named assets. You write controls. The engine evaluates.

Provider expansion becomes catalog work. Cloud-security vendors who want to add SaaS posture management today have to build new products. An invariants engine adds SaaS posture management by adding YAML files.

The ghost reference and compound-chain patterns generalise. A Microsoft 365 Conditional Access policy referencing a deleted group is the same shape as an S3 bucket policy referencing a deleted IAM role. Different domain; same detection architecture. The third compound that nobody saw coming, say, missing MFA + legacy auth enabled + no Conditional Access is just another chain definition over existing predicates.

The 1,300+ controls across six providers are the proof that the abstraction works. The product is the primitive: declare invariants, evaluate against observations, reason across time and absence. The controls are content on top of that primitive.

First in category means there's no competitive benchmark. Anyone can ship more checks. Prowler has many for some AWS services. Anyone can ship a prettier dashboard. Nobody can ship the primitive without building the engine. Nobody can claim "we detect what's missing, we reason across time, and we evaluate any provider without engine changes" because those are architectural properties of the paradigm.

Infrastructure as Code changed how we provision. Policy as Code changed how we gate. Invariant as Code changes how we reason about correctness continuously, across the full inventory, across time, providers, and the gaps that no single resource carries.

How this relates to existing compliance mods

The three-paradigm map names where each existing tool lives. Paradigm 1 (Infrastructure as Code) is Terraform, Pulumi, CloudFormation as provisioning languages. Paradigm 2 (Policy as Code) is the framework-coverage layer: OPA, Sentinel, and SQL-against-live-cloud mods like turbot/steampipe-mod-aws-compliance are all Paradigm-2 implementations. They encode framework requirements as queries that gate or score current state. The AWS Compliance Mod is the canonical open-source Paradigm-2 artifact for AWS, with 540+ controls across CIS / PCI / HIPAA / NIST and a mature Powerpipe dashboard surface. Paradigm 3 (Invariants as Code) the article argues for: declare the cross-resource invariants, evaluate over observation snapshots, reason across time and absence. The three paradigms aren't competitors; they layer. Provision in Paradigm 1, gate in Paradigm 2, reason in Paradigm 3 and the two evaluator paradigms render in the same Powerpipe dashboard. The two-evaluator comparison is at aws-compliance-mod.

The engine is Stave, an air-gapped Go CLI for configuration safety evaluation. The 3,000+ controls live in its control catalog; the engine reads them as data and evaluates predicates over observation snapshots. The "zero engine changes across six providers" claim is auditable in the repo's commit history.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.