Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Scanning infrastructure as code

After this lesson: I can scan a Dockerfile and Terraform for risky settings and fix them.

50 min lesson8 commandsDevSecOps lab: build a security pipeline and a gate

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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

WordWhat it meansExample
IaCservers and networks described in files
Misconfigurationa setting that weakens security
Terraforma tool that builds cloud resources from files
Checkone rule a scanner tests a file againstDS-0002
Driftthe live system no longer matching its files

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Infrastructure 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

Command
docker exec gitleaks git -C /work/tuckshop pull -q

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the shop's Dockerfile

Command
docker exec trivy cat /work/tuckshop/Dockerfile

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the Dockerfile

Command
docker exec trivy trivy config -q /work/tuckshop/Dockerfile

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

The four failed Dockerfile checks

CheckSeverityWhat is wrong
DS-0001MediumFROM python has no tag
DS-0002Highno USER, so it runs as root
DS-0005LowADD where COPY would do
DS-0026Lowno HEALTHCHECK

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The 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

Command
docker exec trivy cat /work/tuckshop/deploy/main.tf

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan it for High and Critical

Command
docker exec trivy trivy config -q --severity HIGH,CRITICAL /work/tuckshop/deploy

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Try it: a firewall check

Code: python
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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A 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

Command
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

Command
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"]
EOF

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the fixed Dockerfile

Command
docker exec trivy trivy config -q /work/fixed

Shown 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.