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.

224 lines
6.4 KiB

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