2018-12-06 21:50:17 +00:00
|
|
|
// Package exoscale implements a DNS provider for solving the DNS-01 challenge using exoscale DNS.
|
2019-03-11 16:56:48 +00:00
|
|
|
package exoscale
|
2016-11-07 07:37:57 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-18 09:49:41 +00:00
|
|
|
"context"
|
2016-11-07 07:37:57 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-09-15 17:07:24 +00:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2016-11-07 07:37:57 +00:00
|
|
|
|
2017-09-13 21:40:58 +00:00
|
|
|
"github.com/exoscale/egoscale"
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v3/platform/config/env"
|
2016-11-07 07:37:57 +00:00
|
|
|
)
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
const defaultBaseURL = "https://api.exoscale.com/dns"
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "EXOSCALE_"
|
|
|
|
|
|
|
|
EnvAPISecret = envNamespace + "API_SECRET"
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvEndpoint = envNamespace + "ENDPOINT"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
APISecret string
|
|
|
|
Endpoint string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-04-29 16:44:37 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-11-07 07:37:57 +00:00
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2016-11-07 07:37:57 +00:00
|
|
|
client *egoscale.Client
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// NewDNSProvider Credentials must be passed in the environment variables:
|
2016-11-07 07:37:57 +00:00
|
|
|
// EXOSCALE_API_KEY, EXOSCALE_API_SECRET, EXOSCALE_ENDPOINT.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey, EnvAPISecret)
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("exoscale: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
config.APISecret = values[EnvAPISecret]
|
|
|
|
config.Endpoint = env.GetOrFile(EnvEndpoint)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Exoscale.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" || config.APISecret == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("exoscale: credentials missing")
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
2018-06-11 15:32:50 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.Endpoint == "" {
|
|
|
|
config.Endpoint = defaultBaseURL
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
client := egoscale.NewClient(config.Endpoint, config.APIKey, config.APISecret)
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 19:07:20 +00:00
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2019-02-18 09:49:41 +00:00
|
|
|
ctx := context.Background()
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
recordID, err := d.FindExistingRecordID(zone, recordName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-15 20:58:24 +00:00
|
|
|
if recordID == 0 {
|
2018-09-08 10:09:59 +00:00
|
|
|
record := egoscale.DNSRecord{
|
|
|
|
Name: recordName,
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: d.config.TTL,
|
2018-09-08 10:09:59 +00:00
|
|
|
Content: value,
|
|
|
|
RecordType: "TXT",
|
|
|
|
}
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
_, err := d.client.CreateRecord(ctx, zone, record)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while creating DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
2018-09-08 10:09:59 +00:00
|
|
|
record := egoscale.UpdateDNSRecord{
|
|
|
|
ID: recordID,
|
|
|
|
Name: recordName,
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: d.config.TTL,
|
2018-09-08 10:09:59 +00:00
|
|
|
Content: value,
|
|
|
|
RecordType: "TXT",
|
|
|
|
}
|
|
|
|
|
2019-02-18 09:49:41 +00:00
|
|
|
_, err := d.client.UpdateRecord(ctx, zone, record)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while updating DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2019-02-18 09:49:41 +00:00
|
|
|
ctx := context.Background()
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-06-21 17:06:16 +00:00
|
|
|
zone, recordName, err := d.FindZoneAndRecordName(fqdn, domain)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
recordID, err := d.FindExistingRecordID(zone, recordName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-15 20:58:24 +00:00
|
|
|
if recordID != 0 {
|
2019-02-18 09:49:41 +00:00
|
|
|
err = d.client.DeleteRecord(ctx, zone, recordID)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Error while deleting DNS record: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|
|
|
|
|
2018-05-30 17:53:04 +00:00
|
|
|
// FindExistingRecordID Query Exoscale to find an existing record for this name.
|
2020-05-08 17:35:25 +00:00
|
|
|
// Returns nil if no record could be found.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) FindExistingRecordID(zone, recordName string) (int64, error) {
|
2019-02-18 09:49:41 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
records, err := d.client.GetRecords(ctx, zone)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, errors.New("Error while retrievening DNS records: " + err.Error())
|
|
|
|
}
|
2018-01-15 20:58:24 +00:00
|
|
|
for _, record := range records {
|
|
|
|
if record.Name == recordName {
|
|
|
|
return record.ID, nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// FindZoneAndRecordName Extract DNS zone and DNS entry name.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) FindZoneAndRecordName(fqdn, domain string) (string, string, error) {
|
2018-12-06 21:50:17 +00:00
|
|
|
zone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2018-12-06 21:50:17 +00:00
|
|
|
zone = dns01.UnFqdn(zone)
|
|
|
|
name := dns01.UnFqdn(fqdn)
|
2016-11-07 07:37:57 +00:00
|
|
|
name = name[:len(name)-len("."+zone)]
|
|
|
|
|
|
|
|
return zone, name, nil
|
|
|
|
}
|