]> git.proxmox.com Git - mirror_acme.sh.git/blob - dnsapi/dns_infomaniak.sh
Merge pull request #4531 from NCDGHA/bugfix/issue_4530_fix_http_status_503
[mirror_acme.sh.git] / dnsapi / dns_infomaniak.sh
1 #!/usr/bin/env sh
2
3 ###############################################################################
4 # Infomaniak API integration
5 #
6 # To use this API you need visit the API dashboard of your account
7 # once logged into https://manager.infomaniak.com add /api/dashboard to the URL
8 #
9 # Please report bugs to
10 # https://github.com/acmesh-official/acme.sh/issues/3188
11 #
12 # Note: the URL looks like this:
13 # https://manager.infomaniak.com/v3/<account_id>/api/dashboard
14 # Then generate a token with the scope Domain
15 # this is given as an environment variable INFOMANIAK_API_TOKEN
16 ###############################################################################
17
18 # base variables
19
20 DEFAULT_INFOMANIAK_API_URL="https://api.infomaniak.com"
21 DEFAULT_INFOMANIAK_TTL=300
22
23 ######## Public functions #####################
24
25 #Usage: dns_infomaniak_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
26 dns_infomaniak_add() {
27
28 INFOMANIAK_API_TOKEN="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
29 INFOMANIAK_API_URL="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
30 INFOMANIAK_TTL="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
31
32 if [ -z "$INFOMANIAK_API_TOKEN" ]; then
33 INFOMANIAK_API_TOKEN=""
34 _err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
35 return 1
36 fi
37
38 if [ -z "$INFOMANIAK_API_URL" ]; then
39 INFOMANIAK_API_URL="$DEFAULT_INFOMANIAK_API_URL"
40 fi
41
42 if [ -z "$INFOMANIAK_TTL" ]; then
43 INFOMANIAK_TTL="$DEFAULT_INFOMANIAK_TTL"
44 fi
45
46 #save the token to the account conf file.
47 _saveaccountconf_mutable INFOMANIAK_API_TOKEN "$INFOMANIAK_API_TOKEN"
48
49 if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
50 _saveaccountconf_mutable INFOMANIAK_API_URL "$INFOMANIAK_API_URL"
51 fi
52
53 if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
54 _saveaccountconf_mutable INFOMANIAK_TTL "$INFOMANIAK_TTL"
55 fi
56
57 export _H1="Authorization: Bearer $INFOMANIAK_API_TOKEN"
58 export _H2="Content-Type: application/json"
59
60 fulldomain="$1"
61 txtvalue="$2"
62
63 _info "Infomaniak DNS API"
64 _debug fulldomain "$fulldomain"
65 _debug txtvalue "$txtvalue"
66
67 fqdn=${fulldomain#_acme-challenge.}
68
69 # guess which base domain to add record to
70 zone_and_id=$(_find_zone "$fqdn")
71 if [ -z "$zone_and_id" ]; then
72 _err "cannot find zone to modify"
73 return 1
74 fi
75 zone=${zone_and_id% *}
76 domain_id=${zone_and_id#* }
77
78 # extract first part of domain
79 key=${fulldomain%."$zone"}
80
81 _debug "zone:$zone id:$domain_id key:$key"
82
83 # payload
84 data="{\"type\": \"TXT\", \"source\": \"$key\", \"target\": \"$txtvalue\", \"ttl\": $INFOMANIAK_TTL}"
85
86 # API call
87 response=$(_post "$data" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record")
88 if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
89 _info "Record added"
90 _debug "Response: $response"
91 return 0
92 fi
93 _err "could not create record"
94 _debug "Response: $response"
95 return 1
96 }
97
98 #Usage: fulldomain txtvalue
99 #Remove the txt record after validation.
100 dns_infomaniak_rm() {
101
102 INFOMANIAK_API_TOKEN="${INFOMANIAK_API_TOKEN:-$(_readaccountconf_mutable INFOMANIAK_API_TOKEN)}"
103 INFOMANIAK_API_URL="${INFOMANIAK_API_URL:-$(_readaccountconf_mutable INFOMANIAK_API_URL)}"
104 INFOMANIAK_TTL="${INFOMANIAK_TTL:-$(_readaccountconf_mutable INFOMANIAK_TTL)}"
105
106 if [ -z "$INFOMANIAK_API_TOKEN" ]; then
107 INFOMANIAK_API_TOKEN=""
108 _err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
109 return 1
110 fi
111
112 if [ -z "$INFOMANIAK_API_URL" ]; then
113 INFOMANIAK_API_URL="$DEFAULT_INFOMANIAK_API_URL"
114 fi
115
116 if [ -z "$INFOMANIAK_TTL" ]; then
117 INFOMANIAK_TTL="$DEFAULT_INFOMANIAK_TTL"
118 fi
119
120 #save the token to the account conf file.
121 _saveaccountconf_mutable INFOMANIAK_API_TOKEN "$INFOMANIAK_API_TOKEN"
122
123 if [ "$INFOMANIAK_API_URL" != "$DEFAULT_INFOMANIAK_API_URL" ]; then
124 _saveaccountconf_mutable INFOMANIAK_API_URL "$INFOMANIAK_API_URL"
125 fi
126
127 if [ "$INFOMANIAK_TTL" != "$DEFAULT_INFOMANIAK_TTL" ]; then
128 _saveaccountconf_mutable INFOMANIAK_TTL "$INFOMANIAK_TTL"
129 fi
130
131 export _H1="Authorization: Bearer $INFOMANIAK_API_TOKEN"
132 export _H2="ContentType: application/json"
133
134 fulldomain=$1
135 txtvalue=$2
136 _info "Infomaniak DNS API"
137 _debug fulldomain "$fulldomain"
138 _debug txtvalue "$txtvalue"
139
140 fqdn=${fulldomain#_acme-challenge.}
141
142 # guess which base domain to add record to
143 zone_and_id=$(_find_zone "$fqdn")
144 if [ -z "$zone_and_id" ]; then
145 _err "cannot find zone to modify"
146 return 1
147 fi
148 zone=${zone_and_id% *}
149 domain_id=${zone_and_id#* }
150
151 # extract first part of domain
152 key=${fulldomain%."$zone"}
153
154 _debug "zone:$zone id:$domain_id key:$key"
155
156 # find previous record
157 # shellcheck disable=SC1004
158 record_id=$(_get "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}\
159 {/g' | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source_idn":"'"$fulldomain"'".*"target_idn":"'"$txtvalue"'".*/\1/p')
160 if [ -z "$record_id" ]; then
161 _err "could not find record to delete"
162 return 1
163 fi
164 _debug "record_id: $record_id"
165
166 # API call
167 response=$(_post "" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record/$record_id" "" DELETE)
168 if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
169 _info "Record deleted"
170 return 0
171 fi
172 _err "could not delete record"
173 return 1
174 }
175
176 #################### Private functions below ##################################
177
178 _get_domain_id() {
179 domain="$1"
180
181 # shellcheck disable=SC1004
182 _get "${INFOMANIAK_API_URL}/1/product?service_name=domain&customer_name=$domain" | sed 's/.*"data":\[{\(.*\)}\]}/\1/; s/,/\
183 /g' | sed -n 's/^"id":\(.*\)/\1/p'
184 }
185
186 _find_zone() {
187 zone="$1"
188
189 # find domain in list, removing . parts sequentialy
190 while _contains "$zone" '\.'; do
191 _debug "testing $zone"
192 id=$(_get_domain_id "$zone")
193 if [ -n "$id" ]; then
194 echo "$zone $id"
195 return
196 fi
197 zone=${zone#*.}
198 done
199 }