mirror of
https://github.com/acmesh-official/acme.sh.git
synced 2025-05-08 12:22:44 +00:00
Use `hostedzonesbyname` Route 53 API endpoint instead of `hostedzones` endpoint. The `hostedzones` endpoint returns all hosted zones for a given Route 53 account in groups of 100. For AWS Route 53 accounts with many domains, this could mean a large number of requests to the `hostedzones` endpoint as it progresses through each page of 100 results. This will often result in a "Rate exceeded" API error from Route 53. Instead of using `hostedzones` endpoint, we can use `hostedzonesbyname` and then filter by the specific domain we are looking for and ask for a `max-items` of 1. The while loop in _get_root() starts with a given domain and removes parts from the front of the given domain if no match is found. For example, when requesting a certificate for `test.www.domain.co.uk`, the while loop will check for Route 53 hosted zones for: 1st: test.www.domain.co.uk 2nd: www.domain.co.uk 3rd: domain.co.uk 4th: co.uk 5th: uk The first two checks will result in no matches, while the third check should be successful (if, of course, domain.co.uk is actually a hosted zone in the given AWS account). Now imagine that the given AWS account owns 2500 domains and, therefore, has 2500 hosted zones. Using the `hostedzones` endpoint would result in: 1st: 25 GET requests to the Route 53 API looking for a match to test.www.domain.co.uk 2nd: 25 GET requests to the Route 53 API looking for a match to www.domain.co.uk 3rd: 25 GET requests to the Route 53 API looking for a match to domain.co.uk 4th: 25 GET requests to the Route 53 API looking for a match to co.uk 5th: 25 GET requests to the Route 53 API looking for a match to uk This would far exceed the Route 53 limit of five requests per second. Using `hostedzonesbyname` results in a dramatic reduction in Route 53 API GET requests for AWS accounts with large numbers of hosted zones.
374 lines
12 KiB
Bash
Executable File
374 lines
12 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
|
|
#
|
|
#AWS_ACCESS_KEY_ID="sdfsdfsdfljlbjkljlkjsdfoiwje"
|
|
#
|
|
#AWS_SECRET_ACCESS_KEY="xxxxxxx"
|
|
|
|
#This is the Amazon Route53 api wrapper for acme.sh
|
|
#All `_sleep` commands are included to avoid Route53 throttling, see
|
|
#https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/DNSLimitations.html#limits-api-requests
|
|
|
|
AWS_HOST="route53.amazonaws.com"
|
|
AWS_URL="https://$AWS_HOST"
|
|
|
|
AWS_WIKI="https://github.com/Neilpang/acme.sh/wiki/How-to-use-Amazon-Route53-API"
|
|
|
|
######## Public functions #####################
|
|
|
|
#Usage: dns_myapi_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
|
|
dns_aws_add() {
|
|
fulldomain=$1
|
|
txtvalue=$2
|
|
|
|
AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
|
|
AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
|
|
AWS_DNS_SLOWRATE="${AWS_DNS_SLOWRATE:-$(_readaccountconf_mutable AWS_DNS_SLOWRATE)}"
|
|
|
|
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
|
|
_use_container_role || _use_instance_role
|
|
fi
|
|
|
|
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
|
|
AWS_ACCESS_KEY_ID=""
|
|
AWS_SECRET_ACCESS_KEY=""
|
|
_err "You haven't specifed the aws route53 api key id and and api key secret yet."
|
|
_err "Please create your key and try again. see $(__green $AWS_WIKI)"
|
|
return 1
|
|
fi
|
|
|
|
#save for future use, unless using a role which will be fetched as needed
|
|
if [ -z "$_using_role" ]; then
|
|
_saveaccountconf_mutable AWS_ACCESS_KEY_ID "$AWS_ACCESS_KEY_ID"
|
|
_saveaccountconf_mutable AWS_SECRET_ACCESS_KEY "$AWS_SECRET_ACCESS_KEY"
|
|
_saveaccountconf_mutable AWS_DNS_SLOWRATE "$AWS_DNS_SLOWRATE"
|
|
fi
|
|
|
|
_debug "First detect the root zone"
|
|
if ! _get_root "$fulldomain"; then
|
|
_err "invalid domain"
|
|
_sleep 1
|
|
return 1
|
|
fi
|
|
_debug _domain_id "$_domain_id"
|
|
_debug _sub_domain "$_sub_domain"
|
|
_debug _domain "$_domain"
|
|
|
|
_info "Getting existing records for $fulldomain"
|
|
if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
|
|
_sleep 1
|
|
return 1
|
|
fi
|
|
|
|
if _contains "$response" "<Name>$fulldomain.</Name>"; then
|
|
_resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
|
|
_debug "_resource_record" "$_resource_record"
|
|
else
|
|
_debug "single new add"
|
|
fi
|
|
|
|
if [ "$_resource_record" ] && _contains "$response" "$txtvalue"; then
|
|
_info "The TXT record already exists. Skipping."
|
|
_sleep 1
|
|
return 0
|
|
fi
|
|
|
|
_debug "Adding records"
|
|
|
|
_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>"
|
|
|
|
if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
|
|
_info "TXT record updated successfully."
|
|
if [ -n "$AWS_DNS_SLOWRATE" ]; then
|
|
_info "Slow rate activated: sleeping for $AWS_DNS_SLOWRATE seconds"
|
|
_sleep "$AWS_DNS_SLOWRATE"
|
|
else
|
|
_sleep 1
|
|
fi
|
|
|
|
return 0
|
|
fi
|
|
_sleep 1
|
|
return 1
|
|
}
|
|
|
|
#fulldomain txtvalue
|
|
dns_aws_rm() {
|
|
fulldomain=$1
|
|
txtvalue=$2
|
|
|
|
AWS_ACCESS_KEY_ID="${AWS_ACCESS_KEY_ID:-$(_readaccountconf_mutable AWS_ACCESS_KEY_ID)}"
|
|
AWS_SECRET_ACCESS_KEY="${AWS_SECRET_ACCESS_KEY:-$(_readaccountconf_mutable AWS_SECRET_ACCESS_KEY)}"
|
|
AWS_DNS_SLOWRATE="${AWS_DNS_SLOWRATE:-$(_readaccountconf_mutable AWS_DNS_SLOWRATE)}"
|
|
|
|
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
|
|
_use_container_role || _use_instance_role
|
|
fi
|
|
|
|
_debug "First detect the root zone"
|
|
if ! _get_root "$fulldomain"; then
|
|
_err "invalid domain"
|
|
_sleep 1
|
|
return 1
|
|
fi
|
|
_debug _domain_id "$_domain_id"
|
|
_debug _sub_domain "$_sub_domain"
|
|
_debug _domain "$_domain"
|
|
|
|
_info "Getting existing records for $fulldomain"
|
|
if ! aws_rest GET "2013-04-01$_domain_id/rrset" "name=$fulldomain&type=TXT"; then
|
|
_sleep 1
|
|
return 1
|
|
fi
|
|
|
|
if _contains "$response" "<Name>$fulldomain.</Name>"; then
|
|
_resource_record="$(echo "$response" | sed 's/<ResourceRecordSet>/"/g' | tr '"' "\n" | grep "<Name>$fulldomain.</Name>" | _egrep_o "<ResourceRecords.*</ResourceRecords>" | sed "s/<ResourceRecords>//" | sed "s#</ResourceRecords>##")"
|
|
_debug "_resource_record" "$_resource_record"
|
|
else
|
|
_debug "no records exist, skip"
|
|
_sleep 1
|
|
return 0
|
|
fi
|
|
|
|
_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>"
|
|
|
|
if aws_rest POST "2013-04-01$_domain_id/rrset/" "" "$_aws_tmpl_xml" && _contains "$response" "ChangeResourceRecordSetsResponse"; then
|
|
_info "TXT record deleted successfully."
|
|
if [ -n "$AWS_DNS_SLOWRATE" ]; then
|
|
_info "Slow rate activated: sleeping for $AWS_DNS_SLOWRATE seconds"
|
|
_sleep "$AWS_DNS_SLOWRATE"
|
|
else
|
|
_sleep 1
|
|
fi
|
|
|
|
return 0
|
|
fi
|
|
_sleep 1
|
|
return 1
|
|
|
|
}
|
|
|
|
#################### Private functions below ##################################
|
|
|
|
_get_root() {
|
|
domain=$1
|
|
# Start with field 2 since each domain starts with _acme-challenge
|
|
# Example: _acme-challenge.www.domain.com
|
|
field=2
|
|
subdomain_part=1
|
|
|
|
while true; do
|
|
hostname=$(printf "%s" "$domain" | cut -d . -f $field-100)
|
|
|
|
if [ -z "$hostname" ]
|
|
then
|
|
_debug "There are no more fields in the hostname to check"
|
|
_err "No matching Route 53 hosted zones for: $domain"
|
|
return 1
|
|
fi
|
|
|
|
_debug "Checking domain: $hostname"
|
|
|
|
if aws_rest GET "2013-04-01/hostedzonesbyname" "dnsname=$hostname&maxitems=1"; then
|
|
if _contains "$response" "<Name>$hostname.</Name>"; then
|
|
hostedzone="$(echo "$response" | sed 's/<HostedZone>/#&/g' | tr '#' '\n' | _egrep_o "<HostedZone><Id>[^<]*<.Id><Name>$hostname.<.Name>.*<PrivateZone>false<.PrivateZone>.*<.HostedZone>")"
|
|
_debug hostedzone "$hostedzone"
|
|
if [ "$hostedzone" ]; then
|
|
_domain_id=$(printf "%s\n" "$hostedzone" | _egrep_o "<Id>.*<.Id>" | head -n 1 | _egrep_o ">.*<" | tr -d "<>")
|
|
if [ "$_domain_id" ]; then
|
|
_sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$subdomain_part)
|
|
_domain=$hostname
|
|
return 0
|
|
fi
|
|
_err "Can't find domain with id: $hostname"
|
|
return 1
|
|
fi
|
|
else
|
|
_debug "Route 53 does not have a hosted zone for: $hostname."
|
|
_debug "Moving on to next part of the hostname"
|
|
fi
|
|
else
|
|
_err "Getting Route 53 hosted zones by name failed for: $domain"
|
|
return 1
|
|
fi
|
|
|
|
subdomain_part=$field
|
|
field=$(_math "$field" + 1)
|
|
|
|
if [ -n "$AWS_DNS_SLOWRATE" ]; then
|
|
_info "Slow rate activated: sleeping for $AWS_DNS_SLOWRATE seconds"
|
|
_sleep "$AWS_DNS_SLOWRATE"
|
|
else
|
|
_sleep 1
|
|
fi
|
|
done
|
|
|
|
_err "_get_root failed for domain: $domain"
|
|
return 1
|
|
}
|
|
|
|
_use_container_role() {
|
|
# automatically set if running inside ECS
|
|
if [ -z "$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" ]; then
|
|
_debug "No ECS environment variable detected"
|
|
return 1
|
|
fi
|
|
_use_metadata "169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
|
|
}
|
|
|
|
_use_instance_role() {
|
|
_url="http://169.254.169.254/latest/meta-data/iam/security-credentials/"
|
|
_debug "_url" "$_url"
|
|
if ! _get "$_url" true 1 | _head_n 1 | grep -Fq 200; then
|
|
_debug "Unable to fetch IAM role from instance metadata"
|
|
return 1
|
|
fi
|
|
_aws_role=$(_get "$_url" "" 1)
|
|
_debug "_aws_role" "$_aws_role"
|
|
_use_metadata "$_url$_aws_role"
|
|
}
|
|
|
|
_use_metadata() {
|
|
_aws_creds="$(
|
|
_get "$1" "" 1 \
|
|
| _normalizeJson \
|
|
| tr '{,}' '\n' \
|
|
| while read -r _line; do
|
|
_key="$(echo "${_line%%:*}" | tr -d '"')"
|
|
_value="${_line#*:}"
|
|
_debug3 "_key" "$_key"
|
|
_secure_debug3 "_value" "$_value"
|
|
case "$_key" in
|
|
AccessKeyId) echo "AWS_ACCESS_KEY_ID=$_value" ;;
|
|
SecretAccessKey) echo "AWS_SECRET_ACCESS_KEY=$_value" ;;
|
|
Token) echo "AWS_SESSION_TOKEN=$_value" ;;
|
|
esac
|
|
done \
|
|
| paste -sd' ' -
|
|
)"
|
|
_secure_debug "_aws_creds" "$_aws_creds"
|
|
|
|
if [ -z "$_aws_creds" ]; then
|
|
return 1
|
|
fi
|
|
|
|
eval "$_aws_creds"
|
|
_using_role=true
|
|
}
|
|
|
|
#method uri qstr data
|
|
aws_rest() {
|
|
mtd="$1"
|
|
ep="$2"
|
|
qsr="$3"
|
|
data="$4"
|
|
|
|
_debug mtd "$mtd"
|
|
_debug ep "$ep"
|
|
_debug qsr "$qsr"
|
|
_debug data "$data"
|
|
|
|
CanonicalURI="/$ep"
|
|
_debug2 CanonicalURI "$CanonicalURI"
|
|
|
|
CanonicalQueryString="$qsr"
|
|
_debug2 CanonicalQueryString "$CanonicalQueryString"
|
|
|
|
RequestDate="$(date -u +"%Y%m%dT%H%M%SZ")"
|
|
_debug2 RequestDate "$RequestDate"
|
|
|
|
#RequestDate="20161120T141056Z" ##############
|
|
|
|
export _H1="x-amz-date: $RequestDate"
|
|
|
|
aws_host="$AWS_HOST"
|
|
CanonicalHeaders="host:$aws_host\nx-amz-date:$RequestDate\n"
|
|
SignedHeaders="host;x-amz-date"
|
|
if [ -n "$AWS_SESSION_TOKEN" ]; then
|
|
export _H3="x-amz-security-token: $AWS_SESSION_TOKEN"
|
|
CanonicalHeaders="${CanonicalHeaders}x-amz-security-token:$AWS_SESSION_TOKEN\n"
|
|
SignedHeaders="${SignedHeaders};x-amz-security-token"
|
|
fi
|
|
_debug2 CanonicalHeaders "$CanonicalHeaders"
|
|
_debug2 SignedHeaders "$SignedHeaders"
|
|
|
|
RequestPayload="$data"
|
|
_debug2 RequestPayload "$RequestPayload"
|
|
|
|
Hash="sha256"
|
|
|
|
CanonicalRequest="$mtd\n$CanonicalURI\n$CanonicalQueryString\n$CanonicalHeaders\n$SignedHeaders\n$(printf "%s" "$RequestPayload" | _digest "$Hash" hex)"
|
|
_debug2 CanonicalRequest "$CanonicalRequest"
|
|
|
|
HashedCanonicalRequest="$(printf "$CanonicalRequest%s" | _digest "$Hash" hex)"
|
|
_debug2 HashedCanonicalRequest "$HashedCanonicalRequest"
|
|
|
|
Algorithm="AWS4-HMAC-SHA256"
|
|
_debug2 Algorithm "$Algorithm"
|
|
|
|
RequestDateOnly="$(echo "$RequestDate" | cut -c 1-8)"
|
|
_debug2 RequestDateOnly "$RequestDateOnly"
|
|
|
|
Region="us-east-1"
|
|
Service="route53"
|
|
|
|
CredentialScope="$RequestDateOnly/$Region/$Service/aws4_request"
|
|
_debug2 CredentialScope "$CredentialScope"
|
|
|
|
StringToSign="$Algorithm\n$RequestDate\n$CredentialScope\n$HashedCanonicalRequest"
|
|
|
|
_debug2 StringToSign "$StringToSign"
|
|
|
|
kSecret="AWS4$AWS_SECRET_ACCESS_KEY"
|
|
|
|
#kSecret="wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY" ############################
|
|
|
|
_secure_debug2 kSecret "$kSecret"
|
|
|
|
kSecretH="$(printf "%s" "$kSecret" | _hex_dump | tr -d " ")"
|
|
_secure_debug2 kSecretH "$kSecretH"
|
|
|
|
kDateH="$(printf "$RequestDateOnly%s" | _hmac "$Hash" "$kSecretH" hex)"
|
|
_debug2 kDateH "$kDateH"
|
|
|
|
kRegionH="$(printf "$Region%s" | _hmac "$Hash" "$kDateH" hex)"
|
|
_debug2 kRegionH "$kRegionH"
|
|
|
|
kServiceH="$(printf "$Service%s" | _hmac "$Hash" "$kRegionH" hex)"
|
|
_debug2 kServiceH "$kServiceH"
|
|
|
|
kSigningH="$(printf "%s" "aws4_request" | _hmac "$Hash" "$kServiceH" hex)"
|
|
_debug2 kSigningH "$kSigningH"
|
|
|
|
signature="$(printf "$StringToSign%s" | _hmac "$Hash" "$kSigningH" hex)"
|
|
_debug2 signature "$signature"
|
|
|
|
Authorization="$Algorithm Credential=$AWS_ACCESS_KEY_ID/$CredentialScope, SignedHeaders=$SignedHeaders, Signature=$signature"
|
|
_debug2 Authorization "$Authorization"
|
|
|
|
_H2="Authorization: $Authorization"
|
|
_debug _H2 "$_H2"
|
|
|
|
url="$AWS_URL/$ep"
|
|
if [ "$qsr" ]; then
|
|
url="$AWS_URL/$ep?$qsr"
|
|
fi
|
|
|
|
if [ "$mtd" = "GET" ]; then
|
|
response="$(_get "$url")"
|
|
else
|
|
response="$(_post "$data" "$url")"
|
|
fi
|
|
|
|
_ret="$?"
|
|
_debug2 response "$response"
|
|
if [ "$_ret" = "0" ]; then
|
|
if _contains "$response" "<ErrorResponse"; then
|
|
_err "Response error:$response"
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
return "$_ret"
|
|
}
|