]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_constellix.sh
Fixes getting the correct domain id using Contellix API.
[mirror_acme.sh.git] / dnsapi / dns_constellix.sh
1 #!/usr/bin/env sh
2
3 # Author: Wout Decre <wout@canodus.be>
4
5 CONSTELLIX_Api="https://api.dns.constellix.com/v1"
6 #CONSTELLIX_Key="XXX"
7 #CONSTELLIX_Secret="XXX"
8
9 ######## Public functions #####################
10
11 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
12 # Used to add txt record
13 dns_constellix_add() {
14 fulldomain=$1
15 txtvalue=$2
16
17 CONSTELLIX_Key="${CONSTELLIX_Key:-$(_readaccountconf_mutable CONSTELLIX_Key)}"
18 CONSTELLIX_Secret="${CONSTELLIX_Secret:-$(_readaccountconf_mutable CONSTELLIX_Secret)}"
19
20 if [ -z "$CONSTELLIX_Key" ] || [ -z "$CONSTELLIX_Secret" ]; then
21 _err "You did not specify the Contellix API key and secret yet."
22 return 1
23 fi
24
25 _saveaccountconf_mutable CONSTELLIX_Key "$CONSTELLIX_Key"
26 _saveaccountconf_mutable CONSTELLIX_Secret "$CONSTELLIX_Secret"
27
28 if ! _get_root "$fulldomain"; then
29 _err "Invalid domain"
30 return 1
31 fi
32
33 _info "Adding TXT record"
34 if _constellix_rest POST "domains/${_domain_id}/records" "[{\"type\":\"txt\",\"add\":true,\"set\":{\"name\":\"${_sub_domain}\",\"ttl\":120,\"roundRobin\":[{\"value\":\"${txtvalue}\"}]}}]"; then
35 if printf -- "%s" "$response" | grep "{\"success\":\"1 record(s) added, 0 record(s) updated, 0 record(s) deleted\"}" >/dev/null; then
36 _info "Added"
37 return 0
38 else
39 _err "Error adding TXT record"
40 return 1
41 fi
42 fi
43 }
44
45 # Usage: fulldomain txtvalue
46 # Used to remove the txt record after validation
47 dns_constellix_rm() {
48 fulldomain=$1
49 txtvalue=$2
50
51 CONSTELLIX_Key="${CONSTELLIX_Key:-$(_readaccountconf_mutable CONSTELLIX_Key)}"
52 CONSTELLIX_Secret="${CONSTELLIX_Secret:-$(_readaccountconf_mutable CONSTELLIX_Secret)}"
53
54 if [ -z "$CONSTELLIX_Key" ] || [ -z "$CONSTELLIX_Secret" ]; then
55 _err "You did not specify the Contellix API key and secret yet."
56 return 1
57 fi
58
59 if ! _get_root "$fulldomain"; then
60 _err "Invalid domain"
61 return 1
62 fi
63
64 _info "Removing TXT record"
65 if _constellix_rest POST "domains/${_domain_id}/records" "[{\"type\":\"txt\",\"delete\":true,\"filter\":{\"field\":\"name\",\"op\":\"eq\",\"value\":\"${_sub_domain}\"}}]"; then
66 if printf -- "%s" "$response" | grep "{\"success\":\"0 record(s) added, 0 record(s) updated, 1 record(s) deleted\"}" >/dev/null; then
67 _info "Removed"
68 return 0
69 else
70 _err "Error removing TXT record"
71 return 1
72 fi
73 fi
74 }
75
76 #################### Private functions below ##################################
77
78 _get_root() {
79 domain=$1
80 i=2
81 p=1
82 _debug "Detecting root zone"
83 while true; do
84 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
85 if [ -z "$h" ]; then
86 return 1
87 fi
88
89 if ! _constellix_rest GET "domains/search?name=$h"; then
90 return 1
91 fi
92
93 if _contains "$response" "\"name\":\"$h\""; then
94 _domain_id=$(printf "%s\n" "$response" | _egrep_o "\"id\":[0-9]+" | cut -d ':' -f 2)
95 if [ "$_domain_id" ]; then
96 _sub_domain=$(printf "%s" "$domain" | cut -d '.' -f 1-$p)
97 _domain="$h"
98
99 _debug _domain_id "$_domain_id"
100 _debug _sub_domain "$_sub_domain"
101 _debug _domain "$_domain"
102 return 0
103 fi
104 return 1
105 fi
106 p=$i
107 i=$(_math "$i" + 1)
108 done
109 return 1
110 }
111
112 _constellix_rest() {
113 m=$1
114 ep="$2"
115 data="$3"
116 _debug "$ep"
117
118 rdate=$(date +"%s")"000"
119 hmac=$(printf "%s" "$rdate" | _hmac sha1 "$(printf "%s" "$CONSTELLIX_Secret" | _hex_dump | tr -d ' ')" | _base64)
120
121 export _H1="x-cnsdns-apiKey: $CONSTELLIX_Key"
122 export _H2="x-cnsdns-requestDate: $rdate"
123 export _H3="x-cnsdns-hmac: $hmac"
124 export _H4="Accept: application/json"
125 export _H5="Content-Type: application/json"
126
127 if [ "$m" != "GET" ]; then
128 _debug data "$data"
129 response="$(_post "$data" "$CONSTELLIX_Api/$ep" "" "$m")"
130 else
131 response="$(_get "$CONSTELLIX_Api/$ep")"
132 fi
133
134 if [ "$?" != "0" ]; then
135 _err "Error $ep"
136 return 1
137 fi
138
139 _debug response "$response"
140 return 0
141 }