Gatekeeper: secure CI/CD delivery chain
Every change passes security gates before an image can reach Amazon ECR, and no AWS key is stored anywhere.
A small containerised Flask API. The app is the thing delivered; the chain around it is the project.
GitHub ActionsDockerTerraformKubernetesTrivySemgrepgitleaksCheckovOWASP ZAPOIDCECR
Read the write-upA risky change should not be able to reach production, and a stolen secret should not let anyone push to it. The app is deliberately small, a Flask API with two planted flaws, so the scanners had something real to find.
Architecture
What I built
- 1Committed a fake key, deleted it in the next commit, then ran gitleaks. It still found it, in the first commit and inside an image layer. Added a pre-commit hook.
- 2Trivy on the dependencies: 3 HIGH CVEs in Flask and Werkzeug. Upgraded to 0.
- 3Semgrep on the code: 5 findings on the two planted flaws. Fixed to 0.
- 4Compared a naive image against a hardened multi-stage one: 1.11 GB to 125 MB.
- 5Terraform for the state bucket, ECR and the OIDC role. The account already had a GitHub OIDC provider, so it is read with a data source rather than owned here.
- 6Checkov on Terraform, Dockerfile, manifests and the workflow: 6 findings, 1 fixed and 5 skipped with a written reason.
- 7ZAP baseline found missing security headers, fixed in the app.
- 8kind cluster job: Pod Security rejects a deliberately bad pod, and NetworkPolicy blocks and allows as designed.
The numbers
- 1.11 GB → 125 MB
- Container image size
- 344 → 44
- HIGH and CRITICAL CVEs in the image
- 5 → 0
- Semgrep findings in my code
- 6 → 0
- Checkov failed checks
- 1 / 3 → 0 / 0
- ZAP alerts, Medium / Low
- 0
- AWS keys stored in CI
What broke and how I fixed it
- Deleting a secret does not remove it. gitleaks found the key in the first commit even after it was deleted, which is why the fix for a real leak is revoking it.
- A pinned base image ages. Two weeks after pinning by digest, the same image had 4 HIGH findings with fixes available. Dependabot now proposes that refresh weekly.
- The scanner skipped the workflow file without saying so. A concurrency value written as an expression made Checkov scan 0 resources and still report success. The job now fails if no checks run.
- Hardening broke a feature and the smoke test caught it. /ping returned 500 because there is no ping program in the hardened image and the pod drops NET_RAW. Replaced with a TCP check, which also removes the command injection surface.
- GitHub's OIDC subject carries numeric IDs. The first publish was refused until the trust policy matched the real claim. IDs are never reused, so a recreated repository with the same name cannot assume the role.