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.

206 lines
5.5 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"
  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. token="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_ProjectID}")"
  20. _debug2 "${token}"
  21. zoneid="$(_get_zoneid "${token}" "${fulldomain}")"
  22. _debug "${zoneid}"
  23. _debug "Adding Record"
  24. _add_record "${token}" "${fulldomain}" "${txtvalue}"
  25. ret="$?"
  26. if [ "${ret}" != "0" ]; then
  27. _err "dns_huaweicloud: Error adding record."
  28. return 1
  29. fi
  30. # Do saving work if all succeeded
  31. _saveaccountconf_mutable HUAWEICLOUD_Username "${HUAWEICLOUD_Username}"
  32. _saveaccountconf_mutable HUAWEICLOUD_Password "${HUAWEICLOUD_Password}"
  33. _saveaccountconf_mutable HUAWEICLOUD_ProjectID "${HUAWEICLOUD_ProjectID}"
  34. return 0
  35. }
  36. # Usage: fulldomain txtvalue
  37. # Used to remove the txt record after validation
  38. #
  39. # Ref: https://support.huaweicloud.com/intl/zh-cn/api-dns/dns_api_64005.html
  40. #
  41. dns_huaweicloud_rm() {
  42. fulldomain=$1
  43. txtvalue=$2
  44. HUAWEICLOUD_Username="${HUAWEICLOUD_Username:-$(_readaccountconf_mutable HUAWEICLOUD_Username)}"
  45. HUAWEICLOUD_Password="${HUAWEICLOUD_Password:-$(_readaccountconf_mutable HUAWEICLOUD_Password)}"
  46. HUAWEICLOUD_ProjectID="${HUAWEICLOUD_ProjectID:-$(_readaccountconf_mutable HUAWEICLOUD_ProjectID)}"
  47. if [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ] || [ -z "${HUAWEICLOUD_Username}" ]; then
  48. _err "Please provide enough information"
  49. return 1
  50. fi
  51. token="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_ProjectID}")"
  52. _debug2 "${token}"
  53. zoneid="$(_get_zoneid "${token}" "${fulldomain}")"
  54. _debug "${zoneid}"
  55. record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
  56. _debug "Record Set ID is: ${record_id}"
  57. # Remove all records
  58. while [ "${record_id}" != "0" ]; do
  59. _debug "Removing Record"
  60. _rm_record "${token}" "${zoneid}" "${record_id}"
  61. record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
  62. done
  63. return 0
  64. }
  65. ################### Private functions below ##################################
  66. # _get_zoneid
  67. #
  68. # _token=$1
  69. # _domain_string=$2
  70. #
  71. # printf "%s" "${_zoneid}"
  72. _get_zoneid() {
  73. _token=$1
  74. _domain_string=$2
  75. export _H1="X-Auth-Token: ${_token}"
  76. i=1
  77. while true; do
  78. h=$(printf "%s" "${_domain_string}" | cut -d . -f $i-100)
  79. if [ -z "$h" ]; then
  80. #not valid
  81. return 1
  82. fi
  83. _debug "$h"
  84. response=$(_get "${dns_api}/v2/zones?name=${h}")
  85. if _contains "${response}" "id"; then
  86. _debug "Get Zone ID Success."
  87. _zoneid=$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
  88. printf "%s" "${_zoneid}"
  89. return 0
  90. fi
  91. i=$(_math "$i" + 1)
  92. done
  93. return 1
  94. }
  95. _get_recordset_id() {
  96. _token=$1
  97. _domain=$2
  98. _zoneid=$3
  99. export _H1="X-Auth-Token: ${_token}"
  100. response=$(_get "${dns_api}/v2/zones/${_zoneid}/recordsets?name=${_domain}")
  101. if _contains "${response}" "id"; then
  102. _id="$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")"
  103. printf "%s" "${_id}"
  104. return 0
  105. fi
  106. printf "%s" "0"
  107. return 1
  108. }
  109. _add_record() {
  110. _token=$1
  111. _domain=$2
  112. _txtvalue=$3
  113. body="{
  114. \"name\": \"${_domain}.\",
  115. \"description\": \"ACME Challenge\",
  116. \"type\": \"TXT\",
  117. \"ttl\": 1,
  118. \"records\": [
  119. \"\\\"${_txtvalue}\\\"\"
  120. ]
  121. }"
  122. _debug2 "${body}"
  123. export _H2="Content-Type: application/json"
  124. export _H1="X-Auth-Token: ${_token}"
  125. _post "${body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
  126. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  127. if [ "$_code" != "202" ]; then
  128. _err "dns_huaweicloud: http code ${_code}"
  129. return 1
  130. fi
  131. return 0
  132. }
  133. _rm_record() {
  134. _token=$1
  135. _zone_id=$2
  136. _record_id=$3
  137. export _H2="Content-Type: application/json"
  138. export _H1="X-Auth-Token: ${_token}"
  139. _post "${body}" "${dns_api}/v2/zones/${_zone_id}/recordsets/${_record_id}" false "DELETE" >/dev/null
  140. return 0
  141. }
  142. _get_token() {
  143. _username=$1
  144. _password=$2
  145. _project=$3
  146. _debug "Getting Token"
  147. body="{
  148. \"auth\": {
  149. \"identity\": {
  150. \"methods\": [
  151. \"password\"
  152. ],
  153. \"password\": {
  154. \"user\": {
  155. \"name\": \"${_username}\",
  156. \"password\": \"${_password}\",
  157. \"domain\": {
  158. \"name\": \"${_username}\"
  159. }
  160. }
  161. }
  162. },
  163. \"scope\": {
  164. \"project\": {
  165. \"id\": \"${_project}\"
  166. }
  167. }
  168. }
  169. }"
  170. export _H1="Content-Type: application/json;charset=utf8"
  171. _post "${body}" "${iam_api}/v3/auth/tokens" >/dev/null
  172. _code=$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")
  173. _token=$(grep "^X-Subject-Token" "$HTTP_HEADER" | cut -d " " -f 2-)
  174. _debug2 "${_code}"
  175. printf "%s" "${_token}"
  176. return 0
  177. }