2018-12-06 21:50:17 +00:00
|
|
|
// Package namecheap implements a DNS provider for solving the DNS-01 challenge using namecheap DNS.
|
2019-03-11 16:56:48 +00:00
|
|
|
package namecheap
|
2016-03-14 21:28:40 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2016-03-14 21:28:40 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2018-09-15 17:07:24 +00:00
|
|
|
"strconv"
|
2016-03-14 21:28:40 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/log"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2023-05-05 07:49:38 +00:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/namecheap/internal"
|
2020-01-18 01:25:50 +00:00
|
|
|
"golang.org/x/net/publicsuffix"
|
2016-03-14 21:28:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Notes about namecheap's tool API:
|
2023-07-29 10:59:24 +00:00
|
|
|
// 1. Using the API requires registration.
|
|
|
|
// Once registered, use your account name and API key to access the API.
|
|
|
|
// 2. There is no API to add or modify a single DNS record.
|
|
|
|
// Instead, you must read the entire list of records, make modifications,
|
|
|
|
// and then write the entire updated list of records. (Yuck.)
|
|
|
|
// 3. Namecheap's DNS updates can be slow to propagate.
|
|
|
|
// I've seen them take as long as an hour.
|
|
|
|
// 4. Namecheap requires you to whitelist the IP address from which you call its APIs.
|
|
|
|
// It also requires all API calls to include the whitelisted IP address as a form or query string value.
|
|
|
|
// This code uses a namecheap service to query the client's IP address.
|
2016-03-14 21:28:40 +00:00
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "NAMECHEAP_"
|
|
|
|
|
|
|
|
EnvAPIUser = envNamespace + "API_USER"
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvSandbox = envNamespace + "SANDBOX"
|
|
|
|
EnvDebug = envNamespace + "DEBUG"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// A challenge represents all the data needed to specify a dns-01 challenge to lets-encrypt.
|
2018-09-15 17:07:24 +00:00
|
|
|
type challenge struct {
|
|
|
|
domain string
|
|
|
|
key string
|
|
|
|
keyFqdn string
|
|
|
|
keyValue string
|
|
|
|
tld string
|
|
|
|
sld string
|
|
|
|
host string
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
// newChallenge builds a challenge record from a domain name and a challenge authentication key.
|
|
|
|
func newChallenge(domain, keyAuth string) (*challenge, error) {
|
|
|
|
domain = dns01.UnFqdn(domain)
|
|
|
|
|
|
|
|
tld, _ := publicsuffix.PublicSuffix(domain)
|
|
|
|
if tld == domain {
|
|
|
|
return nil, fmt.Errorf("invalid domain name %q", domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(domain, ".")
|
|
|
|
longest := len(parts) - strings.Count(tld, ".") - 1
|
|
|
|
sld := parts[longest-1]
|
|
|
|
|
|
|
|
var host string
|
|
|
|
if longest >= 1 {
|
|
|
|
host = strings.Join(parts[:longest-1], ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
|
|
|
|
|
|
|
return &challenge{
|
|
|
|
domain: domain,
|
|
|
|
key: "_acme-challenge." + host,
|
|
|
|
keyFqdn: info.EffectiveFQDN,
|
|
|
|
keyValue: info.Value,
|
|
|
|
tld: tld,
|
|
|
|
sld: sld,
|
|
|
|
host: host,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
Debug bool
|
|
|
|
BaseURL string
|
|
|
|
APIUser string
|
|
|
|
APIKey string
|
|
|
|
ClientIP string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
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 {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL := internal.DefaultBaseURL
|
2020-03-11 22:51:10 +00:00
|
|
|
if env.GetOrDefaultBool(EnvSandbox, false) {
|
2023-05-05 07:49:38 +00:00
|
|
|
baseURL = internal.SandboxBaseURL
|
2020-01-12 15:51:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return &Config{
|
2020-01-12 15:51:14 +00:00
|
|
|
BaseURL: baseURL,
|
2020-03-11 22:51:10 +00:00
|
|
|
Debug: env.GetOrDefaultBool(EnvDebug, false),
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 15*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, 60*time.Second),
|
2018-09-15 17:07:24 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2016-03-14 21:28:40 +00:00
|
|
|
type DNSProvider struct {
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2023-05-05 07:49:38 +00:00
|
|
|
client *internal.Client
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-17 20:59:15 +00:00
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for namecheap.
|
2018-09-15 17:07:24 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// NAMECHEAP_API_USER and NAMECHEAP_API_KEY.
|
2016-03-17 20:59:15 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIUser, EnvAPIKey)
|
2018-06-11 15:32:50 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("namecheap: %w", err)
|
2018-06-11 15:32:50 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIUser = values[EnvAPIUser]
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2016-03-17 20:59:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:40:41 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Namecheap.
|
2018-09-15 17:07:24 +00:00
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("namecheap: the configuration of the DNS provider is nil")
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.APIUser == "" || config.APIKey == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("namecheap: credentials missing")
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
2018-06-21 17:06:16 +00:00
|
|
|
|
2021-03-04 19:16:59 +00:00
|
|
|
if config.ClientIP == "" {
|
2023-05-05 07:49:38 +00:00
|
|
|
clientIP, err := internal.GetClientIP(context.Background(), config.HTTPClient, config.Debug)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
config.ClientIP = clientIP
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
client := internal.NewClient(config.APIUser, config.APIKey, config.ClientIP)
|
|
|
|
client.BaseURL = config.BaseURL
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, nil
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Namecheap can sometimes take a long time to complete an update, so wait up to 60 minutes for the update to propagate.
|
2016-03-14 21:28:40 +00:00
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 17:07:24 +00:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// Present installs a TXT record for the DNS challenge.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2020-01-18 01:25:50 +00:00
|
|
|
ch, err := newChallenge(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
records, err := d.client.GetHosts(ctx, ch.sld, ch.tld)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
record := internal.Record{
|
2018-10-09 16:16:05 +00:00
|
|
|
Name: ch.key,
|
|
|
|
Type: "TXT",
|
|
|
|
Address: ch.keyValue,
|
|
|
|
MXPref: "10",
|
|
|
|
TTL: strconv.Itoa(d.config.TTL),
|
|
|
|
}
|
|
|
|
|
|
|
|
records = append(records, record)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
|
|
|
if d.config.Debug {
|
2018-10-09 16:16:05 +00:00
|
|
|
for _, h := range records {
|
|
|
|
log.Printf("%-5.5s %-30.30s %-6s %-70.70s", h.Type, h.Name, h.TTL, h.Address)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.SetHosts(ctx, ch.sld, ch.tld, records)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// CleanUp removes a TXT record used for a previous DNS challenge.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2022-11-25 17:12:21 +00:00
|
|
|
// TODO(ldez) replace domain by FQDN to follow CNAME.
|
2020-01-18 01:25:50 +00:00
|
|
|
ch, err := newChallenge(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
records, err := d.client.GetHosts(ctx, ch.sld, ch.tld)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:16:05 +00:00
|
|
|
// Find the challenge TXT record and remove it if found.
|
|
|
|
var found bool
|
2023-05-05 07:49:38 +00:00
|
|
|
var newRecords []internal.Record
|
2019-01-24 20:40:44 +00:00
|
|
|
for _, h := range records {
|
2018-10-09 16:16:05 +00:00
|
|
|
if h.Name == ch.key && h.Type == "TXT" {
|
|
|
|
found = true
|
2019-01-24 20:40:44 +00:00
|
|
|
} else {
|
|
|
|
newRecords = append(newRecords, h)
|
2018-10-09 16:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2018-09-15 17:07:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.client.SetHosts(ctx, ch.sld, ch.tld, newRecords)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("namecheap: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
2016-03-14 21:28:40 +00:00
|
|
|
}
|