Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Safeguard against already existing _acme-challenge records #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions cf-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ get_domain() {
' /usr/share/publicsuffix/effective_tld_names.dat
}

list_record_id() {
local zone="$1"
local fqdn="$2"

cf_req "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records?name=${fqdn}" |
jq -r ".result[] | .id"
}

get_zone_id() {
local fqdn="$1"
local domain=$(get_domain "$fqdn")
Expand Down Expand Up @@ -139,6 +147,19 @@ create_record() {
local content="$4"
local recordid

log "Checking for already existing record $fqdn"
if [ ! `list_record_id "$zone" "$fqdn"` == null ]; then
log "Existing record found (from previous failed attempt?) Deleting."
list_record_id "$zone" "$fqdn" |
while read recordid; do
log " - Deleting $recordid"
cf_req -X DELETE "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records/${recordid}" >/dev/null
done
else
log "No existing record"
fi


log "Creating record $fqdn $type $content"

recordid=$(cf_req -X POST "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records" \
Expand All @@ -153,14 +174,6 @@ create_record() {
echo "$recordid"
}

list_record_id() {
local zone="$1"
local fqdn="$2"

cf_req "https://api.cloudflare.com/client/v4/zones/${zone}/dns_records?name=${fqdn}" |
jq -r ".result[] | .id"
}

delete_records() {
local zone="$1"
local fqdn="$2"
Expand Down