2021-12-22 15:58:35 +00:00
|
|
|
// Package tencentcloud implements a DNS provider for solving the DNS-01 challenge using Tencent Cloud DNS.
|
|
|
|
package tencentcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-07-17 12:40:24 +00:00
|
|
|
"math"
|
2021-12-22 15:58:35 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
|
|
|
|
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
|
|
|
|
dnspod "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod/v20210323"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "TENCENTCLOUD_"
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
EnvSecretID = envNamespace + "SECRET_ID"
|
|
|
|
EnvSecretKey = envNamespace + "SECRET_KEY"
|
|
|
|
EnvRegion = envNamespace + "REGION"
|
|
|
|
EnvSessionToken = envNamespace + "SESSION_TOKEN"
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
2022-07-17 12:40:24 +00:00
|
|
|
SecretID string
|
|
|
|
SecretKey string
|
|
|
|
Region string
|
|
|
|
SessionToken string
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 600),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2022-07-17 12:40:24 +00:00
|
|
|
HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *dnspod.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Tencent Cloud DNS.
|
|
|
|
// Credentials must be passed in the environment variable: TENCENTCLOUD_SECRET_ID, TENCENTCLOUD_SECRET_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvSecretID, EnvSecretKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("tencentcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.SecretID = values[EnvSecretID]
|
|
|
|
config.SecretKey = values[EnvSecretKey]
|
|
|
|
config.Region = env.GetOrDefaultString(EnvRegion, "")
|
2022-07-17 12:40:24 +00:00
|
|
|
config.SessionToken = env.GetOrDefaultString(EnvSessionToken, "")
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Tencent Cloud DNS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("tencentcloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
var credential *common.Credential
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case config.SecretID != "" && config.SecretKey != "" && config.SessionToken != "":
|
|
|
|
credential = common.NewTokenCredential(config.SecretID, config.SecretKey, config.SessionToken)
|
|
|
|
case config.SecretID != "" && config.SecretKey != "":
|
|
|
|
credential = common.NewCredential(config.SecretID, config.SecretKey)
|
|
|
|
default:
|
2021-12-22 15:58:35 +00:00
|
|
|
return nil, errors.New("tencentcloud: credentials missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
cpf := profile.NewClientProfile()
|
|
|
|
cpf.HttpProfile.Endpoint = "dnspod.tencentcloudapi.com"
|
2022-07-17 12:40:24 +00:00
|
|
|
cpf.HttpProfile.ReqTimeout = int(math.Round(config.HTTPTimeout.Seconds()))
|
2021-12-22 15:58:35 +00:00
|
|
|
|
|
|
|
client, err := dnspod.NewClient(credential, config.Region, cpf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("tencentcloud: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record to fulfill the dns-01 challenge.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
return fmt.Errorf("tencentcloud: failed to get hosted zone: %w", err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
recordName, err := extractRecordName(fqdn, *zone.Name)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
return fmt.Errorf("tencentcloud: failed to extract record name: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := dnspod.NewCreateRecordRequest()
|
|
|
|
request.Domain = zone.Name
|
|
|
|
request.DomainId = zone.DomainId
|
|
|
|
request.SubDomain = common.StringPtr(recordName)
|
|
|
|
request.RecordType = common.StringPtr("TXT")
|
|
|
|
request.RecordLine = common.StringPtr("默认")
|
|
|
|
request.Value = common.StringPtr(value)
|
|
|
|
request.TTL = common.Uint64Ptr(uint64(d.config.TTL))
|
|
|
|
|
|
|
|
_, err = d.client.CreateRecord(request)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dnspod: API call failed: %w", err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
zone, err := d.getHostedZone(domain)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
return fmt.Errorf("tencentcloud: failed to get hosted zone: %w", err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
records, err := d.findTxtRecords(zone, fqdn)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
2022-07-17 12:40:24 +00:00
|
|
|
return fmt.Errorf("tencentcloud: failed to find TXT records: %w", err)
|
2021-12-22 15:58:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-17 12:40:24 +00:00
|
|
|
for _, record := range records {
|
|
|
|
request := dnspod.NewDeleteRecordRequest()
|
|
|
|
request.Domain = zone.Name
|
|
|
|
request.DomainId = zone.DomainId
|
|
|
|
request.RecordId = record.RecordId
|
|
|
|
|
|
|
|
_, err := d.client.DeleteRecord(request)
|
2021-12-22 15:58:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tencentcloud: delete record failed: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|