|
#!/bin/sh |
|
# SPDX-License-Identifier: MIT |
|
# revamped https://community.hetzner.com/tutorials/letsencrypt-dns |
|
set -e |
|
#set -x |
|
|
|
[ -z "${CERTBOT_DOMAIN}" ] && exit 66 |
|
[ -z "${CERTBOT_VALIDATION}" ] && exit 66 |
|
|
|
TOKEN=/etc/hetzner-dns-token |
|
TOKEN_DIR=/etc/hetzner-dns-tokens |
|
|
|
CERTBOT_TTL="${TTL:-300}" |
|
DOMAIN_NAME=$(expr "$CERTBOT_DOMAIN" : '.*\.\(.*\..*\)') |
|
SUBDOMAIN=".${CERTBOT_DOMAIN%."$DOMAIN_NAME"}" |
|
[ "$CERTBOT_DOMAIN" = "$DOMAIN_NAME" ] && SUBDOMAIN= |
|
|
|
# ONE TOKEN/DOMAIN? *untested* |
|
if [ -d "$TOKEN_DIR" ]; then |
|
DOMAIN_TOKEN="$TOKEN_DIR"/"$CERTBOT_DOMAIN" |
|
if [ -f "$DOMAIN_TOKEN" ]; then |
|
TOKEN="$DOMAIN_TOKEN" |
|
fi |
|
fi |
|
|
|
[ -r "$TOKEN" ] || exit 77 |
|
API_TOKEN="$(cat "$TOKEN")" |
|
|
|
# # https://docs.hetzner.cloud/reference/cloud#zone-rrsets-create-an-rrset |
|
# DATA=$(jq --null-input \ |
|
# --arg certbot_ttl "$CERTBOT_TTL" \ |
|
# --arg certbot_value "${CERTBOT_VALIDATION}" \ |
|
# --arg certbot_name "_acme-challenge.${CERTBOT_DOMAIN}." \ |
|
# ' |
|
# {name: $certbot_name, |
|
# type: "TXT", |
|
# ttl: $certbot_ttl | tonumber, |
|
# records: [{ |
|
# value: $certbot_value | tojson |
|
# }] |
|
# }') |
|
# API_URL=zones/"$DOMAIN_NAME"/rrsets |
|
|
|
|
|
# https://docs.hetzner.cloud/reference/cloud#zone-rrset-actions-add-records-to-an-rrset |
|
# For convenience, the RRSet will be automatically created if it doesn't exist. |
|
# Otherwise, the new records are appended to the existing RRSet. |
|
DATA=$(jq --null-input \ |
|
--arg certbot_ttl "$CERTBOT_TTL" \ |
|
--arg certbot_value "${CERTBOT_VALIDATION}" \ |
|
' |
|
{ ttl: $certbot_ttl | tonumber, |
|
records: [{ |
|
value: $certbot_value | tojson |
|
}] |
|
}') |
|
API_URL=zones/"$DOMAIN_NAME"/rrsets/_acme-challenge"$SUBDOMAIN"/TXT/actions/add_records |
|
|
|
curl -sSfL -o /dev/null \ |
|
-X POST \ |
|
-H "Authorization: Bearer ${API_TOKEN}" \ |
|
-H "Content-Type: application/json" \ |
|
-d "$DATA" \ |
|
https://api.hetzner.cloud/v1/"$API_URL" |
|
|
|
RET="$?" |
|
[ "$RET" -eq 0 ] && sleep 20 |
|
return "$RET" |