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.

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