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.

199 lines
5.3 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. token="$(_get_token "${HUAWEICLOUD_Username}" "${HUAWEICLOUD_Password}" "${HUAWEICLOUD_ProjectID}")"
  48. _debug2 "${token}"
  49. zoneid="$(_get_zoneid "${token}" "${fulldomain}")"
  50. _debug "${zoneid}"
  51. record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
  52. _debug "Record Set ID is: ${record_id}"
  53. while [ "${record_id}" != "0" ]; do
  54. _debug "Adding Record"
  55. _rm_record "${token}" "${zoneid}" "${record_id}"
  56. record_id="$(_get_recordset_id "${token}" "${fulldomain}" "${zoneid}")"
  57. done
  58. return 0
  59. }
  60. ################### Private functions below ##################################
  61. # _get_zoneid
  62. #
  63. # _token=$1
  64. # _domain_string=$2
  65. #
  66. # printf "%s" "${_zoneid}"
  67. _get_zoneid() {
  68. _token=$1
  69. _domain_string=$2
  70. export _H1="X-Auth-Token: ${_token}"
  71. i=1
  72. while true; do
  73. h=$(printf "%s" "${_domain_string}" | cut -d . -f $i-100)
  74. if [ -z "$h" ]; then
  75. #not valid
  76. return 1
  77. fi
  78. _debug "$h"
  79. response=$(_get "${dns_api}/v2/zones?name=${h}")
  80. if _contains "${response}" "id"; then
  81. _debug "Get Zone ID Success."
  82. _zoneid=$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")
  83. printf "%s" "${_zoneid}"
  84. return 0
  85. fi
  86. i=$(_math "$i" + 1)
  87. done
  88. return 1
  89. }
  90. _get_recordset_id() {
  91. _token=$1
  92. _domain=$2
  93. _zoneid=$3
  94. export _H1="X-Auth-Token: ${_token}"
  95. response=$(_get "${dns_api}/v2/zones/${_zoneid}/recordsets?name=${_domain}")
  96. if _contains "${response}" "id"; then
  97. _id="$(echo "${response}" | _egrep_o "\"id\": *\"[^\"]*\"" | cut -d : -f 2 | tr -d \" | tr -d " ")"
  98. printf "%s" "${_id}"
  99. return 0
  100. fi
  101. printf "%s" "0"
  102. return 1
  103. }
  104. _add_record() {
  105. _token=$1
  106. _domain=$2
  107. _txtvalue=$3
  108. body="{
  109. \"name\": \"${_domain}.\",
  110. \"description\": \"ACME Challenge\",
  111. \"type\": \"TXT\",
  112. \"ttl\": 1,
  113. \"records\": [
  114. \"\\\"${_txtvalue}\\\"\"
  115. ]
  116. }"
  117. _debug2 "${body}"
  118. export _H2="Content-Type: application/json"
  119. export _H1="X-Auth-Token: ${_token}"
  120. _post "${body}" "${dns_api}/v2/zones/${zoneid}/recordsets" >/dev/null
  121. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  122. if [ "$_code" != "202" ]; then
  123. _err "dns_huaweicloud: http code ${_code}"
  124. return 1
  125. fi
  126. return 0
  127. }
  128. _rm_record() {
  129. _token=$1
  130. _zone_id=$2
  131. _record_id=$3
  132. export _H2="Content-Type: application/json"
  133. export _H1="X-Auth-Token: ${_token}"
  134. _post "${body}" "${dns_api}/v2/zones/${_zone_id}/recordsets/${_record_id}" false "DELETE"
  135. return 0
  136. }
  137. _get_token() {
  138. _username=$1
  139. _password=$2
  140. _project=$3
  141. _debug "Getting Token"
  142. body="{
  143. \"auth\": {
  144. \"identity\": {
  145. \"methods\": [
  146. \"password\"
  147. ],
  148. \"password\": {
  149. \"user\": {
  150. \"name\": \"${_username}\",
  151. \"password\": \"${_password}\",
  152. \"domain\": {
  153. \"name\": \"${_username}\"
  154. }
  155. }
  156. }
  157. },
  158. \"scope\": {
  159. \"project\": {
  160. \"id\": \"${_project}\"
  161. }
  162. }
  163. }
  164. }"
  165. export _H1="Content-Type: application/json;charset=utf8"
  166. _post "${body}" "${iam_api}/v3/auth/tokens" >/dev/null
  167. _code=$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")
  168. _token=$(grep "^X-Subject-Token" "$HTTP_HEADER" | cut -d " " -f 2-)
  169. _debug2 "${_code}"
  170. printf "%s" "${_token}"
  171. return 0
  172. }