2018-12-06 23:50:17 +02:00
|
|
|
// Package gcloud implements a DNS provider for solving the DNS-01 challenge using Google Cloud DNS.
|
2018-05-30 20:20:21 +02:00
|
|
|
package gcloud
|
2016-03-18 17:22:33 +02:00
|
|
|
|
|
|
|
import (
|
2018-05-30 20:20:21 +02:00
|
|
|
"encoding/json"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2016-03-18 17:22:33 +02:00
|
|
|
"fmt"
|
2018-09-15 19:07:24 +02:00
|
|
|
"net/http"
|
2021-08-25 11:44:11 +02:00
|
|
|
"os"
|
2018-12-22 01:53:05 +02:00
|
|
|
"strconv"
|
2016-03-18 17:22:33 +02:00
|
|
|
"time"
|
|
|
|
|
2020-01-04 18:18:34 +02:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/platform/wait"
|
2016-03-18 17:22:33 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"google.golang.org/api/dns/v1"
|
2018-12-22 01:53:05 +02:00
|
|
|
"google.golang.org/api/googleapi"
|
2019-04-12 20:37:29 +02:00
|
|
|
"google.golang.org/api/option"
|
2016-03-18 17:22:33 +02:00
|
|
|
)
|
|
|
|
|
2019-01-02 21:45:17 +02:00
|
|
|
const (
|
|
|
|
changeStatusDone = "done"
|
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "GCE_"
|
|
|
|
|
2020-09-26 13:29:42 +02:00
|
|
|
EnvServiceAccount = envNamespace + "SERVICE_ACCOUNT"
|
|
|
|
EnvProject = envNamespace + "PROJECT"
|
2024-01-18 23:30:20 +02:00
|
|
|
EnvZoneID = envNamespace + "ZONE_ID"
|
2020-09-26 13:29:42 +02:00
|
|
|
EnvAllowPrivateZone = envNamespace + "ALLOW_PRIVATE_ZONE"
|
|
|
|
EnvDebug = envNamespace + "DEBUG"
|
2020-03-12 00:51:10 +02:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
2018-12-22 01:53:05 +02:00
|
|
|
Debug bool
|
2018-09-15 19:07:24 +02:00
|
|
|
Project string
|
2024-01-18 23:30:20 +02:00
|
|
|
ZoneID string
|
2020-09-26 13:29:42 +02:00
|
|
|
AllowPrivateZone bool
|
2018-09-15 19:07:24 +02:00
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
Debug: env.GetOrDefaultBool(EnvDebug, false),
|
2024-01-18 23:30:20 +02:00
|
|
|
ZoneID: env.GetOrDefaultString(EnvZoneID, ""),
|
2020-09-26 13:29:42 +02:00
|
|
|
AllowPrivateZone: env.GetOrDefaultBool(EnvAllowPrivateZone, false),
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 180*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
|
2018-09-15 19:07:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-18 17:22:33 +02:00
|
|
|
type DNSProvider struct {
|
2018-09-15 19:07:24 +02:00
|
|
|
config *Config
|
|
|
|
client *dns.Service
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Google Cloud DNS.
|
2020-01-04 18:18:34 +02:00
|
|
|
// By default, the project name is auto-detected by using the metadata service,
|
|
|
|
// it can be overridden using the GCE_PROJECT environment variable.
|
2019-02-01 14:14:57 +02:00
|
|
|
// A Service Account can be passed in the environment variable: GCE_SERVICE_ACCOUNT
|
2020-05-08 19:35:25 +02:00
|
|
|
// or by specifying the keyfile location: GCE_SERVICE_ACCOUNT_FILE.
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2019-01-08 15:39:12 +02:00
|
|
|
// Use a service account file if specified via environment variable.
|
2023-05-05 09:49:38 +02:00
|
|
|
if saKey := env.GetOrFile(EnvServiceAccount); saKey != "" {
|
2019-02-01 14:14:57 +02:00
|
|
|
return NewDNSProviderServiceAccountKey([]byte(saKey))
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2019-01-08 15:39:12 +02:00
|
|
|
// Use default credentials.
|
2024-09-14 14:26:25 +02:00
|
|
|
project := env.GetOrDefaultString(EnvProject, autodetectProjectID(context.Background()))
|
2016-03-17 22:59:15 +02:00
|
|
|
return NewDNSProviderCredentials(project)
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for Google Cloud DNS.
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProviderCredentials(project string) (*DNSProvider, error) {
|
2016-03-18 17:22:33 +02:00
|
|
|
if project == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("googlecloud: project name missing")
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope)
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to get Google Cloud client: %w", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Project = project
|
|
|
|
config.HTTPClient = client
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:14:57 +02:00
|
|
|
// NewDNSProviderServiceAccountKey uses the supplied service account JSON
|
2018-09-15 19:07:24 +02:00
|
|
|
// to return a DNSProvider instance configured for Google Cloud DNS.
|
2019-02-01 14:14:57 +02:00
|
|
|
func NewDNSProviderServiceAccountKey(saKey []byte) (*DNSProvider, error) {
|
|
|
|
if len(saKey) == 0 {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("googlecloud: Service Account is missing")
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-05-30 20:20:21 +02:00
|
|
|
|
2019-01-08 15:39:12 +02:00
|
|
|
// If GCE_PROJECT is non-empty it overrides the project in the service
|
|
|
|
// account file.
|
2020-03-12 00:51:10 +02:00
|
|
|
project := env.GetOrDefaultString(EnvProject, "")
|
2019-01-09 09:12:22 +02:00
|
|
|
if project == "" {
|
2019-01-08 15:39:12 +02:00
|
|
|
// read project id from service account file
|
|
|
|
var datJSON struct {
|
|
|
|
ProjectID string `json:"project_id"`
|
|
|
|
}
|
2019-02-01 14:14:57 +02:00
|
|
|
err := json.Unmarshal(saKey, &datJSON)
|
2019-01-08 15:39:12 +02:00
|
|
|
if err != nil || datJSON.ProjectID == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("googlecloud: project ID not found in Google Cloud Service Account file")
|
2019-01-08 15:39:12 +02:00
|
|
|
}
|
|
|
|
project = datJSON.ProjectID
|
2018-05-30 20:20:21 +02:00
|
|
|
}
|
|
|
|
|
2019-02-01 14:14:57 +02:00
|
|
|
conf, err := google.JWTConfigFromJSON(saKey, dns.NdevClouddnsReadwriteScope)
|
2017-07-17 21:40:57 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to acquire config: %w", err)
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-05-30 19:53:04 +02:00
|
|
|
client := conf.Client(context.Background())
|
2017-07-17 21:40:57 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.Project = project
|
|
|
|
config.HTTPClient = client
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
2019-02-01 14:14:57 +02:00
|
|
|
// NewDNSProviderServiceAccount uses the supplied service account JSON file
|
|
|
|
// to return a DNSProvider instance configured for Google Cloud DNS.
|
|
|
|
func NewDNSProviderServiceAccount(saFile string) (*DNSProvider, error) {
|
|
|
|
if saFile == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("googlecloud: Service Account file missing")
|
2019-02-01 14:14:57 +02:00
|
|
|
}
|
|
|
|
|
2021-08-25 11:44:11 +02:00
|
|
|
saKey, err := os.ReadFile(saFile)
|
2019-02-01 14:14:57 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to read Service Account file: %w", err)
|
2019-02-01 14:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NewDNSProviderServiceAccountKey(saKey)
|
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Google Cloud DNS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("googlecloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
2019-04-12 20:37:29 +02:00
|
|
|
if config.HTTPClient == nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("googlecloud: unable to create Google Cloud DNS service: client is nil")
|
2019-04-12 20:37:29 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2019-04-12 20:37:29 +02:00
|
|
|
svc, err := dns.NewService(context.Background(), option.WithHTTPClient(config.HTTPClient))
|
2017-07-17 21:40:57 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to create Google Cloud DNS service: %w", err)
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: svc}, nil
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
|
|
|
|
2018-09-24 21:07:20 +02:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2016-03-18 17:22:33 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
zone, err := d.getHostedZone(info.EffectiveFQDN)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:28:50 +02:00
|
|
|
// Look for existing records.
|
2023-03-07 10:39:05 +02:00
|
|
|
existingRrSet, err := d.findTxtRecords(zone, info.EffectiveFQDN)
|
2018-09-21 17:28:50 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2018-09-21 17:28:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
for _, rrSet := range existingRrSet {
|
|
|
|
var rrd []string
|
|
|
|
for _, rr := range rrSet.Rrdatas {
|
|
|
|
data := mustUnquote(rr)
|
|
|
|
rrd = append(rrd, data)
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
if data == info.Value {
|
|
|
|
log.Printf("skip: the record already exists: %s", info.Value)
|
2018-12-22 01:53:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rrSet.Rrdatas = rrd
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to delete the existing records before adding the new one.
|
|
|
|
if len(existingRrSet) > 0 {
|
|
|
|
if err = d.applyChanges(zone, &dns.Change{Deletions: existingRrSet}); err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2018-12-22 01:53:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:22:33 +02:00
|
|
|
rec := &dns.ResourceRecordSet{
|
2023-03-07 10:39:05 +02:00
|
|
|
Name: info.EffectiveFQDN,
|
|
|
|
Rrdatas: []string{info.Value},
|
2018-09-15 19:07:24 +02:00
|
|
|
Ttl: int64(d.config.TTL),
|
2016-03-18 17:22:33 +02:00
|
|
|
Type: "TXT",
|
|
|
|
}
|
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
// Append existing TXT record data to the new TXT record data
|
|
|
|
for _, rrSet := range existingRrSet {
|
|
|
|
for _, rr := range rrSet.Rrdatas {
|
2023-03-07 10:39:05 +02:00
|
|
|
if rr != info.Value {
|
2019-01-02 21:45:17 +02:00
|
|
|
rec.Rrdatas = append(rec.Rrdatas, rr)
|
2018-12-22 01:53:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 17:28:50 +02:00
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
change := &dns.Change{
|
|
|
|
Additions: []*dns.ResourceRecordSet{rec},
|
|
|
|
}
|
2018-09-21 17:28:50 +02:00
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
if err = d.applyChanges(zone, change); err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2016-11-02 16:47:17 +02:00
|
|
|
}
|
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *DNSProvider) applyChanges(zone string, change *dns.Change) error {
|
|
|
|
if d.config.Debug {
|
|
|
|
data, _ := json.Marshal(change)
|
|
|
|
log.Printf("change (Create): %s", string(data))
|
|
|
|
}
|
2018-09-21 17:28:50 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
chg, err := d.client.Changes.Create(d.config.Project, zone, change).Do()
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2020-10-27 13:01:05 +02:00
|
|
|
var v *googleapi.Error
|
|
|
|
if errors.As(err, &v) && v.Code == http.StatusNotFound {
|
|
|
|
return nil
|
2018-12-22 01:53:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
data, _ := json.Marshal(change)
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("failed to perform changes [zone %s, change %s]: %w", zone, string(data), err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 21:45:17 +02:00
|
|
|
if chg.Status == changeStatusDone {
|
2018-12-22 01:53:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
chgID := chg.Id
|
|
|
|
|
2016-03-18 17:22:33 +02:00
|
|
|
// wait for change to be acknowledged
|
2018-12-22 01:53:05 +02:00
|
|
|
return wait.For("apply change", 30*time.Second, 3*time.Second, func() (bool, error) {
|
|
|
|
if d.config.Debug {
|
|
|
|
data, _ := json.Marshal(change)
|
|
|
|
log.Printf("change (Get): %s", string(data))
|
|
|
|
}
|
2016-03-18 17:22:33 +02:00
|
|
|
|
2018-12-22 01:53:05 +02:00
|
|
|
chg, err = d.client.Changes.Get(d.config.Project, zone, chgID).Do()
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-12-22 01:53:05 +02:00
|
|
|
data, _ := json.Marshal(change)
|
2020-02-27 20:14:46 +02:00
|
|
|
return false, fmt.Errorf("failed to get changes [zone %s, change %s]: %w", zone, string(data), err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-01-02 21:45:17 +02:00
|
|
|
if chg.Status == changeStatusDone {
|
2018-12-22 01:53:05 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, fmt.Errorf("status: %s", chg.Status)
|
|
|
|
})
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2016-03-18 17:22:33 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
zone, err := d.getHostedZone(info.EffectiveFQDN)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
records, err := d.findTxtRecords(zone, info.EffectiveFQDN)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 12:02:03 +02:00
|
|
|
if len(records) == 0 {
|
|
|
|
return nil
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
2018-07-14 12:02:03 +02:00
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
_, err = d.client.Changes.Create(d.config.Project, zone, &dns.Change{Deletions: records}).Do()
|
2018-09-20 23:18:13 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return fmt.Errorf("googlecloud: %w", err)
|
2018-09-20 23:18:13 +02:00
|
|
|
}
|
|
|
|
return nil
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2016-03-19 18:53:16 +02:00
|
|
|
// Timeout customizes the timeout values used by the ACME package for checking
|
|
|
|
// DNS record validity.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 19:07:24 +02:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// getHostedZone returns the managed-zone.
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
|
2024-01-18 23:30:20 +02:00
|
|
|
authZone, zones, err := d.lookupHostedZoneID(domain)
|
2016-08-06 05:41:24 +02:00
|
|
|
if err != nil {
|
2024-01-18 23:30:20 +02:00
|
|
|
return "", err
|
2016-08-06 05:41:24 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 23:30:20 +02:00
|
|
|
if len(zones) == 0 {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", fmt.Errorf("no matching domain found for domain %s", authZone)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 23:30:20 +02:00
|
|
|
for _, z := range zones {
|
2020-09-26 13:29:42 +02:00
|
|
|
if z.Visibility == "public" || z.Visibility == "" || (z.Visibility == "private" && d.config.AllowPrivateZone) {
|
2019-04-12 20:37:29 +02:00
|
|
|
return z.Name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-26 13:29:42 +02:00
|
|
|
if d.config.AllowPrivateZone {
|
|
|
|
return "", fmt.Errorf("no public or private zone found for domain %s", authZone)
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:37:29 +02:00
|
|
|
return "", fmt.Errorf("no public zone found for domain %s", authZone)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2024-01-18 23:30:20 +02:00
|
|
|
// lookupHostedZoneID finds the managed zone ID in Google.
|
|
|
|
//
|
|
|
|
// Be careful here.
|
|
|
|
// An automated system might run in a GCloud Service Account, with access to edit the zone
|
|
|
|
//
|
|
|
|
// (gcloud dns managed-zones get-iam-policy $zone_id) (role roles/dns.admin)
|
|
|
|
//
|
|
|
|
// but not with project-wide access to list all zones
|
|
|
|
//
|
|
|
|
// (gcloud projects get-iam-policy $project_id) (a role with permission dns.managedZones.list)
|
|
|
|
//
|
|
|
|
// If we force a zone list to succeed, we demand more permissions than needed.
|
|
|
|
func (d *DNSProvider) lookupHostedZoneID(domain string) (string, []*dns.ManagedZone, error) {
|
|
|
|
// GCE_ZONE_ID override for service accounts to avoid needing zones-list permission
|
|
|
|
if d.config.ZoneID != "" {
|
|
|
|
zone, err := d.client.ManagedZones.Get(d.config.Project, d.config.ZoneID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("API call ManagedZones.Get for explicit zone ID %q in project %q failed: %w", d.config.ZoneID, d.config.Project, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zone.DnsName, []*dns.ManagedZone{zone}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
|
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return "", nil, fmt.Errorf("could not find zone: %w", err)
|
2024-01-18 23:30:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zones, err := d.client.ManagedZones.
|
|
|
|
List(d.config.Project).
|
|
|
|
DnsName(authZone).
|
|
|
|
Do()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, fmt.Errorf("API call ManagedZones.List failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return authZone, zones.ManagedZones, nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) findTxtRecords(zone, fqdn string) ([]*dns.ResourceRecordSet, error) {
|
2018-09-15 19:07:24 +02:00
|
|
|
recs, err := d.client.ResourceRecordSets.List(d.config.Project, zone).Name(fqdn).Type("TXT").Do()
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-14 12:02:03 +02:00
|
|
|
return recs.Rrsets, nil
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
2018-12-22 01:53:05 +02:00
|
|
|
|
|
|
|
func mustUnquote(raw string) string {
|
|
|
|
clean, err := strconv.Unquote(raw)
|
|
|
|
if err != nil {
|
|
|
|
return raw
|
|
|
|
}
|
|
|
|
return clean
|
|
|
|
}
|
2020-01-04 18:18:34 +02:00
|
|
|
|
2024-09-14 14:26:25 +02:00
|
|
|
func autodetectProjectID(ctx context.Context) string {
|
|
|
|
if pid, err := metadata.ProjectIDWithContext(ctx); err == nil {
|
2020-01-04 18:18:34 +02:00
|
|
|
return pid
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|