From 7c1444b7143710066f5173119a529c3b5e101300 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 20 Apr 2022 15:03:04 +0200 Subject: Move pki to pass --- gen_pki | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100755 gen_pki (limited to 'gen_pki') diff --git a/gen_pki b/gen_pki new file mode 100755 index 0000000..57da699 --- /dev/null +++ b/gen_pki @@ -0,0 +1,118 @@ +#!/usr/bin/env sh + +set -ex + +cd $(dirname $0) + +CLUSTER="$1" +if [ -z "$CLUSTER" ] || [ ! -d "cluster/$CLUSTER" ]; then + echo "Usage: $0 " + echo "The cluster name must be the name of a subdirectory of cluster/" + exit 1 +fi + +PREFIX="deuxfleurs/cluster/$CLUSTER" + +YEAR=$(date +%Y) +for APP in consul nomad; do + # 1. Create certificate authority + if ! pass $PREFIX/$APP-ca.key >/dev/null; then + echo "Generating $APP CA keys..." + openssl genrsa 4096 | pass insert -m $PREFIX/$APP-ca.key + + 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 + fi + + CERT="${APP}${YEAR}" + + # 2. Create and sign certificates for inter-node communication + if ! pass $PREFIX/$CERT.crt >/dev/null; then + echo "Generating $CERT agent keys..." + if ! pass $PREFIX/$CERT.key >/dev/null; then + openssl genrsa 4096 | pass insert -m $PREFIX/$CERT.key + fi + openssl req -new -sha256 -key <(pass $PREFIX/$CERT.key) \ + -subj "/C=FR/O=Deuxfleurs/CN=$APP" \ + -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 \ + -extensions v3_req \ + -extfile <(cat </dev/null; then + echo "Generating $CERT client keys..." + if ! pass $PREFIX/$CERT-client.key >/dev/null; then + openssl genrsa 4096 | pass insert -m $PREFIX/$CERT-client.key + fi + openssl req -new -sha256 -key <(pass $PREFIX/$CERT-client.key) \ + -subj "/C=FR/O=Deuxfleurs/CN=$APP-client" \ + -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 \ + -extensions v3_req \ + -extfile <(cat <