2018-12-06 21:50:17 +00:00
|
|
|
// Package namedotcom implements a DNS provider for solving the DNS-01 challenge using Name.com's DNS service.
|
2019-03-11 16:56:48 +00:00
|
|
|
package namedotcom
|
2018-03-14 17:43:09 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2018-03-14 17:43:09 +00:00
|
|
|
"fmt"
|
2018-09-15 17:07:24 +00:00
|
|
|
"net/http"
|
2018-03-14 17:43:09 +00:00
|
|
|
"strings"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2019-07-30 19:19:32 +00:00
|
|
|
"github.com/go-acme/lego/v3/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v3/platform/config/env"
|
2018-03-14 17:43:09 +00:00
|
|
|
"github.com/namedotcom/go/namecom"
|
|
|
|
)
|
|
|
|
|
2018-10-09 16:22:45 +00:00
|
|
|
// according to https://www.name.com/api-docs/DNS#CreateRecord
|
|
|
|
const minTTL = 300
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "NAMECOM_"
|
|
|
|
|
|
|
|
EnvUsername = envNamespace + "USERNAME"
|
|
|
|
EnvAPIToken = envNamespace + "API_TOKEN"
|
|
|
|
EnvServer = envNamespace + "SERVER"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
Username string
|
|
|
|
APIToken string
|
|
|
|
Server string
|
|
|
|
TTL int
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
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, 15*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-03-14 17:43:09 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
client *namecom.NameCom
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for namedotcom.
|
2018-09-15 17:07:24 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
2020-05-08 17:35:25 +00:00
|
|
|
// NAMECOM_USERNAME and NAMECOM_API_TOKEN.
|
2018-03-14 17:43:09 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvUsername, EnvAPIToken)
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("namedotcom: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.APIToken = values[EnvAPIToken]
|
|
|
|
config.Server = env.GetOrFile(EnvServer)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +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 == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("namedotcom: username is required")
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
if config.APIToken == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("namedotcom: API token is required")
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:22:45 +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-15 17:07:24 +00:00
|
|
|
client := namecom.New(config.Username, config.APIToken)
|
|
|
|
client.Client = config.HTTPClient
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.Server != "" {
|
|
|
|
client.Server = config.Server
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2018-03-14 17:43:09 +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 {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2020-03-18 22:04:54 +00:00
|
|
|
domainDetails, err := d.client.GetDomain(&namecom.GetDomainRequest{DomainName: domain})
|
|
|
|
if err != nil {
|
2020-04-19 13:08:06 +00:00
|
|
|
return fmt.Errorf("namedotcom API call failed: %w", err)
|
2020-03-18 22:04:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 17:43:09 +00:00
|
|
|
request := &namecom.Record{
|
|
|
|
DomainName: domain,
|
2020-03-18 22:04:54 +00:00
|
|
|
Host: d.extractRecordName(fqdn, domainDetails.DomainName),
|
2018-03-14 17:43:09 +00:00
|
|
|
Type: "TXT",
|
2018-09-15 17:07:24 +00:00
|
|
|
TTL: uint32(d.config.TTL),
|
2018-03-14 17:43:09 +00:00
|
|
|
Answer: value,
|
|
|
|
}
|
|
|
|
|
2020-03-18 22:04:54 +00:00
|
|
|
_, err = d.client.CreateRecord(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namedotcom: API call failed: %w", err)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
records, err := d.getRecords(domain)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namedotcom: %w", err)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, rec := range records {
|
|
|
|
if rec.Fqdn == fqdn && rec.Type == "TXT" {
|
|
|
|
request := &namecom.DeleteRecordRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
ID: rec.ID,
|
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.DeleteRecord(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namedotcom: %w", err)
|
2018-03-14 17:43:09 +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
|
|
|
|
}
|
2018-03-14 17:43:09 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
func (d *DNSProvider) getRecords(domain string) ([]*namecom.Record, error) {
|
2018-03-14 17:43:09 +00:00
|
|
|
request := &namecom.ListRecordsRequest{
|
|
|
|
DomainName: domain,
|
|
|
|
Page: 1,
|
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
var records []*namecom.Record
|
2018-03-14 17:43:09 +00:00
|
|
|
for request.Page > 0 {
|
2018-09-15 17:07:24 +00:00
|
|
|
response, err := d.client.ListRecords(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, response.Records...)
|
|
|
|
request.Page = response.NextPage
|
|
|
|
}
|
|
|
|
|
|
|
|
return records, nil
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
func (d *DNSProvider) extractRecordName(fqdn, domain string) string {
|
2018-12-06 21:50:17 +00:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2018-03-14 17:43:09 +00:00
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|