2018-02-12 17:10:05 +00:00
|
|
|
// Package gandiv5 implements a DNS provider for solving the DNS-01
|
|
|
|
// challenge using Gandi LiveDNS api.
|
|
|
|
package gandiv5
|
|
|
|
|
|
|
|
import (
|
2018-09-17 13:16:03 +00:00
|
|
|
"errors"
|
2018-02-12 17:10:05 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-05-31 07:30:04 +00:00
|
|
|
"github.com/xenolf/lego/acme"
|
2018-07-03 10:44:04 +00:00
|
|
|
"github.com/xenolf/lego/log"
|
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2018-02-12 17:10:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Gandi API reference: http://doc.livedns.gandi.net/
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
const (
|
|
|
|
// defaultBaseURL endpoint is the Gandi API endpoint used by Present and CleanUp.
|
|
|
|
defaultBaseURL = "https://dns.api.gandi.net/api/v5"
|
|
|
|
minTTL = 300
|
2018-02-12 17:10:05 +00:00
|
|
|
)
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// findZoneByFqdn determines the DNS zone of an fqdn.
|
|
|
|
// It is overridden during tests.
|
|
|
|
var findZoneByFqdn = acme.FindZoneByFqdn
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// inProgressInfo contains information about an in-progress challenge
|
|
|
|
type inProgressInfo struct {
|
|
|
|
fieldName string
|
|
|
|
authZone string
|
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider
|
|
|
|
type Config struct {
|
|
|
|
BaseURL string
|
|
|
|
APIKey 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("GANDIV5_TTL", minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("GANDIV5_PROPAGATION_TIMEOUT", 20*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("GANDIV5_POLLING_INTERVAL", 20*time.Second),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("GANDIV5_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// DNSProvider is an implementation of the
|
2018-05-31 07:30:04 +00:00
|
|
|
// acme.ChallengeProviderTimeout interface that uses Gandi's LiveDNS
|
2018-02-12 17:10:05 +00:00
|
|
|
// API to manage TXT records for a domain.
|
|
|
|
type DNSProvider struct {
|
2018-09-17 13:16:03 +00:00
|
|
|
config *Config
|
2018-02-12 17:10:05 +00:00
|
|
|
inProgressFQDNs map[string]inProgressInfo
|
|
|
|
inProgressMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Gandi.
|
|
|
|
// Credentials must be passed in the environment variable: GANDIV5_API_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-07-03 10:44:04 +00:00
|
|
|
values, err := env.Get("GANDIV5_API_KEY")
|
|
|
|
if err != nil {
|
2018-09-17 13:16:03 +00:00
|
|
|
return nil, fmt.Errorf("gandi: %v", err)
|
2018-07-03 10:44:04 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = values["GANDIV5_API_KEY"]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for Gandi.
|
|
|
|
// Deprecated
|
2018-02-12 17:10:05 +00:00
|
|
|
func NewDNSProviderCredentials(apiKey string) (*DNSProvider, error) {
|
2018-09-17 13:16:03 +00:00
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.APIKey = apiKey
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIKey == "" {
|
|
|
|
return nil, fmt.Errorf("gandiv5: no API Key given")
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
2018-09-17 13:16:03 +00:00
|
|
|
|
|
|
|
if config.BaseURL == "" {
|
|
|
|
config.BaseURL = defaultBaseURL
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:40:02 +00:00
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("gandiv5: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
return &DNSProvider{
|
2018-09-17 13:16:03 +00:00
|
|
|
config: config,
|
2018-02-12 17:10:05 +00:00
|
|
|
inProgressFQDNs: make(map[string]inProgressInfo),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2018-09-17 13:16:03 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// find authZone
|
2018-05-31 07:30:04 +00:00
|
|
|
authZone, err := findZoneByFqdn(fqdn, acme.RecursiveNameservers)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
2018-09-17 13:16:03 +00:00
|
|
|
return fmt.Errorf("gandiv5: findZoneByFqdn failure: %v", err)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// determine name of TXT record
|
|
|
|
if !strings.HasSuffix(
|
|
|
|
strings.ToLower(fqdn), strings.ToLower("."+authZone)) {
|
2018-09-17 13:16:03 +00:00
|
|
|
return fmt.Errorf("gandiv5: unexpected authZone %s for fqdn %s", authZone, fqdn)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
name := fqdn[:len(fqdn)-len("."+authZone)]
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +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-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// add TXT record into authZone
|
2018-09-17 13:16:03 +00:00
|
|
|
err = d.addTXTRecord(acme.UnFqdn(authZone), name, value, d.config.TTL)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// save data necessary for CleanUp
|
|
|
|
d.inProgressFQDNs[fqdn] = inProgressInfo{
|
|
|
|
authZone: authZone,
|
|
|
|
fieldName: name,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-05-31 07:30:04 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// acquire lock and retrieve authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
|
|
|
if _, ok := d.inProgressFQDNs[fqdn]; !ok {
|
|
|
|
// if there is no cleanup information then just return
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
fieldName := d.inProgressFQDNs[fqdn].fieldName
|
|
|
|
authZone := d.inProgressFQDNs[fqdn].authZone
|
|
|
|
delete(d.inProgressFQDNs, fqdn)
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-02-12 17:10:05 +00:00
|
|
|
// delete TXT record from authZone
|
2018-09-17 13:16:03 +00:00
|
|
|
err := d.deleteTXTRecord(acme.UnFqdn(authZone), fieldName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("gandiv5: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the values (20*time.Minute, 20*time.Second) which
|
|
|
|
// are used by the acme package as timeout and check interval values
|
|
|
|
// when checking for DNS record propagation with Gandi.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-17 13:16:03 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
// functions to perform API actions
|
2018-02-12 17:10:05 +00:00
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
func (d *DNSProvider) addTXTRecord(domain string, name string, value string, ttl int) error {
|
2018-10-10 14:28:04 +00:00
|
|
|
// Get exiting values for the TXT records
|
|
|
|
// Needed to create challenges for both wildcard and base name domains
|
|
|
|
txtRecord, err := d.getTXTRecord(domain, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := []string{value}
|
|
|
|
if len(txtRecord.RRSetValues) > 0 {
|
|
|
|
values = append(values, txtRecord.RRSetValues...)
|
|
|
|
}
|
2018-02-12 17:10:05 +00:00
|
|
|
|
2018-09-17 13:16:03 +00:00
|
|
|
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
|
2018-10-10 14:28:04 +00:00
|
|
|
|
|
|
|
newRecord := &Record{RRSetTTL: ttl, RRSetValues: values}
|
|
|
|
req, err := d.newRequest(http.MethodPut, target, newRecord)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &apiResponse{}
|
|
|
|
err = d.do(req, message)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create TXT record for domain %s and name %s: %v", domain, name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if message != nil && len(message.Message) > 0 {
|
|
|
|
log.Infof("API response: %s", message.Message)
|
2018-09-17 13:16:03 +00:00
|
|
|
}
|
2018-10-10 14:28:04 +00:00
|
|
|
|
|
|
|
return nil
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
func (d *DNSProvider) getTXTRecord(domain, name string) (*Record, error) {
|
|
|
|
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
|
2018-02-12 17:10:05 +00:00
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
// Get exiting values for the TXT records
|
|
|
|
// Needed to create challenges for both wildcard and base name domains
|
|
|
|
req, err := d.newRequest(http.MethodGet, target, nil)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
txtRecord := &Record{}
|
|
|
|
err = d.do(req, txtRecord)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
2018-10-10 14:28:04 +00:00
|
|
|
return nil, fmt.Errorf("unable to get TXT records for domain %s and name %s: %v", domain, name, err)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
2018-05-31 07:30:04 +00:00
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
return txtRecord, nil
|
|
|
|
}
|
2018-02-12 17:10:05 +00:00
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
func (d *DNSProvider) deleteTXTRecord(domain string, name string) error {
|
|
|
|
target := fmt.Sprintf("domains/%s/records/%s/TXT", domain, name)
|
|
|
|
|
|
|
|
req, err := d.newRequest(http.MethodDelete, target, nil)
|
2018-02-12 17:10:05 +00:00
|
|
|
if err != nil {
|
2018-10-10 14:28:04 +00:00
|
|
|
return err
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
message := &apiResponse{}
|
|
|
|
err = d.do(req, message)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to delete TXT record for domain %s and name %s: %v", domain, name, err)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
2018-09-17 13:16:03 +00:00
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
if message != nil && len(message.Message) > 0 {
|
|
|
|
log.Infof("API response: %s", message.Message)
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 14:28:04 +00:00
|
|
|
return nil
|
2018-02-12 17:10:05 +00:00
|
|
|
}
|