#!/bin/bash

source /usr/lib/vault-bootstrap/lib/common.sh \
    || { echo "FATAL: Could not read /usr/lib/vault-bootstrap/lib/common.sh" >&2; exit 1; }

send-to-slack() {
    slack:send-message vault-ops "Vault Bootstrap" vaultboy "$1"
}

error-to-slack() {
    slack:send-message vault-ops "Grumpy Vault Bootstrap" vaultboygrumpy "$1"
}

send-success-notification() {
    send-to-slack "Vault $( vault_running_version ) for cluster \`$cluster_name\` started and unsealed on $ip!"
}

send-error-notification() {
    error-to-slack "Vault for cluster \`$cluster_name\` started on $ip but *failed* to automatically unseal!"
}

while ! vault_is_responding; do
    info "Waiting for Vault to respond..."
    sleep 5
done

while ! vault_is_initialized; do
    info "Vault has not yet been initialized. Waiting for 60 seconds..."
    sleep 60
done
info "Vault has been initialized"

info "Checking seal status..."
if ! vault_is_sealed; then
    info "Vault is unsealed!"
    send-success-notification
    exit 0
fi

case "$unseal_method" in
    manual)
        info "No auto-unseal method was defined. Vault must be manually unsealed."
        send-to-slack "Vault $( vault_running_version ) for cluster \`$cluster_name\` started on $ip but must be *manually unsealed*!"
        exit 0
        ;;

    awskms)
        if [[ -z $AUTO_UNSEAL_AWSKMS_KEY ]]; then
            error-to-slack "Vault for cluster \`$cluster_name\` started on $ip but was *misconfigured* and must be *manually unsealed*!"
            die "AUTO_UNSEAL_AWSKMS_KEY must be set to use 'awskms' unseal method"
        fi
        info "Auto-unseal with AWS KMS is enabled. Vault should automatically unseal..."
        for i in {1..60}; do
            if ! vault_is_sealed; then
                info "Vault is unsealed!"
                send-success-notification
                exit 0
            fi
            sleep 5
        done
        send-error-notification
        die "Vault did not auto-unseal after five minutes."
        ;;

    secretsmanager)
        secrets_path="${UNSEAL_SECRETS_PATH:-vault/${cluster_name}/unseal-keys}"
        info "Fetching unseal secrets from $secrets_path..."
        for i in {1..60}; do
            unseal_secrets=$(
                aws secretsmanager get-secret-value \
                    --output json \
                    --region $aws_region \
                    --secret-id "$secrets_path" \
                | jq -r .SecretString
            )
            if [[ -z $unseal_secrets ]]; then
                info "Unseal secrets were not found at $secrets_path. Waiting..."
                sleep 60
            fi
        done
        if [[ -z $unseal_secrets ]]; then
            send-error-notification
            die "Unseal secrets were not found after an hour. Giving up."
        fi
        # the secret should be a space separated list of unseal keys
        info "Unsealing Vault..."
        for secret in $unseal_secrets; do
            $vault_bin operator unseal $secret
            if ! vault_is_sealed; then
                info "Vault is unsealed!"
                send-success-notification
                exit 0
            fi
        done
        ;;

    *)
        send-error-notification
        die "Unknown unseal method '$unseal_method'"
        ;;
esac

send-error-notification
die "Vault could not be unsealed"
