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.

345 lines
11 KiB

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