Skip to content

Commit

Permalink
[dnsapi] Support adding / removing multiple TXT values for Gandi
Browse files Browse the repository at this point in the history
Gandi supports setting multiple entries by setting multiple array items
for the rrset_values field in their API. Modify the dns_gandi_livedns.sh
script so that it checks for existing entries, appends new ones if
needed, and removes existing ones individually. This enabled wildcard
certificate support on Gandi.

Fixes the dns_gandi_livedns part of #1261.

Tested for creating a multidomain, multiple wild-card certificate on
Gandi and using a test script executing only the dns_gandi_livedns_add
and dns_gandi_livedns_rm functions.
  • Loading branch information
drott committed Dec 28, 2018
1 parent 62d774a commit ff5e868
Showing 1 changed file with 57 additions and 5 deletions.
62 changes: 57 additions & 5 deletions dnsapi/dns_gandi_livedns.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Requires GANDI API KEY set in GANDI_LIVEDNS_KEY set as environment variable
#
#Author: Frédéric Crozat <[email protected]>
# Dominik Röttsches <[email protected]>
#Report Bugs here: https://github.com/fcrozat/acme.sh
#
######## Public functions #####################
Expand Down Expand Up @@ -36,9 +37,7 @@ dns_gandi_livedns_add() {
_debug domain "$_domain"
_debug sub_domain "$_sub_domain"

_gandi_livedns_rest PUT "domains/$_domain/records/$_sub_domain/TXT" "{\"rrset_ttl\": 300, \"rrset_values\":[\"$txtvalue\"]}" \
&& _contains "$response" '{"message": "DNS Record Created"}' \
&& _info "Add $(__green "success")"
_dns_gandi_append_record "$_domain" "$_sub_domain" "$txtvalue"
}

#Usage: fulldomain txtvalue
Expand All @@ -56,9 +55,23 @@ dns_gandi_livedns_rm() {
_debug fulldomain "$fulldomain"
_debug domain "$_domain"
_debug sub_domain "$_sub_domain"
_debug txtvalue "$txtvalue"

_gandi_livedns_rest DELETE "domains/$_domain/records/$_sub_domain/TXT" ""

if ! _dns_gandi_existing_rrset_values "$_domain" "$_sub_domain"; then
return 1
fi
_new_rrset_values=$(echo "$_rrset_values" | sed "s/...$txtvalue...//g")
# Cleanup dangling commata.
_new_rrset_values=$(echo "$_new_rrset_values" | sed "s/, ,/ ,/g")
_new_rrset_values=$(echo "$_new_rrset_values" | sed "s/, *\]/\]/g")
_new_rrset_values=$(echo "$_new_rrset_values" | sed "s/\[ *,/\[/g")
_debug "New rrset_values" "$_new_rrset_values"

_gandi_livedns_rest PUT \
"domains/$_domain/records/$_sub_domain/TXT" \
"{\"rrset_ttl\": 300, \"rrset_values\": $_new_rrset_values}" \
&& _contains "$response" '{"message": "DNS Record Created"}' \
&& _info "Removing record $(__green "success")"
}

#################### Private functions below ##################################
Expand Down Expand Up @@ -98,6 +111,45 @@ _get_root() {
return 1
}

_dns_gandi_append_record() {
domain=$1
sub_domain=$2
txtvalue=$3

if _dns_gandi_existing_rrset_values "$domain" "$sub_domain"; then
_debug "Appending new value"
_rrset_values=$(echo "$_rrset_values" | sed "s/\"]/\",\"$txtvalue\"]/")
else
_debug "Creating new record" "$_rrset_values"
_rrset_values="[\"$txtvalue\"]"
fi
_debug new_rrset_values "$_rrset_values"
_gandi_livedns_rest PUT "domains/$_domain/records/$sub_domain/TXT" \
"{\"rrset_ttl\": 300, \"rrset_values\": $_rrset_values}" \
&& _contains "$response" '{"message": "DNS Record Created"}' \
&& _info "Adding record $(__green "success")"
}

_dns_gandi_existing_rrset_values() {
domain=$1
sub_domain=$2
if ! _gandi_livedns_rest GET "domains/$domain/records/$sub_domain"; then
return 1
fi
if ! _contains "$response" '"rrset_type": "TXT"'; then
_debug "Does not have a _acme-challenge TXT record yet."
return 1
fi
if _contains "$response" '"rrset_values": \[\]'; then
_debug "Empty rrset_values for TXT record, no previous TXT record."
return 1
fi
_debug "Already has TXT record."
_rrset_values=$(echo "$response" | _egrep_o 'rrset_values.*\[.*\]' \
| _egrep_o '\[".*\"]')
return 0
}

_gandi_livedns_rest() {
m=$1
ep="$2"
Expand Down

0 comments on commit ff5e868

Please sign in to comment.