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.

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