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 --- deploy_pki | 10 +++--- gen_pki | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ genpki.sh | 119 ------------------------------------------------------------- 3 files changed, 123 insertions(+), 124 deletions(-) create mode 100755 gen_pki delete mode 100755 genpki.sh diff --git a/deploy_pki b/deploy_pki index 841088c..8cbd456 100755 --- a/deploy_pki +++ b/deploy_pki @@ -1,13 +1,13 @@ #!/usr/bin/env ./sshtool -PKI=cluster/$CLUSTER/secrets/pki +PKI=deuxfleurs/cluster/$CLUSTER YEAR=$(date +%Y) cmd mkdir -p /var/lib/nomad/pki /var/lib/consul/pki for file in consul-ca.crt consul$YEAR.crt consul$YEAR.key consul$YEAR-client.crt consul$YEAR-client.key; do - if [ -f "$PKI/$file" ]; then - copy_secret $PKI/$file /var/lib/consul/pki/$file + if pass $PKI/$file >/dev/null; then + write_pass $PKI/$file /var/lib/consul/pki/$file cmd chown consul:root /var/lib/consul/pki/$file fi done @@ -16,8 +16,8 @@ cmd systemctl restart consul cmd sleep 10 for file in nomad-ca.crt nomad$YEAR.crt nomad$YEAR.key; do - if [ -f "$PKI/$file" ]; then - copy_secret $PKI/$file /var/lib/nomad/pki/$file + if pass $PKI/$file >/dev/null; then + write_pass $PKI/$file /var/lib/nomad/pki/$file fi done 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 <" - echo "The cluster name must be the name of a subdirectory of cluster/" - exit 1 -fi - -cd cluster/$CLUSTER - -mkdir -p secrets/pki -cd secrets/pki - -# Do actual stuff - -YEAR=$(date +%Y) -for APP in consul nomad; do - # 1. Create certificate authority - if [ ! -f $APP-ca.key ]; then - echo "Generating $APP CA keys..." - #openssl genpkey -algorithm ED25519 -out $APP-ca.key - openssl genrsa -out $APP-ca.key 4096 - - openssl req -x509 -new -nodes -key $APP-ca.key -sha256 -days 3650 -out $APP-ca.crt -subj "/C=FR/O=Deuxfleurs/CN=$APP" - fi - - CERT="${APP}${YEAR}" - - # 2. Create and sign certificates for inter-node communication - if [ ! -f $CERT.crt ]; then - echo "Generating $CERT agent keys..." - if [ ! -f $CERT.key ]; then - #openssl genpkey -algorithm ED25519 -out $CERT.key - openssl genrsa -out $CERT.key 4096 - fi - openssl req -new -sha256 -key $CERT.key \ - -subj "/C=FR/O=Deuxfleurs/CN=$APP" \ - -out $CERT.csr - openssl req -in $CERT.csr -noout -text - openssl x509 -req -in $CERT.csr \ - -extensions v3_req \ - -extfile <(cat <