From b00a8358b20ac99912bacafd8fee5466da257e67 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 30 Dec 2021 19:27:32 +0100 Subject: [PATCH] Add TLS to Nomad --- configuration.nix | 9 ++++ deploy.sh | 13 +++++- env.sh | 7 ++++ genpki.sh | 104 ++++++++++++++++++++++++++++++++++++++++++++++ sslproxy.sh | 5 +++ 5 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 env.sh create mode 100755 genpki.sh create mode 100755 sslproxy.sh diff --git a/configuration.nix b/configuration.nix index 649e7e8..205eb6d 100644 --- a/configuration.nix +++ b/configuration.nix @@ -224,6 +224,15 @@ in enabled = true; network_interface = "wg0"; }; + tls = { + http = true; + rpc = true; + ca_file = "/var/lib/nomad/pki/nomad-ca.crt"; + cert_file = "/var/lib/nomad/pki/nomad2021.crt"; + key_file = "/var/lib/nomad/pki/nomad2021.key"; + verify_server_hostname = true; + verify_https_client = true; + }; plugin = [ { docker = [ diff --git a/deploy.sh b/deploy.sh index 4d6387f..a4f18c1 100755 --- a/deploy.sh +++ b/deploy.sh @@ -10,6 +10,8 @@ fi TMP_PATH=/tmp/tmp-deploy-$(date +%s) +YEAR=$(date +%Y) + for NIXHOST in $NIXHOSTLIST; do NIXHOST=${NIXHOST%.*} @@ -23,13 +25,15 @@ for NIXHOST in $NIXHOSTLIST; do echo "Sending NixOS config files" - ssh -F ssh_config $SSH_DEST mkdir -p $TMP_PATH + ssh -F ssh_config $SSH_DEST mkdir -p $TMP_PATH $TMP_PATH/pki cat configuration.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/configuration.nix > /dev/null cat node/$NIXHOST.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/node.nix > /dev/null cat node/$NIXHOST.site.nix | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/site.nix > /dev/null echo "Sending secret files" - test -f secrets/rclone.conf && (cat secrets/rclone.conf | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/rclone.conf > /dev/null) + for SECRET in rclone.conf pki/nomad-ca.crt pki/nomad$YEAR.crt pki/nomad$YEAR.key; do + test -f secrets/$SECRET && (cat secrets/$SECRET | ssh -F ssh_config $SSH_DEST tee $TMP_PATH/$SECRET > /dev/null) + done echo "Rebuilding NixOS" @@ -38,7 +42,12 @@ set -ex cd $TMP_PATH mv configuration.nix node.nix site.nix /etc/nixos + test -f rclone.conf && (mv rclone.conf /root; chmod 600 /root/rclone.conf) + +mkdir -p /var/lib/nomad/pki +test -f pki/nomad-ca.crt && mv -v pki/nomad* /var/lib/nomad/pki + nixos-rebuild switch EOF diff --git a/env.sh b/env.sh new file mode 100644 index 0000000..80812d4 --- /dev/null +++ b/env.sh @@ -0,0 +1,7 @@ +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) +YEAR=$(date +%Y) + +export NOMAD_ADDR=https://localhost:14646 +export NOMAD_CACERT=$SCRIPT_DIR/secrets/pki/nomad-ca.crt +export NOMAD_CLIENT_CERT=$SCRIPT_DIR/secrets/pki/nomad$YEAR-client.crt +export NOMAD_CLIENT_KEY=$SCRIPT_DIR/secrets/pki/nomad$YEAR-client.key diff --git a/genpki.sh b/genpki.sh new file mode 100755 index 0000000..be10f6f --- /dev/null +++ b/genpki.sh @@ -0,0 +1,104 @@ +#!/bin/bash + +set -xe + +cd $(dirname $0) + +mkdir -p secrets/pki +cd secrets/pki + +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 <