dyn.go raw
1 // Package dyn implements a DNS provider for solving the DNS-01 challenge using Dyn Managed DNS.
2 package dyn
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "time"
10
11 "github.com/go-acme/lego/v4/challenge"
12 "github.com/go-acme/lego/v4/challenge/dns01"
13 "github.com/go-acme/lego/v4/platform/config/env"
14 "github.com/go-acme/lego/v4/providers/dns/dyn/internal"
15 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "DYN_"
21
22 EnvCustomerName = envNamespace + "CUSTOMER_NAME"
23 EnvUserName = envNamespace + "USER_NAME"
24 EnvPassword = envNamespace + "PASSWORD"
25
26 EnvTTL = envNamespace + "TTL"
27 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
28 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
29 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
30 )
31
32 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
33
34 // Config is used to configure the creation of the DNSProvider.
35 type Config struct {
36 CustomerName string
37 UserName string
38 Password string
39 HTTPClient *http.Client
40 PropagationTimeout time.Duration
41 PollingInterval time.Duration
42 TTL int
43 }
44
45 // NewDefaultConfig returns a default configuration for the DNSProvider.
46 func NewDefaultConfig() *Config {
47 return &Config{
48 TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
49 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
50 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
51 HTTPClient: &http.Client{
52 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
53 },
54 }
55 }
56
57 // DNSProvider implements the challenge.Provider interface.
58 type DNSProvider struct {
59 config *Config
60 client *internal.Client
61 }
62
63 // NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
64 // Credentials must be passed in the environment variables:
65 // DYN_CUSTOMER_NAME, DYN_USER_NAME and DYN_PASSWORD.
66 func NewDNSProvider() (*DNSProvider, error) {
67 values, err := env.Get(EnvCustomerName, EnvUserName, EnvPassword)
68 if err != nil {
69 return nil, fmt.Errorf("dyn: %w", err)
70 }
71
72 config := NewDefaultConfig()
73 config.CustomerName = values[EnvCustomerName]
74 config.UserName = values[EnvUserName]
75 config.Password = values[EnvPassword]
76
77 return NewDNSProviderConfig(config)
78 }
79
80 // NewDNSProviderConfig return a DNSProvider instance configured for Dyn DNS.
81 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
82 if config == nil {
83 return nil, errors.New("dyn: the configuration of the DNS provider is nil")
84 }
85
86 if config.CustomerName == "" || config.UserName == "" || config.Password == "" {
87 return nil, errors.New("dyn: credentials missing")
88 }
89
90 client := internal.NewClient(config.CustomerName, config.UserName, config.Password)
91
92 if config.HTTPClient != nil {
93 client.HTTPClient = config.HTTPClient
94 }
95
96 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
97
98 return &DNSProvider{config: config, client: client}, nil
99 }
100
101 // Present creates a TXT record using the specified parameters.
102 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
103 info := dns01.GetChallengeInfo(domain, keyAuth)
104
105 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
106 if err != nil {
107 return fmt.Errorf("dyn: could not find zone for domain %q: %w", domain, err)
108 }
109
110 ctx, err := d.client.CreateAuthenticatedContext(context.Background())
111 if err != nil {
112 return fmt.Errorf("dyn: %w", err)
113 }
114
115 err = d.client.AddTXTRecord(ctx, authZone, info.EffectiveFQDN, info.Value, d.config.TTL)
116 if err != nil {
117 return fmt.Errorf("dyn: %w", err)
118 }
119
120 err = d.client.Publish(ctx, authZone, "Added TXT record for ACME dns-01 challenge using lego client")
121 if err != nil {
122 return fmt.Errorf("dyn: %w", err)
123 }
124
125 return d.client.Logout(ctx)
126 }
127
128 // CleanUp removes the TXT record matching the specified parameters.
129 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
130 info := dns01.GetChallengeInfo(domain, keyAuth)
131
132 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
133 if err != nil {
134 return fmt.Errorf("dyn: could not find zone for domain %q: %w", domain, err)
135 }
136
137 ctx, err := d.client.CreateAuthenticatedContext(context.Background())
138 if err != nil {
139 return fmt.Errorf("dyn: %w", err)
140 }
141
142 err = d.client.RemoveTXTRecord(ctx, authZone, info.EffectiveFQDN)
143 if err != nil {
144 return fmt.Errorf("dyn: %w", err)
145 }
146
147 err = d.client.Publish(ctx, authZone, "Removed TXT record for ACME dns-01 challenge using lego client")
148 if err != nil {
149 return fmt.Errorf("dyn: %w", err)
150 }
151
152 return d.client.Logout(ctx)
153 }
154
155 // Timeout returns the timeout and interval to use when checking for DNS propagation.
156 // Adjusting here to cope with spikes in propagation times.
157 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
158 return d.config.PropagationTimeout, d.config.PollingInterval
159 }
160