One word made a bucket public
The shop keeps its receipts in a cloud bucket, described in a file. One word in that file, public-read, lets anybody on the internet read every receipt.
Nothing has been built yet. A scanner can read the file and say so before anyone deploys it.
By the end of this lesson
Note
I can scan a Dockerfile and Terraform for risky settings and fix them.
Key words
| Word | What it means | Example |
|---|---|---|
| IaC | servers and networks described in files | |
| Misconfiguration | a setting that weakens security | |
| Terraform | a tool that builds cloud resources from files | |
| Check | one rule a scanner tests a file against | DS-0002 |
| Drift | the live system no longer matching its files |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsInfrastructure is code, so scan it
A Dockerfile says how to build the shop's container. A Terraform file says which cloud resources to make. Both are IaC, kept in the repository and reviewed like code.
Trivy's config command tests them against hundreds of checks. In the lab it uses the checks built into it, as the lab has no internet.
Bring the lab's copy up to date
docker exec gitleaks git -C /work/tuckshop pull -qShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read the shop's Dockerfile
docker exec trivy cat /work/tuckshop/DockerfileShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the Dockerfile
docker exec trivy trivy config -q /work/tuckshop/DockerfileShown here, not run. Press Start lab to type it into your own lab’s terminal.
The four failed Dockerfile checks
| Check | Severity | What is wrong |
|---|---|---|
| DS-0001 | Medium | FROM python has no tag |
| DS-0002 | High | no USER, so it runs as root |
| DS-0005 | Low | ADD where COPY would do |
| DS-0026 | Low | no HEALTHCHECK |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsThe cloud file has the worst findings
The Terraform file makes a bucket with acl = "public-read" and a firewall rule letting port 22, remote login, in from 0.0.0.0/0, which means every address.
Show only High and Critical, as a busy team would first.
Read the Terraform file
docker exec trivy cat /work/tuckshop/deploy/main.tfShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan it for High and Critical
docker exec trivy trivy config -q --severity HIGH,CRITICAL /work/tuckshop/deployShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try it: a firewall check
rules = [
{"port": 22, "from": "0.0.0.0/0"},
{"port": 443, "from": "0.0.0.0/0"},
{"port": 5432, "from": "10.0.0.0/16"},
]
public_ok = [443]
for rule in rules:
open_to_all = rule["from"] == "0.0.0.0/0"
bad = open_to_all and rule["port"] not in public_ok
print(rule["port"], rule["from"], "FAIL" if bad else "ok")
# Change 22 to come from "10.0.0.0/16". What does it print now?
Make a free account to save your work and track your progress.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a clean IaC scan proves the live cloud is safe. The scan reads files, not the running system. Someone can change a setting by hand in the console, and the files never know: that is drift. Check the live cloud as well.
Make the step fail, then fix the file
With --exit-code 1, Trivy exits 1 when it finds anything at the chosen severity, so the pipeline step fails.
Then fix the cause. The fixed Dockerfile pins the image, copies instead of adding, runs as an unprivileged user and checks its own health.
Run the scan as a pipeline step
docker exec trivy trivy config -q --severity HIGH,CRITICAL --exit-code 1 /work/tuckshop/deploy > /dev/null; echo "exit $?"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Write a fixed Dockerfile
docker exec -i trivy sh -c 'mkdir -p /work/fixed && cat > /work/fixed/Dockerfile' <<'EOF'
FROM python:3.12.15-alpine
WORKDIR /app
COPY . /app
RUN pip install --no-cache-dir -r requirements.txt
USER 65532
HEALTHCHECK CMD python3 -c "import urllib.request as u; u.urlopen('http://127.0.0.1:8000/health')"
EXPOSE 8000
CMD ["python3", "app.py"]
EOFShown here, not run. Press Start lab to type it into your own lab’s terminal.
Scan the fixed Dockerfile
docker exec trivy trivy config -q /work/fixedShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: a Dockerfile checker
Write a small Dockerfile checker. A Dockerfile is given as one string; its lines are numbered from 1, and only lines that start with an instruction count. Write three functions.
instructions(text) returns a list of (line_number, INSTRUCTION, rest) tuples, the instruction in capitals, skipping blank lines and lines starting #. problems(text) returns a sorted list of (line_number, check) tuples: "untagged" for a FROM image with no : (or ending :latest), "add" for every ADD, and (0, "root") when no USER line exists or the last one is root or 0. passes(text) returns True when there are no problems.
def instructions(text):
return []
def problems(text):
return []
def passes(text):
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go