dags/suricata/acme_rsync/Makefile

106 lines
3.3 KiB
Makefile
Raw Normal View History

2023-01-05 18:31:38 +01:00
###############################################################################
WD = /var/lib/dags/acme
CERTS_DIR = $(WD)/certs
DOMAIN = monotremata.xyz
DOMAIN_CERTS_DIR = $(CERTS_DIR)/$(DOMAIN)
###############################################################################
ACME_CA_FILE = $(DOMAIN_CERTS_DIR)/ca.cer
2023-01-05 19:24:50 +01:00
ACME_FULLCHAIN_FILE = $(DOMAIN_CERTS_DIR)/fullchain.cer
ACME_KEY_FILE = $(DOMAIN_CERTS_DIR)/$(DOMAIN).key
2023-01-05 18:31:38 +01:00
###############################################################################
JSON_SECRET = $(WD)/secret.json
SECRET_UPDATED = $(WD)/secret_updated
###############################################################################
K8S_CA_FILE = /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
2023-01-05 19:24:50 +01:00
K8S_TOKEN_FILE = /var/run/secrets/kubernetes.io/serviceaccount/token
2023-01-05 18:31:38 +01:00
K8S_TOKEN = $(shell cat $(K8S_TOKEN_FILE))
K8S_APISERVER = $(KUBERNETES_SERVICE_HOST):$(KUBERNETES_SERVICE_PORT_HTTPS)
2023-01-05 19:24:50 +01:00
K8S_SECRERTS_URL = https://$(K8S_APISERVER)/api/v1/namespaces/$(CERT_NAMESPACE)/secrets
2023-01-05 18:31:38 +01:00
###############################################################################
.PHONY: all sync_certs
all: sync_certs $(SECRET_UPDATED)
###############################################################################
RSYNCD_HOST = narwhal
RSYNCD_USER = user
REMOTE_ACME_PATH=rsync://$(RSYNCD_USER)@$(RSYNCD_HOST)/acme
RSYNC_OPTS=--archive --delete --acls --xattrs --compress --verbose --human-readable
sync_certs:
2023-01-09 18:29:13 +01:00
@mkdir -p $(CERTS_DIR)
2023-01-09 18:32:01 +01:00
@echo "pulling certs with rsync"
2023-01-09 18:29:13 +01:00
@rsync \
2023-01-05 18:31:38 +01:00
$(RSYNC_OPTS) \
$(REMOTE_ACME_PATH) \
$(CERTS_DIR)
$(ACME_CA_FILE): sync_certs
$(ACME_FULLCHAIN_FILE): sync_certs
$(ACME_KEY_FILE): sync_certs
###############################################################################
$(JSON_SECRET): $(ACME_KEY_FILE) $(ACME_FULLCHAIN_FILE) $(ACME_KEY_FILE)
2023-01-09 18:29:13 +01:00
@echo "building json secret file"
@jq --null-input --raw-output \
2023-01-05 18:31:38 +01:00
--arg kind "Secret" \
--arg name "$(SECRET_NAME)" \
--arg cacert "$$(base64 -w 0 $(ACME_CA_FILE))" \
--arg tlscert "$$(base64 -w 0 $(ACME_FULLCHAIN_FILE))" \
--arg tlskey "$$(base64 -w 0 $(ACME_KEY_FILE))" \
2023-01-05 19:24:50 +01:00
'{ kind: $$kind, metadata: {name: $$name}, data: { "ca.crt": $$cacert, "tls.crt": $$tlscert, "tls.key": $$tlskey }}' \
> $@
2023-01-05 18:31:38 +01:00
###############################################################################
define k8s_api
curl \
--include \
--request $(1) \
--write-out "%{http_code}" \
--output /dev/null \
2023-01-05 18:31:38 +01:00
--cacert "$(K8S_CA_FILE)" \
--header "Authorization: Bearer $(K8S_TOKEN)" \
--header 'Accept: application/json' \
--header "Content-Type: application/json"
2023-01-05 18:31:38 +01:00
endef
define get_secret
$(call k8s_api,GET) $(K8S_SECRERTS_URL)/$(SECRET_NAME)
2023-01-05 18:31:38 +01:00
endef
define post_secret
$(call k8s_api,POST) $(K8S_SECRERTS_URL) --data @$(1)
2023-01-05 18:31:38 +01:00
endef
define put_secret
$(call k8s_api,PUT) $(K8S_SECRERTS_URL)/$(SECRET_NAME) --data @$(1)
2023-01-05 18:31:38 +01:00
endef
$(SECRET_UPDATED): $(JSON_SECRET)
2023-01-09 18:29:13 +01:00
@mkdir -p $(@D)
@GET_STATUS_CODE=$$($(call get_secret)); \
2023-01-05 18:31:38 +01:00
if [ "$${GET_STATUS_CODE}" = "404" ]; then \
2023-01-05 19:24:50 +01:00
echo "adding cert"; \
POST_STATUS_CODE=$$($(call post_secret,$^)); \
[ "$${POST_STATUS_CODE}" = "201" ] && touch $@; \
2023-01-05 18:31:38 +01:00
elif [ "$${GET_STATUS_CODE}" = "200" ]; then \
2023-01-05 19:24:50 +01:00
echo "updating existing cert"; \
PUT_STATUS_CODE=$$($(call put_secret,$^)); \
[ "$${PUT_STATUS_CODE}" = "200" ] && touch $@; \
2023-01-05 18:31:38 +01:00
fi
2023-01-09 18:29:13 +01:00
@echo "done"
2023-01-05 18:31:38 +01:00
###############################################################################