#!/usr/bin/env bash
# Real demos for P123 "How to Fix Certificate Errors: 'Your Connection Is Not Private'". Made on the Mac (Docker Desktop).
# Three machines on one private Docker network (lab/Dockerfile):
#   web01    your web server: Ubuntu 24.04 with systemd, Ubuntu's nginx and certbot. It answers for mysite.test,
#            www.mysite.test and staging.mysite.test. The terminal is a real login shell in a pty (demo_driver.py).
#   laptop   your laptop: Chromium (Microsoft's Playwright image) on the lab network, so it loads the real names on
#            port 443; every warning page and error code is Chromium's own. A second Chromium runs under faketime
#            for the wrong-clock step.
#   ca       a practice certificate authority (smallstep's step-ca, speaking ACME) that stands in for Let's Encrypt:
#            certbot on web01 is pointed at it (/etc/letsencrypt/cli.ini), and the laptop's Chromium trusts its root
#            the way a real browser trusts Let's Encrypt's. Said on screen.
# SETUP_ONLY=1 builds and starts the lab and leaves it running; KEEP=1 keeps it after the run.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p123
cleanup() {
  docker rm -f -v $P-web $P-laptop $P-ca >/dev/null 2>&1 || true
  docker network rm $P >/dev/null 2>&1 || true
}
cleanup
[ -n "${KEEP:-}${SETUP_ONLY:-}" ] || trap cleanup EXIT
docker build -q --target web -t $P-web lab >/dev/null
docker build -q --target laptop -t $P-laptop lab >/dev/null
docker network create $P >/dev/null

# --- the practice CA
docker run -d --name $P-ca --network $P --network-alias acme.practice-ca.test \
  -e DOCKER_STEPCA_INIT_NAME="Practice CA" -e DOCKER_STEPCA_INIT_DNS_NAMES=acme.practice-ca.test \
  -e DOCKER_STEPCA_INIT_ADDRESS=":443" -e DOCKER_STEPCA_INIT_ACME=true smallstep/step-ca:latest >/dev/null
for i in $(seq 60); do docker exec $P-ca test -s /home/step/config/ca.json 2>/dev/null && docker exec $P-ca step ca health >/dev/null 2>&1 && break; sleep 1; done
# certificates last 90 days, like Let's Encrypt's (step-ca's default is one day)
docker exec $P-ca sh -c 'jq "(.authority.provisioners[] | select(.type==\"ACME\")) += {claims:{maxTLSCertDuration:\"2160h\",defaultTLSCertDuration:\"2160h\"}}" /home/step/config/ca.json > /tmp/ca.json && cp /tmp/ca.json /home/step/config/ca.json'
docker restart $P-ca >/dev/null
for i in $(seq 60); do docker exec $P-ca step ca health >/dev/null 2>&1 && break; sleep 1; done

# --- the web server
docker run -d --name $P-web --hostname web01 --network $P --privileged --cgroupns=private --tmpfs /run --tmpfs /run/lock \
  --network-alias mysite.test --network-alias www.mysite.test --network-alias staging.mysite.test $P-web >/dev/null
for i in $(seq 60); do docker exec $P-web systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded' && break; sleep 1; done

# --- the laptop: Chromium trusts the practice CA's root (the user's NSS store, where Chromium on Linux looks)
docker run -d --name $P-laptop --hostname laptop --network $P -p 127.0.0.1:28123:3000 -p 127.0.0.1:28124:3001 $P-laptop >/dev/null
docker exec $P-ca cat /home/step/certs/root_ca.crt | docker exec -i $P-laptop sh -c 'cat > /tmp/practice-root.crt'
docker exec $P-laptop sh -c 'mkdir -p ~/.pki/nssdb && certutil -d sql:$HOME/.pki/nssdb -N --empty-password && certutil -d sql:$HOME/.pki/nssdb -A -t "C,," -n "Practice CA Root" -i /tmp/practice-root.crt'
docker exec -d $P-laptop sh -c "faketime -f '-3y' python3 -m playwright run-server --port 3001 --host 0.0.0.0"
sleep 3
[ -n "${SETUP_ONLY:-}" ] && exit 0
python3 demo_driver.py $P
