#!/bin/sh # Local PKI for the lab stand. Operator-run, outside Git: everything is written under ./secrets (ignored by Git). # scripts/pki.sh init create root CA, workload CA and all leaf identities (idempotent: existing files are kept) # scripts/pki.sh leaf re-issue ONE leaf with a NEW key (rotation, no image rebuild); the old file is replaced atomically # Identity model: root CA signs infrastructure leaves; intermediate "workload CA" signs application identities (api, worker, dispatcher, sweeper). set -eu umask 022 S="${SECRETS_DIR:-secrets}"; PKI="$S/pki"; CA="$PKI/ca" INFRA_DAYS="${INFRA_DAYS:-90}"; WORKLOAD_DAYS="${WORKLOAD_DAYS:-14}" mkca() { [ -f "$CA/ca.crt" ] && return 0 mkdir -p "$CA" openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -days 3650 -subj "/CN=mtusi-lab7-root-ca" \ -addext "basicConstraints=critical,CA:TRUE" -addext "keyUsage=critical,keyCertSign,cRLSign" -keyout "$CA/ca.key" -out "$CA/ca.crt" 2>/dev/null chmod 600 "$CA/ca.key" } mkworkloadca() { [ -f "$CA/workload-ca.crt" ] && return 0 openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -subj "/CN=mtusi-lab7-workload-ca" -keyout "$CA/workload-ca.key" -out "$CA/workload-ca.csr" 2>/dev/null printf "basicConstraints=critical,CA:TRUE,pathlen:0\nkeyUsage=critical,keyCertSign,cRLSign\n" > "$CA/wl.ext" openssl x509 -req -in "$CA/workload-ca.csr" -CA "$CA/ca.crt" -CAkey "$CA/ca.key" -set_serial "0x$(openssl rand -hex 8)" -days 1825 -extfile "$CA/wl.ext" -out "$CA/workload-ca.crt" 2>/dev/null chmod 600 "$CA/workload-ca.key" } # issue issue() { name="$1"; signer="$2"; sans="$3"; days="$4"; d="$PKI/$name"; tmp="$PKI/.tmp-$name" rm -rf "$tmp"; mkdir -p "$tmp" openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -subj "/CN=$name" -keyout "$tmp/tls.key" -out "$tmp/tls.csr" 2>/dev/null printf "basicConstraints=CA:FALSE\nkeyUsage=critical,digitalSignature\nextendedKeyUsage=serverAuth,clientAuth\nsubjectAltName=%s\n" "$sans" > "$tmp/ext" if [ "$signer" = workload ]; then ca=workload-ca; else ca=ca; fi openssl x509 -req -in "$tmp/tls.csr" -CA "$CA/$ca.crt" -CAkey "$CA/$ca.key" -set_serial "0x$(openssl rand -hex 8)" -days "$days" -extfile "$tmp/ext" -out "$tmp/leaf.crt" 2>/dev/null if [ "$signer" = workload ]; then cat "$tmp/leaf.crt" "$CA/workload-ca.crt" > "$tmp/tls.crt"; else cp "$tmp/leaf.crt" "$tmp/tls.crt"; fi cp "$CA/ca.crt" "$tmp/ca.crt" cat "$tmp/tls.key" "$tmp/tls.crt" > "$tmp/keystore.pem" # key first: HAProxy "crt" and Kafka PEM keystore format openssl pkcs12 -export -in "$tmp/tls.crt" -inkey "$tmp/tls.key" -out "$tmp/keystore.p12" -passout "file:$S/ops/kafka_ssl_password" 2>/dev/null openssl pkcs12 -export -nokeys -in "$CA/ca.crt" -out "$tmp/truststore.p12" -passout "file:$S/ops/kafka_ssl_password" 2>/dev/null rm -f "$tmp/tls.csr" "$tmp/ext" "$tmp/leaf.crt" chmod 644 "$tmp"/* # ./secrets itself is 0700; containers run as other uids mkdir -p "$d"; mv -f "$tmp"/* "$d"/; rmdir "$tmp" echo "issued $name ($signer CA, $days d)" } spec() { # name signer SANs -- the single source of truth for identities cat </dev/null openssl req -newkey ec -pkeyopt ec_paramgen_curve:prime256v1 -nodes -subj "/CN=lb" -keyout "$PKI/rogue/tls.key" -out "$PKI/rogue/tls.csr" 2>/dev/null printf "subjectAltName=DNS:lb\nextendedKeyUsage=clientAuth\n" > "$PKI/rogue/ext" openssl x509 -req -in "$PKI/rogue/tls.csr" -CA "$PKI/rogue/rogue-ca.crt" -CAkey "$PKI/rogue/rogue-ca.key" -CAcreateserial -days 30 -extfile "$PKI/rogue/ext" -out "$PKI/rogue/tls.crt" 2>/dev/null fi ;; leaf) n="${2:?usage: pki.sh leaf }" line="$(spec | awk -v n="$n" "\$1==n")"; [ -n "$line" ] || { echo "unknown identity $n" >&2; exit 1; } set -- $line; issue "$1" "$2" "$3" "$(days_for "$2")" ;; *) echo "usage: pki.sh init | leaf " >&2; exit 2 ;; esac