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.

178 lines
4.2 KiB

1 month ago
1 month ago
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. }
  37. dns_scaleway_rm() {
  38. fulldomain=$1
  39. txtvalue=$2
  40. if ! _scaleway_check_config; then
  41. return 1
  42. fi
  43. _debug "First detect the root zone"
  44. if ! _get_root "$fulldomain"; then
  45. _err "invalid domain"
  46. return 1
  47. fi
  48. _debug _sub_domain "$_sub_domain"
  49. _debug _domain "$_domain"
  50. _info "Deleting record"
  51. _scaleway_delete_TXT_record "$_domain" "$_sub_domain" "$txtvalue"
  52. if _contains "$response" "records"; then
  53. return 0
  54. else
  55. _err error "$response"
  56. return 1
  57. fi
  58. }
  59. #################### Private functions below ##################################
  60. _scaleway_check_config() {
  61. SCALEWAY_API_TOKEN="${SCALEWAY_API_TOKEN:-$(_readaccountconf_mutable SCALEWAY_API_TOKEN)}"
  62. if [ -z "$SCALEWAY_API_TOKEN" ]; then
  63. _err "No API key specified for Scaleway API."
  64. _err "Create your key and export it as SCALEWAY_API_TOKEN"
  65. return 1
  66. fi
  67. if ! _scaleway_rest GET "dns-zones"; then
  68. _err "Invalid API key specified for Scaleway API."
  69. return 1
  70. fi
  71. _saveaccountconf_mutable SCALEWAY_API_TOKEN "$SCALEWAY_API_TOKEN"
  72. return 0
  73. }
  74. #_acme-challenge.www.domain.com
  75. #returns
  76. # _sub_domain=_acme-challenge.www
  77. # _domain=domain.com
  78. _get_root() {
  79. domain=$1
  80. i=1
  81. p=1
  82. while true; do
  83. h=$(printf "%s" "$domain" | cut -d . -f "$i"-100)
  84. if [ -z "$h" ]; then
  85. #not valid
  86. return 1
  87. fi
  88. _scaleway_rest GET "dns-zones/$h/records"
  89. if ! _contains "$response" "subdomain not found" >/dev/null; then
  90. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-"$p")
  91. _domain="$h"
  92. return 0
  93. fi
  94. p=$i
  95. i=$(_math "$i" + 1)
  96. done
  97. _err "Unable to retrive DNS zone matching this domain"
  98. return 1
  99. }
  100. # this function add a TXT record
  101. _scaleway_create_TXT_record() {
  102. txt_zone=$1
  103. txt_name=$2
  104. txt_value=$3
  105. _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}]}}]}"
  106. if _contains "$response" "records"; then
  107. return 0
  108. else
  109. _err "error1 $response"
  110. return 1
  111. fi
  112. }
  113. # this function delete a TXT record based on name and content
  114. _scaleway_delete_TXT_record() {
  115. txt_zone=$1
  116. txt_name=$2
  117. txt_value=$3
  118. _scaleway_rest PATCH "dns-zones/$txt_zone/records" "{\"return_all_records\":false,\"changes\":[{\"delete\":{\"id_fields\":{\"name\":\"$txt_name\",\"data\":\"$txt_value\",\"type\":\"TXT\"}}}]}"
  119. if _contains "$response" "records"; then
  120. return 0
  121. else
  122. _err "error2 $response"
  123. return 1
  124. fi
  125. }
  126. _scaleway_rest() {
  127. m=$1
  128. ep="$2"
  129. data="$3"
  130. _debug "$ep"
  131. _scaleway_url="$SCALEWAY_API/$ep"
  132. _debug2 _scaleway_url "$_scaleway_url"
  133. export _H1="x-auth-token: $SCALEWAY_API_TOKEN"
  134. export _H2="Accept: application/json"
  135. export _H3="Content-Type: application/json"
  136. if [ "$data" ] || [ "$m" != "GET" ]; then
  137. _debug data "$data"
  138. response="$(_post "$data" "$_scaleway_url" "" "$m")"
  139. else
  140. response="$(_get "$_scaleway_url")"
  141. fi
  142. if [ "$?" != "0" ] || _contains "$response" "denied_authentication" || _contains "$response" "Method not allowed" || _contains "$response" "json parse error: unexpected EOF"; then
  143. _err "error $response"
  144. return 1
  145. fi
  146. _debug2 response "$response"
  147. return 0
  148. }