]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_exoscale.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_exoscale.sh
1 #!/usr/bin/env sh
2
3 EXOSCALE_API=https://api.exoscale.com/dns/v1
4
5 ######## Public functions #####################
6
7 # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
8 # Used to add txt record
9 dns_exoscale_add() {
10 fulldomain=$1
11 txtvalue=$2
12
13 if ! _checkAuth; then
14 return 1
15 fi
16
17 _debug "First detect the root zone"
18 if ! _get_root "$fulldomain"; then
19 _err "invalid domain"
20 return 1
21 fi
22
23 _debug _sub_domain "$_sub_domain"
24 _debug _domain "$_domain"
25
26 _info "Adding record"
27 if _exoscale_rest POST "domains/$_domain_id/records" "{\"record\":{\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}}" "$_domain_token"; then
28 if _contains "$response" "$txtvalue"; then
29 _info "Added, OK"
30 return 0
31 fi
32 fi
33 _err "Add txt record error."
34 return 1
35
36 }
37
38 # Usage: fulldomain txtvalue
39 # Used to remove the txt record after validation
40 dns_exoscale_rm() {
41 fulldomain=$1
42 txtvalue=$2
43
44 if ! _checkAuth; then
45 return 1
46 fi
47
48 _debug "First detect the root zone"
49 if ! _get_root "$fulldomain"; then
50 _err "invalid domain"
51 return 1
52 fi
53
54 _debug _sub_domain "$_sub_domain"
55 _debug _domain "$_domain"
56
57 _debug "Getting txt records"
58 _exoscale_rest GET "domains/${_domain_id}/records?type=TXT&name=$_sub_domain" "" "$_domain_token"
59 if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
60 _record_id=$(echo "$response" | tr '{' "\n" | grep "\"content\":\"$txtvalue\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
61 fi
62
63 if [ -z "$_record_id" ]; then
64 _err "Can not get record id to remove."
65 return 1
66 fi
67
68 _debug "Deleting record $_record_id"
69
70 if ! _exoscale_rest DELETE "domains/$_domain_id/records/$_record_id" "" "$_domain_token"; then
71 _err "Delete record error."
72 return 1
73 fi
74
75 return 0
76 }
77
78 #################### Private functions below ##################################
79
80 _checkAuth() {
81 EXOSCALE_API_KEY="${EXOSCALE_API_KEY:-$(_readaccountconf_mutable EXOSCALE_API_KEY)}"
82 EXOSCALE_SECRET_KEY="${EXOSCALE_SECRET_KEY:-$(_readaccountconf_mutable EXOSCALE_SECRET_KEY)}"
83
84 if [ -z "$EXOSCALE_API_KEY" ] || [ -z "$EXOSCALE_SECRET_KEY" ]; then
85 EXOSCALE_API_KEY=""
86 EXOSCALE_SECRET_KEY=""
87 _err "You don't specify Exoscale application key and application secret yet."
88 _err "Please create you key and try again."
89 return 1
90 fi
91
92 _saveaccountconf_mutable EXOSCALE_API_KEY "$EXOSCALE_API_KEY"
93 _saveaccountconf_mutable EXOSCALE_SECRET_KEY "$EXOSCALE_SECRET_KEY"
94
95 return 0
96 }
97
98 #_acme-challenge.www.domain.com
99 #returns
100 # _sub_domain=_acme-challenge.www
101 # _domain=domain.com
102 # _domain_id=sdjkglgdfewsdfg
103 # _domain_token=sdjkglgdfewsdfg
104 _get_root() {
105
106 if ! _exoscale_rest GET "domains"; then
107 return 1
108 fi
109
110 domain=$1
111 i=2
112 p=1
113 while true; do
114 h=$(printf "%s" "$domain" | cut -d . -f $i-100)
115 _debug h "$h"
116 if [ -z "$h" ]; then
117 #not valid
118 return 1
119 fi
120
121 if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
122 _domain_id=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
123 _domain_token=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
124 if [ "$_domain_token" ] && [ "$_domain_id" ]; then
125 _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
126 _domain=$h
127 return 0
128 fi
129 return 1
130 fi
131 p=$i
132 i=$(_math "$i" + 1)
133 done
134 return 1
135 }
136
137 # returns response
138 _exoscale_rest() {
139 method=$1
140 path="$2"
141 data="$3"
142 token="$4"
143 request_url="$EXOSCALE_API/$path"
144 _debug "$path"
145
146 export _H1="Accept: application/json"
147
148 if [ "$token" ]; then
149 export _H2="X-DNS-Domain-Token: $token"
150 else
151 export _H2="X-DNS-Token: $EXOSCALE_API_KEY:$EXOSCALE_SECRET_KEY"
152 fi
153
154 if [ "$data" ] || [ "$method" = "DELETE" ]; then
155 export _H3="Content-Type: application/json"
156 _debug data "$data"
157 response="$(_post "$data" "$request_url" "" "$method")"
158 else
159 response="$(_get "$request_url" "" "" "$method")"
160 fi
161
162 if [ "$?" != "0" ]; then
163 _err "error $request_url"
164 return 1
165 fi
166 _debug2 response "$response"
167 return 0
168 }