2018-03-14 17:43:09 +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-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
|
|
|
"os"
|
|
|
|
"strings"
|
2018-09-15 17:07:24 +00:00
|
|
|
"time"
|
2018-03-14 17:43:09 +00:00
|
|
|
|
|
|
|
"github.com/namedotcom/go/namecom"
|
|
|
|
"github.com/xenolf/lego/acme"
|
2018-06-11 15:32:50 +00:00
|
|
|
"github.com/xenolf/lego/platform/config/env"
|
2018-03-14 17:43:09 +00:00
|
|
|
)
|
|
|
|
|
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{
|
|
|
|
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-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"]
|
|
|
|
config.Server = os.Getenv("NAMECOM_SERVER")
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-03-14 17:43:09 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderCredentials uses the supplied credentials
|
|
|
|
// to return a DNSProvider instance configured for namedotcom.
|
|
|
|
// Deprecated
|
2018-03-14 17:43:09 +00:00
|
|
|
func NewDNSProviderCredentials(username, apiToken, server string) (*DNSProvider, error) {
|
2018-09-15 17:07:24 +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-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-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-09-15 17:07:24 +00:00
|
|
|
fqdn, value, _ := acme.DNS01Record(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-03-14 17:43:09 +00:00
|
|
|
fqdn, _, _ := acme.DNS01Record(domain, keyAuth)
|
|
|
|
|
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-03-14 17:43:09 +00:00
|
|
|
name := acme.UnFqdn(fqdn)
|
|
|
|
if idx := strings.Index(name, "."+domain); idx != -1 {
|
|
|
|
return name[:idx]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|