#!/bin/bash
export SSL_PATH=/opt/ssl
EC2_AVAIL_ZONE=$(curl -s --max-time 5 https://169.254.169.254/latest/meta-data/placement/availability-zone)
if [[ $? -eq 0 ]]; then
# shellcheck disable=SC2001,SC2006
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
export AWS_DEFAULT_REGION=$EC2_REGION
export AWS_REGION=$EC2_REGION
fi
[[ -z "${VAULT_TOKEN}" ]] && vault auth -method=aws >/dev/null
VAULT_JSON="$(vault write -format=json cuddletech_ops/issue/jenkins common_name=jenkins ttl=15551999)"
echo "$VAULT_JSON" | jq -r .data.private_key > ${SSL_PATH}/key.pem
echo "$VAULT_JSON" | jq -r .data.certificate > ${SSL_PATH}/cert.pem
echo "$VAULT_JSON" | jq -r .data.issuing_ca > ${SSL_PATH}/ca.pem
cat ${SSL_PATH}/cert.pem ${SSL_PATH}/ca.pem > ${SSL_PATH}/bundle.pem
# run your command and point to the certs generated above. Example in nginx:
# ssl on;
# ssl_certificate /opt/ssl/bundle.pem;
# ssl_certificate_key /opt/ssl/key.pem;
Terraforming the certificate issuer
If you somehow get the previous part to run, it will fail because you have to create the role in Vault before you can just start generating certs off it. In the cuddletech guide this is under Requesting a Certificate for a Web Server as:
12345
$ vault write cuddletech_ops/roles/web_server \
> key_bits=2048 \
> max_ttl=8760h \
> allow_any_name=true
Success! Data written to: cuddletech_ops/roles/web_server
But what if I told you, you could do that in terraform?
If you add that with your ECS task along with the vault role and policy from the previous post about Vault Anything in Terraform, all your dependencies are met and kept in code!
You can now generate certificates off your PKI backend and grab secrets all in one go!