2022-08-04 14:35:02 +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
|
|
|
"time"
|
2016-11-07 07:37:57 +00:00
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
egoscale "github.com/exoscale/egoscale/v2"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2016-11-07 07:37:57 +00:00
|
|
|
)
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
// Default Exoscale API endpoint.
|
|
|
|
const defaultBaseURL = "https://api.exoscale.com/v2"
|
|
|
|
|
|
|
|
// Default Exosacle API zone.
|
|
|
|
// Each data center location hosts the API and API zone determines which one to connect to.
|
|
|
|
const defaultAPIZone = "ch-gva-2"
|
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"
|
2022-08-04 14:35:02 +00:00
|
|
|
EnvAPIZone = envNamespace + "API_ZONE"
|
2020-03-11 22:51:10 +00:00
|
|
|
|
|
|
|
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
|
2022-08-04 14:35:02 +00:00
|
|
|
HTTPTimeout time.Duration
|
2018-09-15 17:07:24 +00:00
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
2022-08-04 14:35:02 +00:00
|
|
|
TTL int64
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
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{
|
2022-08-04 14:35:02 +00:00
|
|
|
TTL: int64(env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL)),
|
2020-03-11 22:51:10 +00:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2022-08-04 14:35:02 +00:00
|
|
|
HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*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 {
|
2022-08-04 14:35:02 +00:00
|
|
|
config *Config
|
|
|
|
client *egoscale.Client
|
|
|
|
apiZone string
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-04 14:35:02 +00:00
|
|
|
return nil, errors.New("exoscale: the configuration of the DNS provider is nil")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
client, err := egoscale.NewClient(
|
2021-04-20 10:08:06 +00:00
|
|
|
config.APIKey,
|
|
|
|
config.APISecret,
|
2022-08-04 14:35:02 +00:00
|
|
|
egoscale.ClientOptWithAPIEndpoint(config.Endpoint),
|
|
|
|
egoscale.ClientOptWithTimeout(config.HTTPTimeout),
|
2021-04-20 10:08:06 +00:00
|
|
|
)
|
2022-08-04 14:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("exoscale: initializing client: %w", err)
|
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
return &DNSProvider{
|
|
|
|
client: client,
|
|
|
|
config: config,
|
|
|
|
apiZone: env.GetOrDefaultString(EnvAPIZone, defaultAPIZone),
|
|
|
|
}, 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()
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-11-25 17:12:21 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
zoneName, recordName, err := d.findZoneAndRecordName(info.EffectiveFQDN)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("exoscale: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
zone, err := d.findExistingZone(zoneName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2022-08-04 14:35:02 +00:00
|
|
|
return fmt.Errorf("exoscale: %w", err)
|
|
|
|
}
|
|
|
|
if zone == nil {
|
|
|
|
return fmt.Errorf("exoscale: zone %q not found", zoneName)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
recordID, err := d.findExistingRecordID(deref(zone.ID), recordName)
|
2022-08-04 14:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exoscale: %w", err)
|
|
|
|
}
|
2018-09-08 10:09:59 +00:00
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
if recordID == "" {
|
|
|
|
record := egoscale.DNSDomainRecord{
|
2023-03-07 08:39:05 +00:00
|
|
|
Name: pointer(recordName),
|
|
|
|
TTL: pointer(d.config.TTL),
|
|
|
|
Content: pointer(info.Value),
|
|
|
|
Type: pointer("TXT"),
|
2018-09-08 10:09:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
_, err = d.client.CreateDNSDomainRecord(ctx, d.apiZone, deref(zone.ID), &record)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2022-08-04 14:35:02 +00:00
|
|
|
return fmt.Errorf("exoscale: error while creating DNS record: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
2022-08-04 14:35:02 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
record := egoscale.DNSDomainRecord{
|
2023-03-07 08:39:05 +00:00
|
|
|
ID: pointer(recordID),
|
|
|
|
Name: pointer(recordName),
|
|
|
|
TTL: pointer(d.config.TTL),
|
|
|
|
Content: pointer(info.Value),
|
|
|
|
Type: pointer("TXT"),
|
2022-08-04 14:35:02 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
err = d.client.UpdateDNSDomainRecord(ctx, d.apiZone, deref(zone.ID), &record)
|
2022-08-04 14:35:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exoscale: error while updating DNS record: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2022-11-25 17:12:21 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
zoneName, recordName, err := d.findZoneAndRecordName(info.EffectiveFQDN)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return fmt.Errorf("exoscale: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
zone, err := d.findExistingZone(zoneName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("exoscale: %w", err)
|
|
|
|
}
|
|
|
|
if zone == nil {
|
|
|
|
return fmt.Errorf("exoscale: zone %q not found", zoneName)
|
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
recordID, err := d.findExistingRecordID(deref(zone.ID), recordName)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
if recordID != "" {
|
2023-03-07 08:39:05 +00:00
|
|
|
err = d.client.DeleteDNSDomainRecord(ctx, d.apiZone, deref(zone.ID), &egoscale.DNSDomainRecord{ID: &recordID})
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2022-08-04 14:35:02 +00:00
|
|
|
return fmt.Errorf("exoscale: error while deleting DNS record: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
// findExistingZone Query Exoscale to find an existing zone for this name.
|
|
|
|
// Returns nil result if no zone could be found.
|
|
|
|
func (d *DNSProvider) findExistingZone(zoneName string) (*egoscale.DNSDomain, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
zones, err := d.client.ListDNSDomains(ctx, d.apiZone)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error while retrieving DNS zones: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, zone := range zones {
|
2023-03-07 08:39:05 +00:00
|
|
|
if zone.UnicodeName != nil && deref(zone.UnicodeName) == zoneName {
|
2022-08-04 14:35:02 +00:00
|
|
|
return &zone, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// findExistingRecordID Query Exoscale to find an existing record for this name.
|
|
|
|
// Returns empty result if no record could be found.
|
|
|
|
func (d *DNSProvider) findExistingRecordID(zoneID, recordName string) (string, error) {
|
2019-02-18 09:49:41 +00:00
|
|
|
ctx := context.Background()
|
2022-08-04 14:35:02 +00:00
|
|
|
|
|
|
|
records, err := d.client.ListDNSDomainRecords(ctx, d.apiZone, zoneID)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2022-08-04 14:35:02 +00:00
|
|
|
return "", fmt.Errorf("error while retrieving DNS records: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
2022-08-04 14:35:02 +00:00
|
|
|
|
2018-01-15 20:58:24 +00:00
|
|
|
for _, record := range records {
|
2023-03-07 08:39:05 +00:00
|
|
|
if deref(record.Name) == recordName &&
|
|
|
|
deref(record.Type) == "TXT" {
|
|
|
|
return deref(record.ID), nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-04 14:35:02 +00:00
|
|
|
|
|
|
|
return "", nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
|
|
|
|
2022-08-04 14:35:02 +00:00
|
|
|
// findZoneAndRecordName Extract DNS zone and DNS entry name.
|
2022-11-25 17:12:21 +00:00
|
|
|
func (d *DNSProvider) findZoneAndRecordName(fqdn string) (string, string, error) {
|
|
|
|
zone, err := dns01.FindZoneByFqdn(fqdn)
|
2016-11-07 07:37:57 +00:00
|
|
|
if err != nil {
|
2024-02-11 13:37:09 +00:00
|
|
|
return "", "", fmt.Errorf("could not find zone: %w", err)
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
2022-08-04 14:35:02 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
zone = dns01.UnFqdn(zone)
|
2022-08-04 14:35:02 +00:00
|
|
|
|
2022-11-28 17:53:13 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2016-11-07 07:37:57 +00:00
|
|
|
|
2022-11-28 17:53:13 +00:00
|
|
|
return zone, subDomain, nil
|
2016-11-07 07:37:57 +00:00
|
|
|
}
|
2023-03-07 08:39:05 +00:00
|
|
|
|
|
|
|
func pointer[T string | int | int32 | int64](v T) *T { return &v }
|
|
|
|
|
|
|
|
func deref[T string | int | int32 | int64](v *T) T {
|
|
|
|
if v == nil {
|
|
|
|
var zero T
|
|
|
|
return zero
|
|
|
|
}
|
|
|
|
|
|
|
|
return *v
|
|
|
|
}
|