fix: update Exoscale DNS script

This updates the Exoscale DNS script to work with v2 of their API.
This commit is contained in:
Ivan Mirić
2025-12-05 15:12:27 +01:00
parent 0c9d2dafe3
commit 1413aa332b

226
dnsapi/dns_exoscale.sh Executable file → Normal file
View File

@@ -8,9 +8,9 @@ Options:
EXOSCALE_SECRET_KEY API Secret key EXOSCALE_SECRET_KEY API Secret key
' '
EXOSCALE_API=https://api.exoscale.com/dns/v1 EXOSCALE_API="https://api-ch-gva-2.exoscale.com/v2"
######## Public functions ##################### ######## Public functions ########
# Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs" # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
# Used to add txt record # Used to add txt record
@@ -18,159 +18,197 @@ dns_exoscale_add() {
fulldomain=$1 fulldomain=$1
txtvalue=$2 txtvalue=$2
if ! _checkAuth; then _debug "Using Exoscale DNS v2 API"
_debug fulldomain "$fulldomain"
_debug txtvalue "$txtvalue"
if ! _check_auth; then
return 1 return 1
fi fi
_debug "First detect the root zone" root_domain_id=$(_get_root_domain_id "$fulldomain")
if ! _get_root "$fulldomain"; then if [ -z "$root_domain_id" ]; then
_err "invalid domain" _err "Unable to determine root domain ID for $fulldomain"
return 1 return 1
fi fi
_debug root_domain_id "$root_domain_id"
_debug _sub_domain "$_sub_domain" # Always get the subdomain part first
_debug _domain "$_domain" sub_domain=$(_get_sub_domain "$fulldomain" "$root_domain_id")
_debug sub_domain "$sub_domain"
_info "Adding record" # Build the record name properly
if _exoscale_rest POST "domains/$_domain_id/records" "{\"record\":{\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}}" "$_domain_token"; then if [ -z "$sub_domain" ]; then
if _contains "$response" "$txtvalue"; then record_name="_acme-challenge"
_info "Added, OK" else
return 0 record_name="_acme-challenge.$sub_domain"
fi
fi fi
_err "Add txt record error."
return 1
payload=$(printf '{"name":"%s","type":"TXT","content":"%s","ttl":120}' "$record_name" "$txtvalue")
_debug payload "$payload"
response=$(_exoscale_rest POST "/dns-domain/${root_domain_id}/record" "$payload")
if _contains "$response" "\"id\""; then
_info "TXT record added successfully."
return 0
else
_err "Error adding TXT record: $response"
return 1
fi
} }
# Usage: fulldomain txtvalue
# Used to remove the txt record after validation
dns_exoscale_rm() { dns_exoscale_rm() {
fulldomain=$1 fulldomain=$1
txtvalue=$2
if ! _checkAuth; then _debug "Using Exoscale DNS v2 API for removal"
_debug fulldomain "$fulldomain"
if ! _check_auth; then
return 1 return 1
fi fi
_debug "First detect the root zone" root_domain_id=$(_get_root_domain_id "$fulldomain")
if ! _get_root "$fulldomain"; then if [ -z "$root_domain_id" ]; then
_err "invalid domain" _err "Unable to determine root domain ID for $fulldomain"
return 1 return 1
fi fi
_debug _sub_domain "$_sub_domain" record_name="_acme-challenge"
_debug _domain "$_domain" sub_domain=$(_get_sub_domain "$fulldomain" "$root_domain_id")
if [ -n "$sub_domain" ]; then
_debug "Getting txt records" record_name="_acme-challenge.$sub_domain"
_exoscale_rest GET "domains/${_domain_id}/records?type=TXT&name=$_sub_domain" "" "$_domain_token"
if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
_record_id=$(echo "$response" | tr '{' "\n" | grep "\"content\":\"$txtvalue\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
fi fi
if [ -z "$_record_id" ]; then record_id=$(_find_record_id "$root_domain_id" "$record_name")
_err "Can not get record id to remove." if [ -z "$record_id" ]; then
_err "TXT record not found for deletion."
return 1 return 1
fi fi
_debug "Deleting record $_record_id" response=$(_exoscale_rest DELETE "/dns-domain/$root_domain_id/record/$record_id")
if _contains "$response" "\"state\":\"success\""; then
if ! _exoscale_rest DELETE "domains/$_domain_id/records/$_record_id" "" "$_domain_token"; then _info "TXT record deleted successfully."
_err "Delete record error." return 0
else
_err "Error deleting TXT record: $response"
return 1 return 1
fi fi
return 0
} }
#################### Private functions below ################################## ######## Private helpers ########
_checkAuth() { _check_auth() {
EXOSCALE_API_KEY="${EXOSCALE_API_KEY:-$(_readaccountconf_mutable EXOSCALE_API_KEY)}" EXOSCALE_API_KEY="${EXOSCALE_API_KEY:-$(_readaccountconf_mutable EXOSCALE_API_KEY)}"
EXOSCALE_SECRET_KEY="${EXOSCALE_SECRET_KEY:-$(_readaccountconf_mutable EXOSCALE_SECRET_KEY)}" EXOSCALE_SECRET_KEY="${EXOSCALE_SECRET_KEY:-$(_readaccountconf_mutable EXOSCALE_SECRET_KEY)}"
if [ -z "$EXOSCALE_API_KEY" ] || [ -z "$EXOSCALE_SECRET_KEY" ]; then if [ -z "$EXOSCALE_API_KEY" ] || [ -z "$EXOSCALE_SECRET_KEY" ]; then
EXOSCALE_API_KEY="" _err "EXOSCALE_API_KEY and EXOSCALE_SECRET_KEY must be set."
EXOSCALE_SECRET_KEY=""
_err "You don't specify Exoscale application key and application secret yet."
_err "Please create you key and try again."
return 1 return 1
fi fi
_saveaccountconf_mutable EXOSCALE_API_KEY "$EXOSCALE_API_KEY" _saveaccountconf_mutable EXOSCALE_API_KEY "$EXOSCALE_API_KEY"
_saveaccountconf_mutable EXOSCALE_SECRET_KEY "$EXOSCALE_SECRET_KEY" _saveaccountconf_mutable EXOSCALE_SECRET_KEY "$EXOSCALE_SECRET_KEY"
return 0 return 0
} }
#_acme-challenge.www.domain.com _get_root_domain_id() {
#returns
# _sub_domain=_acme-challenge.www
# _domain=domain.com
# _domain_id=sdjkglgdfewsdfg
# _domain_token=sdjkglgdfewsdfg
_get_root() {
if ! _exoscale_rest GET "domains"; then
return 1
fi
domain=$1 domain=$1
i=2 i=1
p=1
while true; do while true; do
h=$(printf "%s" "$domain" | cut -d . -f "$i"-100) candidate=$(printf "%s" "$domain" | cut -d . -f "${i}-100")
_debug h "$h" [ -z "$candidate" ] && return 1
if [ -z "$h" ]; then _debug "Trying root domain candidate: $candidate"
#not valid domains=$(_exoscale_rest GET "/dns-domain")
return 1 # Extract from dns-domains array
fi result=$(echo "$domains" | _egrep_o '"dns-domains":\[.*\]' | _egrep_o '\{"id":"[^"]*","created-at":"[^"]*","unicode-name":"[^"]*"\}' | while read -r item; do
name=$(echo "$item" | _egrep_o '"unicode-name":"[^"]*"' | cut -d'"' -f4)
if _contains "$response" "\"name\":\"$h\"" >/dev/null; then id=$(echo "$item" | _egrep_o '"id":"[^"]*"' | cut -d'"' -f4)
_domain_id=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \") if [ "$name" = "$candidate" ]; then
_domain_token=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \") echo "$id"
if [ "$_domain_token" ] && [ "$_domain_id" ]; then break
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
_domain=$h
return 0
fi fi
return 1 done)
if [ -n "$result" ]; then
echo "$result"
return 0
fi fi
p=$i
i=$(_math "$i" + 1) i=$(_math "$i" + 1)
done done
return 1
} }
# returns response _get_sub_domain() {
fulldomain=$1
root_id=$2
root_info=$(_exoscale_rest GET "/dns-domain/$root_id")
_debug root_info "$root_info"
root_name=$(echo "$root_info" | _egrep_o "\"unicode-name\":\"[^\"]*\"" | cut -d\" -f4)
sub=${fulldomain%%."$root_name"}
if [ "$sub" = "_acme-challenge" ]; then
echo ""
else
# Remove _acme-challenge. prefix to get the actual subdomain
echo "${sub#_acme-challenge.}"
fi
}
_find_record_id() {
root_id=$1
name=$2
records=$(_exoscale_rest GET "/dns-domain/$root_id/record")
# Convert search name to lowercase for case-insensitive matching
name_lower=$(echo "$name" | tr '[:upper:]' '[:lower:]')
echo "$records" | _egrep_o '\{[^}]*"name":"[^"]*"[^}]*\}' | while read -r record; do
record_name=$(echo "$record" | _egrep_o '"name":"[^"]*"' | cut -d'"' -f4)
record_name_lower=$(echo "$record_name" | tr '[:upper:]' '[:lower:]')
if [ "$record_name_lower" = "$name_lower" ]; then
echo "$record" | _egrep_o '"id":"[^"]*"' | _head_n 1 | cut -d'"' -f4
break
fi
done
}
_exoscale_sign() {
k=$1
shift
hex_key=$(printf %b "$k" | _hex_dump | tr -d ' ')
printf %s "$@" | _hmac sha256 "$hex_key"
}
_exoscale_rest() { _exoscale_rest() {
method=$1 method=$1
path="$2" path=$2
data="$3" data=$3
token="$4"
request_url="$EXOSCALE_API/$path" url="${EXOSCALE_API}${path}"
_debug "$path" expiration=$(_math "$(date +%s)" + 300) # 5m from now
# Build the message with the actual body or empty line
message=$(printf "%s %s\n%s\n\n\n%s" "$method" "/v2$path" "$data" "$expiration")
signature=$(_exoscale_sign "$EXOSCALE_SECRET_KEY" "$message" | _base64)
auth="EXO2-HMAC-SHA256 credential=${EXOSCALE_API_KEY},expires=${expiration},signature=${signature}"
_debug "API request: $method $url"
_debug "Signed message: [$message]"
_debug "Authorization header: [$auth]"
export _H1="Accept: application/json" export _H1="Accept: application/json"
export _H2="Authorization: ${auth}"
if [ "$token" ]; then
export _H2="X-DNS-Domain-Token: $token"
else
export _H2="X-DNS-Token: $EXOSCALE_API_KEY:$EXOSCALE_SECRET_KEY"
fi
if [ "$data" ] || [ "$method" = "DELETE" ]; then if [ "$data" ] || [ "$method" = "DELETE" ]; then
export _H3="Content-Type: application/json" export _H3="Content-Type: application/json"
_debug data "$data" _debug data "$data"
response="$(_post "$data" "$request_url" "" "$method")" response="$(_post "$data" "$url" "" "$method")"
else else
response="$(_get "$request_url" "" "" "$method")" response="$(_get "$url" "" "" "$method")"
fi fi
if [ "$?" != "0" ]; then # shellcheck disable=SC2181
_err "error $request_url" if [ "$?" -ne 0 ]; then
_err "error $url"
return 1 return 1
fi fi
_debug2 response "$response" _debug2 response "$response"
echo "$response"
return 0 return 0
} }