2018-05-30 20:20:21 +02:00
|
|
|
// Package gcloud implements a DNS provider for solving the DNS-01
|
2016-03-19 20:20:01 +02:00
|
|
|
// 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"
|
2017-07-17 21:40:57 +02:00
|
|
|
"io/ioutil"
|
2018-09-15 19:07:24 +02:00
|
|
|
"net/http"
|
2016-03-18 17:22:33 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-09-15 19:07:24 +02:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
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-09-15 19:07:24 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
Project string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt("GCE_TTL", 120),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("GCE_PROPAGATION_TIMEOUT", 180*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("GCE_POLLING_INTERVAL", 5*time.Second),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:22:33 +02:00
|
|
|
// DNSProvider is an implementation of the DNSProvider interface.
|
|
|
|
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.
|
|
|
|
// Project name must be passed in the environment variable: GCE_PROJECT.
|
|
|
|
// A Service Account file can be passed in the environment variable: GCE_SERVICE_ACCOUNT_FILE
|
2016-03-17 22:59:15 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2017-07-17 21:40:57 +02:00
|
|
|
if saFile, ok := os.LookupEnv("GCE_SERVICE_ACCOUNT_FILE"); ok {
|
2018-05-30 20:20:21 +02:00
|
|
|
return NewDNSProviderServiceAccount(saFile)
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-06-11 17:32:50 +02:00
|
|
|
|
2018-05-30 20:20:21 +02:00
|
|
|
project := os.Getenv("GCE_PROJECT")
|
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 == "" {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: project name missing")
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
client, err := google.DefaultClient(context.Background(), dns.NdevClouddnsReadwriteScope)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to get Google Cloud client: %v", 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
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderServiceAccount uses the supplied service account JSON file
|
|
|
|
// to return a DNSProvider instance configured for Google Cloud DNS.
|
2018-05-30 20:20:21 +02:00
|
|
|
func NewDNSProviderServiceAccount(saFile string) (*DNSProvider, error) {
|
2017-07-17 21:40:57 +02:00
|
|
|
if saFile == "" {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: Service Account file missing")
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dat, err := ioutil.ReadFile(saFile)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to read Service Account file: %v", err)
|
2017-07-17 21:40:57 +02:00
|
|
|
}
|
2018-05-30 20:20:21 +02:00
|
|
|
|
|
|
|
// read project id from service account file
|
|
|
|
var datJSON struct {
|
|
|
|
ProjectID string `json:"project_id"`
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(dat, &datJSON)
|
|
|
|
if err != nil || datJSON.ProjectID == "" {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: project ID not found in Google Cloud Service Account file")
|
2018-05-30 20:20:21 +02:00
|
|
|
}
|
|
|
|
project := datJSON.ProjectID
|
|
|
|
|
2017-07-17 21:40:57 +02:00
|
|
|
conf, err := google.JWTConfigFromJSON(dat, dns.NdevClouddnsReadwriteScope)
|
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to acquire config: %v", 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
svc, err := dns.New(config.HTTPClient)
|
2017-07-17 21:40:57 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return nil, fmt.Errorf("googlecloud: unable to create Google Cloud DNS service: %v", 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 {
|
2018-09-15 19:07:24 +02:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
2016-03-18 17:22:33 +02:00
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:28:50 +02:00
|
|
|
// Look for existing records.
|
|
|
|
existing, err := d.findTxtRecords(zone, fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-18 17:22:33 +02:00
|
|
|
rec := &dns.ResourceRecordSet{
|
|
|
|
Name: fqdn,
|
|
|
|
Rrdatas: []string{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-09-21 17:28:50 +02:00
|
|
|
change := &dns.Change{}
|
|
|
|
|
2018-07-14 12:02:03 +02:00
|
|
|
if len(existing) > 0 {
|
2016-11-02 16:47:17 +02:00
|
|
|
// Attempt to delete the existing records when adding our new one.
|
2018-07-14 12:02:03 +02:00
|
|
|
change.Deletions = existing
|
2018-09-21 17:28:50 +02:00
|
|
|
|
|
|
|
// Append existing TXT record data to the new TXT record data
|
|
|
|
for _, value := range existing {
|
|
|
|
rec.Rrdatas = append(rec.Rrdatas, value.Rrdatas...)
|
|
|
|
}
|
2016-11-02 16:47:17 +02:00
|
|
|
}
|
|
|
|
|
2018-09-21 17:28:50 +02:00
|
|
|
change.Additions = []*dns.ResourceRecordSet{rec}
|
|
|
|
|
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 {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// wait for change to be acknowledged
|
|
|
|
for chg.Status == "pending" {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
chg, err = d.client.Changes.Get(d.config.Project, zone, chg.Id).Do()
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-03-18 17:22:33 +02:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
records, err := d.findTxtRecords(zone, fqdn)
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return fmt.Errorf("googlecloud: %v", 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 {
|
|
|
|
return fmt.Errorf("googlecloud: %v", err)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// getHostedZone returns the managed-zone
|
2018-06-21 19:06:16 +02:00
|
|
|
func (d *DNSProvider) getHostedZone(domain string) (string, error) {
|
2016-08-06 05:41:24 +02:00
|
|
|
authZone, err := acme.FindZoneByFqdn(acme.ToFqdn(domain), acme.RecursiveNameservers)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:06:16 +02:00
|
|
|
zones, err := d.client.ManagedZones.
|
2018-09-15 19:07:24 +02:00
|
|
|
List(d.config.Project).
|
2016-08-06 05:41:24 +02:00
|
|
|
DnsName(authZone).
|
2016-07-21 21:05:16 +02:00
|
|
|
Do()
|
2016-03-18 17:22:33 +02:00
|
|
|
if err != nil {
|
2018-09-15 19:07:24 +02:00
|
|
|
return "", fmt.Errorf("API call failed: %v", err)
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 05:41:24 +02:00
|
|
|
if len(zones.ManagedZones) == 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
|
|
|
}
|
|
|
|
|
2016-08-06 05:41:24 +02:00
|
|
|
return zones.ManagedZones[0].Name, nil
|
2016-03-18 17:22:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|