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.

182 lines
4.3 KiB

4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. # shellcheck disable=SC2034
  3. dns_scaleway_info='ScaleWay.com
  4. Site: ScaleWay.com
  5. Docs: github.com/acmesh-official/acme.sh/wiki/dnsapi2#dns_scaleway
  6. Options:
  7. SCALEWAY_API_TOKEN API Token
  8. Issues: github.com/acmesh-official/acme.sh/issues/3295
  9. '
  10. # Scaleway API
  11. # https://developers.scaleway.com/en/products/domain/dns/api/
  12. ######## Public functions #####################
  13. SCALEWAY_API="https://api.scaleway.com/domain/v2beta1"
  14. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  15. dns_scaleway_add() {
  16. fulldomain=$1
  17. txtvalue=$2
  18. if ! _scaleway_check_config; then
  19. return 1
  20. fi
  21. _debug "First detect the root zone"
  22. if ! _get_root "$fulldomain"; then
  23. _err "invalid domain"
  24. return 1
  25. fi
  26. _debug _sub_domain "$_sub_domain"
  27. _debug _domain "$_domain"
  28. _info "Adding record"
  29. _scaleway_create_TXT_record "$_domain" "$_sub_domain" "$txtvalue"
  30. if _contains "$response" "records"; then
  31. return 0
  32. else
  33. _err error "$response"
  34. return 1
  35. fi
  36. _info "Record added."
  37. return 0
  38. }
  39. dns_scaleway_rm() {
  40. fulldomain=$1
  41. txtvalue=$2
  42. if ! _scaleway_check_config; then
  43. return 1
  44. fi
  45. _debug "First detect the root zone"
  46. if ! _get_root "$fulldomain"; then
  47. _err "invalid domain"
  48. return 1
  49. fi
  50. _debug _sub_domain "$_sub_domain"
  51. _debug _domain "$_domain"
  52. _info "Deleting record"
  53. _scaleway_delete_TXT_record "$_domain" "$_sub_domain" "$txtvalue"
  54. if _contains "$response" "records"; then
  55. return 0
  56. else
  57. _err error "$response"
  58. return 1
  59. fi
  60. _info "Record deleted."
  61. return 0
  62. }
  63. #################### Private functions below ##################################
  64. _scaleway_check_config() {
  65. SCALEWAY_API_TOKEN="${SCALEWAY_API_TOKEN:-$(_readaccountconf_mutable SCALEWAY_API_TOKEN)}"
  66. if [ -z "$SCALEWAY_API_TOKEN" ]; then
  67. _err "No API key specified for Scaleway API."
  68. _err "Create your key and export it as SCALEWAY_API_TOKEN"
  69. return 1
  70. fi
  71. if ! _scaleway_rest GET "dns-zones"; then
  72. _err "Invalid API key specified for Scaleway API."
  73. return 1
  74. fi
  75. _saveaccountconf_mutable SCALEWAY_API_TOKEN "$SCALEWAY_API_TOKEN"
  76. return 0
  77. }
  78. #_acme-challenge.www.domain.com
  79. #returns
  80. # _sub_domain=_acme-challenge.www
  81. # _domain=domain.com
  82. _get_root() {
  83. domain=$1
  84. i=1
  85. p=1
  86. while true; do
  87. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  88. if [ -z "$h" ]; then
  89. #not valid
  90. return 1
  91. fi
  92. _scaleway_rest GET "dns-zones/$h/records"
  93. if ! _contains "$response" "subdomain not found" >/dev/null; then
  94. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  95. _domain="$h"
  96. return 0
  97. fi
  98. p=$i
  99. i=$(_math "$i" + 1)
  100. done
  101. _err "Unable to retrive DNS zone matching this domain"
  102. return 1
  103. }
  104. # this function add a TXT record
  105. _scaleway_create_TXT_record() {
  106. txt_zone=$1
  107. txt_name=$2
  108. txt_value=$3
  109. _scaleway_rest PATCH "dns-zones/$txt_zone/records" "{\"return_all_records\":false,\"changes\":[{\"add\":{\"records\":[{\"name\":\"$txt_name\",\"data\":\"$txt_value\",\"type\":\"TXT\",\"ttl\":60}]}}]}"
  110. if _contains "$response" "records"; then
  111. return 0
  112. else
  113. _err "error1 $response"
  114. return 1
  115. fi
  116. }
  117. # this function delete a TXT record based on name and content
  118. _scaleway_delete_TXT_record() {
  119. txt_zone=$1
  120. txt_name=$2
  121. txt_value=$3
  122. _scaleway_rest PATCH "dns-zones/$txt_zone/records" "{\"return_all_records\":false,\"changes\":[{\"delete\":{\"id_fields\":{\"name\":\"$txt_name\",\"data\":\"$txt_value\",\"type\":\"TXT\"}}}]}"
  123. if _contains "$response" "records"; then
  124. return 0
  125. else
  126. _err "error2 $response"
  127. return 1
  128. fi
  129. }
  130. _scaleway_rest() {
  131. m=$1
  132. ep="$2"
  133. data="$3"
  134. _debug "$ep"
  135. _scaleway_url="$SCALEWAY_API/$ep"
  136. _debug2 _scaleway_url "$_scaleway_url"
  137. export _H1="x-auth-token: $SCALEWAY_API_TOKEN"
  138. export _H2="Accept: application/json"
  139. export _H3="Content-Type: application/json"
  140. if [ "$data" ] || [ "$m" != "GET" ]; then
  141. _debug data "$data"
  142. response="$(_post "$data" "$_scaleway_url" "" "$m")"
  143. else
  144. response="$(_get "$_scaleway_url")"
  145. fi
  146. if [ "$?" != "0" ] || _contains "$response" "denied_authentication" || _contains "$response" "Method not allowed" || _contains "$response" "json parse error: unexpected EOF"; then
  147. _err "error $response"
  148. return 1
  149. fi
  150. _debug2 response "$response"
  151. return 0
  152. }