Secrets & Policy as Code
Two problems show up in every team that ships infrastructure from Git:
- Secrets. Applications, pipelines and Terraform need database passwords, API keys, signing keys and certificates. They must reach the software that needs them without being written into the repository, the CI log, the container image or the Terraform plan, and they must be replaceable at any time.
- Rules. "Databases are encrypted", "nothing listens on 0.0.0.0/0 on port 5432", "containers don't run as root", "every resource has an owner tag". Written in a wiki, such rules are checked by whoever reviews the pull request, if they remember. Written as code, every plan and every manifest is checked the same way, every time.
This course treats both as engineering problems with tools, tests and pipelines, the same way the Terraform course treats infrastructure.
What you will use
| Tool | Role | In this course |
|---|---|---|
| HashiCorp Vault 2.2 | a secrets server: stores secrets, issues short-lived credentials, encrypts data, runs a CA | real sessions in dev mode |
| OpenBao | the Linux Foundation fork of Vault (MPL 2.0), same API and CLI shape | Vault in Production & OpenBao |
| SOPS + age | encrypted values inside YAML/JSON/ENV files that live in Git | real sessions |
| External Secrets Operator, Sealed Secrets | get secrets into Kubernetes without committing them | sample manifests |
| OPA + Rego | a general policy engine and its language | real sessions (opa eval, opa test) |
| Conftest | runs Rego policies against config files: Terraform plans, Kubernetes YAML, Dockerfiles | real sessions |
| Kyverno, Gatekeeper | policy in the cluster, at admission | Kyverno CLI real; clusters as samples |
| gitleaks | secret scanning | real sessions, building on the CI/CD course |
The running example is linkly, the link-shortener API from the DevOps Capstone: a Go service with a PostgreSQL database whose password has to come from somewhere safe. The Mini Project ends with a pipeline gate that rejects a Terraform plan and a Kubernetes manifest that break policy, while linkly's database password is served by Vault.
A first taste
Every session in this course runs offline in a fresh sandbox. Vault starts in dev mode inside the session: in memory, already unsealed, with a root token you choose. That is fine for learning and never acceptable anywhere else.
vault server -dev -dev-root-token-id=root -dev-listen-address=127.0.0.1:8200 > vault.log 2>&1 &
for i in $(seq 50); do vault status > /dev/null 2>&1 && break; sleep 0.2; done
export VAULT_TOKEN=root
vault kv put -mount=secret linkly/db username=linkly password='c0rrect-h0rse' > /dev/null
echo "password from Vault: $(vault kv get -mount=secret -field=password linkly/db)"
mkdir policy
cat > policy/db.rego <<'REGO'
package main
deny contains msg if {
input.resource.aws_db_instance[name].storage_encrypted == false
msg := sprintf("aws_db_instance.%s must be encrypted", [name])
}
REGO
cat > db.json <<'JSON'
{"resource": {"aws_db_instance": {"main": {"engine": "postgres", "storage_encrypted": false}}}}
JSON
conftest test db.jsonpassword from Vault: c0rrect-h0rse
FAIL - db.json - main - aws_db_instance.main must be encrypted
1 test, 0 passed, 0 warnings, 1 failure, 0 exceptions
Two lines of Rego turned a review comment into a check with an exit code a pipeline can act on. The same rule runs against real Terraform plans in Conftest on Terraform Plans.
What this course does not repeat
- Cryptography (symmetric vs asymmetric, hashing, certificates): Cybersecurity and PKI & Certificates.
- Scanning commits with gitleaks and TruffleHog: Secret Detection with gitleaks and TruffleHog & GitGuardian.
- Kubernetes Secrets and admission webhooks: Secrets and Admission Controllers.
- Image signatures and supply-chain admission policies: Admission Control: Kyverno & policy-controller.
- Terraform state and
sensitivevalues: What State Is and Input Variables. - SOPS with Flux in a GitOps repository: Secrets in GitOps.
How the course is organised
- Start Here and Secret Hygiene: how secrets leak, their lifecycle, scanning, rotation.
- Vault: from dev mode to policies, auth methods (including CI OIDC and Kubernetes), leases, dynamic database credentials, transit, PKI and Vault Agent.
- Encrypted files: age, SOPS, and the Kubernetes operators.
- Policy as code: Rego,
opa test, Conftest on plans and manifests, Kyverno and Gatekeeper. - Toolkit, Field Notes, Exercises and the Mini Project.
Test yourself
Why is Vault dev mode only for learning?
- It is slower
- It keeps everything in memory, starts unsealed and uses a known root token
- It cannot store KV secrets
- It needs a license
What does policy as code give you that a wiki page of rules doesn't?
- Prettier formatting
- The same rules checked automatically on every change, with an exit code a pipeline can act on
- Faster deploys
- Encrypted secrets