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.

176 lines
4.1 KiB

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