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.

320 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. if [ -n "${AWS_USE_INSTANCE_ROLE:=$(_readaccountconf_mutable AWS_USE_INSTANCE_ROLE)}" ]; then
  17. _use_instance_role
  18. fi
  19. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  20. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  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
  29. if [ -n "$AWS_USE_INSTANCE_ROLE" ]; then
  30. _saveaccountconf_mutable AWS_USE_INSTANCE_ROLE "$AWS_USE_INSTANCE_ROLE"
  31. else
  32. _saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
  33. _saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
  34. fi
  35. _debug "First detect the root zone"
  36. if ! _get_root "$fulldomain"; then
  37. _err "invalid domain"
  38. return 1
  39. fi
  40. _debug _domain_id "$_domain_id"
  41. _debug _sub_domain "$_sub_domain"
  42. _debug _domain "$_domain"
  43. _info "Geting existing records for $fulldomain"
  44. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  45. return 1
  46. fi
  47. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  48. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  49. _debug "_resource_record" "$_resource_record"
  50. else
  51. _debug "single new add"
  52. fi
  53. if [ "$_resource_record" ] && _contains "$response" "$txtvalue"; then
  54. _info "The txt record already exists, skip"
  55. return 0
  56. fi
  57. _debug "Adding records"
  58. _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>"
  59. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  60. _info "txt record updated success."
  61. return 0
  62. fi
  63. return 1
  64. }
  65. #fulldomain txtvalue
  66. dns_aws_rm() {
  67. fulldomain=$1
  68. txtvalue=$2
  69. if [ -n "${AWS_USE_INSTANCE_ROLE:=$(_readaccountconf_mutable AWS_USE_INSTANCE_ROLE)}" ]; then
  70. _use_instance_role
  71. fi
  72. AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
  73. AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
  74. _debug "First detect the root zone"
  75. if ! _get_root "$fulldomain"; then
  76. _err "invalid domain"
  77. return 1
  78. fi
  79. _debug _domain_id "$_domain_id"
  80. _debug _sub_domain "$_sub_domain"
  81. _debug _domain "$_domain"
  82. _info "Geting existing records for $fulldomain"
  83. if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
  84. return 1
  85. fi
  86. if _contains "$response" "<Name>$fulldomain.</Name>"; then
  87. _resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
  88. _debug "_resource_record" "$_resource_record"
  89. else
  90. _debug "no records exists, skip"
  91. return 0
  92. fi
  93. _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>"
  94. if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
  95. _info "txt record deleted success."
  96. return 0
  97. fi
  98. return 1
  99. }
  100. #################### Private functions below ##################################
  101. _get_root() {
  102. domain=$1
  103. i=2
  104. p=1
  105. if aws_rest GET "2013-04-01/hostedzone"; then
  106. while true; do
  107. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  108. _debug2 "Checking domain: $h"
  109. if [ -z "$h" ]; then
  110. if _contains "$response" "<IsTruncated>true</IsTruncated>" && _contains "$response" "<NextMarker>"; then
  111. _debug "IsTruncated"
  112. _nextMarker="$(echo "$response" | _egrep_o "<NextMarker>.*</NextMarker>" | cut -d '>' -f 2 | cut -d '<' -f 1)"
  113. _debug "NextMarker" "$_nextMarker"
  114. if aws_rest GET "2013-04-01/hostedzone" "marker=$_nextMarker"; then
  115. _debug "Truncated request OK"
  116. i=2
  117. p=1
  118. continue
  119. else
  120. _err "Truncated request error."
  121. fi
  122. fi
  123. #not valid
  124. _err "Invalid domain"
  125. return 1
  126. fi
  127. if _contains "$response" "<Name>$h.</Name>"; then
  128. hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$h.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
  129. _debug hostedzone "$hostedzone"
  130. if [ "$hostedzone" ]; then
  131. _domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
  132. if [ "$_domain_id" ]; then
  133. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  134. _domain=$h
  135. return 0
  136. fi
  137. _err "Can not find domain id: $h"
  138. return 1
  139. fi
  140. fi
  141. p=$i
  142. i=$(_math "$i" + 1)
  143. done
  144. fi
  145. return 1
  146. }
  147. _use_instance_role() {
  148. if ! _get "$AWS_METADATA_URL/iam/security-credentials/" true | _head_n 1 | grep -Fq 200; then
  149. _err "Unable to fetch IAM role from AWS instance metadata."
  150. return
  151. fi
  152. _aws_role=$(_get "$AWS_METADATA_URL/iam/security-credentials/")
  153. _debug "_aws_role" "$_aws_role"
  154. _aws_creds="$(
  155. _get "$AWS_METADATA_URL/iam/security-credentials/$_aws_role" \
  156. | _normalizeJson \
  157. | tr '{,}' '\n' \
  158. | while read -r _line; do
  159. _key="$(echo "${_line%%:*}" | tr -d '"')"
  160. _value="${_line#*:}"
  161. _debug3 "_key" "$_key"
  162. _secure_debug3 "_value" "$_value"
  163. case "$_key" in
  164. AccessKeyId) echo "AWS_ACCESS_KEY_ID=$_value" ;;
  165. SecretAccessKey) echo "AWS_SECRET_ACCESS_KEY=$_value" ;;
  166. Token) echo "AWS_SESSION_TOKEN=$_value" ;;
  167. esac
  168. done \
  169. | paste -sd' ' -
  170. )"
  171. _secure_debug "_aws_creds" "$_aws_creds"
  172. eval "$_aws_creds"
  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. }