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.

225 lines
6.4 KiB

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