Pular para o conteúdo
← DevSecOps and software supply chain security

Aula 6 de 7 · 8 min

Infrastructure as code and policy as code

When servers, networks and permissions are defined in files, they can be reviewed and tested like software. Learn how Terraform and CloudFormation work, and how policy as code catches misconfigurations before deployment.

Por enquanto esta página está em inglês.

Not long ago, setting up infrastructure meant clicking through consoles or running commands by hand. Nobody could say for certain how a server had been configured, and two “identical” environments rarely were. Infrastructure as code (IaC) replaced this with files that describe the desired infrastructure: networks, virtual machines, storage buckets, firewall rules and permissions. A tool reads the files and makes reality match them.

Two common IaC tools

ToolMade byHow it works
TerraformHashiCorpDeclarative configuration files; works with many cloud and service providers through plug-in “providers”; keeps a state file recording what it manages
AWS CloudFormationAmazon Web ServicesTemplates in JSON or YAML describing AWS resources, deployed together as a “stack”

Both are declarative: you describe what should exist, and the tool works out the steps. Both also let you preview changes before applying them, which is where security reviews fit naturally.

Why IaC is a security opportunity

  • Every change is visible in version control, with an author, a review and a history.
  • The same reviewed template produces the same environment every time, removing hand-made differences.
  • Mistakes can be caught automatically before anything is deployed, which is the heart of shifting left.
  • Recovery is faster: a damaged environment can be rebuilt from known-good code.

It is also a risk. A single insecure template, such as a storage bucket open to the public or a firewall rule allowing the whole internet in, is copied into every environment built from it. And IaC files, state files and pipeline variables can contain secrets or reveal the layout of your infrastructure, so they need the same protection as source code.

Policy as code

Policy as code means writing security rules in a machine-readable form so a tool can check them automatically. Instead of a document saying “storage must never be public”, you have a rule that fails the pipeline when a template makes storage public. Open Policy Agent (OPA), a graduated project of the Cloud Native Computing Foundation, is a general-purpose policy engine with its own policy language, Rego, used for IaC checks, Kubernetes admission control and more. Scanners such as Checkov come with many ready-made checks for Terraform, CloudFormation and Kubernetes files.

Example policyMisconfiguration it prevents
Storage buckets must block public accessAccidental exposure of private files
No security group may allow administrative ports from anywhereRemote administration exposed to the internet
Encryption at rest must be enabled for databases and volumesReadable data if storage is copied
IAM policies may not use wildcard actions on all resourcesOver-permissioned identities
Every resource must carry an owner tagOrphaned resources nobody maintains

Where to run the checks

  1. In the editor, so developers see problems while writing.
  2. In the pull request, blocking merge on high-severity rule violations.
  3. On the planned change, just before it is applied.
  4. At deploy time in the platform, for example Kubernetes admission control, as a last gate.
  5. Continuously against the live environment, because not every change goes through code. That is the subject of the next lesson.

Where FireAI fits

FireAI works on a single Mac rather than in cloud infrastructure, but it shares one habit with IaC: configuration you can keep as a file. Its rules can be exported to back them up or move them to another Mac, and imported back in safely.

Para lembrar

  • IaC describes infrastructure in reviewable, repeatable files; Terraform and CloudFormation are common tools.
  • One insecure template spreads its mistake everywhere, so templates need review and testing.
  • Policy as code turns security rules into automated checks, with tools such as OPA and Checkov.
  • Check early in the editor and pull request, and again at deploy time and in the live environment.

Teste seus conhecimentos

  1. 1. What does “declarative” mean for IaC tools like Terraform?

    • You list every command to run in order
    • You describe the desired end state and the tool works out the steps — Certo.
    • The tool guesses what you want
    • It only works on one cloud

    Declarative tools compare the desired state to reality and plan the changes themselves.

  2. 2. What is policy as code?

    • Writing laws in a programming language
    • Expressing security rules in machine-readable form so tools can check them automatically — Certo.
    • Encrypting configuration files
    • Signing Terraform files

    Policy as code turns written rules into automated checks that can block unsafe changes.

  3. 3. Why can a single insecure IaC template be especially harmful?

    • It makes the pipeline slower
    • Its mistake is copied into every environment built from it — Certo.
    • It deletes the state file
    • It cannot be reviewed

    Repeatability cuts both ways: a flaw is reproduced just as reliably as a good configuration.

Praticar com o FireAI

Coloque esta lição em prática no seu próprio Mac.

Fontes

Coloque em prática no seu Mac

Teste todos os recursos grátis por 17 dias, sem cartão.

Baixar para Mac Docs