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/genpki.sh b/gen_pki similarity index 50% rename from genpki.sh rename to gen_pki index 6afb160..57da699 100755 --- a/genpki.sh +++ b/gen_pki @@ -1,8 +1,6 @@ -#!/bin/bash +#!/usr/bin/env sh -set -xe - -# Enter proper cluster subdirectory +set -ex cd $(dirname $0) @@ -13,38 +11,34 @@ if [ -z "$CLUSTER" ] || [ ! -d "cluster/$CLUSTER" ]; then exit 1 fi -cd cluster/$CLUSTER - -mkdir -p secrets/pki -cd secrets/pki - -# Do actual stuff +PREFIX="deuxfleurs/cluster/$CLUSTER" YEAR=$(date +%Y) for APP in consul nomad; do # 1. Create certificate authority - if [ ! -f $APP-ca.key ]; then + if ! pass $PREFIX/$APP-ca.key >/dev/null; then echo "Generating $APP CA keys..." - #openssl genpkey -algorithm ED25519 -out $APP-ca.key - openssl genrsa -out $APP-ca.key 4096 + openssl genrsa 4096 | pass insert -m $PREFIX/$APP-ca.key - openssl req -x509 -new -nodes -key $APP-ca.key -sha256 -days 3650 -out $APP-ca.crt -subj "/C=FR/O=Deuxfleurs/CN=$APP" + 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 [ ! -f $CERT.crt ]; then + if ! pass $PREFIX/$CERT.crt >/dev/null; then echo "Generating $CERT agent keys..." - if [ ! -f $CERT.key ]; then - #openssl genpkey -algorithm ED25519 -out $CERT.key - openssl genrsa -out $CERT.key 4096 + if ! pass $PREFIX/$CERT.key >/dev/null; then + openssl genrsa 4096 | pass insert -m $PREFIX/$CERT.key fi - openssl req -new -sha256 -key $CERT.key \ + openssl req -new -sha256 -key <(pass $PREFIX/$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 \ + -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 [ ! -f $CERT-client.key ]; then - #openssl genpkey -algorithm ED25519 -out $CERT-client.key - openssl genrsa -out $CERT-client.key 4096 + 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 $CERT-client.key \ + openssl req -new -sha256 -key <(pass $PREFIX/$CERT-client.key) \ -subj "/C=FR/O=Deuxfleurs/CN=$APP-client" \ - -out $CERT-client.csr - openssl req -in $CERT-client.csr -noout -text - openssl x509 -req -in $CERT-client.csr \ + -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 <