Skip to content

Commit 28c0886

Browse files
authored
Merge pull request #6440 from jf-lines/dns_infomaniak_API_v2
dns_infomaniak: upgraded to API v2. v1 is deprecated.
2 parents d8053ed + 274fd53 commit 28c0886

File tree

1 file changed

+47
-56
lines changed

1 file changed

+47
-56
lines changed

dnsapi/dns_infomaniak.sh

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_infomaniak
66
Options:
77
INFOMANIAK_API_TOKEN API Token
88
Issues: github.com/acmesh-official/acme.sh/issues/3188
9+
910
'
1011

11-
# To use this API you need visit the API dashboard of your account
12-
# once logged into https://manager.infomaniak.com add /api/dashboard to the URL
13-
#
12+
# To use this API you need visit the API dashboard of your account.
1413
# Note: the URL looks like this:
15-
# https://manager.infomaniak.com/v3/<account_id>/api/dashboard
16-
# Then generate a token with the scope Domain
14+
# https://manager.infomaniak.com/v3/<account_id>/ng/profile/user/token/list
15+
# Then generate a token with following scopes :
16+
# - domain:read
17+
# - dns:read
18+
# - dns:write
1719
# this is given as an environment variable INFOMANIAK_API_TOKEN
1820

1921
# base variables
@@ -65,33 +67,32 @@ dns_infomaniak_add() {
6567
_debug fulldomain "$fulldomain"
6668
_debug txtvalue "$txtvalue"
6769

68-
fqdn=${fulldomain#_acme-challenge.}
69-
7070
# guess which base domain to add record to
71-
zone_and_id=$(_find_zone "$fqdn")
72-
if [ -z "$zone_and_id" ]; then
73-
_err "cannot find zone to modify"
71+
zone=$(_get_zone "$fulldomain")
72+
if [ -z "$zone" ]; then
73+
_err "cannot find zone:<${zone}> to modify"
7474
return 1
7575
fi
76-
zone=${zone_and_id% *}
77-
domain_id=${zone_and_id#* }
7876

7977
# extract first part of domain
8078
key=${fulldomain%."$zone"}
8179

82-
_debug "zone:$zone id:$domain_id key:$key"
80+
_debug "key:$key"
81+
_debug "txtvalue: $txtvalue"
8382

8483
# payload
8584
data="{\"type\": \"TXT\", \"source\": \"$key\", \"target\": \"$txtvalue\", \"ttl\": $INFOMANIAK_TTL}"
8685

8786
# API call
88-
response=$(_post "$data" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record")
89-
if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
90-
_info "Record added"
91-
_debug "Response: $response"
92-
return 0
87+
response=$(_post "$data" "${INFOMANIAK_API_URL}/2/zones/${zone}/records")
88+
if [ -n "$response" ]; then
89+
if [ ! "$(echo "$response" | _contains '"result":"success"')" ]; then
90+
_info "Record added"
91+
_debug "response: $response"
92+
return 0
93+
fi
9394
fi
94-
_err "could not create record"
95+
_err "Could not create record."
9596
_debug "Response: $response"
9697
return 1
9798
}
@@ -106,7 +107,7 @@ dns_infomaniak_rm() {
106107

107108
if [ -z "$INFOMANIAK_API_TOKEN" ]; then
108109
INFOMANIAK_API_TOKEN=""
109-
_err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN"
110+
_err "Please provide a valid Infomaniak API token in variable INFOMANIAK_API_TOKEN."
110111
return 1
111112
fi
112113

@@ -138,63 +139,53 @@ dns_infomaniak_rm() {
138139
_debug fulldomain "$fulldomain"
139140
_debug txtvalue "$txtvalue"
140141

141-
fqdn=${fulldomain#_acme-challenge.}
142-
143142
# guess which base domain to add record to
144-
zone_and_id=$(_find_zone "$fqdn")
145-
if [ -z "$zone_and_id" ]; then
146-
_err "cannot find zone to modify"
143+
zone=$(_get_zone "$fulldomain")
144+
if [ -z "$zone" ]; then
145+
_err "cannot find zone:<$zone> to modify"
147146
return 1
148147
fi
149-
zone=${zone_and_id% *}
150-
domain_id=${zone_and_id#* }
151148

152149
# extract first part of domain
153150
key=${fulldomain%."$zone"}
151+
key=$(echo "$key" | _lower_case)
154152

155-
_debug "zone:$zone id:$domain_id key:$key"
153+
_debug "zone:$zone"
154+
_debug "key:$key"
156155

157156
# find previous record
158-
# shellcheck disable=SC1004
159-
record_id=$(_get "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}\
160-
{/g' | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source_idn":"'"$fulldomain"'".*"target_idn":"'"$txtvalue"'".*/\1/p')
157+
# shellcheck disable=SC2086
158+
response=$(_get "${INFOMANIAK_API_URL}/2/zones/${zone}/records" | sed 's/.*"data":\[\(.*\)\]}/\1/; s/},{/}{/g')
159+
record_id=$(echo "$response" | sed -n 's/.*"id":"*\([0-9]*\)"*.*"source":"'"$key"'".*"target":"\\"'"$txtvalue"'\\"".*/\1/p')
160+
_debug "key: $key"
161+
_debug "txtvalue: $txtvalue"
162+
_debug "record_id: $record_id"
163+
161164
if [ -z "$record_id" ]; then
162165
_err "could not find record to delete"
166+
_debug "response: $response"
163167
return 1
164168
fi
165-
_debug "record_id: $record_id"
166169

167170
# API call
168-
response=$(_post "" "${INFOMANIAK_API_URL}/1/domain/$domain_id/dns/record/$record_id" "" DELETE)
169-
if [ -n "$response" ] && echo "$response" | _contains '"result":"success"'; then
170-
_info "Record deleted"
171-
return 0
171+
response=$(_post "" "${INFOMANIAK_API_URL}/2/zones/${zone}/records/${record_id}" "" DELETE)
172+
if [ -n "$response" ]; then
173+
if [ ! "$(echo "$response" | _contains '"result":"success"')" ]; then
174+
_info "Record deleted"
175+
return 0
176+
fi
172177
fi
173-
_err "could not delete record"
178+
_err "Could not delete record."
179+
_debug "Response: $response"
174180
return 1
175181
}
176182

177183
#################### Private functions below ##################################
178184

179-
_get_domain_id() {
185+
_get_zone() {
180186
domain="$1"
181-
187+
# Whatever the domain is, you can get the fqdn with the following.
182188
# shellcheck disable=SC1004
183-
_get "${INFOMANIAK_API_URL}/1/product?service_name=domain&customer_name=$domain" | sed 's/.*"data":\[{\(.*\)}\]}/\1/; s/,/\
184-
/g' | sed -n 's/^"id":\(.*\)/\1/p'
185-
}
186-
187-
_find_zone() {
188-
zone="$1"
189-
190-
# find domain in list, removing . parts sequentialy
191-
while _contains "$zone" '\.'; do
192-
_debug "testing $zone"
193-
id=$(_get_domain_id "$zone")
194-
if [ -n "$id" ]; then
195-
echo "$zone $id"
196-
return
197-
fi
198-
zone=${zone#*.}
199-
done
189+
response=$(_get "${INFOMANIAK_API_URL}/2/domains/${domain}/zones" | sed 's/.*\[{"fqdn"\:"\(.*\)/\1/')
190+
echo "${response%%\"*}"
200191
}

0 commit comments

Comments
 (0)