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.

221 lines
6.3 KiB

4 years ago
4 years ago
4 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=$(gcloud auth list \
  44. --filter "status:ACTIVE" \
  45. --format "value(account)" \
  46. --verbosity error
  47. )
  48. if [ "$account" ]; then
  49. _info "_dns_gcloud_authenticate: already authenticated"
  50. return 0
  51. fi
  52. _debug "_dns_gcloud_authenticate: attempting to authenticate using service account key"
  53. GCLOUD_Service_Account_Key="${CF_Token:-$(_readaccountconf_mutable GCLOUD_Service_Account_Key)}"
  54. GCLOUD_Project_ID="${CF_Account_ID:-$(_readaccountconf_mutable GCLOUD_Project_ID)}"
  55. if [ -z "$GCLOUD_Service_Account_Key" ]; then
  56. GCLOUD_Service_Account_Key=""
  57. GCLOUD_Project_ID=""
  58. _err "_dns_gcloud_authenticate: missing Google Cloud service account key"
  59. return 1
  60. fi
  61. if [ -z "$GCLOUD_Project_ID" ]; then
  62. GCLOUD_Service_Account_Key=""
  63. GCLOUD_Project_ID=""
  64. _err "_dns_gcloud_authenticate: missing Google Cloud project ID"
  65. return 1
  66. fi
  67. if ! echo "$GCLOUD_Service_Account_Key" | gcloud auth activate-service-account --key-file -; then
  68. _err "_dns_gcloud_authenticate: failed to authenticate with service account key"
  69. return 1
  70. fi
  71. _info "_dns_gcloud_authenticate: successfully authenticated using service account key"
  72. gcloud config set project "$GCLOUD_Project_ID"
  73. _info "_dns_gcloud_authenticate: configured gcloud project"
  74. }
  75. _dns_gcloud_authenticate() {
  76. account=$(gcloud auth list --filter "status:ACTIVE" --format "value(account)")
  77. }
  78. _dns_gcloud_start_tr() {
  79. if ! trd=$(mktemp -d); then
  80. _err "_dns_gcloud_start_tr: failed to create temporary directory"
  81. return 1
  82. fi
  83. tr="$trd/tr.yaml"
  84. _debug tr "$tr"
  85. if ! gcloud dns record-sets transaction start \
  86. --transaction-file="$tr" \
  87. --zone="$managedZone"; then
  88. rm -r "$trd"
  89. _err "_dns_gcloud_start_tr: failed to execute transaction"
  90. return 1
  91. fi
  92. }
  93. _dns_gcloud_execute_tr() {
  94. if ! gcloud dns record-sets transaction execute \
  95. --transaction-file="$tr" \
  96. --zone="$managedZone"; then
  97. _debug tr "$(cat "$tr")"
  98. rm -r "$trd"
  99. _err "_dns_gcloud_execute_tr: failed to execute transaction"
  100. return 1
  101. fi
  102. rm -r "$trd"
  103. for i in $(seq 1 120); do
  104. if gcloud dns record-sets changes list \
  105. --zone="$managedZone" \
  106. --filter='status != done' |
  107. grep -q '^.*'; then
  108. _info "_dns_gcloud_execute_tr: waiting for transaction to be comitted ($i/120)..."
  109. sleep 5
  110. else
  111. return 0
  112. fi
  113. done
  114. _err "_dns_gcloud_execute_tr: transaction is still pending after 10 minutes"
  115. rm -r "$trd"
  116. return 1
  117. }
  118. _dns_gcloud_remove_rrs() {
  119. if ! xargs -r gcloud dns record-sets transaction remove \
  120. --name="$fulldomain." \
  121. --ttl="$ttl" \
  122. --type=TXT \
  123. --zone="$managedZone" \
  124. --transaction-file="$tr"; then
  125. _debug tr "$(cat "$tr")"
  126. rm -r "$trd"
  127. _err "_dns_gcloud_remove_rrs: failed to remove RRs"
  128. return 1
  129. fi
  130. }
  131. _dns_gcloud_add_rrs() {
  132. ttl=60
  133. if ! xargs -r gcloud dns record-sets transaction add \
  134. --name="$fulldomain." \
  135. --ttl="$ttl" \
  136. --type=TXT \
  137. --zone="$managedZone" \
  138. --transaction-file="$tr"; then
  139. _debug tr "$(cat "$tr")"
  140. rm -r "$trd"
  141. _err "_dns_gcloud_add_rrs: failed to add RRs"
  142. return 1
  143. fi
  144. }
  145. _dns_gcloud_find_zone() {
  146. # Prepare a filter that matches zones that are suiteable for this entry.
  147. # For example, _acme-challenge.something.domain.com might need to go into something.domain.com or domain.com;
  148. # this function finds the longest postfix that has a managed zone.
  149. part="$fulldomain"
  150. filter="dnsName=( "
  151. while [ "$part" != "" ]; do
  152. filter="$filter$part. "
  153. part="$(echo "$part" | sed 's/[^.]*\.*//')"
  154. done
  155. filter="$filter) AND visibility=public"
  156. _debug filter "$filter"
  157. # List domains and find the zone with the deepest sub-domain (in case of some levels of delegation)
  158. if ! match=$(gcloud dns managed-zones list \
  159. --format="value(name, dnsName)" \
  160. --filter="$filter" |
  161. while read -r dnsName name; do
  162. printf "%s\t%s\t%s\n" "$(echo "$name" | awk -F"." '{print NF-1}')" "$dnsName" "$name"
  163. done |
  164. sort -n -r | _head_n 1 | cut -f2,3 | grep '^.*'); then
  165. _err "_dns_gcloud_find_zone: Can't find a matching managed zone! Perhaps wrong project or gcloud credentials?"
  166. return 1
  167. fi
  168. dnsName=$(echo "$match" | cut -f2)
  169. _debug dnsName "$dnsName"
  170. managedZone=$(echo "$match" | cut -f1)
  171. _debug managedZone "$managedZone"
  172. }
  173. _dns_gcloud_get_rrdatas() {
  174. if ! rrdatas=$(gcloud dns record-sets list \
  175. --zone="$managedZone" \
  176. --name="$fulldomain." \
  177. --type=TXT \
  178. --format="value(ttl,rrdatas)"); then
  179. _err "_dns_gcloud_get_rrdatas: Failed to list record-sets"
  180. rm -r "$trd"
  181. return 1
  182. fi
  183. ttl=$(echo "$rrdatas" | cut -f1)
  184. rrdatas=$(echo "$rrdatas" | cut -f2 | sed 's/","/"\n"/g')
  185. }