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.

170 lines
4.2 KiB

6 years ago
6 years ago
  1. #!/usr/bin/env sh
  2. EXOSCALE_API=https://api.exoscale.com/dns/v1
  3. ######## Public functions #####################
  4. # Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  5. # Used to add txt record
  6. dns_exoscale_add() {
  7. fulldomain=$1
  8. txtvalue=$2
  9. if ! _checkAuth; then
  10. return 1
  11. fi
  12. _debug "First detect the root zone"
  13. if ! _get_root "$fulldomain"; then
  14. _err "invalid domain"
  15. return 1
  16. fi
  17. _debug _sub_domain "$_sub_domain"
  18. _debug _domain "$_domain"
  19. _info "Adding record"
  20. if _exoscale_rest POST "domains/$_domain_id/records" "{\"record\":{\"name\":\"$_sub_domain\",\"record_type\":\"TXT\",\"content\":\"$txtvalue\",\"ttl\":120}}" "$_domain_token"; then
  21. if _contains "$response" "$txtvalue"; then
  22. _info "Added, OK"
  23. return 0
  24. fi
  25. fi
  26. _err "Add txt record error."
  27. return 1
  28. }
  29. # Usage: fulldomain txtvalue
  30. # Used to remove the txt record after validation
  31. dns_exoscale_rm() {
  32. fulldomain=$1
  33. txtvalue=$2
  34. if ! _checkAuth; then
  35. return 1
  36. fi
  37. _debug "First detect the root zone"
  38. if ! _get_root "$fulldomain"; then
  39. _err "invalid domain"
  40. return 1
  41. fi
  42. _debug _sub_domain "$_sub_domain"
  43. _debug _domain "$_domain"
  44. _debug "Getting txt records"
  45. _exoscale_rest GET "domains/${_domain_id}/records?type=TXT&name=$_sub_domain" "" "$_domain_token"
  46. if _contains "$response" "\"name\":\"$_sub_domain\"" >/dev/null; then
  47. _record_id=$(echo "$response" | tr '{' "\n" | grep "\"content\":\"$txtvalue\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
  48. fi
  49. if [ -z "$_record_id" ]; then
  50. _err "Can not get record id to remove."
  51. return 1
  52. fi
  53. _debug "Deleting record $_record_id"
  54. if ! _exoscale_rest DELETE "domains/$_domain_id/records/$_record_id" "" "$_domain_token"; then
  55. _err "Delete record error."
  56. return 1
  57. fi
  58. return 0
  59. }
  60. #################### Private functions below ##################################
  61. _checkAuth() {
  62. EXOSCALE_API_KEY="${EXOSCALE_API_KEY:-$(_readaccountconf_mutable EXOSCALE_API_KEY)}"
  63. EXOSCALE_SECRET_KEY="${EXOSCALE_SECRET_KEY:-$(_readaccountconf_mutable EXOSCALE_SECRET_KEY)}"
  64. if [ -z "$EXOSCALE_API_KEY" ] || [ -z "$EXOSCALE_SECRET_KEY" ]; then
  65. EXOSCALE_API_KEY=""
  66. EXOSCALE_SECRET_KEY=""
  67. _err "You don't specify Exoscale application key and application secret yet."
  68. _err "Please create you key and try again."
  69. return 1
  70. fi
  71. _saveaccountconf_mutable EXOSCALE_API_KEY "$EXOSCALE_API_KEY"
  72. _saveaccountconf_mutable EXOSCALE_SECRET_KEY "$EXOSCALE_SECRET_KEY"
  73. return 0
  74. }
  75. #_acme-challenge.www.domain.com
  76. #returns
  77. # _sub_domain=_acme-challenge.www
  78. # _domain=domain.com
  79. # _domain_id=sdjkglgdfewsdfg
  80. # _domain_token=sdjkglgdfewsdfg
  81. _get_root() {
  82. if ! _exoscale_rest GET "domains"; then
  83. return 1
  84. fi
  85. domain=$1
  86. i=2
  87. p=1
  88. while true; do
  89. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  90. _debug h "$h"
  91. if [ -z "$h" ]; then
  92. #not valid
  93. return 1
  94. fi
  95. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  96. _domain_id=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"id\":[^,]+" | _head_n 1 | cut -d : -f 2 | tr -d \")
  97. _domain_token=$(echo "$response" | tr '{' "\n" | grep "\"name\":\"$h\"" | _egrep_o "\"token\":\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d \")
  98. if [ "$_domain_token" ] && [ "$_domain_id" ]; then
  99. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  100. _domain=$h
  101. return 0
  102. fi
  103. return 1
  104. fi
  105. p=$i
  106. i=$(_math "$i" + 1)
  107. done
  108. return 1
  109. }
  110. # returns response
  111. _exoscale_rest() {
  112. method=$1
  113. path="$2"
  114. data="$3"
  115. token="$4"
  116. request_url="$EXOSCALE_API/$path"
  117. _debug "$path"
  118. export _H1="Accept: application/json"
  119. if [ "$token" ]; then
  120. export _H2="X-DNS-Domain-Token: $token"
  121. else
  122. export _H2="X-DNS-Token: $EXOSCALE_API_KEY:$EXOSCALE_SECRET_KEY"
  123. fi
  124. if [ "$data" ] || [ "$method" = "DELETE" ]; then
  125. export _H3="Content-Type: application/json"
  126. _debug data "$data"
  127. response="$(_post "$data" "$request_url" "" "$method")"
  128. else
  129. response="$(_get "$request_url" "" "" "$method")"
  130. fi
  131. if [ "$?" != "0" ]; then
  132. _err "error $request_url"
  133. return 1
  134. fi
  135. _debug2 response "$response"
  136. return 0
  137. }