dags/narwhal/acme_renew/Makefile

182 lines
4.8 KiB
Makefile
Raw Normal View History

2022-09-16 10:34:57 +02:00
WD=/var/lib/dags/acme_renew
2023-05-30 17:40:37 +02:00
.PHONY: all refresh_pg renew_certs lb_sync
2022-09-16 10:34:57 +02:00
NGINX_RELOAD=$(WD)/nginx_reload
2023-05-09 15:42:51 +02:00
CALADAN_SYNC=$(WD)/caladan_sync
FUGU_SYNC=$(WD)/fugu_sync
2023-05-30 17:40:37 +02:00
LB_SYNC=$(WD)/lb_sync
2023-07-10 15:26:11 +02:00
SURICATA_SYNC=$(WD)/suricata_sync
2023-05-09 15:42:51 +02:00
CALADAN_TRIGGER=$(WD)/caladan_trigger
FUGU_TRIGGER=$(WD)/fugu_trigger
2023-05-30 17:40:37 +02:00
LB_TRIGGER=$(WD)/lb_trigger
2023-07-10 15:34:48 +02:00
SURICATA_TRIGGER=$(WD)/suricata_trigger
2023-05-09 15:42:51 +02:00
all: renew_certs $(CALADAN_TRIGGER) $(FUGU_TRIGGER) $(LB_TRIGGER) $(SURICATA_TRIGGER) $(NGINX_RELOAD) refresh_pg refresh_vault
2022-09-16 10:34:57 +02:00
###############################################################################
2023-05-08 18:18:41 +02:00
ACME_DIR=/srv/certs/acme
2022-09-16 18:02:53 +02:00
DOMAIN=monotremata.xyz
2022-09-16 10:34:57 +02:00
2023-07-12 16:13:01 +02:00
CERT_PATH=$(ACME_DIR)/$(DOMAIN)
2023-07-12 15:28:46 +02:00
FULLCHAIN=$(CERT_PATH)/fullchain.pem
CERT=$(CERT_PATH)/cert.pem
KEY=$(CERT_PATH)/key.pem
2022-09-16 10:34:57 +02:00
2023-07-12 16:13:01 +02:00
MAIL_CERT_PATH=$(ACME_DIR)/mail.$(DOMAIN)
MAIL_FULLCHAIN=$(MAIL_CERT_PATH)/fullchain.pem
MAIL_CERT=$(MAIL_CERT_PATH)/cert.pem
MAIL_KEY=$(MAIL_CERT_PATH)/key.pem
2023-08-18 17:24:45 +02:00
CUINA_CERT_PATH=$(ACME_DIR)/cuina.$(DOMAIN)
2023-08-18 17:44:26 +02:00
CUINA_FULLCHAIN=$(CUINA_CERT_PATH)/fullchain.pem
CUINA_CERT=$(CUINA_CERT_PATH)/cert.pem
CUINA_KEY=$(CUINA_CERT_PATH)/key.pem
2023-08-18 17:24:45 +02:00
2023-07-12 16:32:59 +02:00
VAULT_CERT_PATH=$(ACME_DIR)/vault.$(DOMAIN)
VAULT_FULLCHAIN=$(VAULT_CERT_PATH)/fullchain.pem
VAULT_CERT=$(VAULT_CERT_PATH)/cert.pem
VAULT_KEY=$(VAULT_CERT_PATH)/key.pem
2023-05-08 18:18:41 +02:00
###############################################################################
SSH_KEY=/srv/certs/ssh/users/dags/id_ed25519
2022-09-16 10:34:57 +02:00
###############################################################################
# Renew the certificates using acme.sh. Because `renew_certs` is a phony
# target, it will be run each time, but the certificate files will only be
# updated if a renewal happens
CERT_DOMAINS=-d $(DOMAIN) -d '*.$(DOMAIN)' -d '*.narwhal.$(DOMAIN)' -d '*.caladan.$(DOMAIN)' -d '*.xmpp.$(DOMAIN)'
2023-05-09 15:42:51 +02:00
$(FULLCHAIN): renew_certs
2022-09-16 18:02:53 +02:00
$(CERT): renew_certs
$(KEY): renew_certs
2022-09-16 10:34:57 +02:00
HETZNER_TOKEN=/srv/secrets/hetzner_token
DOCKER_IMAGE=neilpang/acme.sh
ACME_DATA_DIR=/mnt/docker_volumes/acmesh/data
2022-09-16 10:34:57 +02:00
ACMESH=docker run --rm -it \
-v $(ACME_DATA_DIR):/acme.sh \
-v $(CERT_PATH):/acme.sh/$(DOMAIN) \
-e "HETZNER_Token=$$(cat $(HETZNER_TOKEN))" \
$(DOCKER_IMAGE) \
/root/.acme.sh/acme.sh
RENEW_CMD=--cron --home /root/.acme.sh --config-home /acme.sh
2022-09-16 10:34:57 +02:00
# DNS_ARGS=--dns dns_linode_v4 --dnssleep 900
DNS_ARGS=--dns dns_hetzner
2023-07-12 15:28:46 +02:00
renew_certs: $(HETZNER_TOKEN)
2022-09-23 10:21:15 +02:00
@echo "renewing certs"
$(ACMESH) $(RENEW_CMD)
2023-05-09 15:42:51 +02:00
###############################################################################
# Sync the certs to remote hosts and trigger DAGs there
RSYNC_ARGS=--archive --delete --compress --verbose --human-readable --rsh "ssh -i $(SSH_KEY)"
$(CALADAN_SYNC): $(FULLCHAIN) $(CERT) $(KEY)
mkdir -p $(@D)
2023-05-08 18:18:41 +02:00
rsync \
2023-05-09 15:42:51 +02:00
$(RSYNC_ARGS) \
2023-05-08 18:18:41 +02:00
--rsync-path="doas rsync" \
2023-05-09 15:42:51 +02:00
$^ \
2023-05-10 12:56:15 +02:00
dags@caladan:$(CERT_PATH)
2023-05-09 15:42:51 +02:00
touch $@
2023-05-08 18:18:41 +02:00
2023-07-12 16:13:01 +02:00
$(FUGU_SYNC): $(MAIL_FULLCHAIN) $(MAIL_CERT) $(MAIL_KEY)
2023-05-09 15:42:51 +02:00
mkdir -p $(@D)
2023-05-08 18:18:41 +02:00
rsync \
2023-05-09 15:42:51 +02:00
$(RSYNC_ARGS) \
2023-05-08 18:18:41 +02:00
--rsync-path="doas rsync" \
2023-05-09 15:42:51 +02:00
$^ \
2023-07-12 16:13:01 +02:00
dags@fugu:$(MAIL_CERT_PATH)
2023-05-09 15:42:51 +02:00
touch $@
2023-05-08 18:18:41 +02:00
2023-07-12 16:32:59 +02:00
$(SURICATA_SYNC): $(VAULT_FULLCHAIN) $(VAULT_CERT) $(VAULT_KEY)
2023-07-10 15:26:11 +02:00
mkdir -p $(@D)
rsync \
$(RSYNC_ARGS) \
--rsync-path="doas rsync" \
$^ \
2023-07-12 16:32:59 +02:00
dags@suricata:$(VAULT_CERT_PATH)
2023-07-10 15:26:11 +02:00
touch $@
2023-08-18 17:24:45 +02:00
$(LB_SYNC): $(FULLCHAIN) $(CERT) $(KEY) $(CUINA_FULLCHAIN) $(CUINA_CERT) $(CUINA_KEY)
2023-05-30 17:40:37 +02:00
mkdir -p $(@D)
rsync \
$(RSYNC_ARGS) \
--rsync-path="doas rsync" \
2023-08-18 17:24:45 +02:00
$(FULLCHAIN) $(CERT) $(KEY) \
2023-06-02 18:02:00 +02:00
dags@lb:/etc/nginx/acme/$(DOMAIN)
2023-08-18 17:24:45 +02:00
rsync \
$(RSYNC_ARGS) \
--rsync-path="doas rsync" \
$(CUINA_FULLCHAIN) $(CUINA_CERT) $(CUINA_KEY) \
dags@lb:/etc/nginx/acme/cuina.$(DOMAIN)
2023-05-30 17:40:37 +02:00
touch $@
KVMD_PST_DATA=/var/lib/kvmd/pst/data
2023-05-09 10:15:54 +02:00
2023-05-09 15:42:51 +02:00
define remote_dag_trigger
mkdir -p $(@D)
ssh -i $(SSH_KEY) dags@$(1) "doas /srv/dags/$(1)/$(2)/run.sh"
touch $@
endef
2023-05-09 10:15:54 +02:00
2023-05-09 15:42:51 +02:00
$(CALADAN_TRIGGER): $(CALADAN_SYNC)
2023-05-10 12:48:52 +02:00
$(call remote_dag_trigger,caladan,acme_refresh)
2023-05-09 15:42:51 +02:00
$(FUGU_TRIGGER): $(FUGU_SYNC)
2023-05-10 12:48:52 +02:00
$(call remote_dag_trigger,fugu,acme_refresh)
2023-05-30 17:40:37 +02:00
$(LB_TRIGGER): $(LB_SYNC)
$(call remote_dag_trigger,lb,acme_refresh)
2023-07-10 15:34:48 +02:00
$(SURICATA_TRIGGER): $(SURICATA_SYNC)
$(call remote_dag_trigger,suricata,acme_refresh)
2022-09-16 10:34:57 +02:00
###############################################################################
# Copy the certificate for the postgresql domain to the folder where postgre
# service expects it
PG_SSL_PATH=/mnt/docker_volumes/postgres/ssl
PG_CERT=$(PG_SSL_PATH)/server.crt
PG_KEY=$(PG_SSL_PATH)/server.key
2023-05-09 15:42:51 +02:00
$(PG_CERT): $(FULLCHAIN)
2022-09-16 10:34:57 +02:00
mkdir -p $(@D)
rsync --copy-links $< $@
2022-09-16 18:02:53 +02:00
$(PG_KEY): $(KEY)
2022-09-16 10:34:57 +02:00
mkdir -p $(@D)
rsync --copy-links $< $@
refresh_pg: $(PG_CERT) $(PG_KEY)
###############################################################################
# Copy Vault certificate
2023-07-12 17:34:41 +02:00
VAULT_DEST_PATH=/srv/certs/vault/tls
VAULT_DEST_CERT=$(VAULT_DEST_PATH)/tls.crt
VAULT_DEST_KEY=$(VAULT_DEST_PATH)/tls.key
$(VAULT_DEST_CERT): $(VAULT_CERT)
mkdir -p $(@D)
install -o vault -g vault -m 600 $^ $@
$(VAULT_DEST_KEY): $(VAULT_KEY)
mkdir -p $(@D)
install -o vault -g vault -m 600 $^ $@
refresh_vault: $(VAULT_DEST_CERT) $(VAULT_DEST_KEY)