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.

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