Files
mtusi2026lab7/scripts/init_pki.sh
T
HellsTomatoandCopilot 2ef0299e34
Skeleton contracts / contracts (pull_request) Canceled after 0s
Complete lab7 grade 4 implementation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-06 15:35:53 +03:00

53 lines
2.4 KiB
Bash

#!/usr/bin/env sh
# Local lab PKI. Generates only synthetic keys under secrets/pki (ignored by Git).
set -eu
umask 077
export LANG=C.UTF-8 LC_ALL=C.UTF-8
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
pki="$root/secrets/pki"
mkdir -p "$pki"
[ -f "$pki/ca.crt" ] || openssl req -x509 -newkey rsa:3072 -nodes -days 30 -sha256 \
-subj '/CN=mtusi-lab7-local-ca' -keyout "$pki/ca.key" -out "$pki/ca.crt" >/dev/null 2>&1
issue() {
name=$1; san=$2
[ -f "$pki/$name.crt" ] && return
openssl req -newkey rsa:2048 -nodes -subj "/CN=$name" -keyout "$pki/$name.key" -out "$pki/$name.csr" >/dev/null 2>&1
printf 'subjectAltName=%s\nextendedKeyUsage=serverAuth,clientAuth\n' "$san" > "$pki/$name.ext"
openssl x509 -req -days 14 -sha256 -CA "$pki/ca.crt" -CAkey "$pki/ca.key" -CAcreateserial \
-in "$pki/$name.csr" -out "$pki/$name.crt" -extfile "$pki/$name.ext" >/dev/null 2>&1
rm -f "$pki/$name.csr" "$pki/$name.ext"
}
issue ingress 'DNS:localhost,DNS:nginx'
issue api 'DNS:api,DNS:api-1,DNS:api-2'
issue lb 'DNS:lb,DNS:nginx'
issue broker 'DNS:rabbitmq'
issue api-broker 'DNS:api-broker'
issue worker-broker 'DNS:worker-broker'
issue collector 'DNS:otel-collector,DNS:otel-gateway'
issue api-otel 'DNS:api-otel'
issue worker-otel 'DNS:worker-otel'
issue openbao 'DNS:openbao'
issue postgres 'DNS:postgres'
issue redis 'DNS:redis'
issue s3-tls 'DNS:s3-tls'
# RabbitMQ's Java client uses PKCS#12 because the client identity must be presented
# during the AMQPS handshake. The password protects the keystore at rest in secrets/.
for identity in api-broker worker-broker; do
if [ ! -f "$pki/$identity.p12" ]; then
openssl pkcs12 -export -out "$pki/$identity.p12" -inkey "$pki/$identity.key" \
-in "$pki/$identity.crt" -certfile "$pki/ca.crt" -passout pass:changeit >/dev/null 2>&1
fi
done
if [ ! -f "$pki/ca.p12" ]; then
if ! command -v keytool >/dev/null 2>&1; then
printf '%s\n' 'keytool is required to create the Java truststore ca.p12.' >&2
exit 1
fi
keytool -importcert -noprompt -storetype PKCS12 -keystore "$pki/ca.p12" \
-storepass changeit -alias mtusi-lab7-ca -file "$pki/ca.crt" >/dev/null 2>&1
fi
# Compose mounts local secret files directly on Docker Desktop; service processes
# still need read access to the generated certificates and keys.
chmod 644 "$pki"/*.crt "$pki"/*.key "$pki"/*.p12
printf '%s\n' 'PKI ready in secrets/pki; CA private key remains local.'