2018-12-06 21:50:17 +00:00
|
|
|
// Package gandiv5 implements a DNS provider for solving the DNS-01 challenge using Gandi LiveDNS api.
|
2018-02-02 19:22:33 +00:00
|
|
|
package gandiv5
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2018-02-02 19:22:33 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2023-05-05 07:49:38 +00:00
|
|
|
"net/url"
|
2018-02-02 19:22:33 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
2023-09-20 03:42:25 +00:00
|
|
|
"github.com/go-acme/lego/v4/log"
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/gandiv5/internal"
|
2018-02-02 19:22:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Gandi API reference: http://doc.livedns.gandi.net/
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
const minTTL = 300
|
2018-02-02 19:22:33 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "GANDIV5_"
|
|
|
|
|
2023-09-20 03:42:25 +00:00
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvPersonalAccessToken = envNamespace + "PERSONAL_ACCESS_TOKEN"
|
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
|
|
|
// inProgressInfo contains information about an in-progress challenge.
|
2018-02-02 19:22:33 +00:00
|
|
|
type inProgressInfo struct {
|
|
|
|
fieldName string
|
|
|
|
authZone string
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-09-20 03:42:25 +00:00
|
|
|
BaseURL string
|
|
|
|
APIKey string // Deprecated use PersonalAccessToken
|
|
|
|
PersonalAccessToken string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
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{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 20*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-02-02 19:22:33 +00:00
|
|
|
type DNSProvider struct {
|
2023-05-05 07:49:38 +00:00
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
inProgressFQDNs map[string]inProgressInfo
|
|
|
|
inProgressMu sync.Mutex
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
// findZoneByFqdn determines the DNS zone of a FQDN.
|
|
|
|
// It is overridden during tests.
|
|
|
|
// only for testing purpose.
|
2018-12-06 21:50:17 +00:00
|
|
|
findZoneByFqdn func(fqdn string) (string, error)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Gandi.
|
|
|
|
// Credentials must be passed in the environment variable: GANDIV5_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2023-09-20 03:42:25 +00:00
|
|
|
// TODO(ldez): rewrite this when APIKey will be removed.
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2023-09-20 03:42:25 +00:00
|
|
|
config.APIKey = env.GetOrFile(EnvAPIKey)
|
|
|
|
config.PersonalAccessToken = env.GetOrFile(EnvPersonalAccessToken)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Gandi.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("gandiv5: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:42:25 +00:00
|
|
|
if config.APIKey != "" {
|
|
|
|
log.Print("gandiv5: API Key is deprecated, use Personal Access Token instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" && config.PersonalAccessToken == "" {
|
|
|
|
return nil, errors.New("gandiv5: credentials information are missing")
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-10-12 17:29:18 +00:00
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("gandiv5: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
2023-09-20 03:42:25 +00:00
|
|
|
client := internal.NewClient(config.APIKey, config.PersonalAccessToken)
|
2023-05-05 07:49:38 +00:00
|
|
|
|
|
|
|
if config.BaseURL != "" {
|
|
|
|
baseURL, err := url.Parse(config.BaseURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("gandiv5: %w", err)
|
|
|
|
}
|
|
|
|
client.BaseURL = baseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
return &DNSProvider{
|
2018-09-15 17:07:24 +00:00
|
|
|
config: config,
|
2023-05-05 07:49:38 +00:00
|
|
|
client: client,
|
2018-02-02 19:22:33 +00:00
|
|
|
inProgressFQDNs: make(map[string]inProgressInfo),
|
2018-12-06 21:50:17 +00:00
|
|
|
findZoneByFqdn: dns01.FindZoneByFqdn,
|
2018-02-02 19:22:33 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// find authZone
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := d.findZoneByFqdn(info.EffectiveFQDN)
|
2018-02-02 19:22:33 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("gandiv5: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// determine name of TXT record
|
2023-03-07 08:39:05 +00:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gandiv5: %w", err)
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// acquire lock and check there is not a challenge already in
|
|
|
|
// progress for this value of authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// add TXT record into authZone
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value, d.config.TTL)
|
2018-02-02 19:22:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// save data necessary for CleanUp
|
2023-03-07 08:39:05 +00:00
|
|
|
d.inProgressFQDNs[info.EffectiveFQDN] = inProgressInfo{
|
2018-02-02 19:22:33 +00:00
|
|
|
authZone: authZone,
|
2022-11-28 17:53:13 +00:00
|
|
|
fieldName: subDomain,
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// acquire lock and retrieve authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
2023-03-07 08:39:05 +00:00
|
|
|
if _, ok := d.inProgressFQDNs[info.EffectiveFQDN]; !ok {
|
2018-02-02 19:22:33 +00:00
|
|
|
// if there is no cleanup information then just return
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
fieldName := d.inProgressFQDNs[info.EffectiveFQDN].fieldName
|
|
|
|
authZone := d.inProgressFQDNs[info.EffectiveFQDN].authZone
|
|
|
|
delete(d.inProgressFQDNs, info.EffectiveFQDN)
|
2018-05-30 17:53:04 +00:00
|
|
|
|
2018-02-02 19:22:33 +00:00
|
|
|
// delete TXT record from authZone
|
2023-05-05 07:49:38 +00:00
|
|
|
err := d.client.DeleteTXTRecord(context.Background(), dns01.UnFqdn(authZone), fieldName)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("gandiv5: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
2018-02-02 19:22:33 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-02-02 19:22:33 +00:00
|
|
|
}
|