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.

162 lines
3.8 KiB

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