nixcfg/os/secretgen/gen_pki

119 lines
3.2 KiB
Text
Raw Normal View History

2022-04-20 13:03:04 +00:00
#!/usr/bin/env sh
2021-12-30 18:27:32 +00:00
2022-04-20 13:03:04 +00:00
set -ex
2021-12-30 18:27:32 +00:00
cd $(dirname $0)
CLUSTER="$1"
2022-04-20 12:14:15 +00:00
if [ -z "$CLUSTER" ] || [ ! -d "cluster/$CLUSTER" ]; then
echo "Usage: $0 <cluster name>"
echo "The cluster name must be the name of a subdirectory of cluster/"
exit 1
fi
2022-04-20 13:03:04 +00:00
PREFIX="deuxfleurs/cluster/$CLUSTER"
2021-12-30 18:27:32 +00:00
YEAR=$(date +%Y)
for APP in consul nomad; do
# 1. Create certificate authority
2022-04-20 13:03:04 +00:00
if ! pass $PREFIX/$APP-ca.key >/dev/null; then
2021-12-30 18:27:32 +00:00
echo "Generating $APP CA keys..."
2022-04-20 13:03:04 +00:00
openssl genrsa 4096 | pass insert -m $PREFIX/$APP-ca.key
2021-12-30 18:27:32 +00:00
2022-04-20 13:03:04 +00:00
openssl req -x509 -new -nodes \
-key <(pass $PREFIX/$APP-ca.key) -sha256 \
-days 3650 -subj "/C=FR/O=Deuxfleurs/CN=$APP" \
| pass insert -m -f $PREFIX/$APP-ca.crt
2021-12-30 18:27:32 +00:00
fi
CERT="${APP}${YEAR}"
# 2. Create and sign certificates for inter-node communication
2022-04-20 13:03:04 +00:00
if ! pass $PREFIX/$CERT.crt >/dev/null; then
2021-12-30 18:27:32 +00:00
echo "Generating $CERT agent keys..."
2022-04-20 13:03:04 +00:00
if ! pass $PREFIX/$CERT.key >/dev/null; then
openssl genrsa 4096 | pass insert -m $PREFIX/$CERT.key
2021-12-30 18:27:32 +00:00
fi
2022-04-20 13:03:04 +00:00
openssl req -new -sha256 -key <(pass $PREFIX/$CERT.key) \
2021-12-30 18:27:32 +00:00
-subj "/C=FR/O=Deuxfleurs/CN=$APP" \
2022-04-20 13:03:04 +00:00
-out /tmp/tmp-$CLUSTER-$CERT.csr
openssl req -in /tmp/tmp-$CLUSTER-$CERT.csr -noout -text
openssl x509 -req -in /tmp/tmp-$CLUSTER-$CERT.csr \
2021-12-30 18:27:32 +00:00
-extensions v3_req \
-extfile <(cat <<EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[req_distinguished_name]
C = FR
O = Deuxfleurs
CN = $APP
[v3_req]
keyUsage = keyEncipherment, keyCertSign, dataEncipherment
extendedKeyUsage = serverAuth, clientAuth
2021-12-30 18:27:32 +00:00
subjectAltName = @alt_names
[alt_names]
2022-02-09 14:38:36 +00:00
DNS.1 = server.$CLUSTER.$APP
DNS.2 = client.$CLUSTER.$APP
2021-12-30 18:27:32 +00:00
DNS.3 = localhost
DNS.4 = 127.0.0.1
EOF
) \
2022-04-20 13:03:04 +00:00
-CA <(pass $PREFIX/$APP-ca.crt) \
-CAkey <(pass $PREFIX/$APP-ca.key) -CAcreateserial \
-CAserial /tmp/tmp-$CLUSTER-$CERT.srl \
-days 700 \
| pass insert -m $PREFIX/$CERT.crt
rm /tmp/tmp-$CLUSTER-$CERT.{csr,srl}
2021-12-30 18:27:32 +00:00
fi
# 3. Create client-only certificate used for the CLI
2022-04-20 13:03:04 +00:00
if ! pass $PREFIX/$CERT-client.crt >/dev/null; then
2021-12-30 18:27:32 +00:00
echo "Generating $CERT client keys..."
2022-04-20 13:03:04 +00:00
if ! pass $PREFIX/$CERT-client.key >/dev/null; then
openssl genrsa 4096 | pass insert -m $PREFIX/$CERT-client.key
2021-12-30 18:27:32 +00:00
fi
2022-04-20 13:03:04 +00:00
openssl req -new -sha256 -key <(pass $PREFIX/$CERT-client.key) \
2021-12-30 18:27:32 +00:00
-subj "/C=FR/O=Deuxfleurs/CN=$APP-client" \
2022-04-20 13:03:04 +00:00
-out /tmp/tmp-$CLUSTER-$CERT-client.csr
openssl req -in /tmp/tmp-$CLUSTER-$CERT-client.csr -noout -text
openssl x509 -req -in /tmp/tmp-$CLUSTER-$CERT-client.csr \
2021-12-30 18:27:32 +00:00
-extensions v3_req \
-extfile <(cat <<EOF
[req]
distinguished_name = req_distinguished_name
req_extensions = v3_req
prompt = no
[req_distinguished_name]
C = FR
O = Deuxfleurs
CN = $APP-client
[v3_req]
keyUsage = keyEncipherment, keyCertSign, dataEncipherment
extendedKeyUsage = clientAuth
2021-12-30 18:27:32 +00:00
subjectAltName = @alt_names
[alt_names]
2022-02-09 14:38:36 +00:00
DNS.1 = client.$CLUSTER.$APP
2021-12-30 18:27:32 +00:00
EOF
) \
2022-04-20 13:03:04 +00:00
-CA <(pass $PREFIX/$APP-ca.crt) \
-CAkey <(pass $PREFIX/$APP-ca.key) \
-CAcreateserial -days 700 \
-CAserial /tmp/tmp-$CLUSTER-$CERT-client.srl \
| pass insert -m $PREFIX/$CERT-client.crt
rm /tmp/tmp-$CLUSTER-$CERT-client.{csr,srl}
2021-12-30 18:27:32 +00:00
fi
#if [ ! -f $CERT-client.p12 ]; then
# openssl pkcs12 -export -out $CERT-client.p12 \
# -in $APP-ca.pem -in $CERT-client.crt -inkey $CERT-client.key
#fi
2021-12-30 18:27:32 +00:00
done