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.

364 lines
11 KiB

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