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
4.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #!/usr/bin/env sh
  2. #NETLIFY_ACCESS_TOKEN="xxxx"
  3. NETLIFY_HOST="api.netlify.com/api/v1/"
  4. NETLIFY_URL="https://$NETLIFY_HOST"
  5. ######## Public functions #####################
  6. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  7. dns_netlify_add() {
  8. fulldomain=$1
  9. txtvalue=$2
  10. NETLIFY_ACCESS_TOKEN="${NETLIFY_ACCESS_TOKEN:-$(_readaccountconf_mutable NETLIFY_ACCESS_TOKEN)}"
  11. if [ -z "$NETLIFY_ACCESS_TOKEN" ]; then
  12. NETLIFY_ACCESS_TOKEN=""
  13. _err "Please specify your Netlify Access Token and try again."
  14. return 1
  15. fi
  16. _info "Using Netlify"
  17. _debug fulldomain "$fulldomain"
  18. _debug txtvalue "$txtvalue"
  19. _saveaccountconf_mutable NETLIFY_ACCESS_TOKEN "$NETLIFY_ACCESS_TOKEN"
  20. if ! _get_root "$fulldomain" "$accesstoken"; then
  21. _err "invalid domain"
  22. return 1
  23. fi
  24. _debug _domain_id "$_domain_id"
  25. _debug _sub_domain "$_sub_domain"
  26. _debug _domain "$_domain"
  27. dnsRecordURI="dns_zones/$_domain_id/dns_records"
  28. body="{\"type\":\"TXT\", \"hostname\":\"$_sub_domain\", \"value\":\"$txtvalue\", \"ttl\":\"10\"}"
  29. _netlify_rest POST "$dnsRecordURI" "$body" "$NETLIFY_ACCESS_TOKEN"
  30. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  31. if [ "$_code" = "200" ] || [ "$_code" = '201' ]; then
  32. _info "validation value added"
  33. return 0
  34. else
  35. _err "error adding validation value ($_code)"
  36. return 1
  37. fi
  38. _err "Not fully implemented!"
  39. return 1
  40. }
  41. #Usage: dns_myapi_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  42. #Remove the txt record after validation.
  43. dns_netlify_rm() {
  44. _info "Using Netlify"
  45. txtdomain="$1"
  46. txt="$2"
  47. _debug txtdomain "$txtdomain"
  48. _debug txt "$txt"
  49. _saveaccountconf_mutable NETLIFY_ACCESS_TOKEN "$NETLIFY_ACCESS_TOKEN"
  50. if ! _get_root "$txtdomain" "$accesstoken"; then
  51. _err "invalid domain"
  52. return 1
  53. fi
  54. _debug _domain_id "$_domain_id"
  55. _debug _sub_domain "$_sub_domain"
  56. _debug _domain "$_domain"
  57. dnsRecordURI="dns_zones/$_domain_id/dns_records"
  58. _netlify_rest GET "$dnsRecordURI" "" "$NETLIFY_ACCESS_TOKEN"
  59. _record_id=$(echo "$response" | _egrep_o "\"type\":\"TXT\",[^\}]*\"value\":\"$txt\"" | head -n 1 | _egrep_o "\"id\":\"[^\"\}]*\"" | cut -d : -f 2 | tr -d \")
  60. _debug _record_id "$_record_id"
  61. if [ "$_record_id" ]; then
  62. _netlify_rest DELETE "$dnsRecordURI/$_record_id" "" "$NETLIFY_ACCESS_TOKEN"
  63. _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  64. if [ "$_code" = "200" ] || [ "$_code" = '204' ]; then
  65. _info "validation value removed"
  66. return 0
  67. else
  68. _err "error removing validation value ($_code)"
  69. return 1
  70. fi
  71. return 0
  72. fi
  73. return 1
  74. }
  75. #################### Private functions below ##################################
  76. _get_root() {
  77. domain=$1
  78. accesstoken=$2
  79. i=1
  80. p=1
  81. _netlify_rest GET "dns_zones" "" "$accesstoken"
  82. while true; do
  83. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  84. _debug2 "Checking domain: $h"
  85. if [ -z "$h" ]; then
  86. #not valid
  87. _err "Invalid domain"
  88. return 1
  89. fi
  90. if _contains "$response" "\"name\":\"$h\"" >/dev/null; then
  91. _domain_id=$(echo "$response" | _egrep_o "\"[^\"]*\",\"name\":\"$h" | cut -d , -f 1 | tr -d \")
  92. if [ "$_domain_id" ]; then
  93. if [ "$i" = 1 ]; then
  94. #create the record at the domain apex (@) if only the domain name was provided as --domain-alias
  95. _sub_domain="@"
  96. else
  97. _sub_domain=$(echo "$domain" | cut -d . -f 1-$p)
  98. fi
  99. _domain=$h
  100. return 0
  101. fi
  102. return 1
  103. fi
  104. p=$i
  105. i=$(_math "$i" + 1)
  106. done
  107. return 1
  108. }
  109. _netlify_rest() {
  110. m=$1
  111. ep="$2"
  112. data="$3"
  113. _debug "$ep"
  114. token_trimmed=$(echo "$NETLIFY_ACCESS_TOKEN" | tr -d '"')
  115. export _H1="Content-Type: application/json"
  116. export _H2="Authorization: Bearer $token_trimmed"
  117. : >"$HTTP_HEADER"
  118. if [ "$m" != "GET" ]; then
  119. _debug data "$data"
  120. response="$(_post "$data" "$NETLIFY_URL$ep" "" "$m")"
  121. else
  122. response="$(_get "$NETLIFY_URL$ep")"
  123. fi
  124. if [ "$?" != "0" ]; then
  125. _err "error $ep"
  126. return 1
  127. fi
  128. _debug2 response "$response"
  129. return 0
  130. }