forked from mtusi2026/mtusi2026lab7
Перенесён минимум lab6 (API без изменений), добавлена защита на границах доверия: - внешний HTTPS; обязательный mTLS LB -> API с allowlist identity и отзывом по serial; - TLS на всех связях: PostgreSQL verify-full, MinIO HTTPS, RabbitMQ AMQPS mTLS (EXTERNAL), Valkey TLS + ACL, Collector и хранилища телеметрии через mTLS с отдельными CA; - OpenBao в server mode: cert auth, токены 15 мин, политики только на свой путь; - роли БД, S3-политики и права брокера по принципу минимальных прав; - Argon2id с замером, rehash, общий лимит входа, бюджет защиты от login DoS; - hardening всех контейнеров, scan образов с разбором, модель угроз, матрица доверия; - отрицательные тесты: TLS-аудит (135 проверок), ACL, ротации, sealed OpenBao.
191 lines
11 KiB
Bash
Executable File
191 lines
11 KiB
Bash
Executable File
#!/bin/sh
|
|
# Local lab PKI (operator tool). Private keys stay in secrets/pki/ (git-ignored, never baked into images).
|
|
#
|
|
# scripts/pki.sh init create CAs and every leaf that does not exist yet (idempotent)
|
|
# scripts/pki.sh issue <leaf> (re)issue one leaf with a new key and serial (rotation)
|
|
# scripts/pki.sh revoke <leaf> add the CURRENT serial of <leaf> to secrets/pki/revoked-serials.txt
|
|
# scripts/pki.sh revoke-serial <hex> revoke a serial that is no longer the current one (after a rotation)
|
|
# scripts/pki.sh expired <leaf> test-only: an ALREADY EXPIRED copy of <leaf> (same identity) -> <leaf>-expired.*
|
|
# scripts/pki.sh serial <leaf> current serial of <leaf>
|
|
# scripts/pki.sh list leaf names, identities, serials and expiry (no key material)
|
|
#
|
|
# Trust layout (identity is checked by every server, not only the chain):
|
|
# root CA "Messenger Lab Root CA" signs every server certificate and the service client identities
|
|
# (lb, api, worker) used for LB->API, RabbitMQ (EXTERNAL = certificate CN) and OpenBao cert auth.
|
|
# ingest CA (intermediate) signs ONLY api-otel / worker-otel: the Collector trusts nothing else, so a
|
|
# root-signed certificate (e.g. the LB one) cannot push telemetry.
|
|
# telemetry CA (intermediate) signs ONLY collector-out / grafana-out / operator: Prometheus, Loki and Tempo
|
|
# accept only these clients.
|
|
# intruder root-signed, valid, but with an identity that no server allows (negative tests).
|
|
# foreign CA unrelated CA; foreign-client claims the lb/api/worker names (negative tests of chain checks).
|
|
# OpenSSL runs in a pinned container, so the host OpenSSL/LibreSSL version does not matter.
|
|
set -eu
|
|
cd "$(dirname "$0")/.."
|
|
DIR=secrets/pki
|
|
IMAGE="alpine/openssl:3.3.2@sha256:5008e829163320a6e8166883c03e68189e8925ade68cde36584dc2a41cfa5248"
|
|
LEAF_DAYS="${LEAF_DAYS:-90}"
|
|
CA_DAYS=825
|
|
|
|
# name | signer | usage | CN | DNS/IP SANs
|
|
LEAVES='
|
|
lb-server root server localhost localhost,nginx,IP:127.0.0.1
|
|
lb-client root client lb.client.messenger lb.client.messenger
|
|
api-1 root server api-1 api-1,api.internal
|
|
api-2 root server api-2 api-2,api.internal
|
|
api root client api.client.messenger api.client.messenger
|
|
worker root client worker.client.messenger worker.client.messenger
|
|
postgres root server postgres postgres
|
|
minio root server minio minio
|
|
rabbitmq root server rabbitmq rabbitmq
|
|
valkey root server valkey valkey
|
|
openbao root server openbao openbao,localhost,IP:127.0.0.1
|
|
otel-collector root server otel-collector otel-collector
|
|
prometheus root server prometheus prometheus,localhost,IP:127.0.0.1
|
|
loki root server loki loki,localhost,IP:127.0.0.1
|
|
tempo root server tempo tempo,localhost,IP:127.0.0.1
|
|
grafana root server grafana grafana,localhost,IP:127.0.0.1
|
|
api-otel ingest client api.otel.messenger api.otel.messenger
|
|
worker-otel ingest client worker.otel.messenger worker.otel.messenger
|
|
collector-out telemetry client collector.out.messenger collector.out.messenger
|
|
grafana-out telemetry client grafana.out.messenger grafana.out.messenger
|
|
operator telemetry client operator.messenger operator.messenger
|
|
intruder root client intruder.client.messenger intruder.client.messenger
|
|
'
|
|
|
|
ossl() { # quiet on success, the OpenSSL error on failure
|
|
out=$(docker run --rm -u "$(id -u):$(id -g)" -v "$PWD/$DIR:/pki" -w /pki "$IMAGE" "$@" 2>&1) || {
|
|
echo "openssl $1 failed: $out" >&2; exit 1; }
|
|
}
|
|
|
|
ca_init() { # ca_init <name> <subject> [parent]
|
|
[ -f "$DIR/$1.crt" ] && return 0
|
|
cat > "$DIR/.ext-$1" <<EOF
|
|
basicConstraints=critical,CA:true,pathlen:0
|
|
keyUsage=critical,keyCertSign,cRLSign
|
|
subjectKeyIdentifier=hash
|
|
authorityKeyIdentifier=keyid
|
|
EOF
|
|
ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$1.key"
|
|
if [ $# -eq 2 ]; then
|
|
ossl req -x509 -new -key "$1.key" -subj "$2" -days "$CA_DAYS" -sha256 \
|
|
-addext "basicConstraints=critical,CA:true,pathlen:1" \
|
|
-addext "keyUsage=critical,keyCertSign,cRLSign" -out "$1.crt"
|
|
else
|
|
ossl req -new -key "$1.key" -subj "$2" -out "$1.csr"
|
|
ossl x509 -req -in "$1.csr" -CA "$3.crt" -CAkey "$3.key" -CAcreateserial -days "$CA_DAYS" -sha256 \
|
|
-extfile ".ext-$1" -out "$1.crt"
|
|
rm -f "$DIR/$1.csr"
|
|
fi
|
|
rm -f "$DIR/.ext-$1"
|
|
echo "created CA $1"
|
|
}
|
|
|
|
leaf_line() { echo "$LEAVES" | awk -v n="$1" '$1==n'; }
|
|
|
|
issue() { # issue <leaf>
|
|
line=$(leaf_line "$1")
|
|
[ -n "$line" ] || { echo "unknown leaf: $1" >&2; exit 2; }
|
|
set -- $line
|
|
name=$1 signer=$2 usage=$3 cn=$4 sans=$5
|
|
eku=serverAuth; [ "$usage" = client ] && eku=clientAuth
|
|
alt=$(echo "$sans" | tr ',' '\n' | awk '/^IP:/ {print; next} {print "DNS:" $0}' | paste -sd, -)
|
|
cat > "$DIR/.ext-$name" <<EOF
|
|
basicConstraints=critical,CA:false
|
|
keyUsage=critical,digitalSignature,keyEncipherment
|
|
extendedKeyUsage=$eku
|
|
subjectAltName=$alt
|
|
subjectKeyIdentifier=hash
|
|
authorityKeyIdentifier=keyid
|
|
EOF
|
|
ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$name.key.new"
|
|
ossl req -new -key "$name.key.new" -subj "/O=Messenger Lab/CN=$cn" -out "$name.csr"
|
|
ossl x509 -req -in "$name.csr" -CA "$signer.crt" -CAkey "$signer.key" -CAcreateserial -days "$LEAF_DAYS" \
|
|
-sha256 -extfile ".ext-$name" -out "$name.crt.new"
|
|
# Leaf files carry the chain up to (not including) the root, so intermediates verify against ca.crt alone.
|
|
if [ -f "$DIR/$name.crt" ]; then cp "$DIR/$name.crt" "$DIR/$name.crt.previous"; cp "$DIR/$name.key" "$DIR/$name.key.previous"; fi
|
|
# Overwrite in place (cat >): the files are bind-mounted Docker secrets, a new inode would not be visible.
|
|
if [ "$signer" = root ]; then cat "$DIR/$name.crt.new" > "$DIR/$name.crt"; else cat "$DIR/$name.crt.new" "$DIR/$signer.crt" > "$DIR/$name.crt"; fi
|
|
cat "$DIR/$name.key.new" > "$DIR/$name.key"; rm -f "$DIR/$name.key.new"
|
|
rm -f "$DIR/$name.crt.new" "$DIR/$name.csr" "$DIR/.ext-$name"
|
|
echo "issued $name ($cn) serial=$(serial "$name") valid ${LEAF_DAYS}d"
|
|
}
|
|
|
|
foreign() { # an unrelated CA and a leaf that CLAIMS the LB/api/worker names: negative tests of chain checks
|
|
[ -f "$DIR/foreign-client.crt" ] && return 0
|
|
ca_init foreign "/O=Somebody Else/CN=Foreign CA"
|
|
printf '%s\n' "basicConstraints=critical,CA:false" "keyUsage=critical,digitalSignature,keyEncipherment" \
|
|
"extendedKeyUsage=clientAuth,serverAuth" \
|
|
"subjectAltName=DNS:lb.client.messenger,DNS:api.client.messenger,DNS:worker.client.messenger" \
|
|
> "$DIR/.ext-foreign-client"
|
|
ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out foreign-client.key
|
|
ossl req -new -key foreign-client.key -subj "/O=Somebody Else/CN=lb.client.messenger" -out foreign-client.csr
|
|
ossl x509 -req -in foreign-client.csr -CA foreign.crt -CAkey foreign.key -CAcreateserial -days 30 -sha256 \
|
|
-extfile .ext-foreign-client -out foreign-client.crt
|
|
rm -f "$DIR/foreign-client.csr" "$DIR/.ext-foreign-client"
|
|
echo "created foreign CA + foreign-client (claims lb/api/worker identities, trusted nowhere)"
|
|
}
|
|
|
|
expired() { # same CN/SAN/EKU as <leaf>, validity in the past (openssl ca allows explicit dates)
|
|
line=$(leaf_line "$1"); [ -n "$line" ] || { echo "unknown leaf: $1" >&2; exit 2; }
|
|
set -- $line
|
|
name=$1 signer=$2 usage=$3 cn=$4 sans=$5
|
|
eku=serverAuth; [ "$usage" = client ] && eku=clientAuth
|
|
alt=$(echo "$sans" | tr ',' '\n' | awk '/^IP:/ {print; next} {print "DNS:" $0}' | paste -sd, -)
|
|
t="$DIR/.ca-$name"; rm -rf "$t"; mkdir -p "$t"; touch "$t/index.txt"; echo 01 > "$t/serial"
|
|
printf '%s\n' "[ca]" "default_ca=x" "[x]" "database=/pki/.ca-$name/index.txt" "serial=/pki/.ca-$name/serial" \
|
|
"new_certs_dir=/pki/.ca-$name" "default_md=sha256" "policy=p" "unique_subject=no" "[p]" "commonName=supplied" \
|
|
"[ext]" "basicConstraints=critical,CA:false" "keyUsage=critical,digitalSignature" "extendedKeyUsage=$eku" \
|
|
"subjectAltName=$alt" > "$t/ca.cnf"
|
|
ossl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$name-expired.key"
|
|
ossl req -new -key "$name-expired.key" -subj "/O=Messenger Lab/CN=$cn" -out ".ca-$name/req.csr"
|
|
ossl ca -batch -config ".ca-$name/ca.cnf" -extensions ext -cert "$signer.crt" -keyfile "$signer.key" \
|
|
-startdate 20250101000000Z -enddate 20250102000000Z -in ".ca-$name/req.csr" -out "$name-expired.crt" -notext
|
|
rm -rf "$t"
|
|
echo "issued $name-expired ($cn), validity 2025-01-01..2025-01-02 (already expired, test only)"
|
|
}
|
|
|
|
serial() { docker run --rm -v "$PWD/$DIR:/pki" -w /pki "$IMAGE" x509 -in "$1.crt" -noout -serial | cut -d= -f2; }
|
|
|
|
finish() {
|
|
cp "$DIR/root.crt" "$DIR/ca.crt" # public CA bundle for clients (make test CA_FILE=...)
|
|
cat "$DIR/root.crt" "$DIR/ingest.crt" > "$DIR/ingest-bundle.crt"
|
|
cat "$DIR/root.crt" "$DIR/telemetry.crt" > "$DIR/telemetry-bundle.crt"
|
|
touch "$DIR/revoked-serials.txt"
|
|
# Containers run as other uids and read keys through Docker secrets: files readable, directory closed.
|
|
chmod 700 secrets "$DIR"
|
|
chmod 644 "$DIR"/*.crt "$DIR"/*.key "$DIR/revoked-serials.txt"
|
|
chmod 600 "$DIR"/*.previous 2>/dev/null || true
|
|
chmod 600 "$DIR"/root.key "$DIR"/ingest.key "$DIR"/telemetry.key "$DIR"/foreign.key
|
|
}
|
|
|
|
cmd="${1:-init}"
|
|
mkdir -p "$DIR"
|
|
case "$cmd" in
|
|
init)
|
|
ca_init root "/O=Messenger Lab/CN=Messenger Lab Root CA"
|
|
ca_init ingest "/O=Messenger Lab/CN=Messenger Telemetry Ingest CA" root
|
|
ca_init telemetry "/O=Messenger Lab/CN=Messenger Telemetry Clients CA" root
|
|
for name in $(echo "$LEAVES" | awk 'NF {print $1}'); do
|
|
[ -f "$DIR/$name.crt" ] || issue "$name"
|
|
done
|
|
foreign
|
|
finish ;;
|
|
issue) issue "$2"; finish ;;
|
|
revoke)
|
|
s=$(serial "$2"); grep -qx "$s" "$DIR/revoked-serials.txt" 2>/dev/null || echo "$s" >> "$DIR/revoked-serials.txt"
|
|
echo "revoked $2 serial=$s"; finish ;;
|
|
revoke-serial)
|
|
s=$(echo "$2" | tr 'a-f' 'A-F')
|
|
# append in place: the file is bind-mounted into the API containers (a new inode would not be seen)
|
|
grep -qx "$s" "$DIR/revoked-serials.txt" 2>/dev/null || echo "$s" >> "$DIR/revoked-serials.txt"
|
|
echo "revoked serial=$s" ;;
|
|
expired) expired "$2"; finish ;;
|
|
serial) serial "$2" ;;
|
|
list)
|
|
for name in $(echo "$LEAVES" | awk 'NF {print $1}'); do
|
|
printf '%-15s %-28s %s %s\n' "$name" "$(leaf_line "$name" | awk '{print $4}')" "$(serial "$name")" \
|
|
"$(docker run --rm -v "$PWD/$DIR:/pki" -w /pki "$IMAGE" x509 -in "$name.crt" -noout -enddate | cut -d= -f2)"
|
|
done ;;
|
|
*) echo "usage: $0 init|issue <leaf>|revoke <leaf>|revoke-serial <hex>|expired <leaf>|serial <leaf>|list" >&2; exit 2 ;;
|
|
esac
|