InfraSec: AI does the work, deterministic code decides, a human approves production

Inspiration

While deploying an ML pipeline I hit the same class of failure three times, and every one traced back to GitHub OIDC and IAM configuration. The pipeline code was fine each time. The identity and permissions around it were wrong, and nothing flagged that until a deploy failed, or worse, until it succeeded with more access than it should have had.

AI can write infrastructure code in seconds, but the step after that, where identity, permissions and blast radius get checked, is still manual.

My principle: AI does the work, deterministic code makes the decision, and a human approves what reaches production. The agent can explain a risk and propose a fix, but it cannot decide what counts as risky, cannot approve its own fix, and cannot merge. Every fix it writes must pass the same fixed rules as a change from any developer.

What it does

On a merge request that touches Terraform:

  1. Gate. CI sends the Terraform plan to the decision API. A deterministic evaluator returns auto_apply or hold. On hold, the pipeline fails.
  2. Notify. A CI job posts the decision report on the MR and mentions the Duo flow, so the agent starts without a human prompt.
  3. Explain and fix. The custom Duo flow posts a plain-language explanation, reads the file on the target branch, and commits the exact previous value back, tagged [infrasec-autofix]. If it cannot find the previous value, it commits nothing and asks a human.
  4. Re-check. CI runs the same gate on the fixed commit.
  5. Automerge. A CI job merges only if the commit is still the MR head, carries the autofix tag, and the gate passed. The merge call pins the head SHA, so any later push cancels it.
  6. Production approval. Deploying to production is a manual job. A human approves the outcome.

A repo-aware suggester (scripts/infrasec_suggest.py) also reads the code and generates least-privilege IAM, a pinned OIDC trust and environment config as Terraform, which the same gate then reviews.

What runs where

Component Where it runs What it does
Decision API (agent/: plan parser, OIDC diff, cost estimate, decision engine, handler.py) AWS Lambda (container image), IAM-authenticated Function URL, deployed with Terraform (terraform/) Receives a plan, returns auto_apply or hold with reasons. The only place a verdict is made.
Decision history and scoped GitHub OIDC client role AWS (Terraform-managed) Stores decisions and lets pipelines sign requests without long-lived keys.
Gate, notify and automerge jobs GitLab CI runners Run terraform plan, call the Lambda API with a signed request, post the report, merge verified fixes.
Autofix agent (flows/infrasec-autofix.yaml) GitLab Duo Agent Platform (custom flow) Explains the hold and commits the minimal fix. Cannot change the verdict, cannot merge.
Suggester (scripts/infrasec_suggest.py) GitLab CI runners (deterministic Python) Generates IAM, OIDC and environment Terraform from the repo's code.
Status page (deploy-app/) Google Cloud Run Deployed by CI from main after the gate passes.

Nothing is downloaded or self-hosted for the agent. It runs on GitLab's Duo Agent Platform from a flow I defined in YAML and registered in the flow editor. The only infrastructure I host myself for the decision is the Lambda API. An optional GLiNER classifier can attach advisory context to the API response, but it cannot change the decision.

Path B: what existed, and what I added on or after October 5

Original project (MIT): https://github.com/spacey-cadet/infra-sec

Existed before October 5: Infra Risk

  • Deterministic, fail-closed evaluator that reads a Terraform plan and returns auto_apply or hold.
  • Rules for OIDC trust widening, IAM and policy changes, new resource types, and cost change of at least $2/month or 40%.
  • IAM-authenticated decision API on AWS Lambda with Terraform, and the infra-risk Python client.
  • A GitHub Actions example. A failed check ended with a human reading logs.

Built during the Submission Period

  • GitLab CI pipeline: infrasec:decide, infrasec:notify, infrasec:automerge, and a manual deploy:production.
  • Custom Duo flow flows/infrasec-autofix.yaml, triggered by a mention on the MR.
  • Scripts infrasec_notify.py and infrasec_automerge.py, and the suggester.
  • Demo target with a deliberately risky security group (demo-target/), tests, and the Cloud Run deployment (deploy-app/).
  • Fresh deployment of the Lambda decision API: function terraform-guardian-agent (the earlier name of the Infra Risk service) was created on 2026-10-16 and its container image was last updated on 2026-10-27 (AWS get-function, us-east-1). Cloud Run deployment on [DATE].

The decision logic itself did not change. The verdict still comes only from the deterministic evaluator.

Autonomy level: Supervised

Once a risky change is pushed, the loop runs without a person: block, explain, fix, re-check, merge. The human approves the outcome through the manual production job, so this is Supervised. It is not Hands-off because a person still approves production, and not Assisted because nobody approves the fix or the merge.

Guardrails:

  • The verdict is deterministic. The agent cannot change it.
  • The agent cannot merge. GitLab blocks /merge in AI workflows, so a CI job with a scoped token merges.
  • It fails closed. If the agent cannot find the exact previous value, it commits nothing. If the fixed commit still gets hold, nothing merges.
  • Stale fixes are rejected because the merge pins the head SHA.

How GitLab is used

GitLab feature Role
CI/CD pipeline The whole control loop is visible in the MR pipeline
Duo Agent Platform custom flow Explains the hold and commits the fix, using MR notes and commits
Duo Sessions The agent's tool calls and reasoning, which I used to debug runs
Project access token and CI/CD variables Scoped merge token and flow handle, no secrets in the repo
Merge requests Decision, agent actions and merge all recorded in one place

Lifecycle stages covered

Plan (agent explanation), Create (agent commit), Verify (decision API and tests), Secure (OIDC, IAM and cost rules), Release (automerge, then manual production approval), Govern (every decision, agent action and merge recorded on the MR, with agent commits tagged [infrasec-autofix]).

Challenges

  • GitLab blocks /merge in AI workflows. The agent got a 403. Merging moved to a CI job with a scoped token, which is safer.
  • The agent guessed a value. It restored 10.0.0.0/16 when main had 10.0.0.0/8. The gate could have passed on a wrong value. The prompt now requires reading the file on the target branch, restoring the exact previous value, and stopping if none is found.
  • A race after the fix. A later push could still be merged. Automerge now checks the head commit and pins its SHA.
  • Keeping AI out of the decision. The verdict stays deterministic and reproducible.

What I learned

Agents are most trustworthy around a deterministic core. Prompts need hard stops. And the Duo session log is the fastest way to debug an agent. I had never used GitLab before this hackathon.

What's next

Azure OIDC federation and Kubernetes RBAC rules, auto-promotion of low-risk changes under a configurable threshold, and decision trends over time.

Try it

Built With

Share this project:

Updates

Submission history