Skip to content

Commit

Permalink
Add DNS provider for hosting.nl
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Nov 9, 2024
1 parent f8db554 commit 339f955
Show file tree
Hide file tree
Showing 11 changed files with 610 additions and 0 deletions.
161 changes: 161 additions & 0 deletions providers/dns/hostingnl/hostingnl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Package hostingnl implements a DNS provider for solving the DNS-01 challenge using hosting.nl.
package hostingnl

import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"sync"
"time"

"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/providers/dns/hostingnl/internal"
)

// Environment variables names.
const (
envNamespace = "HOSTINGNL_"

EnvAPIKey = envNamespace + "API_KEY"

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 {
APIKey string
HTTPClient *http.Client
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}

// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
},
}
}

// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
config *Config
client *internal.Client

recordIDs map[string]string
recordIDsMu sync.Mutex
}

// NewDNSProvider returns a DNSProvider instance configured for hosting.nl.
// Credentials must be passed in the environment variables:
// HOSTINGNL_APIKEY.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey)
if err != nil {
return nil, fmt.Errorf("hostingnl: %w", err)
}

config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]

return NewDNSProviderConfig(config)
}

// NewDNSProviderConfig return a DNSProvider instance configured for hosting.nl.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("hostingnl: the configuration of the DNS provider is nil")
}

if config.APIKey == "" {
return nil, errors.New("hostingnl: APIKey is missing")
}

client := internal.NewClient(config.APIKey)

if config.HTTPClient != nil {
client.HTTPClient = config.HTTPClient
}

return &DNSProvider{
config: config,
client: client,
recordIDs: make(map[string]string),
}, nil
}

// Present creates a TXT record using the specified parameters.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)

authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil {
return fmt.Errorf("hostingnl: could not find zone for domain %q: %w", domain, err)
}

record := internal.Record{
Name: info.EffectiveFQDN,
Type: "TXT",
Content: strconv.Quote(info.Value),
TTL: strconv.Itoa(d.config.TTL),
Priority: "0",
}

newRecord, err := d.client.AddRecord(context.Background(), dns01.UnFqdn(authZone), record)
if err != nil {
return fmt.Errorf("hostingnl: failed to create TXT record, fqdn=%s: %w", info.EffectiveFQDN, err)
}

d.recordIDsMu.Lock()
d.recordIDs[token] = newRecord.ID
d.recordIDsMu.Unlock()

return nil
}

// CleanUp removes the TXT records matching the specified parameters.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)

authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
if err != nil {
return fmt.Errorf("hostingnl: could not find zone for domain %q: %w", domain, err)
}

// gets the record's unique ID
d.recordIDsMu.Lock()
recordID, ok := d.recordIDs[token]
d.recordIDsMu.Unlock()
if !ok {
return fmt.Errorf("hostingnl: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
}

err = d.client.DeleteRecord(context.Background(), dns01.UnFqdn(authZone), recordID)
if err != nil {
return fmt.Errorf("hostingnl: failed to delete TXT record, id=%s: %w", recordID, err)
}

// deletes record ID from map
d.recordIDsMu.Lock()
delete(d.recordIDs, token)
d.recordIDsMu.Unlock()

return 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
}
22 changes: 22 additions & 0 deletions providers/dns/hostingnl/hostingnl.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Name = "Hosting.nl"
Description = ''''''
URL = "https://hosting.nl"
Code = "hostingnl"
Since = "v4.20.0"

Example = '''
HOSTINGNL_API_KEY="xxxxxxxxxxxxxxxxxxxxx" \
lego --email [email protected] --dns hostingnl -d '*.example.com' -d example.com run
'''

[Configuration]
[Configuration.Credentials]
HOSTINGNL_API_KEY = "The API key"
[Configuration.Additional]
HOSTINGNL_POLLING_INTERVAL = "Time between DNS propagation check"
HOSTINGNL_PROPAGATION_TIMEOUT = "Maximum waiting time for DNS propagation"
HOSTINGNL_TTL = "The TTL of the TXT record used for the DNS challenge"
HOSTINGNL_HTTP_TIMEOUT = "API request timeout"

[Links]
API = "https://api.hosting.nl/api/documentation"
113 changes: 113 additions & 0 deletions providers/dns/hostingnl/hostingnl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package hostingnl

import (
"testing"

"github.com/go-acme/lego/v4/platform/tester"
"github.com/stretchr/testify/require"
)

const envDomain = envNamespace + "DOMAIN"

var envTest = tester.NewEnvTest(EnvAPIKey).WithDomain(envDomain)

func TestNewDNSProvider(t *testing.T) {
testCases := []struct {
desc string
envVars map[string]string
expected string
}{
{
desc: "success",
envVars: map[string]string{
EnvAPIKey: "key",
},
},
{
desc: "missing API key",
envVars: map[string]string{},
expected: "hostingnl: some credentials information are missing: HOSTINGNL_API_KEY",
},
}

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
defer envTest.RestoreEnv()
envTest.ClearEnv()

envTest.Apply(test.envVars)

p, err := NewDNSProvider()

if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}

func TestNewDNSProviderConfig(t *testing.T) {
testCases := []struct {
desc string
apiKey string
expected string
}{
{
desc: "success",
apiKey: "key",
},
{
desc: "missing API key",
expected: "hostingnl: APIKey is missing",
},
}

for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {
config := NewDefaultConfig()
config.APIKey = test.apiKey

p, err := NewDNSProviderConfig(config)

if test.expected == "" {
require.NoError(t, err)
require.NotNil(t, p)
require.NotNil(t, p.config)
require.NotNil(t, p.client)
} else {
require.EqualError(t, err, test.expected)
}
})
}
}

func TestLivePresent(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}

envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)

err = provider.Present(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

func TestLiveCleanUp(t *testing.T) {
if !envTest.IsLiveTest() {
t.Skip("skipping live test")
}

envTest.RestoreEnv()
provider, err := NewDNSProvider()
require.NoError(t, err)

err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}
Loading

0 comments on commit 339f955

Please sign in to comment.