traefik/vendor/github.com/go-acme/lego/providers/dns/namedotcom/namedotcom.go

171 lines
4.6 KiB
Go
Raw Normal View History

2019-01-07 17:30:06 +00:00
// 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"
"strings"
2018-09-17 13:16:03 +00:00
"time"
2019-03-14 10:04:04 +00:00
"github.com/go-acme/lego/challenge/dns01"
"github.com/go-acme/lego/platform/config/env"
"github.com/namedotcom/go/namecom"
)
2018-10-10 14:28:04 +00:00
// according to https://www.name.com/api-docs/DNS#CreateRecord
const minTTL = 300
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{
2018-10-10 14:28:04 +00:00
TTL: env.GetOrDefaultInt("NAMECOM_TTL", minTTL),
2019-01-07 17:30:06 +00:00
PropagationTimeout: env.GetOrDefaultSecond("NAMECOM_PROPAGATION_TIMEOUT", 15*time.Minute),
PollingInterval: env.GetOrDefaultSecond("NAMECOM_POLLING_INTERVAL", 20*time.Second),
2018-09-17 13:16:03 +00:00
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"]
2018-10-10 14:28:04 +00:00
config.Server = env.GetOrFile("NAMECOM_SERVER")
2018-09-17 13:16:03 +00:00
return NewDNSProviderConfig(config)
}
2018-09-17 13:16:03 +00:00
// 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-10-10 14:28:04 +00:00
if config.TTL < minTTL {
return nil, fmt.Errorf("namedotcom: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
}
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
}
2018-10-10 14:28:04 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge.
2018-07-03 10:44:04 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
2019-01-07 17:30:06 +00:00
fqdn, value := dns01.GetRecord(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 {
2019-01-07 17:30:06 +00:00
fqdn, _ := dns01.GetRecord(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 {
2019-01-07 17:30:06 +00:00
name := dns01.UnFqdn(fqdn)
if idx := strings.Index(name, "."+domain); idx != -1 {
return name[:idx]
}
return name
}