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.

216 lines
6.3 KiB

4 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # Author: Janos Lenart <janos@lenart.io>
  3. ######## Public functions #####################
  4. # Usage: dns_gcloud_add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  5. dns_gcloud_add() {
  6. fulldomain=$1
  7. txtvalue=$2
  8. _info "Using gcloud"
  9. _debug fulldomain "$fulldomain"
  10. _debug txtvalue "$txtvalue"
  11. _dns_gcloud_authenticate || return $?
  12. _dns_gcloud_find_zone || return $?
  13. # Add an extra RR
  14. _dns_gcloud_start_tr || return $?
  15. _dns_gcloud_get_rrdatas || return $?
  16. echo "$rrdatas" | _dns_gcloud_remove_rrs || return $?
  17. printf "%s\n%s\n" "$rrdatas" "\"$txtvalue\"" | grep -v '^$' | _dns_gcloud_add_rrs || return $?
  18. _dns_gcloud_execute_tr || return $?
  19. _info "$fulldomain record added"
  20. }
  21. # Usage: dns_gcloud_rm _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  22. # Remove the txt record after validation.
  23. dns_gcloud_rm() {
  24. fulldomain=$1
  25. txtvalue=$2
  26. _info "Using gcloud"
  27. _debug fulldomain "$fulldomain"
  28. _debug txtvalue "$txtvalue"
  29. _dns_gcloud_authenticate || return $?
  30. _dns_gcloud_find_zone || return $?
  31. # Remove one RR
  32. _dns_gcloud_start_tr || return $?
  33. _dns_gcloud_get_rrdatas || return $?
  34. echo "$rrdatas" | _dns_gcloud_remove_rrs || return $?
  35. echo "$rrdatas" | grep -F -v "\"$txtvalue\"" | _dns_gcloud_add_rrs || return $?
  36. _dns_gcloud_execute_tr || return $?
  37. _info "$fulldomain record added"
  38. }
  39. #################### Private functions below ##################################
  40. _dns_gcloud_authenticate() {
  41. _info "_dns_gcloud_authenticate: authenticating gcloud"
  42. _debug "_dns_gcloud_authenticate: checking authenticated status"
  43. account=$(
  44. gcloud auth list \
  45. --filter "status:ACTIVE" \
  46. --format "value(account)" \
  47. --verbosity error
  48. )
  49. if [ "$account" ]; then
  50. _info "_dns_gcloud_authenticate: already authenticated"
  51. return 0
  52. fi
  53. _debug "_dns_gcloud_authenticate: unauthenticated"
  54. _debug "_dns_gcloud_authenticate: authenticating using service account key"
  55. GCLOUD_Service_Account_Key="${GCLOUD_Service_Account_Key:-$(_readaccountconf_mutable GCLOUD_Service_Account_Key)}"
  56. GCLOUD_Project_ID="${GCLOUD_Project_ID:-$(_readaccountconf_mutable GCLOUD_Project_ID)}"
  57. if [ -z "$GCLOUD_Service_Account_Key" ] || [ -z "$GCLOUD_Project_ID" ]; then
  58. GCLOUD_Service_Account_Key=""
  59. GCLOUD_Project_ID=""
  60. _err "_dns_gcloud_authenticate: missing Google Cloud service account key and or project ID"
  61. return 1
  62. fi
  63. #save the service account api key and project ID to the account conf file.
  64. _saveaccountconf_mutable GCLOUD_Service_Account_Key "$GCLOUD_Service_Account_Key"
  65. _saveaccountconf_mutable GCLOUD_Project_ID "$GCLOUD_Project_ID"
  66. if ! echo "$GCLOUD_Service_Account_Key" | gcloud auth activate-service-account --key-file -; then
  67. _err "_dns_gcloud_authenticate: failed to authenticate with service account key"
  68. return 1
  69. fi
  70. _info "_dns_gcloud_authenticate: successfully authenticated using service account key"
  71. gcloud config set project "$GCLOUD_Project_ID"
  72. _info "_dns_gcloud_authenticate: configured gcloud project"
  73. }
  74. _dns_gcloud_start_tr() {
  75. if ! trd=$(mktemp -d); then
  76. _err "_dns_gcloud_start_tr: failed to create temporary directory"
  77. return 1
  78. fi
  79. tr="$trd/tr.yaml"
  80. _debug tr "$tr"
  81. if ! gcloud dns record-sets transaction start \
  82. --transaction-file="$tr" \
  83. --zone="$managedZone"; then
  84. rm -r "$trd"
  85. _err "_dns_gcloud_start_tr: failed to execute transaction"
  86. return 1
  87. fi
  88. }
  89. _dns_gcloud_execute_tr() {
  90. if ! gcloud dns record-sets transaction execute \
  91. --transaction-file="$tr" \
  92. --zone="$managedZone"; then
  93. _debug tr "$(cat "$tr")"
  94. rm -r "$trd"
  95. _err "_dns_gcloud_execute_tr: failed to execute transaction"
  96. return 1
  97. fi
  98. rm -r "$trd"
  99. for i in $(seq 1 120); do
  100. if gcloud dns record-sets changes list \
  101. --zone="$managedZone" \
  102. --filter='status != done' |
  103. grep -q '^.*'; then
  104. _info "_dns_gcloud_execute_tr: waiting for transaction to be comitted ($i/120)..."
  105. sleep 5
  106. else
  107. return 0
  108. fi
  109. done
  110. _err "_dns_gcloud_execute_tr: transaction is still pending after 10 minutes"
  111. rm -r "$trd"
  112. return 1
  113. }
  114. _dns_gcloud_remove_rrs() {
  115. if ! xargs -r gcloud dns record-sets transaction remove \
  116. --name="$fulldomain." \
  117. --ttl="$ttl" \
  118. --type=TXT \
  119. --zone="$managedZone" \
  120. --transaction-file="$tr"; then
  121. _debug tr "$(cat "$tr")"
  122. rm -r "$trd"
  123. _err "_dns_gcloud_remove_rrs: failed to remove RRs"
  124. return 1
  125. fi
  126. }
  127. _dns_gcloud_add_rrs() {
  128. ttl=60
  129. if ! xargs -r gcloud dns record-sets transaction add \
  130. --name="$fulldomain." \
  131. --ttl="$ttl" \
  132. --type=TXT \
  133. --zone="$managedZone" \
  134. --transaction-file="$tr"; then
  135. _debug tr "$(cat "$tr")"
  136. rm -r "$trd"
  137. _err "_dns_gcloud_add_rrs: failed to add RRs"
  138. return 1
  139. fi
  140. }
  141. _dns_gcloud_find_zone() {
  142. # Prepare a filter that matches zones that are suiteable for this entry.
  143. # For example, _acme-challenge.something.domain.com might need to go into something.domain.com or domain.com;
  144. # this function finds the longest postfix that has a managed zone.
  145. part="$fulldomain"
  146. filter="dnsName=( "
  147. while [ "$part" != "" ]; do
  148. filter="$filter$part. "
  149. part="$(echo "$part" | sed 's/[^.]*\.*//')"
  150. done
  151. filter="$filter) AND visibility=public"
  152. _debug filter "$filter"
  153. # List domains and find the zone with the deepest sub-domain (in case of some levels of delegation)
  154. if ! match=$(gcloud dns managed-zones list \
  155. --format="value(name, dnsName)" \
  156. --filter="$filter" |
  157. while read -r dnsName name; do
  158. printf "%s\t%s\t%s\n" "$(echo "$name" | awk -F"." '{print NF-1}')" "$dnsName" "$name"
  159. done |
  160. sort -n -r | _head_n 1 | cut -f2,3 | grep '^.*'); then
  161. _err "_dns_gcloud_find_zone: Can't find a matching managed zone! Perhaps wrong project or gcloud credentials?"
  162. return 1
  163. fi
  164. dnsName=$(echo "$match" | cut -f2)
  165. _debug dnsName "$dnsName"
  166. managedZone=$(echo "$match" | cut -f1)
  167. _debug managedZone "$managedZone"
  168. }
  169. _dns_gcloud_get_rrdatas() {
  170. if ! rrdatas=$(gcloud dns record-sets list \
  171. --zone="$managedZone" \
  172. --name="$fulldomain." \
  173. --type=TXT \
  174. --format="value(ttl,rrdatas)"); then
  175. _err "_dns_gcloud_get_rrdatas: Failed to list record-sets"
  176. rm -r "$trd"
  177. return 1
  178. fi
  179. ttl=$(echo "$rrdatas" | cut -f1)
  180. rrdatas=$(echo "$rrdatas" | cut -f2 | sed 's/","/"\n"/g')
  181. }