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.

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