|
| 1 | +#!/usr/bin/env sh |
| 2 | +# shellcheck disable=SC2034 |
| 3 | + |
| 4 | +# EdgeCenter DNS API integration for acme.sh |
| 5 | +# Author: Konstantin Ruchev <konstantin.ruchev@edgecenter.ru> |
| 6 | +dns_edgecenter_info='edgecenter DNS API |
| 7 | +Site: https://edgecenter.ru |
| 8 | +Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi#dns_edgecenter |
| 9 | +Options: |
| 10 | + EDGECENTER_API_KEY auth APIKey' |
| 11 | + |
| 12 | +EDGECENTER_API="https://api.edgecenter.ru" |
| 13 | +DOMAIN_TYPE= |
| 14 | +DOMAIN_MASTER= |
| 15 | + |
| 16 | +######## Public functions ##################### |
| 17 | + |
| 18 | +#Usage: dns_edgecenter_add _acme-challenge.www.domain.com "TXT_RECORD_VALUE" |
| 19 | +dns_edgecenter_add() { |
| 20 | + fulldomain="$1" |
| 21 | + txtvalue="$2" |
| 22 | + |
| 23 | + _info "Using EdgeCenter DNS API" |
| 24 | + |
| 25 | + if ! _dns_edgecenter_init_check; then |
| 26 | + return 1 |
| 27 | + fi |
| 28 | + |
| 29 | + _debug "Detecting root zone for $fulldomain" |
| 30 | + if ! _get_root "$fulldomain"; then |
| 31 | + return 1 |
| 32 | + fi |
| 33 | + |
| 34 | + subdomain="${fulldomain%."$_zone"}" |
| 35 | + subdomain=${subdomain%.} |
| 36 | + |
| 37 | + _debug "Zone: $_zone" |
| 38 | + _debug "Subdomain: $subdomain" |
| 39 | + _debug "TXT value: $txtvalue" |
| 40 | + |
| 41 | + payload='{"resource_records": [ { "content": ["'"$txtvalue"'"] } ], "ttl": 60 }' |
| 42 | + _dns_edgecenter_http_api_call "post" "dns/v2/zones/$_zone/$subdomain.$_zone/txt" "$payload" |
| 43 | + |
| 44 | + if _contains "$response" '"error":"rrset is already exists"'; then |
| 45 | + _debug "RRSet exists, merging values" |
| 46 | + _dns_edgecenter_http_api_call "get" "dns/v2/zones/$_zone/$subdomain.$_zone/txt" |
| 47 | + current="$response" |
| 48 | + newlist="" |
| 49 | + for v in $(echo "$current" | sed -n 's/.*"content":\["\([^"]*\)"\].*/\1/p'); do |
| 50 | + newlist="$newlist {\"content\":[\"$v\"]}," |
| 51 | + done |
| 52 | + newlist="$newlist{\"content\":[\"$txtvalue\"]}" |
| 53 | + putdata="{\"resource_records\":[${newlist}]} |
| 54 | +" |
| 55 | + _dns_edgecenter_http_api_call "put" "dns/v2/zones/$_zone/$subdomain.$_zone/txt" "$putdata" |
| 56 | + _info "Updated existing RRSet with new TXT value." |
| 57 | + return 0 |
| 58 | + fi |
| 59 | + |
| 60 | + if _contains "$response" '"exception":'; then |
| 61 | + _err "Record cannot be added." |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + |
| 65 | + _info "TXT record added successfully." |
| 66 | + return 0 |
| 67 | +} |
| 68 | + |
| 69 | +#Usage: dns_edgecenter_rm _acme-challenge.www.domain.com "TXT_RECORD_VALUE" |
| 70 | +dns_edgecenter_rm() { |
| 71 | + fulldomain="$1" |
| 72 | + txtvalue="$2" |
| 73 | + |
| 74 | + _info "Removing TXT record for $fulldomain" |
| 75 | + |
| 76 | + if ! _dns_edgecenter_init_check; then |
| 77 | + return 1 |
| 78 | + fi |
| 79 | + |
| 80 | + if ! _get_root "$fulldomain"; then |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + |
| 84 | + subdomain="${fulldomain%."$_zone"}" |
| 85 | + subdomain=${subdomain%.} |
| 86 | + |
| 87 | + _dns_edgecenter_http_api_call "delete" "dns/v2/zones/$_zone/$subdomain.$_zone/txt" |
| 88 | + |
| 89 | + if [ -z "$response" ]; then |
| 90 | + _info "TXT record deleted successfully." |
| 91 | + else |
| 92 | + _info "TXT record may not have been deleted: $response" |
| 93 | + fi |
| 94 | + return 0 |
| 95 | +} |
| 96 | + |
| 97 | +#################### Private functions below ################################## |
| 98 | + |
| 99 | +_dns_edgecenter_init_check() { |
| 100 | + EDGECENTER_API_KEY="${EDGECENTER_API_KEY:-$(_readaccountconf_mutable EDGECENTER_API_KEY)}" |
| 101 | + if [ -z "$EDGECENTER_API_KEY" ]; then |
| 102 | + _err "EDGECENTER_API_KEY was not exported." |
| 103 | + return 1 |
| 104 | + fi |
| 105 | + |
| 106 | + _saveaccountconf_mutable EDGECENTER_API_KEY "$EDGECENTER_API_KEY" |
| 107 | + export _H1="Authorization: APIKey $EDGECENTER_API_KEY" |
| 108 | + |
| 109 | + _dns_edgecenter_http_api_call "get" "dns/v2/clients/me/features" |
| 110 | + if ! _contains "$response" '"id":'; then |
| 111 | + _err "Invalid API key." |
| 112 | + return 1 |
| 113 | + fi |
| 114 | + return 0 |
| 115 | +} |
| 116 | + |
| 117 | +_get_root() { |
| 118 | + domain="$1" |
| 119 | + i=1 |
| 120 | + while true; do |
| 121 | + h=$(printf "%s" "$domain" | cut -d . -f "$i"-) |
| 122 | + if [ -z "$h" ]; then |
| 123 | + return 1 |
| 124 | + fi |
| 125 | + _dns_edgecenter_http_api_call "get" "dns/v2/zones/$h" |
| 126 | + if ! _contains "$response" 'zone is not found'; then |
| 127 | + _zone="$h" |
| 128 | + return 0 |
| 129 | + fi |
| 130 | + i=$((i + 1)) |
| 131 | + done |
| 132 | + return 1 |
| 133 | +} |
| 134 | + |
| 135 | +_dns_edgecenter_http_api_call() { |
| 136 | + mtd="$1" |
| 137 | + endpoint="$2" |
| 138 | + data="$3" |
| 139 | + |
| 140 | + export _H1="Authorization: APIKey $EDGECENTER_API_KEY" |
| 141 | + |
| 142 | + case "$mtd" in |
| 143 | + get) |
| 144 | + response="$(_get "$EDGECENTER_API/$endpoint")" |
| 145 | + ;; |
| 146 | + post) |
| 147 | + response="$(_post "$data" "$EDGECENTER_API/$endpoint")" |
| 148 | + ;; |
| 149 | + delete) |
| 150 | + response="$(_post "" "$EDGECENTER_API/$endpoint" "" "DELETE")" |
| 151 | + ;; |
| 152 | + put) |
| 153 | + response="$(_post "$data" "$EDGECENTER_API/$endpoint" "" "PUT")" |
| 154 | + ;; |
| 155 | + *) |
| 156 | + _err "Unknown HTTP method $mtd" |
| 157 | + return 1 |
| 158 | + ;; |
| 159 | + esac |
| 160 | + |
| 161 | + _debug "HTTP $mtd response: $response" |
| 162 | + return 0 |
| 163 | +} |
0 commit comments