forked from mtusi2026/mtusi2026lab7
72 lines
1.6 KiB
Bash
72 lines
1.6 KiB
Bash
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
umask 077
|
|
|
|
BASE="$HOME/.local/share/mtusi2026lab7/pki"
|
|
DEST="$BASE/openbao-clients"
|
|
|
|
test -f "$BASE/ca.crt"
|
|
test -f "$BASE/ca.key"
|
|
|
|
mkdir -p "$DEST"
|
|
chmod 700 "$DEST"
|
|
|
|
for service in api1 api2 worker; do
|
|
cert="$DEST/${service}.crt"
|
|
key="$DEST/${service}.key"
|
|
|
|
if [[ -e "$cert" || -e "$key" ]]; then
|
|
echo "FAIL: certificate already exists for $service" >&2
|
|
exit 1
|
|
fi
|
|
done
|
|
|
|
for service in api1 api2 worker; do
|
|
name="lab7-openbao-${service}"
|
|
|
|
temp="$(mktemp -d "$DEST/.${service}.XXXXXX")"
|
|
|
|
openssl req \
|
|
-new \
|
|
-newkey rsa:3072 \
|
|
-nodes \
|
|
-keyout "$temp/client.key" \
|
|
-out "$temp/client.csr" \
|
|
-subj "/CN=$name" \
|
|
>/dev/null 2>&1
|
|
|
|
cat > "$temp/extensions.cnf" <<EOF
|
|
basicConstraints=critical,CA:FALSE
|
|
keyUsage=critical,digitalSignature
|
|
extendedKeyUsage=clientAuth
|
|
subjectKeyIdentifier=hash
|
|
EOF
|
|
|
|
serial="0x$(openssl rand -hex 16)"
|
|
|
|
openssl x509 \
|
|
-req \
|
|
-in "$temp/client.csr" \
|
|
-CA "$BASE/ca.crt" \
|
|
-CAkey "$BASE/ca.key" \
|
|
-set_serial "$serial" \
|
|
-out "$temp/client.crt" \
|
|
-days 180 \
|
|
-sha256 \
|
|
-extfile "$temp/extensions.cnf" \
|
|
>/dev/null 2>&1
|
|
|
|
chmod 600 "$temp/client.key"
|
|
chmod 644 "$temp/client.crt"
|
|
|
|
mv "$temp/client.key" "$DEST/${service}.key"
|
|
mv "$temp/client.crt" "$DEST/${service}.crt"
|
|
|
|
rm -rf -- "$temp"
|
|
|
|
echo "PASS: OpenBao client certificate issued for $service"
|
|
done
|
|
|
|
echo "PASS: three separate OpenBao client identities created"
|