Advanced / Securing AI infrastructure
Securing AI infrastructure cheat sheet
Set up the lab (lesson 0)
| Task | Command or code |
|---|---|
| Make the environment and switch it on | python3 -m venv .venv, source .venv/bin/activate |
| Install the pinned libraries | pip install -r requirements.txt |
| Train a model as plain JSON | python train.py v1 |
| Check the lab | python labcheck.py |
| Find what uses a port | ss -ltnp |
Draw the architecture (lesson 1)
| Task | Command or code |
|---|---|
| Check the architecture file | python arch_check.py architecture.json |
| Show each connection and whether it crosses a boundary | python arch_check.py architecture.json --table |
| Read the exit code of the last command | echo "exit code $?" |
| Replace text on each line | sed 's/old/new/' file > copy |
Serve a model (lesson 2)
| Task | Command or code |
|---|---|
| Run the table of good and bad requests | python test_service.py |
| Start the service in the background | KR_GATEWAY_TOKEN=... python service.py 8421 &, echo $! > service.pid |
| Send a request with a header and a body | curl -s -w ' %{http_code}\n' -X POST -H 'Header: value' -d '{...}' URL |
| Stop it | kill $(cat service.pid) |
| Compare the hash of the model file | hashlib.sha256(text.encode()).hexdigest() |
Gateway (lesson 3)
| Task | Command or code |
|---|---|
| Make a key for a client | python keystore.py add alpha predict |
| Send a key | -H "Authorization: Bearer $KEY" |
| Run the gateway tests | python test_gateway.py |
| Print only the status codes of many calls | curl -s -o /dev/null -w '%{http_code} ' |
| See the headers of an answer | curl -s -i URL |
| Read the audit log | head -2 audit.log |
Keys and secrets (lesson 4)
| Task | Command or code |
|---|---|
| Rotate a key with an overlap (0 after a leak) | python rotate.py alpha --overlap 7 |
| Scan a folder for keys and open secret files | python scan_secrets.py folder |
| Make a file that only you can read | chmod 600 secrets/alpha.key |
| Make new files private (in a subshell) | ( umask 077; command ) |
| Show the environment of a process | tr '\0' '\n' < /proc/PID/environ (a secret in use is in clear; the file is mode 400) |
Isolation (lesson 5)
| Task | Command or code |
|---|---|
| Set limits for a program | ( ulimit -n 64 -v 2097152 -f 10240; exec python service.py ) |
| Make a file read-only, a folder private | chmod 444 file, chmod 700 folder |
| Audit a running process | python audit_isolation.py PID MODEL WORK SECRETS |
| Audit written settings | python audit_isolation.py --settings settings.json |
| Run in a sandbox with no network (optional) | bwrap --ro-bind / / --dev /dev --proc /proc --bind "$PWD/work" "$PWD/work" --unshare-all python probe.py |
| Run as a separate user (needs administrator rights) | sudo --preserve-env=KR_GATEWAY_TOKEN -u krmodel python3 service.py |
Release (lesson 6)
| Task | Command or code |
|---|---|
| Run the gates and promote | python release.py v2 |
| Judge a recorded result | python release.py check results.json |
| Go back one version | python release.py rollback |
| Train a version with a lost column | python train.py v2 1 failed |
| Swap a file in one step | os.replace(temp, path) |
Drift (lesson 7)
| Task | Command or code |
|---|---|
| Make the fixed samples | python make_samples.py |
| Compare a live sample with the reference | python drift.py samples/reference.json samples/live-b.json |
| Save the report | --out report.json --today 2026-11-21 |
| Choose an action | drift.advise(status, promoted_recently, accuracy_fell) |
Cost and capacity (lesson 8)
| Task | Command or code |
|---|---|
| Start the gateway with extra stages | KR_EXTRA=budget python gateway.py |
| Estimate capacity | capacity.capacity(service_ms, workers, 0.7) |
| Replay a burst through a queue | capacity.replay(arrivals_ms, service_ms, workers, queue_max) |
| Send many calls at once | seq 60 | xargs -P 30 -I{} curl ... |
Policy (lesson 9)
| Task | Command or code |
|---|---|
| Report the decisions on a table of requests | python policy.py policy.json policy-cases.json |
| Enforce policy and budget in the gateway | KR_EXTRA=policy,budget python gateway.py |
| Send a data class | -H "X-Data-Class: internal" |
| Protect data in use | A TEE with attestation (confidential computing), homomorphic encryption, or secure multi-party computation: ask for one when secret data runs on a host that someone else operates |
Readiness (lesson 10)
| Task | Command or code |
|---|---|
| Check a system | python readiness.py --root DIR --today 2026-11-30 |
| Read the verdict | readiness: 10 of 10 pass, exit code 0 |
Safety habits
- Run the model service on
127.0.0.1only, and let it accept the gateway only. - Never load a pickle that you did not make. Save a model as plain numbers and check its hash.
- Check type, size and range of every field, and return one fixed answer shape.
- Keep keys as hashes at rest, and as files of mode 600 in use. Give each client its own key and scope.
- Rotate with an overlap, never with a gap. After a leak, rotate at once with
--overlap 0. - Log who called, when, what happened and why. Never log keys, tokens or bodies.
- Give a process only the rights that it needs: not root, read-only model, private work folder, limits.
- Release a model only when every gate passes, and keep the rollback tested.
- Watch the data that arrives as well as the answers that leave. Write the response to an alert before the alert.
- Deny by default. Let a deny rule win. Test the rules with a table that has cases that must be denied.
- Data in use is plain in memory. Encryption in transit and at rest does not cover it, so keep
secretdata off hosts that you do not run, or ask for a TEE. - A missing file is a FAIL, never a pass. A pass means that your checklist is met, not that the system is safe.