You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

331 lines
9.2 KiB

  1. #!/usr/bin/env sh
  2. # HUAWEICLOUD_Username
  3. # HUAWEICLOUD_Password
  4. # HUAWEICLOUD_DomainName
  5. iam_api="https://iam.myhuaweicloud.com"
  6. dns_api="https://dns.ap-southeast-1.myhuaweicloud.com" # Should work
  7. ######## Public functions #####################
  8. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  9. # Used to add txt record
  10. #
  11. # Ref: https://support.huaweicloud.com/intl/zh-cn/api-dns/zh-cn_topic_0132421999.html
  12. #
  13. # About "DomainName" parameters see: https://support.huaweicloud.com/api-iam/iam_01_0006.html
  14. #
  15. dns_huaweicloud_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. HUAWEICLOUD_Username="${HUAWEICLOUD_Username:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
  19. HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
  20. HUAWEICLOUD_DomainName="${HUAWEICLOUD_DomainName:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
  21. # Check information
  22. if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Password}" ] || [ -z "${HUAWEICLOUD_DomainName}" ]; then
  23. _err "Not enough information provided to dns_huaweicloud!"
  24. return 1
  25. fi
  26. unset token # Clear token
  27. token="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_DomainName}")"
  28. if [ -z "${token}" ]; then # Check token
  29. _err "dns_api(dns_huaweicloud): Error getting token."
  30. return 1
  31. fi
  32. _secure_debug "Access token is:" "${token}"
  33. unset zoneid
  34. zoneid="$(_get_zoneid "${token}" "${fulldomain}")"
  35. if [ -z "${zoneid}" ]; then
  36. _err "dns_api(dns_huaweicloud): Error getting zone id."
  37. return 1
  38. fi
  39. _debug "Zone ID is:" "${zoneid}"
  40. _debug "Adding Record"
  41. _add_record "${token}" "${fulldomain}" "${txtvalue}"
  42. ret="$?"
  43. if [ "${ret}" != "0" ]; then
  44. _err "dns_api(dns_huaweicloud): Error adding record."
  45. return 1
  46. fi
  47. # Do saving work if all succeeded
  48. _saveaccountconf_mutable HUAWEICLOUD_Username "${HUAWEICLOUD_Username}"
  49. _saveaccountconf_mutable HUAWEICLOUD_Password "${HUAWEICLOUD_Password}"
  50. _saveaccountconf_mutable HUAWEICLOUD_DomainName "${HUAWEICLOUD_DomainName}"
  51. return 0
  52. }
  53. # Usage: fulldomain txtvalue
  54. # Used to remove the txt record after validation
  55. #
  56. # Ref: https://support.huaweicloud.com/intl/zh-cn/api-dns/dns_api_64005.html
  57. #
  58. dns_huaweicloud_rm() {
  59. fulldomain=$1
  60. txtvalue=$2
  61. HUAWEICLOUD_Username="${HUAWEICLOUD_Username:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
  62. HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
  63. HUAWEICLOUD_DomainName="${HUAWEICLOUD_DomainName:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
  64. # Check information
  65. if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Password}" ] || [ -z "${HUAWEICLOUD_DomainName}" ]; then
  66. _err "Not enough information provided to dns_huaweicloud!"
  67. return 1
  68. fi
  69. unset token # Clear token
  70. token="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_DomainName}")"
  71. if [ -z "${token}" ]; then # Check token
  72. _err "dns_api(dns_huaweicloud): Error getting token."
  73. return 1
  74. fi
  75. _secure_debug "Access token is:" "${token}"
  76. unset zoneid
  77. zoneid="$(_get_zoneid "${token}" "${fulldomain}")"
  78. if [ -z "${zoneid}" ]; then
  79. _err "dns_api(dns_huaweicloud): Error getting zone id."
  80. return 1
  81. fi
  82. _debug "Zone ID is:" "${zoneid}"
  83. _recursive_rm_record "${token}" "${fulldomain}" "${zoneid}" "${record_id}"
  84. ret="$?"
  85. if [ "${ret}" != "0" ]; then
  86. _err "dns_api(dns_huaweicloud): Error removing record."
  87. return 1
  88. fi
  89. return 0
  90. }
  91. ################### Private functions below ##################################
  92. # _recursive_rm_record
  93. # remove all records from the record set
  94. #
  95. # _token=$1
  96. # _domain=$2
  97. # _zoneid=$3
  98. # _record_id=$4
  99. #
  100. # Returns 0 on success
  101. _recursive_rm_record() {
  102. _token=$1
  103. _domain=$2
  104. _zoneid=$3
  105. _record_id=$4
  106. # Most likely to have problems will huaweicloud side if more than 50 attempts but still cannot fully remove the record set
  107. # Maybe can be removed manually in the dashboard
  108. _retry_cnt=50
  109. # Remove all records
  110. # Therotically HuaweiCloud does not allow more than one record set
  111. # But remove them recurringly to increase robusty
  112. while [ "${_record_id}" != "0" && "${_retry_cnt}" != "0" ]; do
  113. _debug "Removing Record"
  114. _retry_cnt=$((${_retry_cnt} - 1))
  115. _rm_record "${_token}" "${_zoneid}" "${_record_id}"
  116. _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${_zoneid}")"
  117. _debug2 "Checking record exists: record_id=${_record_id}"
  118. done
  119. # Check if retry count is reached
  120. if [ "${_retry_cnt}" == "0" ]; then
  121. _debug "Failed to remove record after 50 attempts, please try removing it manually in the dashboard"
  122. return 1
  123. fi
  124. return 0
  125. }
  126. # _get_zoneid
  127. #
  128. # _token=$1
  129. # _domain_string=$2
  130. #
  131. # printf "%s" "${_zoneid}"
  132. _get_zoneid() {
  133. _token=$1
  134. _domain_string=$2
  135. export _H1="X-Auth-Token: ${_token}"
  136. i=1
  137. while true; do
  138. h=$(printf "%s" "${_domain_string}" | cut -d . -f $i-100)
  139. if [ -z "$h" ]; then
  140. #not valid
  141. return 1
  142. fi
  143. _debug "$h"
  144. response=$(_get "${dns_api}/v2/zones?name=${h}")
  145. _debug2 "$response"
  146. if _contains "${response}" '"id"'; then
  147. zoneidlist=$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
  148. zonenamelist=$(echo "${response}" | _egrep_o "\"name\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
  149. _debug2 "Return Zone ID(s):" "${zoneidlist}"
  150. _debug2 "Return Zone Name(s):" "${zonenamelist}"
  151. zoneidnum=0
  152. zoneidcount=$(echo "${zoneidlist}" | grep -c '^')
  153. _debug "Retund Zone ID(s) Count:" "${zoneidcount}"
  154. while [ "${zoneidnum}" -lt "${zoneidcount}" ]; do
  155. zoneidnum=$(_math "$zoneidnum" + 1)
  156. _zoneid=$(echo "${zoneidlist}" | sed -n "${zoneidnum}p")
  157. zonename=$(echo "${zonenamelist}" | sed -n "${zoneidnum}p")
  158. _debug "Check Zone Name" "${zonename}"
  159. if [ "${zonename}" = "${h}." ]; then
  160. _debug "Get Zone ID Success."
  161. _debug "ZoneID:" "${_zoneid}"
  162. printf "%s" "${_zoneid}"
  163. return 0
  164. fi
  165. done
  166. fi
  167. i=$(_math "$i" + 1)
  168. done
  169. return 1
  170. }
  171. _get_recordset_id() {
  172. _token=$1
  173. _domain=$2
  174. _zoneid=$3
  175. export _H1="X-Auth-Token: ${_token}"
  176. response=$(_get "${dns_api}/v2/zones/${_zoneid}/recordsets?name=${_domain}")
  177. if _contains "${response}" '"id"'; then
  178. _id="$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")"
  179. printf "%s" "${_id}"
  180. return 0
  181. fi
  182. printf "%s" "0"
  183. return 1
  184. }
  185. _add_record() {
  186. _token=$1
  187. _domain=$2
  188. _txtvalue=$3
  189. # Get Existing Records
  190. export _H1="X-Auth-Token: ${_token}"
  191. response=$(_get "${dns_api}/v2/zones/${zoneid}/recordsets?name=${_domain}")
  192. _debug2 "${response}"
  193. _exist_record=$(echo "${response}" | _egrep_o '"records":[^]]*' | sed 's/\"records\"\:\[//g')
  194. _debug "${_exist_record}"
  195. # Check if record exist
  196. # Generate body data
  197. if [ -z "${_exist_record}" ]; then
  198. _post_body="{
  199. \"name\": \"${_domain}.\",
  200. \"description\": \"ACME Challenge\",
  201. \"type\": \"TXT\",
  202. \"ttl\": 1,
  203. \"records\": [
  204. \"\\\"${_txtvalue}\\\"\"
  205. ]
  206. }"
  207. else
  208. _post_body="{
  209. \"name\": \"${_domain}.\",
  210. \"description\": \"ACME Challenge\",
  211. \"type\": \"TXT\",
  212. \"ttl\": 1,
  213. \"records\": [
  214. ${_exist_record},
  215. \"\\\"${_txtvalue}\\\"\"
  216. ]
  217. }"
  218. fi
  219. _record_id="$(_get_recordset_id "${_token}" "${_domain}" "${zoneid}")"
  220. _debug "Record Set ID is:" "${_record_id}"
  221. # Remove all records
  222. _recursive_rm_record "${token}" "${_domain}" "${_zoneid}" "${_record_id}"
  223. ret="$?"
  224. if [ "${ret}" != "0" ]; then
  225. return 1
  226. fi
  227. # Add brand new records with all old and new records
  228. export _H2="Content-Type: application/json"
  229. export _H1="X-Auth-Token: ${_token}"
  230. _debug2 "${_post_body}"
  231. _post "${_post_body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
  232. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  233. if [ "$_code" != "202" ]; then
  234. _err "dns_huaweicloud: http code ${_code}"
  235. return 1
  236. fi
  237. return 0
  238. }
  239. # _rm_record $token $zoneid $recordid
  240. # assume ${dns_api} exist
  241. # no output
  242. # return 0
  243. _rm_record() {
  244. _token=$1
  245. _zone_id=$2
  246. _record_id=$3
  247. export _H2="Content-Type: application/json"
  248. export _H1="X-Auth-Token: ${_token}"
  249. _post "" "${dns_api}/v2/zones/${_zone_id}/recordsets/${_record_id}" false "DELETE" >/dev/null
  250. return $?
  251. }
  252. _get_token() {
  253. _username=$1
  254. _password=$2
  255. _domain_name=$3
  256. _debug "Getting Token"
  257. body="{
  258. \"auth\": {
  259. \"identity\": {
  260. \"methods\": [
  261. \"password\"
  262. ],
  263. \"password\": {
  264. \"user\": {
  265. \"name\": \"${_username}\",
  266. \"password\": \"${_password}\",
  267. \"domain\": {
  268. \"name\": \"${_domain_name}\"
  269. }
  270. }
  271. }
  272. },
  273. \"scope\": {
  274. \"project\": {
  275. \"name\": \"ap-southeast-1\"
  276. }
  277. }
  278. }
  279. }"
  280. export _H1="Content-Type: application/json;charset=utf8"
  281. _post "${body}" "${iam_api}/v3/auth/tokens" >/dev/null
  282. _code=$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")
  283. _token=$(grep "^X-Subject-Token" "$HTTP_HEADER" | cut -d " " -f 2-)
  284. _secure_debug "${_code}"
  285. printf "%s" "${_token}"
  286. return 0
  287. }