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.

322 lines
10 KiB

7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #!/usr/bin/env sh
  2. #
  3. #AWS_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
  4. #
  5. #AWS_SECRET_ACCESS_KEY="xxxxxxx"
  6. #This is the Amazon Route53 api wrapper for acme.sh
  7. AWS_HOST="route53.amazonaws.com"
  8. AWS_URL="https://$AWS_HOST"
  9. AWS_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-use-Amazon-Route53-API"
  10. ######## Public functions #####################
  11. #Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  12. dns_aws_add() {
  13. fulldomain=$1
  14. txtvalue=$2
  15. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  16. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  17. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  18. _use_instance_role
  19. fi
  20. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  21. AWS_ACCESS_KEY_ID=""
  22. AWS_SECRET_ACCESS_KEY=""
  23. _err "You don't specify aws route53 api key id and and api key secret yet."
  24. _err "Please create your key and try again. see $(__green $AWS_WIKI)"
  25. return 1
  26. fi
  27. #save for future use, unless using a role which will be fetched as needed
  28. if [ -z "$_using_role" ]; then
  29. _saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  30. _saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  31. fi
  32. _debug "First detect the root zone"
  33. if ! _get_root "$fulldomain"; then
  34. _err "invalid domain"
  35. return 1
  36. fi
  37. _debug _domain_id "$_domain_id"
  38. _debug _sub_domain "$_sub_domain"
  39. _debug _domain "$_domain"
  40. _info "Geting existing records for $fulldomain"
  41. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  42. return 1
  43. fi
  44. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  45. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  46. _debug "_resource_record" "$_resource_record"
  47. else
  48. _debug "single new add"
  49. fi
  50. if [ "$_resource_record" ] && _contains "$response" "$txtvalue"; then
  51. _info "The txt record already exists, skip"
  52. return 0
  53. fi
  54. _debug "Adding records"
  55. _aws_tmpl_xml="<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"><ChangeBatch><Changes><Change><Action>UPSERT</Action><ResourceRecordSet><Name>$fulldomain</Name><Type>TXT</Type><TTL>300</TTL><ResourceRecords>$_resource_record<ResourceRecord><Value>\"$txtvalue\"</Value></ResourceRecord></ResourceRecords></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  56. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  57. _info "txt record updated success."
  58. return 0
  59. fi
  60. return 1
  61. }
  62. #fulldomain txtvalue
  63. dns_aws_rm() {
  64. fulldomain=$1
  65. txtvalue=$2
  66. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  67. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  68. if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
  69. _use_instance_role
  70. fi
  71. _debug "First detect the root zone"
  72. if ! _get_root "$fulldomain"; then
  73. _err "invalid domain"
  74. return 1
  75. fi
  76. _debug _domain_id "$_domain_id"
  77. _debug _sub_domain "$_sub_domain"
  78. _debug _domain "$_domain"
  79. _info "Geting existing records for $fulldomain"
  80. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  81. return 1
  82. fi
  83. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  84. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  85. _debug "_resource_record" "$_resource_record"
  86. else
  87. _debug "no records exists, skip"
  88. return 0
  89. fi
  90. _aws_tmpl_xml="<ChangeResourceRecordSetsRequest xmlns=\"https://route53.amazonaws.com/doc/2013-04-01/\"><ChangeBatch><Changes><Change><Action>DELETE</Action><ResourceRecordSet><ResourceRecords>$_resource_record</ResourceRecords><Name>$fulldomain.</Name><Type>TXT</Type><TTL>300</TTL></ResourceRecordSet></Change></Changes></ChangeBatch></ChangeResourceRecordSetsRequest>"
  91. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  92. _info "txt record deleted success."
  93. return 0
  94. fi
  95. return 1
  96. }
  97. #################### Private functions below ##################################
  98. _get_root() {
  99. domain=$1
  100. i=2
  101. p=1
  102. if aws_rest GET "2013-04-01/hostedzone"; then
  103. while true; do
  104. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  105. _debug2 "Checking domain: $h"
  106. if [ -z "$h" ]; then
  107. if _contains "$response" "<IsTruncated>true</IsTruncated>" && _contains "$response" "<NextMarker>"; then
  108. _debug "IsTruncated"
  109. _nextMarker="$(echo "$response" | _egrep_o "<NextMarker>.*</NextMarker>" | cut -d '>' -f 2 | cut -d '<' -f 1)"
  110. _debug "NextMarker" "$_nextMarker"
  111. if aws_rest GET "2013-04-01/hostedzone" "marker=$_nextMarker"; then
  112. _debug "Truncated request OK"
  113. i=2
  114. p=1
  115. continue
  116. else
  117. _err "Truncated request error."
  118. fi
  119. fi
  120. #not valid
  121. _err "Invalid domain"
  122. return 1
  123. fi
  124. if _contains "$response" "<Name>$h.</Name>"; then
  125. hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$h.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
  126. _debug hostedzone "$hostedzone"
  127. if [ "$hostedzone" ]; then
  128. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  129. if [ "$_domain_id" ]; then
  130. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  131. _domain=$h
  132. return 0
  133. fi
  134. _err "Can not find domain id: $h"
  135. return 1
  136. fi
  137. fi
  138. p=$i
  139. i=$(_math "$i" + 1)
  140. done
  141. fi
  142. return 1
  143. }
  144. _use_instance_role() {
  145. _url="http://169.254.169.254/latest/meta-data/iam/security-credentials/"
  146. _debug "_url" "$_url"
  147. if ! _get "$_url" true 1 | _head_n 1 | grep -Fq 200; then
  148. _err "Unable to fetch IAM role from AWS instance metadata."
  149. return
  150. fi
  151. _aws_role=$(_get "$_url" "" 1)
  152. _debug "_aws_role" "$_aws_role"
  153. _aws_creds="$(
  154. _get "$_url$_aws_role" "" 1 \
  155. | _normalizeJson \
  156. | tr '{,}' '\n' \
  157. | while read -r _line; do
  158. _key="$(echo "${_line%%:*}" | tr -d '"')"
  159. _value="${_line#*:}"
  160. _debug3 "_key" "$_key"
  161. _secure_debug3 "_value" "$_value"
  162. case "$_key" in
  163. AccessKeyId) echo "AWS_ACCESS_KEY_ID=$_value" ;;
  164. SecretAccessKey) echo "AWS_SECRET_ACCESS_KEY=$_value" ;;
  165. Token) echo "AWS_SESSION_TOKEN=$_value" ;;
  166. esac
  167. done \
  168. | paste -sd' ' -
  169. )"
  170. _secure_debug "_aws_creds" "$_aws_creds"
  171. eval "$_aws_creds"
  172. _using_role=true
  173. }
  174. #method uri qstr data
  175. aws_rest() {
  176. mtd="$1"
  177. ep="$2"
  178. qsr="$3"
  179. data="$4"
  180. _debug mtd "$mtd"
  181. _debug ep "$ep"
  182. _debug qsr "$qsr"
  183. _debug data "$data"
  184. CanonicalURI="/$ep"
  185. _debug2 CanonicalURI "$CanonicalURI"
  186. CanonicalQueryString="$qsr"
  187. _debug2 CanonicalQueryString "$CanonicalQueryString"
  188. RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
  189. _debug2 RequestDate "$RequestDate"
  190. #RequestDate="20161120T141056Z" ##############
  191. export _H1="x-amz-date: $RequestDate"
  192. aws_host="$AWS_HOST"
  193. CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
  194. SignedHeaders="host;x-amz-date"
  195. if [ -n "$AWS_SESSION_TOKEN" ]; then
  196. export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
  197. CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
  198. SignedHeaders="${SignedHeaders};x-amz-security-token"
  199. fi
  200. _debug2 CanonicalHeaders "$CanonicalHeaders"
  201. _debug2 SignedHeaders "$SignedHeaders"
  202. RequestPayload="$data"
  203. _debug2 RequestPayload "$RequestPayload"
  204. Hash="sha256"
  205. CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
  206. _debug2 CanonicalRequest "$CanonicalRequest"
  207. HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
  208. _debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
  209. Algorithm="AWS4-HMAC-SHA256"
  210. _debug2 Algorithm "$Algorithm"
  211. RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
  212. _debug2 RequestDateOnly "$RequestDateOnly"
  213. Region="us-east-1"
  214. Service="route53"
  215. CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
  216. _debug2 CredentialScope "$CredentialScope"
  217. StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
  218. _debug2 StringToSign "$StringToSign"
  219. kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
  220. #kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
  221. _secure_debug2 kSecret "$kSecret"
  222. kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
  223. _secure_debug2 kSecretH "$kSecretH"
  224. kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
  225. _debug2 kDateH "$kDateH"
  226. kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
  227. _debug2 kRegionH "$kRegionH"
  228. kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
  229. _debug2 kServiceH "$kServiceH"
  230. kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
  231. _debug2 kSigningH "$kSigningH"
  232. signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
  233. _debug2 signature "$signature"
  234. Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
  235. _debug2 Authorization "$Authorization"
  236. _H2="Authorization: $Authorization"
  237. _debug _H2 "$_H2"
  238. url="$AWS_URL/$ep"
  239. if [ "$qsr" ]; then
  240. url="$AWS_URL/$ep?$qsr"
  241. fi
  242. if [ "$mtd" = "GET" ]; then
  243. response="$(_get "$url")"
  244. else
  245. response="$(_post "$data" "$url")"
  246. fi
  247. _ret="$?"
  248. _debug2 response "$response"
  249. if [ "$_ret" = "0" ]; then
  250. if _contains "$response" "<ErrorResponse"; then
  251. _err "Response error:$response"
  252. return 1
  253. fi
  254. fi
  255. return "$_ret"
  256. }