Skip to content

Instantly share code, notes, and snippets.

@AllanTaylor314

AllanTaylor314/auth-hook Secret

Created Jun 26, 2020
Embed
What would you like to do?
Cloudflare Auth Hook for Certbot
#!/bin/bash
echo $CERTBOT_DOMAIN
echo $CERTBOT_VALIDATION
if [ -z "$CERTBOT_TOKEN" ]
then
echo 'DNS'
API_TOKEN="YOUR_TOKEN_HERE"
DOMAIN=$CERTBOT_DOMAIN
ZONE_ID="zone_id_here"
echo $ZONE_ID
CREATE_DOMAIN="_acme-challenge.$CERTBOT_DOMAIN"
RECORD_ID_PRE=$(curl -s -X POST "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"type":"TXT","name":"'"$CREATE_DOMAIN"'","content":"'"$CERTBOT_VALIDATION"'","ttl":120}')
echo "RecordID=$RECORD_ID_PRE"
RECORD_ID=$(echo $RECORD_ID_PRE | python -c "import sys,json;print(json.load(sys.stdin)['result']['id'])")
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 25
else
echo 'HTTP'
echo $CERTBOT_VALIDATION > /var/www/html/.well-known/acme-challenge/$CERTBOT_TOKEN
fi
#!/bin/bash
if [ -z "$CERTBOT_TOKEN" ]
then
echo 'DNS'
API_TOKEN="YOUR_TOKEN_HERE"
if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID ]; then
ZONE_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID)
rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/ZONE_ID
fi
if [ -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID ]; then
RECORD_ID=$(cat /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID)
rm -f /tmp/CERTBOT_$CERTBOT_DOMAIN/RECORD_ID
fi
if [ -n "${ZONE_ID}" ]; then
if [ -n "${RECORD_ID}" ]; then
curl -s -X DELETE "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $API_TOKEN" \
-H "Content-Type: application/json"
fi
fi
else
echo 'HTTP'
rm -f /var/www/html/.well-known/acme-challenge/$CERTBOT_TOKEN
fi
#!/bin/bash
systemctl restart apache2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.