webnamesca.go raw
1 // Package webnamesca implements a DNS provider for solving the DNS-01 challenge using webnames.ca.
2 package webnamesca
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "time"
10
11 "github.com/go-acme/lego/v4/challenge/dns01"
12 "github.com/go-acme/lego/v4/platform/config/env"
13 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
14 "github.com/go-acme/lego/v4/providers/dns/webnamesca/internal"
15 )
16
17 // Environment variables names.
18 const (
19 envNamespace = "WEBNAMESCA_"
20
21 EnvAPIUser = envNamespace + "API_USER"
22 EnvAPIKey = envNamespace + "API_KEY"
23
24 EnvTTL = envNamespace + "TTL"
25 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
26 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
27 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
28 )
29
30 // Config is used to configure the creation of the DNSProvider.
31 type Config struct {
32 APIUser string
33 APIKey string
34
35 PropagationTimeout time.Duration
36 PollingInterval time.Duration
37 TTL int
38 HTTPClient *http.Client
39 }
40
41 // NewDefaultConfig returns a default configuration for the DNSProvider.
42 func NewDefaultConfig() *Config {
43 return &Config{
44 TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
45 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
46 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
47 HTTPClient: &http.Client{
48 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
49 },
50 }
51 }
52
53 // DNSProvider implements the challenge.Provider interface.
54 type DNSProvider struct {
55 config *Config
56 client *internal.Client
57 }
58
59 // NewDNSProvider returns a DNSProvider instance configured for webnames.ca.
60 func NewDNSProvider() (*DNSProvider, error) {
61 values, err := env.Get(EnvAPIUser, EnvAPIKey)
62 if err != nil {
63 return nil, fmt.Errorf("webnamesca: %w", err)
64 }
65
66 config := NewDefaultConfig()
67 config.APIUser = values[EnvAPIUser]
68 config.APIKey = values[EnvAPIKey]
69
70 return NewDNSProviderConfig(config)
71 }
72
73 // NewDNSProviderConfig return a DNSProvider instance configured for webnames.ca.
74 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
75 if config == nil {
76 return nil, errors.New("webnamesca: the configuration of the DNS provider is nil")
77 }
78
79 client, err := internal.NewClient(config.APIUser, config.APIKey)
80 if err != nil {
81 return nil, fmt.Errorf("webnamesca: %w", err)
82 }
83
84 if config.HTTPClient != nil {
85 client.HTTPClient = config.HTTPClient
86 }
87
88 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
89
90 return &DNSProvider{
91 config: config,
92 client: client,
93 }, nil
94 }
95
96 // Present creates a TXT record using the specified parameters.
97 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
98 info := dns01.GetChallengeInfo(domain, keyAuth)
99
100 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
101 if err != nil {
102 return fmt.Errorf("webnamesca: could not find zone for domain %q: %w", domain, err)
103 }
104
105 _, err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), dns01.UnFqdn(info.EffectiveFQDN), info.Value)
106 if err != nil {
107 return fmt.Errorf("webnamesca: add TXT record: %w", err)
108 }
109
110 return nil
111 }
112
113 // CleanUp removes the TXT record matching the specified parameters.
114 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
115 info := dns01.GetChallengeInfo(domain, keyAuth)
116
117 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
118 if err != nil {
119 return fmt.Errorf("webnamesca: could not find zone for domain %q: %w", domain, err)
120 }
121
122 _, err = d.client.DeleteTXTRecord(context.Background(), dns01.UnFqdn(authZone), dns01.UnFqdn(info.EffectiveFQDN), info.Value)
123 if err != nil {
124 return fmt.Errorf("webnamesca: delete TXT record: %w", err)
125 }
126
127 return nil
128 }
129
130 // Timeout returns the timeout and interval to use when checking for DNS propagation.
131 // Adjusting here to cope with spikes in propagation times.
132 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
133 return d.config.PropagationTimeout, d.config.PollingInterval
134 }
135