Lição 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 agora 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
| 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.
A reter
- 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-se
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. 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. 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
Ponha esta lição em prática no seu próprio Mac.
- Regras: app, sítio, domínio, IP ou um intervalo, para sempre ou até reiniciar — Escreva uma regra tão precisa como um único endereço ou tão ampla como um domínio inteiro.
- Importe e exporte as suas regras — Leve as suas regras para um Mac novo, ou guarde uma cópia de segurança, em dois cliques.
- Como o FireAI vigia as ligações do seu Mac — Saiba que app está a falar com a internet, em termos simples, sem instalar nada que funcione como um serviço oculto em segundo plano.
- Listas de ameaças (opcional) — Compare o seu tráfego com dados públicos de ameaças sem o enviar para lado nenhum.
- O Mapa-múndi — Veja para onde os seus dados vão de facto, não apenas um nome de anfitrião que teria de procurar você mesmo.
Fontes
- 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
Ponha em prática no seu Mac
Experimente todas as funcionalidades grátis durante 17 dias, sem cartão.