feat(lab7): implement secure messenger infrastructure with TLS, OpenBao and observability
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
umask 077
|
||||
|
||||
PKI_DIR="${LAB7_PKI_DIR:-$HOME/.local/share/mtusi2026lab7/pki}"
|
||||
|
||||
if [[ ! -f "$PKI_DIR/ca.crt" || ! -f "$PKI_DIR/ca.key" ]]; then
|
||||
echo "ERROR: Lab7 CA not found."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ -e "$PKI_DIR/db.key" || -e "$PKI_DIR/db.crt" ]]; then
|
||||
echo "ERROR: PostgreSQL certificate already exists."
|
||||
echo "Refusing to overwrite existing credentials."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
WORK_DIR="$(mktemp -d)"
|
||||
trap 'rm -rf "$WORK_DIR"' EXIT
|
||||
|
||||
echo "Generating PostgreSQL private key..."
|
||||
|
||||
openssl genpkey \
|
||||
-algorithm RSA \
|
||||
-pkeyopt rsa_keygen_bits:2048 \
|
||||
-out "$WORK_DIR/db.key" 2>/dev/null
|
||||
|
||||
echo "Creating certificate signing request..."
|
||||
|
||||
openssl req -new -sha256 \
|
||||
-key "$WORK_DIR/db.key" \
|
||||
-out "$WORK_DIR/db.csr" \
|
||||
-subj "/CN=db"
|
||||
|
||||
cat > "$WORK_DIR/db.ext" <<EOF
|
||||
basicConstraints=critical,CA:FALSE
|
||||
keyUsage=critical,digitalSignature,keyEncipherment
|
||||
extendedKeyUsage=serverAuth
|
||||
subjectAltName=DNS:db
|
||||
EOF
|
||||
|
||||
echo "Signing certificate with Lab7 CA..."
|
||||
|
||||
openssl x509 -req -sha256 \
|
||||
-in "$WORK_DIR/db.csr" \
|
||||
-CA "$PKI_DIR/ca.crt" \
|
||||
-CAkey "$PKI_DIR/ca.key" \
|
||||
-CAserial "$WORK_DIR/ca.srl" \
|
||||
-CAcreateserial \
|
||||
-days 90 \
|
||||
-out "$WORK_DIR/db.crt" \
|
||||
-extfile "$WORK_DIR/db.ext" 2>/dev/null
|
||||
|
||||
echo "Verifying CA and hostname..."
|
||||
|
||||
openssl verify \
|
||||
-CAfile "$PKI_DIR/ca.crt" \
|
||||
-purpose sslserver \
|
||||
-verify_hostname db \
|
||||
"$WORK_DIR/db.crt"
|
||||
|
||||
install -m 600 "$WORK_DIR/db.key" "$PKI_DIR/db.key"
|
||||
install -m 644 "$WORK_DIR/db.crt" "$PKI_DIR/db.crt"
|
||||
|
||||
echo
|
||||
echo "PASS: PostgreSQL TLS certificate created."
|
||||
echo "SAN: DNS:db"
|
||||
echo "Private key retained outside Git."
|
||||
Reference in New Issue
Block a user