2018-12-06 21:50:17 +00:00
|
|
|
// Package nifcloud implements a DNS provider for solving the DNS-01 challenge using NIFCLOUD DNS.
|
2019-03-11 16:56:48 +00:00
|
|
|
package nifcloud
|
2018-07-01 00:13:22 +00:00
|
|
|
|
|
|
|
import (
|
2018-09-15 17:07:24 +00:00
|
|
|
"errors"
|
2018-07-01 00:13:22 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/platform/wait"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/nifcloud/internal"
|
2018-07-01 00:13:22 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "NIFCLOUD_"
|
|
|
|
|
|
|
|
EnvAccessKeyID = envNamespace + "ACCESS_KEY_ID"
|
|
|
|
EnvSecretAccessKey = envNamespace + "SECRET_ACCESS_KEY"
|
|
|
|
EnvDNSEndpoint = envNamespace + "DNS_ENDPOINT"
|
|
|
|
|
|
|
|
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 {
|
|
|
|
BaseURL string
|
|
|
|
AccessKey string
|
|
|
|
SecretKey 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 {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2018-09-15 17:07:24 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*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-07-01 00:13:22 +00:00
|
|
|
type DNSProvider struct {
|
2018-12-06 21:50:17 +00:00
|
|
|
client *internal.Client
|
2018-09-15 17:07:24 +00:00
|
|
|
config *Config
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for the NIFCLOUD DNS service.
|
2018-09-15 17:07:24 +00:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// NIFCLOUD_ACCESS_KEY_ID and NIFCLOUD_SECRET_ACCESS_KEY.
|
2018-07-01 00:13:22 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAccessKeyID, EnvSecretAccessKey)
|
2018-07-01 00:13:22 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("nifcloud: %w", err)
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.BaseURL = env.GetOrFile(EnvDNSEndpoint)
|
|
|
|
config.AccessKey = values[EnvAccessKeyID]
|
|
|
|
config.SecretKey = values[EnvSecretAccessKey]
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
return NewDNSProviderConfig(config)
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for NIFCLOUD.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("nifcloud: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
client, err := internal.NewClient(config.AccessKey, config.SecretKey)
|
2018-10-12 17:29:18 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("nifcloud: %w", err)
|
2018-10-12 17:29:18 +00:00
|
|
|
}
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2018-09-15 17:07:24 +00:00
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.BaseURL) > 0 {
|
|
|
|
client.BaseURL = config.BaseURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2018-07-01 00:13:22 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
err := d.changeRecord("CREATE", info.EffectiveFQDN, info.Value, d.config.TTL)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("nifcloud: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return err
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2018-07-01 00:13:22 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 17:07:24 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
err := d.changeRecord("DELETE", info.EffectiveFQDN, info.Value, d.config.TTL)
|
2018-09-15 17:07:24 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("nifcloud: %w", err)
|
2018-09-15 17:07:24 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 19:05:25 +00:00
|
|
|
func (d *DNSProvider) changeRecord(action, fqdn, value string, ttl int) error {
|
2018-12-06 21:50:17 +00:00
|
|
|
name := dns01.UnFqdn(fqdn)
|
2018-07-01 00:13:22 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
reqParams := internal.ChangeResourceRecordSetsRequest{
|
|
|
|
XMLNs: internal.XMLNs,
|
|
|
|
ChangeBatch: internal.ChangeBatch{
|
2018-07-01 00:13:22 +00:00
|
|
|
Comment: "Managed by Lego",
|
2018-12-06 21:50:17 +00:00
|
|
|
Changes: internal.Changes{
|
|
|
|
Change: []internal.Change{
|
2018-07-01 00:13:22 +00:00
|
|
|
{
|
|
|
|
Action: action,
|
2018-12-06 21:50:17 +00:00
|
|
|
ResourceRecordSet: internal.ResourceRecordSet{
|
2018-07-01 00:13:22 +00:00
|
|
|
Name: name,
|
|
|
|
Type: "TXT",
|
|
|
|
TTL: ttl,
|
2018-12-06 21:50:17 +00:00
|
|
|
ResourceRecords: internal.ResourceRecords{
|
|
|
|
ResourceRecord: []internal.ResourceRecord{
|
2018-07-01 00:13:22 +00:00
|
|
|
{
|
|
|
|
Value: value,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-08 19:05:25 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
2018-07-01 00:13:22 +00:00
|
|
|
if err != nil {
|
2021-04-08 19:05:25 +00:00
|
|
|
return fmt.Errorf("failed to find zone: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := d.client.ChangeResourceRecordSets(dns01.UnFqdn(authZone), reqParams)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to change record set: %w", err)
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
statusID := resp.ChangeInfo.ID
|
|
|
|
|
2018-12-21 23:53:05 +00:00
|
|
|
return wait.For("nifcloud", 120*time.Second, 4*time.Second, func() (bool, error) {
|
2018-07-01 00:13:22 +00:00
|
|
|
resp, err := d.client.GetChange(statusID)
|
|
|
|
if err != nil {
|
2021-04-08 19:05:25 +00:00
|
|
|
return false, fmt.Errorf("failed to query change status: %w", err)
|
2018-07-01 00:13:22 +00:00
|
|
|
}
|
|
|
|
return resp.ChangeInfo.Status == "INSYNC", nil
|
|
|
|
})
|
|
|
|
}
|