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
|
|
|
|
|
2018-09-15 17:07:24 +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-09 16:22:45 +00:00
|
|
|
TTL: env.GetOrDefaultInt("NAMECOM_TTL", minTTL),
|
2018-12-06 21:50:17 +00:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond("NAMECOM_PROPAGATION_TIMEOUT", 15*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond("NAMECOM_POLLING_INTERVAL", 20*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond("NAMECOM_HTTP_TIMEOUT", 10*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:43:09 +00:00
|
|
|
// DNSProvider is an implementation of the acme.ChallengeProvider interface.
|
|
|
|
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:
|
|
|
|
// NAMECOM_USERNAME and NAMECOM_API_TOKEN
|
2018-03-14 17:43:09 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2018-06-11 15:32:50 +00:00
|
|
|
values, err := env.Get("NAMECOM_USERNAME", "NAMECOM_API_TOKEN")
|
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil, fmt.Errorf("namedotcom: %v", 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()
|
|
|
|
config.Username = values["NAMECOM_USERNAME"]
|
|
|
|
config.APIToken = values["NAMECOM_API_TOKEN"]
|
2018-10-06 13:33:15 +00:00
|
|
|
config.Server = env.GetOrFile("NAMECOM_SERVER")
|
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 == "" {
|
|
|
|
return nil, fmt.Errorf("namedotcom: username is required")
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
if config.APIToken == "" {
|
|
|
|
return nil, fmt.Errorf("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
|
|
|
|
|
|
|
request := &namecom.Record{
|
|
|
|
DomainName: domain,
|
2018-06-21 17:06:16 +00:00
|
|
|
Host: d.extractRecordName(fqdn, domain),
|
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,
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:06:16 +00:00
|
|
|
_, err := d.client.CreateRecord(request)
|
2018-03-14 17:43:09 +00:00
|
|
|
if err != nil {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("namedotcom: API call failed: %v", 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 {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("namedotcom: %v", 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 {
|
2018-09-15 17:07:24 +00:00
|
|
|
return fmt.Errorf("namedotcom: %v", 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
|
|
|
|
}
|