# The step-ca of a Ydentifi deployment (https://ydentifi.io/docs/step-ca-recipes),
# started from this directory with 3 values in the environment, or in a file
# .env beside this one:
#
#   YDFI_TRUST_DOMAIN   the deployment's SPIFFE trust domain, the server's
#                       YDFI_SERVER_TRUST_DOMAIN (ydentifi.local when that
#                       is unset)
#   YDFI_CA_NAME        the name the CA's root and intermediate carry
#   YDFI_CA_DNS_NAMES   the host names the CA is reached by, comma-separated;
#                       localhost is added, for the commands run in the
#                       container
#
# On its first start step-ca makes, in the volume `stepca`, a root and an
# intermediate under it, each valid for 10 years, and a password for their
# keys and for the provisioners' keys. The volume is the CA: it is backed up
# with the care given to the root's key, which it holds until the recipe
# with an offline root removes it. Port 9000 is published on every
# interface of the host; only the hosts that renew the server's
# certificates and the operator's workstation need it, so the host's
# firewall closes it to the rest: step-ca renews any certificate under its
# root that no provisioner issued, so whoever holds one and reaches the CA
# keeps it alive. A port Docker publishes passes Docker's
# own rules before the host's, so that firewall rule goes in the
# DOCKER-USER chain.
services:
  stepca:
    image: smallstep/step-ca:0.30.2@sha256:a2b17872915c193259b75a5474c398326f41bd199f0842093e52cf4182bc8270
    entrypoint: ["/bin/sh", "/pki/step-ca/entrypoint.sh"]
    environment:
      DOCKER_STEPCA_INIT_NAME: ${YDFI_CA_NAME:?the CA's name}
      DOCKER_STEPCA_INIT_DNS_NAMES: ${YDFI_CA_DNS_NAMES:?the host names the CA is reached by}
      YDFI_TRUST_DOMAIN: ${YDFI_TRUST_DOMAIN:?the deployment's SPIFFE trust domain}
    ports:
      - "9000:9000"
    volumes:
      - stepca:/home/step
      - ..:/pki:ro
    healthcheck:
      # Healthy once it serves, with the contract's provisioners in place.
      test: ["CMD", "sh", "-c", "step ca health --ca-url https://localhost:9000 --root /home/step/certs/root_ca.crt && step ca provisioner list --ca-url https://localhost:9000 --root /home/step/certs/root_ca.crt | grep -q ydfi-"]
      interval: 2s
      timeout: 5s
      retries: 30
    restart: unless-stopped

volumes:
  stepca:
