Skip to content

Instantly share code, notes, and snippets.

@bilhackmac
Created August 3, 2021 08:29
Show Gist options
  • Save bilhackmac/52e20ded8b7f008410fe061778932351 to your computer and use it in GitHub Desktop.
Save bilhackmac/52e20ded8b7f008410fe061778932351 to your computer and use it in GitHub Desktop.
ACME DNS01 challenge script for HTTPD mod_md MDChallengeDns01 directive — CloudFlare
#!/usr/bin/env sh
# Env vars CLOUDFLARE_API_TOKEN is required in any case
# Script require cURL an jq
CLOUDFLARE_API_URL=${CLOUDFLARE_API_URL:-"https://api.cloudflare.com/client/v4/"}
request() {
curl -sX${1} \
-H "Content-Type: application/json;charset=utf-8" \
-H "Authorization: Bearer ${CLOUDFLARE_API_TOKEN}" \
-d "${3}" \
"${CLOUDFLARE_API_URL}${2}"
}
zoneId() {
request GET "zones?name=${1}" | jq -r .result[0].id
}
setup() {
${0} teardown "${@}"
local zoneId="$(zoneId ${1})"
request POST "zones/${zoneId}/dns_records" "{\"type\": \"TXT\",\"name\": \"_acme-challenge.${1}\",\"content\": \"${2}\"}" > /dev/null
}
teardown() {
local zoneId="$(zoneId ${1})"
local records="$(request GET "zones/${zoneId}/dns_records?type=TXT&name=_acme-challenge.${1}" | jq -r .result[].id)"
for record in ${records}; do
request DELETE "zones/${zoneId}/dns_records/${record}" > /dev/null
done
}
call=${1}
shift
${call} "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment