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