Five reports, nobody reading them
The shop now has five scanners. Each one prints a long report, and the team has stopped reading them.
A gate reads every report for them, makes one decision, and stops a change from being merged until the decision is yes.
By the end of this lesson
Note
I can set up a security gate that stops a risky change being merged.
Key words
| Word | What it means | Example |
|---|---|---|
| Gate | a pipeline step that passes or blocks a change | |
| Policy | the rules a gate applies | "fail_at": "high" |
| Baseline | known findings accepted for a time | |
| Commit status | a pass or fail mark on one commit | |
| Protected branch | a branch that needs checks before a merge |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsOne file runs every scanner
A pipeline is a file in the repository listing each step. The lab's Gitea has no runner attached, so you run the file yourself, as a runner would.
Each scanner writes a JSON report to /work/reports. The last step, gate.py, reads them all and applies the policy.
Write the pipeline file
cat > pipeline.sh <<'EOF'
docker exec gitleaks git -C /work/tuckshop pull -q
docker exec semgrep semgrep scan --config /work/tuckshop/.semgrep.yml --metrics=off --quiet --json-output=/work/reports/semgrep.json /work/tuckshop > /dev/null
docker exec gitleaks gitleaks git /work/tuckshop --no-banner --redact --report-format json --report-path /work/reports/gitleaks.json 2> /dev/null
docker exec ci pip install -q --user --no-warn-script-location pip-audit==2.10.1
docker exec ci python3 -m pip_audit -r /work/tuckshop/requirements.txt --no-deps --disable-pip -f json -o /work/reports/audit.json 2> /dev/null
docker exec trivy trivy config -q -f json -o /work/reports/trivy.json /work/tuckshop
docker exec ci python3 dast.py http://shop:8000/ --json /work/reports/dast.json > /dev/null
docker exec ci python3 gate.py
EOFShown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the pipeline on main
sh pipeline.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA protected branch needs the status
A protected branch can require a check before anything is merged into it. The gate reports by setting a commit status called security-gate on the newest commit.
Protect main, then let the gate mark main's newest commit. Open Gitea from the lab's tab to see the red cross.
Require security-gate on main
curl -s -o /dev/null -w "%{http_code}\n" -u student:student -H "Content-Type: application/json" -d '{"rule_name":"main","enable_status_check":true,"status_check_contexts":["security-gate"]}' http://gitea:3000/api/v1/repos/student/tuckshop/branch_protectionsShown here, not run. Press Start lab to type it into your own lab’s terminal.
Mark main's newest commit
docker exec ci python3 gate.py --post mainShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA common mistake
Warning
Many people think a gate should block every finding from day one. On a project full of old findings, that blocks every change, and teams switch the gate off. Start by blocking the worst, accept known debt with a reason and a date, and tighten the policy as it is fixed.
A policy with a reason and a date
The policy file lives in the repository, so a change to it is reviewed like code. This one blocks only critical findings. Its baseline accepts the rotated key until 2030.
It also holds an entry with no reason and a past date. The gate ignores that one, as a gate should.
Clone the shop in your terminal
test -d tuckshop || git clone -q http://student:student@gitea:3000/student/tuckshop.gitShown here, not run. Press Start lab to type it into your own lab’s terminal.
Start a branch for the policy
git -C tuckshop switch -q -c gate-policyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the policy file
cat > tuckshop/security-policy.json <<'EOF'
{
"fail_at": "critical",
"baseline": [
{"key": "gitleaks:generic-api-key:settings.py", "reason": "Rotated on 13 January 2026, old key revoked", "expires": "2030-06-30"},
{"key": "semgrep:weak-password-hash:app.py", "reason": "", "expires": "2026-01-31"}
]
}
EOFShown here, not run. Press Start lab to type it into your own lab’s terminal.
Commit and push the branch
git -C tuckshop add security-policy.json && git -C tuckshop -c user.name=Student -c [email protected] commit -qm "Add the security policy" && git -C tuckshop push -q -u origin gate-policyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Open a pull request
curl -s -u student:student -H "Content-Type: application/json" -d '{"head":"gate-policy","base":"main","title":"Add the security policy"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls | grep -o '"number":[0-9]*' | head -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to merge before the gate runs
curl -s -u student:student -H "Content-Type: application/json" -d '{"Do":"merge"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls/1/mergeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check out the branch for the pipeline
docker exec gitleaks sh -c "cd /work/tuckshop && git fetch -q origin && git checkout -q gate-policy"Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Run the pipeline on the branch
sh pipeline.shShown here, not run. Press Start lab to type it into your own lab’s terminal.
Mark the branch's commit
docker exec ci python3 gate.py --post gate-policyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Merge now the gate has passed
curl -s -o /dev/null -w "%{http_code}\n" -u student:student -H "Content-Type: application/json" -d '{"Do":"merge"}' http://gitea:3000/api/v1/repos/student/tuckshop/pulls/1/mergeShown here, not run. Press Start lab to type it into your own lab’s terminal.
Assignment: decide like a gate
A finding is a tuple (tool, rule, where, level), with level one of "low", "medium", "high", "critical". A finding's key is tool:rule:where. A policy is a dictionary such as {"fail_at": "high", "baseline": [{"key": "...", "reason": "...", "expires": "2030-06-30"}]}. Dates are YYYY-MM-DD strings. Write three functions.
accepted(policy, today) returns the set of baseline keys that have a non-empty reason and an expires date on or after today. blocking(findings, policy, today) returns the findings, in the order given, whose level is at or above fail_at and whose key is not accepted. decide(findings, policy, today) returns "PASS" when nothing blocks and otherwise "FAIL: " followed by the number of blocking findings.
SCALE = ["low", "medium", "high", "critical"]
def accepted(policy, today):
return set()
def blocking(findings, policy, today):
return []
def decide(findings, policy, today):
return ""
Make a free account to save your work and track your progress.
Exit quiz
Have a go