From d5354319cf1b327719929415e5c4300774ccfc76 Mon Sep 17 00:00:00 2001 From: Rich O Date: Fri, 9 Sep 2022 14:11:07 +0000 Subject: [PATCH] Update cloudflare_certbot_dns01.sh --- cloudflare_certbot_dns01.sh | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cloudflare_certbot_dns01.sh diff --git a/cloudflare_certbot_dns01.sh b/cloudflare_certbot_dns01.sh new file mode 100644 index 0000000..fd2d7fd --- /dev/null +++ b/cloudflare_certbot_dns01.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Get your API key from https://www.cloudflare.com/a/account/my-account +API_KEY="your-api-key" +EMAIL="your.email@example.com" + +# Strip only the top domain to get the zone id +DOMAIN=$(expr match "$CERTBOT_DOMAIN" '.*\.\(.*\..*\)') + +# Get the Cloudflare zone id +ZONE_EXTRA_PARAMS="status=active&page=1&per_page=20&order=status&direction=desc&match=all" +ZONE_ID=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$DOMAIN&$ZONE_EXTRA_PARAMS" \ + -H "X-Auth-Email: $EMAIL" \ + -H "X-Auth-Key: $API_KEY" \ + -H "Content-Type: application/json" | python -c "import sys,json;print(json.load(sys.stdin)['result'][0]['id'])") + +# Create TXT record +CREATE_DOMAIN="_acme-challenge.$CERTBOT_DOMAIN" +RECORD_ID=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ + -H "X-Auth-Email: $EMAIL" \ + -H "X-Auth-Key: $API_KEY" \ + -H "Content-Type: application/json" \ + --data '{"type":"TXT","name":"'"$CREATE_DOMAIN"'","content":"'"$CERTBOT_VALIDATION"'","ttl":120}' \ + | python -c "import sys,json;print(json.load(sys.stdin)['result']['id'])") +# Save info for cleanup +if [ ! -d /tmp/CERTBOT_$CERTBOT_DOMAIN ];then + mkdir -m 0700 /tmp/CERTBOT_$CERTBOT_DOMAIN +fi +echo $ZONE_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID +echo $RECORD_ID > /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID + +# Sleep to make sure the change has time to propagate over to DNS +sleep 25 -- 2.39.5