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.

286 lines
8.1 KiB

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