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