traefik/vendor/github.com/xenolf/lego/providers/dns/namedotcom/namedotcom.go

178 lines
4.8 KiB
Go
Raw Normal View History

// Package namedotcom implements a DNS provider for solving the DNS-01 challenge
// using Name.com's DNS service.
package namedotcom
import (
2018-09-17 13:16:03 +00:00
"errors"
"fmt"
2018-09-17 13:16:03 +00:00
"net/http"
"os"
"strings"
2018-09-17 13:16:03 +00:00
"time"
"github.com/namedotcom/go/namecom"
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/platform/config/env"
)
2018-09-17 13:16:03 +00:00
// Config is used to configure the creation of the DNSProvider
type Config struct {
Username string
APIToken string
Server string
TTL int
PropagationTimeout time.Duration
PollingInterval time.Duration
HTTPClient *http.Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt("NAMECOM_TTL", 120),
PropagationTimeout: env.GetOrDefaultSecond("NAMECOM_PROPAGATION_TIMEOUT", acme.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond("NAMECOM_POLLING_INTERVAL", acme.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond("NAMECOM_HTTP_TIMEOUT", 10*time.Second),
},
}
}
2018-05-31 07:30:04 +00:00
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
type DNSProvider struct {
client *namecom.NameCom
2018-09-17 13:16:03 +00:00
config *Config
}
// NewDNSProvider returns a DNSProvider instance configured for namedotcom.
2018-09-17 13:16:03 +00:00
// Credentials must be passed in the environment variables:
// NAMECOM_USERNAME and NAMECOM_API_TOKEN
func NewDNSProvider() (*DNSProvider, error) {
2018-07-03 10:44:04 +00:00
values, err := env.Get("NAMECOM_USERNAME", "NAMECOM_API_TOKEN")
if err != nil {
2018-09-17 13:16:03 +00:00
return nil, fmt.Errorf("namedotcom: %v", err)
2018-07-03 10:44:04 +00:00
}
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.Username = values["NAMECOM_USERNAME"]
config.APIToken = values["NAMECOM_API_TOKEN"]
config.Server = os.Getenv("NAMECOM_SERVER")
return NewDNSProviderConfig(config)
}
2018-09-17 13:16:03 +00:00
// NewDNSProviderCredentials uses the supplied credentials
// to return a DNSProvider instance configured for namedotcom.
// Deprecated
func NewDNSProviderCredentials(username, apiToken, server string) (*DNSProvider, error) {
2018-09-17 13:16:03 +00:00
config := NewDefaultConfig()
config.Username = username
config.APIToken = apiToken
config.Server = server
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for namedotcom.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("namedotcom: the configuration of the DNS provider is nil")
}
if config.Username == "" {
return nil, fmt.Errorf("namedotcom: username is required")
}
2018-09-17 13:16:03 +00:00
if config.APIToken == "" {
return nil, fmt.Errorf("namedotcom: API token is required")
}
2018-09-17 13:16:03 +00:00
client := namecom.New(config.Username, config.APIToken)
client.Client = config.HTTPClient
2018-09-17 13:16:03 +00:00
if config.Server != "" {
client.Server = config.Server
}
2018-09-17 13:16:03 +00:00
return &DNSProvider{client: client, config: config}, nil
}
// Present creates a TXT record to fulfil the dns-01 challenge.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2018-09-17 13:16:03 +00:00
fqdn, value, _ := acme.DNS01Record(domain, keyAuth)
request := &namecom.Record{
DomainName: domain,
2018-07-03 10:44:04 +00:00
Host: d.extractRecordName(fqdn, domain),
Type: "TXT",
2018-09-17 13:16:03 +00:00
TTL: uint32(d.config.TTL),
Answer: value,
}
2018-07-03 10:44:04 +00:00
_, err := d.client.CreateRecord(request)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("namedotcom: API call failed: %v", err)
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
2018-05-31 07:30:04 +00:00
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
2018-07-03 10:44:04 +00:00
records, err := d.getRecords(domain)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("namedotcom: %v", err)
}
for _, rec := range records {
if rec.Fqdn == fqdn && rec.Type == "TXT" {
request := &namecom.DeleteRecordRequest{
DomainName: domain,
ID: rec.ID,
}
2018-07-03 10:44:04 +00:00
_, err := d.client.DeleteRecord(request)
if err != nil {
2018-09-17 13:16:03 +00:00
return fmt.Errorf("namedotcom: %v", err)
}
}
}
return nil
}
2018-09-17 13:16:03 +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-09-17 13:16:03 +00:00
func (d *DNSProvider) getRecords(domain string) ([]*namecom.Record, error) {
request := &namecom.ListRecordsRequest{
DomainName: domain,
Page: 1,
}
2018-09-17 13:16:03 +00:00
var records []*namecom.Record
for request.Page > 0 {
2018-09-17 13:16:03 +00:00
response, err := d.client.ListRecords(request)
if err != nil {
return nil, err
}
records = append(records, response.Records...)
request.Page = response.NextPage
}
return records, nil
}
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
2018-05-31 07:30:04 +00:00
name := acme.UnFqdn(fqdn)
if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx]
}
return name
}