#!/bin/sh
# step-ca for a Ydentifi deployment (https://ydentifi.io/docs/step-ca-recipes). On its first
# start the image's own entrypoint initialises the CA in its volume: a root,
# an intermediate under it, and a password, generated unless one is given.
# Before step-ca serves, this adds the certificate contract's provisioners,
# under the trust domain YDFI_TRUST_DOMAIN, in the CA's configuration. They
# are added while the CA holds the image's own provisioner and none of the
# contract's: at its first start, or the next one when that set-up was cut
# short. A CA that has held them is never given them again, so one renamed or
# removed afterwards stays as it was left, and a retired name does not come
# back. When they cannot be set up, as when the CA's provisioners issue under
# another trust domain, the CA does not start.
#
# Once they are in, the image's own provisioner, which issues without a
# template, is removed, at that start and every later one; the CA's own
# certificate names localhost beside the names it is reached by, as the
# commands run in its container reach it there; the CA does not start while a
# provisioner issues without a template, or none of the contract's is left;
# and each of the contract's provisioners that issues through a template other
# than its role's in /pki/templates is updated to it, as once the templates
# change: step-ca keeps the template each provisioner was added with. A
# provisioner named as the contract's that is of no role of it stops the CA.
set -eu

STEPPATH=/home/step
config="$STEPPATH/config/ca.json"
. /pki/step-ca/provisioners.sh

trust_domain="${YDFI_TRUST_DOMAIN:-}"
if ! ydfi_check_trust_domain "$trust_domain"; then
	echo "entrypoint: YDFI_TRUST_DOMAIN must name the deployment's SPIFFE trust domain, as YDFI_SERVER_TRUST_DOMAIN does" >&2
	exit 1
fi

# A CA started again under another trust domain stops before it serves.
if [ -f "$config" ] && ! ydfi_check_stored_trust_domain "$config" "$trust_domain"; then
	exit 1
fi

# The image's entrypoint initialises a CA that has no configuration, then
# runs what it is given: here, nothing.
/bin/bash /entrypoint.sh true

# The contract's provisioners are added to a CA that holds the image's own
# provisioner and none of theirs, in a copy of its configuration that
# replaces it once all 4 are in: a set-up cut short leaves none, and the
# image's provisioner with them, so it is made again at the next start.
init_provisioner="${DOCKER_STEPCA_INIT_PROVISIONER_NAME:-admin}"
if ! jq -e --arg n "$init_provisioner" 'any(.authority.provisioners[]?; .name == $n) and (any(.authority.provisioners[]?; .name | startswith("ydfi-")) | not)' "$config" >/dev/null; then
	echo "entrypoint: the CA's provisioners were set up, and are left as they are"
else
	setup="$config.setup"
	cp "$config" "$setup"
	if ydfi_add_provisioners "$setup" "$STEPPATH/secrets/password" /pki/templates "$trust_domain"; then
		mv "$setup" "$config"
	else
		rm -f "$setup"
		echo "entrypoint: the contract's provisioners could not be set up; the CA does not start, rather than serve without them" >&2
		exit 1
	fi
	echo "entrypoint: the contract's provisioners are in place, under spiffe://$trust_domain"
fi

if jq -e --arg n "$init_provisioner" '.authority.provisioners[]? | select(.name == $n)' "$config" >/dev/null; then
	step ca provisioner remove "$init_provisioner" --ca-config "$config" >/dev/null
fi

if ! jq -e 'any(.dnsNames[]?; . == "localhost")' "$config" >/dev/null; then
	jq '.dnsNames += ["localhost"]' "$config" >"$config.next"
	mv "$config.next" "$config"
fi

if ! jq -e '[.authority.provisioners[]?] | any(.name | startswith("ydfi-")) and all(.options.x509.template != null)' "$config" >/dev/null; then
	untemplated=$(jq -r '[.authority.provisioners[]? | select(.options.x509.template == null) | .name] | join(", ")' "$config")
	echo "entrypoint: the CA holds no provisioner of the contract, or one that issues without a template (${untemplated:-none}); it does not start, and a CA left with none of the contract's is set up again only from an empty volume" >&2
	exit 1
fi

if ! updated=$(ydfi_update_templates "$config" /pki/templates); then
	echo "entrypoint: the contract's provisioners could not be updated to the templates in /pki/templates; the CA does not start" >&2
	exit 1
fi
if [ -n "$updated" ]; then
	echo "entrypoint: the provisioners $(printf '%s' "$updated" | tr '\n' ' ') now issue through the templates in /pki/templates"
fi

exec /usr/local/bin/step-ca --password-file "$STEPPATH/secrets/password" "$config"
