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.

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