#!/usr/bin/env bash # Run in the regional AWS CloudShell after creating the EC2 stack. # This stages installation files. It does not start or reconfigure a running host. set -euo pipefail umask 077 export AWS_PAGER="" stack=${1:?Usage: bash prepare-hosts.sh STACK_NAME RDS_INSTANCE_ID} database=${2:?Supply the RDS instance identifier as the second argument.} region=${AWS_REGION:-${AWS_DEFAULT_REGION:?Open CloudShell in the deployment region.}} scratch=$(mktemp -d) trap 'rm -rf "$scratch"' EXIT # The settings file holds identifiers and secret references. Each conductor # obtains the actual application password and cluster key from its own IAM role. aws cloudformation describe-stacks --region "$region" --stack-name "$stack" --output json > "$scratch/stack.json" stack_id=$(jq -er '.Stacks[0] | select(.StackStatus == "CREATE_COMPLETE" or .StackStatus == "UPDATE_COMPLETE") | .StackId' "$scratch/stack.json") jq -e '.Stacks[0].Outputs | map({key:.OutputKey,value:.OutputValue}) | from_entries' "$scratch/stack.json" > "$scratch/outputs.json" bucket=$(jq -er .Bucket "$scratch/outputs.json") vpc=$(jq -er .VPC "$scratch/outputs.json") listener=$(jq -er .Listener "$scratch/outputs.json") aws rds describe-db-instances --region "$region" --db-instance-identifier "$database" --output json > "$scratch/db.json" host=$(jq -er --arg vpc "$vpc" '.DBInstances[0] | select(.DBSubnetGroup.VpcId == $vpc and .DBInstanceStatus == "available" and .Engine == "postgres" and .DBName == "xyops" and .MasterUsername == "dbowner" and .Endpoint.Port == 5432) | .Endpoint.Address' "$scratch/db.json") || { echo 'Check that RDS is Available in the stack VPC with database xyops, owner dbowner and port 5432.' >&2 exit 1 } alb=$(aws elbv2 describe-listeners --region "$region" --listener-arns "$listener" --query 'Listeners[0].LoadBalancerArn' --output text) image=${XY_IMAGE:-ghcr.io/pixlcore/xyops:latest} jq -e --arg host "$host" --arg alb "$alb" --arg image "$image" \ '{Bucket, AppSecret, KeySecret, BrowserName, Pool, Client} + {DBHost:$host, ALB:$alb, Image:$image} | select(all(.[]; type == "string" and length > 0))' \ "$scratch/outputs.json" > "$scratch/settings.json" # Keep the signing key in the operator's private CloudShell home directory. # Bind it to the stack UUID and compare the public CA before reusing the bucket. directory="$HOME/xyops-ca/${stack_id##*/}" mkdir -p "$directory" cd "$directory" exec 9>.lock flock -n 9 || { echo 'Another preparation is in progress.' >&2; exit 1; } account=$(cut -d: -f5 <<< "$stack_id") existing=$(aws s3api list-objects-v2 --bucket "$bucket" --expected-bucket-owner "$account" --prefix bootstrap/ca.crt \ --query "length(Contents[?Key=='bootstrap/ca.crt'] || \`[]\`)" --output text) if [[ $existing != 0 ]]; then aws s3 cp "s3://$bucket/bootstrap/ca.crt" "$scratch/previous-ca.crt" --only-show-errors if [[ ! -s ca.key ]] || ! cmp -s ca.crt "$scratch/previous-ca.crt"; then echo "Restore this cluster's original CA files to $directory before continuing." >&2 exit 1 fi elif [[ ! -e ca.key && ! -e ca.crt ]]; then openssl req -x509 -newkey rsa:3072 -nodes -sha256 -days 365 -subj '/CN=xyOps private CA' \ -addext 'basicConstraints=critical,CA:TRUE' -addext 'keyUsage=critical,keyCertSign,cRLSign' \ -keyout "$scratch/ca.key" -out "$scratch/ca.crt" mv "$scratch/ca.key" "$scratch/ca.crt" . fi # The two hosts have independent TLS keys and permanent private DNS names. # Keep existing certificates on reruns so a retry cannot silently rotate trust. for slot in c1 c2; do if [[ ! -e "$slot.crt" && ! -e "$slot.key" ]]; then openssl req -new -newkey rsa:2048 -nodes -subj "/CN=$slot.xyops.internal" \ -keyout "$scratch/$slot.key" -out "$scratch/$slot.csr" printf 'subjectAltName=DNS:%s.xyops.internal\nbasicConstraints=CA:FALSE\nextendedKeyUsage=serverAuth\n' "$slot" > "$scratch/$slot.ext" openssl x509 -req -in "$scratch/$slot.csr" -CA ca.crt -CAkey ca.key -CAcreateserial \ -days 90 -sha256 -extfile "$scratch/$slot.ext" -out "$scratch/$slot.crt" mv "$scratch/$slot.key" "$scratch/$slot.crt" . fi openssl verify -CAfile ca.crt -verify_hostname "$slot.xyops.internal" "$slot.crt" test -s "$slot.key" done # Publish only the files consumed on EC2. The CA signing key stays here. # Claim an empty bucket prefix just once, even when two operators start together. if [[ $existing == 0 ]]; then aws s3api put-object --bucket "$bucket" --expected-bucket-owner "$account" --key bootstrap/ca.crt \ --body ca.crt --if-none-match '*' > /dev/null fi for file in c1.crt c1.key c2.crt c2.key "$scratch/settings.json"; do aws s3 cp "$file" "s3://$bucket/bootstrap/${file##*/}" --only-show-errors done printf 'Installation files are ready in s3://%s/bootstrap/\nPrivate CA backup directory: %s\n' "$bucket" "$directory"