lego/providers/dns/hostingde/hostingde.go

220 lines
5.8 KiB
Go
Raw Permalink Normal View History

// Package hostingde implements a DNS provider for solving the DNS-01 challenge using hosting.de.
2018-09-15 21:25:14 +00:00
package hostingde
import (
2023-05-05 07:49:38 +00:00
"context"
2018-09-15 21:25:14 +00:00
"errors"
"fmt"
"net/http"
"sync"
"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"
2023-09-20 01:40:26 +00:00
"github.com/go-acme/lego/v4/providers/dns/internal/hostingde"
2018-09-15 21:25:14 +00:00
)
// Environment variables names.
const (
envNamespace = "HOSTINGDE_"
EnvAPIKey = envNamespace + "API_KEY"
EnvZoneName = envNamespace + "ZONE_NAME"
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 21:25:14 +00:00
type Config struct {
APIKey string
ZoneName 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 21:25:14 +00:00
func NewDefaultConfig() *Config {
return &Config{
ZoneName: env.GetOrFile(EnvZoneName),
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 2*time.Minute),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 2*time.Second),
2018-09-15 21:25:14 +00:00
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
2018-09-15 21:25:14 +00:00
},
}
}
2020-05-08 17:35:25 +00:00
// DNSProvider implements the challenge.Provider interface.
2018-09-15 21:25:14 +00:00
type DNSProvider struct {
2023-05-05 07:49:38 +00:00
config *Config
2023-09-20 01:40:26 +00:00
client *hostingde.Client
2023-05-05 07:49:38 +00:00
2018-09-15 21:25:14 +00:00
recordIDs map[string]string
recordIDsMu sync.Mutex
}
// NewDNSProvider returns a DNSProvider instance configured for hosting.de.
// Credentials must be passed in the environment variables:
2020-05-08 17:35:25 +00:00
// HOSTINGDE_ZONE_NAME and HOSTINGDE_API_KEY.
2018-09-15 21:25:14 +00:00
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey)
2018-09-15 21:25:14 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("hostingde: %w", err)
2018-09-15 21:25:14 +00:00
}
config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
2018-09-15 21:25:14 +00:00
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for hosting.de.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("hostingde: the configuration of the DNS provider is nil")
}
if config.APIKey == "" {
return nil, errors.New("hostingde: API key missing")
}
return &DNSProvider{
config: config,
2023-09-20 01:40:26 +00:00
client: hostingde.NewClient(config.APIKey),
2018-09-15 21:25:14 +00:00
recordIDs: make(map[string]string),
}, 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
}
2020-05-08 17:35:25 +00:00
// Present creates a TXT record to fulfill the dns-01 challenge.
2018-09-15 21:25:14 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2018-09-15 21:25:14 +00:00
zoneName, err := d.getZoneName(info.EffectiveFQDN)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("hostingde: could not find zone for domain %q: %w", domain, err)
}
2023-05-05 07:49:38 +00:00
ctx := context.Background()
// get the ZoneConfig for that domain
2023-09-20 01:40:26 +00:00
zonesFind := hostingde.ZoneConfigsFindRequest{
Filter: hostingde.Filter{Field: "zoneName", Value: zoneName},
Limit: 1,
Page: 1,
}
2023-05-05 07:49:38 +00:00
zoneConfig, err := d.client.GetZone(ctx, zonesFind)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("hostingde: %w", err)
}
2023-05-05 07:49:38 +00:00
zoneConfig.Name = zoneName
2023-09-20 01:40:26 +00:00
rec := []hostingde.DNSRecord{{
2018-09-15 21:25:14 +00:00
Type: "TXT",
Name: dns01.UnFqdn(info.EffectiveFQDN),
Content: info.Value,
2018-09-15 21:25:14 +00:00
TTL: d.config.TTL,
}}
2023-09-20 01:40:26 +00:00
req := hostingde.ZoneUpdateRequest{
ZoneConfig: *zoneConfig,
2018-09-15 21:25:14 +00:00
RecordsToAdd: rec,
}
2023-05-05 07:49:38 +00:00
response, err := d.client.UpdateZone(ctx, req)
2018-09-15 21:25:14 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("hostingde: %w", err)
2018-09-15 21:25:14 +00:00
}
2023-05-05 07:49:38 +00:00
for _, record := range response.Records {
if record.Name == dns01.UnFqdn(info.EffectiveFQDN) && record.Content == fmt.Sprintf(`%q`, info.Value) {
2018-09-15 21:25:14 +00:00
d.recordIDsMu.Lock()
d.recordIDs[info.EffectiveFQDN] = record.ID
2018-09-15 21:25:14 +00:00
d.recordIDsMu.Unlock()
}
}
if d.recordIDs[info.EffectiveFQDN] == "" {
2018-09-15 21:25:14 +00:00
return fmt.Errorf("hostingde: error getting ID of just created record, for domain %s", domain)
}
return nil
}
2020-05-08 17:35:25 +00:00
// CleanUp removes the TXT record matching the specified parameters.
2018-09-15 21:25:14 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2018-09-15 21:25:14 +00:00
zoneName, err := d.getZoneName(info.EffectiveFQDN)
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("hostingde: could not find zone for domain %q: %w", domain, err)
}
2023-05-05 07:49:38 +00:00
ctx := context.Background()
2018-09-15 21:25:14 +00:00
// get the ZoneConfig for that domain
2023-09-20 01:40:26 +00:00
zonesFind := hostingde.ZoneConfigsFindRequest{
Filter: hostingde.Filter{Field: "zoneName", Value: zoneName},
Limit: 1,
Page: 1,
}
2023-05-05 07:49:38 +00:00
zoneConfig, err := d.client.GetZone(ctx, zonesFind)
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("hostingde: %w", err)
}
zoneConfig.Name = zoneName
2023-09-20 01:40:26 +00:00
rec := []hostingde.DNSRecord{{
2023-05-05 07:49:38 +00:00
Type: "TXT",
Name: dns01.UnFqdn(info.EffectiveFQDN),
Content: `"` + info.Value + `"`,
}}
2023-09-20 01:40:26 +00:00
req := hostingde.ZoneUpdateRequest{
ZoneConfig: *zoneConfig,
2018-09-15 21:25:14 +00:00
RecordsToDelete: rec,
}
// Delete record ID from map
d.recordIDsMu.Lock()
delete(d.recordIDs, info.EffectiveFQDN)
2018-09-15 21:25:14 +00:00
d.recordIDsMu.Unlock()
2023-05-05 07:49:38 +00:00
_, err = d.client.UpdateZone(ctx, req)
2018-09-15 21:25:14 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("hostingde: %w", err)
2018-09-15 21:25:14 +00:00
}
return nil
}
func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
if d.config.ZoneName != "" {
return d.config.ZoneName, nil
}
zoneName, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
}
if zoneName == "" {
return "", errors.New("empty zone name")
}
return dns01.UnFqdn(zoneName), nil
}