Lezione 6 di 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.
Per ora questa pagina è in inglese.
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
| Tool | Made by | How it works |
|---|---|---|
| Terraform | HashiCorp | Declarative configuration files; works with many cloud and service providers through plug-in “providers”; keeps a state file recording what it manages |
| AWS CloudFormation | Amazon Web Services | Templates 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 policy | Misconfiguration it prevents |
|---|---|
| Storage buckets must block public access | Accidental exposure of private files |
| No security group may allow administrative ports from anywhere | Remote administration exposed to the internet |
| Encryption at rest must be enabled for databases and volumes | Readable data if storage is copied |
| IAM policies may not use wildcard actions on all resources | Over-permissioned identities |
| Every resource must carry an owner tag | Orphaned resources nobody maintains |
Where to run the checks
- In the editor, so developers see problems while writing.
- In the pull request, blocking merge on high-severity rule violations.
- On the planned change, just before it is applied.
- At deploy time in the platform, for example Kubernetes admission control, as a last gate.
- 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.
Da ricordare
- 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.
Mettiti alla prova
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 — Esatto.
- 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. What is policy as code?
- Writing laws in a programming language
- Expressing security rules in machine-readable form so tools can check them automatically — Esatto.
- Encrypting configuration files
- Signing Terraform files
Policy as code turns written rules into automated checks that can block unsafe changes.
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 — Esatto.
- It deletes the state file
- It cannot be reviewed
Repeatability cuts both ways: a flaw is reproduced just as reliably as a good configuration.
Mettilo in pratica con FireAI
Metti in pratica questa lezione sul tuo Mac.
- Regole: app, sito, dominio, IP o un intervallo, per sempre o fino al riavvio — Scrivi una regola precisa quanto un solo indirizzo o ampia quanto un intero dominio.
- Importa ed esporta le tue regole — Sposta le tue regole su un nuovo Mac, o fanne un backup, in un paio di clic.
- Come FireAI osserva le connessioni del tuo Mac — Sappi quale app sta comunicando con internet, in termini chiari, senza installare nulla che funzioni come servizio nascosto in background.
- Liste di minacce (facoltative) — Confronta il tuo traffico con dati pubblici sulle minacce senza inviarlo da nessuna parte.
- La Mappa del mondo — Vedi dove vanno davvero i tuoi dati, non solo un nome host che dovresti cercare da solo.
Fonti
- HashiCorp: What is Terraform?
- AWS: What is CloudFormation?
- Open Policy Agent
- Checkov
- NIST SP 800-53 Rev. 5: Security and Privacy Controls
- NIST SP 800-204D: Software supply chain security in DevSecOps CI/CD pipelines
Mettilo in pratica sul tuo Mac
Prova tutte le funzioni gratis per 17 giorni, senza carta.