Skip to content

Commit

Permalink
Improve matching for domain ID
Browse files Browse the repository at this point in the history
  • Loading branch information
softins committed Jan 1, 2024
1 parent 69fc825 commit 4e71eca
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
28 changes: 20 additions & 8 deletions dns_scripts/dns_add_linode
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,37 @@ if [[ -z "$LINODE_KEY" ]]; then
exit 1
fi

domain_root=${fulldomain#*.}
domain=${fulldomain%.$domain_root}
txtname="_acme-challenge.$domain"

# Get Domain ID
# Get Domain List
response=$(curl --silent ${api_url}/domains \
-H "User-Agent: getssl/0.1" -H "Authorization: Bearer ${api_key}")
domain_id=$(echo "$response" | jq ".data[] | select (.domain==\"$domain_root\") | .id")
if [[ $domain_id == "" ]]; then

# Get Domain ID for longest match
domain_root="$fulldomain"
domain=""

while [[ "$domain_root" == *.* ]] ; do
domain_id=$(echo "$response" | jq ".data[]? | select (.domain==\"$domain_root\") | .id")
if [[ "$domain_id" != "" ]] ; then
break
fi
domain_root=${domain_root#*.}
domain=${fulldomain%.$domain_root}
done

if [[ "$domain_id" == "" ]]; then
echo "Failed to fetch DomainID"
exit 1
fi

txtname="_acme-challenge${domain:+.$domain}"

# Create TXT record

response=$(curl --silent -X POST ${api_url}/domains/${domain_id}/records \
-H "Content-Type: application/json" -H "User-Agent: getssl/0.1" -H "Authorization: Bearer ${api_key}" \
-d '{"type": "TXT", "name": "'${txtname}'", "target": "'$token'", "ttl_sec": 30}')
errors=$(echo "$response" | jq ".errors[]?.reason")
if [[ $errors != "" ]]; then
if [[ "$errors" != "" ]]; then
echo "Something went wrong: $errors"
exit 1
fi
29 changes: 20 additions & 9 deletions dns_scripts/dns_del_linode
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@ if [[ -z "$LINODE_KEY" ]]; then
exit 1
fi

domain_root=${fulldomain#*.}
domain=${fulldomain%.$domain_root}
txtname="_acme-challenge.$domain"

# Get Domain ID
# Get Domain List
response=$(curl --silent ${api_url}/domains \
-H "User-Agent: getssl/0.1" -H "Authorization: Bearer ${api_key}")
domain_id=$(echo "$response" | jq ".data[] | select (.domain==\"$domain_root\") | .id")
if [[ $domain_id == "" ]]; then

# Get Domain ID for longest match
domain_root="$fulldomain"
domain=""

while [[ "$domain_root" == *.* ]] ; do
domain_id=$(echo "$response" | jq ".data[]? | select (.domain==\"$domain_root\") | .id")
if [[ "$domain_id" != "" ]] ; then
break
fi
domain_root=${domain_root#*.}
domain=${fulldomain%.$domain_root}
done

if [[ "$domain_id" == "" ]]; then
echo "Failed to fetch DomainID"
exit 1
fi

txtname="_acme-challenge${domain:+.$domain}"

# Get Resource ID
response=$(curl --silent ${api_url}/domains/${domain_id}/records \
-H "User-Agent: getssl/0.1" -H "Authorization: Bearer ${api_key}")
resource_id=$(echo "$response" | jq ".data[] | select (.name==\"$txtname\") | .id")
if [[ $resource_id == "" ]]; then
if [[ "$resource_id" == "" ]]; then
echo "Failed to fetch ResourceID"
exit 1
fi
Expand All @@ -40,7 +51,7 @@ fi
response=$(curl --silent -X DELETE ${api_url}/domains/${domain_id}/records/${resource_id} \
-H "User-Agent: getssl/0.1" -H "Authorization: Bearer ${api_key}")
errors=$(echo "$response" | jq ".errors[]?.reason")
if [[ $errors != "" ]]; then
if [[ "$errors" != "" ]]; then
echo "Something went wrong: $errors"
exit 1
fi

0 comments on commit 4e71eca

Please sign in to comment.