ЛР7: TLS/mTLS на всех границах, OpenBao вне dev-mode, least privilege
- внутренняя PKI: root -> ca-servers / ca-clients, leaf на каждый сервис; pki/, secrets/, bootstrap/ не коммитятся и не попадают в образ - внешний HTTPS :8443 с проверкой CA и hostname (IP намеренно не в SAN) - обязательный mTLS LB -> каждая API-реплика: relay проверяет цепочку, CN и серийник (identities.json) и scope; uvicorn слушает только loopback - mTLS к RabbitMQ (TLS + EXTERNAL auth, CN -> user, ACL topology) и к OTLP-ingress (nginx: allowlist CN, Collector доступен только из otel) - TLS к PostgreSQL (verify-full), S3, OpenBao, Loki/Tempo, Grafana - OpenBao: file storage + volume, AppRole с secret_id 24h и token 20m, scoped KV-policy; root token/unseal только у оператора - раздельные DB-роли и S3-ключи с prefix-rights; read_only/cap_drop, stateful-исключения задокументированы - rate limit входа в PostgreSQL (общий для реплик), Argon2id + rehash, семафор хеширования; ротация/отзыв leaf и рабочего секрета без пересборки образов Co-Authored-By: O3 Agent Claude <noreply@ozon.ru>
This commit is contained in:
Executable
+207
@@ -0,0 +1,207 @@
|
||||
#!/bin/sh
|
||||
# Lab7 internal PKI.
|
||||
#
|
||||
# root CA ─┬─ ca-servers (TLS server certificates: ingress, api, postgres, minio,
|
||||
# │ rabbitmq, otel-collector, prometheus, loki, tempo, openbao)
|
||||
# └─ ca-clients (mTLS client certificates: lb, messenger-api, messenger-worker)
|
||||
#
|
||||
# Private keys stay in pki/ (gitignored) and are handed to containers as docker secrets.
|
||||
# Never commit pki/, never bake it into an image.
|
||||
#
|
||||
# usage:
|
||||
# scripts/pki.sh init # create CA + all leaf certificates (idempotent)
|
||||
# scripts/pki.sh issue <leaf> # re-issue one leaf (rotation), refresh identities.json
|
||||
# scripts/pki.sh rogue # untrusted material for negative tests
|
||||
# scripts/pki.sh show # print leaf CN/serial/validity
|
||||
set -eu
|
||||
umask 077
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
PKI=${PKI:-pki}
|
||||
ROOT="$PKI/root"
|
||||
INTER="$PKI/inter"
|
||||
LEAF="$PKI/leaf"
|
||||
TRUST="$PKI/trust"
|
||||
CSR="$PKI/csr"
|
||||
|
||||
DAYS_ROOT=${DAYS_ROOT:-3650}
|
||||
DAYS_INTER=${DAYS_INTER:-1825}
|
||||
DAYS_LEAF=${DAYS_LEAF:-90}
|
||||
|
||||
PY=${PYTHON:-python3}
|
||||
command -v "$PY" >/dev/null 2>&1 || PY=python
|
||||
|
||||
mkdir -p "$ROOT" "$INTER" "$LEAF" "$TRUST" "$CSR"
|
||||
|
||||
# name cn ca san eku
|
||||
leaf_spec() {
|
||||
case "$1" in
|
||||
# Без IP в SAN: подключение к https://127.0.0.1 обязано падать на проверке
|
||||
# hostname — это воспроизводимый отрицательный тест внешнего TLS.
|
||||
ingress-server) echo "ingress-server lb ca-servers DNS:ingress,DNS:nginx,DNS:localhost serverAuth" ;;
|
||||
ingress-client) echo "ingress-client lb ca-clients DNS:lb clientAuth" ;;
|
||||
api-server) echo "api-server messenger-api ca-servers DNS:api,DNS:api-1,DNS:api-2,DNS:messenger-lab7-api-1,DNS:messenger-lab7-api-2,DNS:localhost,IP:127.0.0.1 serverAuth" ;;
|
||||
api-client) echo "api-client messenger-api ca-clients DNS:messenger-api clientAuth" ;;
|
||||
worker-client) echo "worker-client messenger-worker ca-clients DNS:messenger-worker clientAuth" ;;
|
||||
health-client) echo "health-client health ca-clients DNS:health clientAuth" ;;
|
||||
postgres) echo "postgres postgres ca-servers DNS:postgres serverAuth" ;;
|
||||
minio) echo "minio minio ca-servers DNS:minio serverAuth" ;;
|
||||
rabbitmq) echo "rabbitmq rabbitmq ca-servers DNS:rabbitmq serverAuth" ;;
|
||||
collector) echo "collector otel-collector ca-servers DNS:otel-collector serverAuth" ;;
|
||||
prometheus) echo "prometheus prometheus ca-servers DNS:prometheus serverAuth" ;;
|
||||
loki) echo "loki loki ca-servers DNS:loki serverAuth" ;;
|
||||
tempo) echo "tempo tempo ca-servers DNS:tempo serverAuth" ;;
|
||||
openbao) echo "openbao openbao ca-servers DNS:openbao,IP:127.0.0.1 serverAuth" ;;
|
||||
*) return 1 ;;
|
||||
esac
|
||||
}
|
||||
|
||||
LEAVES="ingress-server ingress-client api-server api-client worker-client health-client
|
||||
postgres minio rabbitmq collector prometheus loki tempo openbao"
|
||||
|
||||
root_ca() {
|
||||
[ -s "$ROOT/root-ca.key" ] && return 0
|
||||
echo "== root CA"
|
||||
openssl genrsa -out "$ROOT/root-ca.key" 4096 2>/dev/null
|
||||
openssl req -x509 -new -key "$ROOT/root-ca.key" -sha256 -days "$DAYS_ROOT" \
|
||||
-subj "/O=lab7/CN=Lab7 Root CA" \
|
||||
-addext "basicConstraints=critical,CA:TRUE,pathlen:1" \
|
||||
-addext "keyUsage=critical,keyCertSign,cRLSign" \
|
||||
-addext "subjectKeyIdentifier=hash" \
|
||||
-out "$ROOT/root-ca.crt"
|
||||
}
|
||||
|
||||
inter_ca() {
|
||||
name=$1
|
||||
cn=$2
|
||||
[ -s "$INTER/$name.key" ] && return 0
|
||||
echo "== intermediate CA $name ($cn)"
|
||||
openssl genrsa -out "$INTER/$name.key" 4096 2>/dev/null
|
||||
openssl req -new -key "$INTER/$name.key" -subj "/O=lab7/CN=$cn" -out "$CSR/$name.csr"
|
||||
cat > "$CSR/$name.ext" <<EOF
|
||||
basicConstraints=critical,CA:TRUE,pathlen:0
|
||||
keyUsage=critical,keyCertSign,cRLSign
|
||||
subjectKeyIdentifier=hash
|
||||
authorityKeyIdentifier=keyid,issuer
|
||||
EOF
|
||||
openssl x509 -req -in "$CSR/$name.csr" -CA "$ROOT/root-ca.crt" -CAkey "$ROOT/root-ca.key" \
|
||||
-CAcreateserial -days "$DAYS_INTER" -sha256 -extfile "$CSR/$name.ext" \
|
||||
-out "$INTER/$name.crt" 2>/dev/null
|
||||
}
|
||||
|
||||
issue() {
|
||||
name=$1
|
||||
cn=$2
|
||||
ca=$3
|
||||
san=$4
|
||||
eku=$5
|
||||
openssl genrsa -out "$LEAF/$name.key" 2048 2>/dev/null
|
||||
openssl req -new -key "$LEAF/$name.key" -subj "/O=lab7/CN=$cn" -out "$CSR/$name.csr"
|
||||
cat > "$CSR/$name.ext" <<EOF
|
||||
basicConstraints=critical,CA:FALSE
|
||||
keyUsage=critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=$eku
|
||||
subjectKeyIdentifier=hash
|
||||
authorityKeyIdentifier=keyid,issuer
|
||||
subjectAltName=$san
|
||||
EOF
|
||||
openssl x509 -req -in "$CSR/$name.csr" -CA "$INTER/$ca.crt" -CAkey "$INTER/$ca.key" \
|
||||
-CAcreateserial -days "$DAYS_LEAF" -sha256 -extfile "$CSR/$name.ext" \
|
||||
-out "$LEAF/$name.crt" 2>/dev/null
|
||||
cat "$LEAF/$name.crt" "$INTER/$ca.crt" > "$LEAF/$name-fullchain.crt"
|
||||
chmod 600 "$LEAF/$name.key"
|
||||
# Публичные части должны читаться non-root контейнерами (bind-mount CA в Prometheus).
|
||||
chmod 644 "$LEAF/$name.crt" "$LEAF/$name-fullchain.crt"
|
||||
echo " $name CN=$cn ca=$ca ${DAYS_LEAF}d"
|
||||
}
|
||||
|
||||
issue_named() {
|
||||
spec=$(leaf_spec "$1") || { echo "unknown leaf: $1" >&2; exit 2; }
|
||||
# shellcheck disable=SC2086
|
||||
set -- $spec
|
||||
issue "$@"
|
||||
}
|
||||
|
||||
bundles() {
|
||||
cat "$ROOT/root-ca.crt" "$INTER/ca-servers.crt" > "$TRUST/ca-servers-bundle.crt"
|
||||
cat "$ROOT/root-ca.crt" "$INTER/ca-clients.crt" > "$TRUST/ca-clients-bundle.crt"
|
||||
# CA certificate that external clients receive to verify the public HTTPS endpoint.
|
||||
cp "$TRUST/ca-servers-bundle.crt" "$TRUST/public-ca.crt"
|
||||
chmod 644 "$TRUST"/*.crt "$ROOT/root-ca.crt" "$INTER"/*.crt
|
||||
}
|
||||
|
||||
identities() {
|
||||
"$PY" scripts/pki_identities.py "$PKI" > "$PKI/identities.json.tmp"
|
||||
mv "$PKI/identities.json.tmp" "$PKI/identities.json"
|
||||
chmod 644 "$PKI/identities.json"
|
||||
echo "== allowed mTLS identities ($PKI/identities.json)"
|
||||
"$PY" -c "import json,sys;d=json.load(open(sys.argv[1]));[print(' %-6s serial=%s scope=%s' % (v['cn'], ','.join(v['serials']), v['scope'])) for v in d.values()]" "$PKI/identities.json"
|
||||
}
|
||||
|
||||
rogue() {
|
||||
# (1) certificate signed by the SAME client CA but with a foreign identity;
|
||||
# (2) certificate from a completely foreign CA that reuses the "lb" identity.
|
||||
mkdir -p "$PKI/rogue"
|
||||
if [ ! -s "$PKI/rogue/rogue-same-ca.crt" ]; then
|
||||
openssl genrsa -out "$PKI/rogue/rogue.key" 2048 2>/dev/null
|
||||
openssl req -new -key "$PKI/rogue/rogue.key" -subj "/O=lab7/CN=rogue" -out "$CSR/rogue.csr"
|
||||
cat > "$CSR/rogue.ext" <<EOF
|
||||
basicConstraints=critical,CA:FALSE
|
||||
keyUsage=critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=clientAuth,serverAuth
|
||||
subjectAltName=DNS:rogue
|
||||
EOF
|
||||
openssl x509 -req -in "$CSR/rogue.csr" -CA "$INTER/ca-clients.crt" -CAkey "$INTER/ca-clients.key" \
|
||||
-CAcreateserial -days 30 -sha256 -extfile "$CSR/rogue.ext" \
|
||||
-out "$PKI/rogue/rogue-same-ca.crt" 2>/dev/null
|
||||
echo "== rogue-same-ca: CN=rogue signed by ca-clients"
|
||||
fi
|
||||
if [ ! -s "$PKI/rogue/foreign.crt" ]; then
|
||||
openssl genrsa -out "$PKI/rogue/foreign-ca.key" 2048 2>/dev/null
|
||||
openssl req -x509 -new -key "$PKI/rogue/foreign-ca.key" -sha256 -days 30 \
|
||||
-subj "/O=rogue/CN=Rogue CA" -out "$PKI/rogue/foreign-ca.crt"
|
||||
openssl genrsa -out "$PKI/rogue/foreign.key" 2048 2>/dev/null
|
||||
openssl req -new -key "$PKI/rogue/foreign.key" -subj "/O=lab7/CN=lb" -out "$CSR/foreign.csr"
|
||||
openssl x509 -req -in "$CSR/foreign.csr" -CA "$PKI/rogue/foreign-ca.crt" \
|
||||
-CAkey "$PKI/rogue/foreign-ca.key" -CAcreateserial -days 30 -sha256 \
|
||||
-extfile "$CSR/rogue.ext" -out "$PKI/rogue/foreign.crt" 2>/dev/null
|
||||
echo "== foreign: CN=lb signed by an untrusted CA"
|
||||
fi
|
||||
}
|
||||
|
||||
show() {
|
||||
for name in $LEAVES; do
|
||||
[ -s "$LEAF/$name.crt" ] || continue
|
||||
printf '%-16s %s\n' "$name" \
|
||||
"$(openssl x509 -in "$LEAF/$name.crt" -noout -subject -serial -enddate | tr '\n' ' ')"
|
||||
done
|
||||
}
|
||||
|
||||
case "${1:-init}" in
|
||||
init)
|
||||
root_ca
|
||||
inter_ca ca-servers "Lab7 Servers CA"
|
||||
inter_ca ca-clients "Lab7 Clients CA"
|
||||
echo "== leaf certificates"
|
||||
for name in $LEAVES; do issue_named "$name"; done
|
||||
bundles
|
||||
identities
|
||||
echo "== done: $PKI/"
|
||||
;;
|
||||
issue)
|
||||
[ $# -ge 2 ] || { echo "usage: $0 issue <leaf>" >&2; exit 2; }
|
||||
issue_named "$2"
|
||||
bundles
|
||||
identities
|
||||
;;
|
||||
rogue)
|
||||
rogue
|
||||
;;
|
||||
show)
|
||||
show
|
||||
;;
|
||||
*)
|
||||
echo "usage: $0 {init|issue <leaf>|rogue|show}" >&2
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
Reference in New Issue
Block a user