Mutation testing changes the program on purpose to find out whether the tests notice. If you can delete a line and nothing fails, you learn something about the tests that a green build never tells you.

The same move works on architectural constraints. Part 2 argued that a boundary is real when a test can fail because of it. This part goes one step further: introduce the forbidden change in a controlled fixture, confirm the check rejects it, then restore. The mutation job passes when the guardrail fails, and fails when a mutant survives.

Three examples follow, one per layer that fits in CI, each with the constraint, the check, the mutation and the restore. Then the two drills that do not fit in CI.

Example 1: the application role in PostgreSQL

The constraint from Part 1: the running application connects as app_rw, which may read and write its tables and nothing else. It must not own them, must not be able to create objects, and must not inherit anything stronger.

Ask the catalog, not the GRANT statements. PostgreSQL’s privilege inquiry functions answer for the effective privilege, including anything inherited.

-- Negative checks: every row must be false or zero.
select rolsuper, rolbypassrls, rolcreaterole, rolcreatedb
  from pg_roles where rolname = 'app_rw';
select count(*) from pg_tables
  where schemaname = 'public' and tableowner = 'app_rw';
select has_schema_privilege('app_rw', 'public', 'CREATE');
select pg_has_role('app_rw', 'migrator', 'MEMBER');

-- Positive check: the app can still do its job.
select has_table_privilege('app_rw', 'invoices', 'SELECT')
   and has_table_privilege('app_rw', 'invoices', 'INSERT');

The ownership query matters because table owners bypass row-level security unless the table is marked FORCE ROW LEVEL SECURITY. The pg_has_role query catches the quiet route to privilege: nobody granted app_rw anything, but someone made it a member of the migration role two years ago. Keep the positive check. A test that only forbids will be deleted the first time it blocks a release, and a role that can do nothing passes every negative check.

Run this against a throwaway database (a container is fine) built with the same grants script production uses. Then mutate:

grant create on schema public to app_rw;   -- mutation A
grant migrator to app_rw;                  -- mutation B

Each one should turn a check red. Revoke, re-run, green. If mutation B sails through, your check is reading pg_roles for direct grants and missing inheritance, and you have found that in a container rather than in an incident. The read-only version of the same queries can run against production metadata on a schedule, as a drift alarm.

Example 2: an IAM policy under Conftest

The constraint: no CI or routine engineering role gains an administrative capability, whatever the role is named. Terraform will tell you what it intends to do before it does it, and OPA evaluates Rego against that plan. Conftest wraps the same engine in a test-runner shape.

terraform plan -out tfplan.binary
terraform show -json tfplan.binary > tfplan.json
conftest test tfplan.json -p policy/

A policy that catches wildcard and IAM-administration actions on roles whose names mark them as routine:

package main
import rego.v1

routine(name) if startswith(name, "ci-")
routine(name) if startswith(name, "dev-")

actions(s) := s.Action if is_array(s.Action)
actions(s) := [s.Action] if is_string(s.Action)

deny contains msg if {
  some rc in input.resource_changes
  rc.type == "aws_iam_role_policy"
  routine(rc.change.after.role)
  doc := json.unmarshal(rc.change.after.policy)
  some s in doc.Statement
  s.Effect == "Allow"
  some a in actions(s)
  a in {"*", "iam:*", "iam:CreatePolicy", "iam:AttachRolePolicy", "iam:PutRolePolicy"}
  msg := sprintf("%s grants %s to a routine role", [rc.address, a])
}

The mutation is a second plan fixture, committed next to the first, in which the ci-deploy role’s policy gains "iam:*". The mutation job runs Conftest against it and asserts a non-zero exit. Both fixtures live in the repository, so this needs no cloud account to run and will keep running on every change to the policy itself, which is exactly when a Rego rule tends to break.

A note on fidelity: this catches what the plan says. For the deployed truth, pair it with the continuous privilege comparison from Part 2, or with Access Analyzer’s check for whether an edited policy grants new access relative to the version already live.

Example 3: a dependency boundary in TypeScript

The constraint: nothing under src/domain imports from src/infrastructure. dependency-cruiser takes the rule as data.

// .dependency-cruiser.cjs
module.exports = {
  forbidden: [
    {
      name: 'domain-must-not-import-infrastructure',
      severity: 'error',
      from: { path: '^src/domain' },
      to: { path: '^src/infrastructure' },
    },
  ],
};

Check with npx depcruise src --config .dependency-cruiser.cjs. The mutation is a one-line patch that adds import { PgInvoiceRepo } from '../infrastructure/pg'; to a file under src/domain, applied with git apply, followed by the check, an asserted failure, and git checkout -- src/domain.

This is the smallest of the three, and the one to start with. It takes an afternoon, and it teaches the team the shape of the exercise before anyone touches IAM.

Wiring the mutation job

Keep the mutations in a directory next to the checks they attack, one file per mutation, with a comment naming the constraint it is meant to trip. A short script applies each mutation, runs the relevant check, asserts the check failed (! conftest test ..., ! npx depcruise ..., or a psql exit code), and restores. Any mutation that survives fails the job and names itself.

Run it weekly, and on every pull request that touches the checks or the policies. Those are the two moments the guardrails themselves change, and the moments nobody is watching them.

Resist the urge to make this clever. Three mutations that run every week beat thirty that someone meant to finish.

The two drills that do not fit in CI

Break-glass access and backup restoration are the fourth layer from Part 2, and neither can be mutation-tested in a container. They need a calendar.

For break-glass, once a quarter, have someone request emergency production access through the prescribed path and record four things: the alert fired, the access expired without anyone revoking it, the audit log shows who and when, and the everyday identities could not reach the same capability by any other route. Any “no” is a finding.

For restoration, restore last night’s backup into a fresh environment and run the application’s smoke tests against it. Record the clock time from start to green. That number is your real recovery time; whatever the runbook says is a hope until the drill agrees with it. And check that the identity which runs production cannot delete the backups, because a backup inside the blast radius is part of the blast.

What to tell the team

Frame this as the cheapest penetration test you will ever run. Every mutation that survives is a finding you got for free, before an attacker or an over-eager agent found it for you. Nobody is being caught out; the check is. Give each constraint an owner, put the mutation job’s output somewhere visible, and celebrate the first surviving mutant as loudly as the first green build.

We have spent years writing tests to prove that software does what we want. It costs an afternoon to start testing that the architecture prevents what we do not.

If your team wants a second pair of hands setting up the first mutation job, jfsi can pair on it.

Sources: PostgreSQL: Access Privilege Inquiry Functions · PostgreSQL: Row Security Policies · Open Policy Agent: Terraform · Conftest · AWS: Validate policies with IAM Access Analyzer · dependency-cruiser