ispconfigddns.go raw
1 // Package ispconfigddns implements a DNS provider for solving the DNS-01 challenge using ISPConfig 3 Dynamic DNS (DDNS) Module.
2 package ispconfigddns
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/internal/clientdebug"
15 "github.com/go-acme/lego/v4/providers/dns/ispconfigddns/internal"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "ISPCONFIG_DDNS_"
21
22 EnvServerURL = envNamespace + "SERVER_URL"
23 EnvToken = envNamespace + "TOKEN"
24
25 EnvTTL = envNamespace + "TTL"
26 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
27 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
28 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
29 )
30
31 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
32
33 // Config is used to configure the creation of the DNSProvider.
34 type Config struct {
35 ServerURL string
36 Token string
37
38 PropagationTimeout time.Duration
39 PollingInterval time.Duration
40 TTL int
41 HTTPClient *http.Client
42 }
43
44 // NewDefaultConfig returns a default configuration for the DNSProvider.
45 func NewDefaultConfig() *Config {
46 return &Config{
47 TTL: env.GetOrDefaultInt(EnvTTL, 3600),
48 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
49 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
50 HTTPClient: &http.Client{
51 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
52 },
53 }
54 }
55
56 // DNSProvider implements the challenge.Provider interface.
57 type DNSProvider struct {
58 config *Config
59 client *internal.Client
60 }
61
62 // NewDNSProvider returns a DNSProvider instance configured for ISPConfig 3 Dynamic DNS (DDNS) Module.
63 func NewDNSProvider() (*DNSProvider, error) {
64 values, err := env.Get(EnvServerURL, EnvToken)
65 if err != nil {
66 return nil, fmt.Errorf("ispconfig (DDNS module): %w", err)
67 }
68
69 config := NewDefaultConfig()
70 config.ServerURL = values[EnvServerURL]
71 config.Token = values[EnvToken]
72
73 return NewDNSProviderConfig(config)
74 }
75
76 // NewDNSProviderConfig return a DNSProvider instance configured for ISPConfig 3 Dynamic DNS (DDNS) Module.
77 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
78 if config == nil {
79 return nil, errors.New("ispconfig (DDNS module): the configuration of the DNS provider is nil")
80 }
81
82 if config.ServerURL == "" {
83 return nil, errors.New("ispconfig (DDNS module): missing server URL")
84 }
85
86 if config.Token == "" {
87 return nil, errors.New("ispconfig (DDNS module): missing token")
88 }
89
90 client, err := internal.NewClient(config.ServerURL, config.Token)
91 if err != nil {
92 return nil, fmt.Errorf("ispconfig (DDNS module): %w", err)
93 }
94
95 if config.HTTPClient != nil {
96 client.HTTPClient = config.HTTPClient
97 }
98
99 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
100
101 return &DNSProvider{
102 config: config,
103 client: client,
104 }, nil
105 }
106
107 // Timeout returns the timeout and interval to use when checking for DNS propagation.
108 // Adjusting here to control checking compliance to spec.
109 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
110 return d.config.PropagationTimeout, d.config.PollingInterval
111 }
112
113 // Present creates a TXT record to fulfill the dns-01 challenge.
114 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
115 info := dns01.GetChallengeInfo(domain, keyAuth)
116
117 zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
118 if err != nil {
119 return fmt.Errorf("ispconfig (DDNS module): could not find zone for domain %q: %w", domain, err)
120 }
121
122 err = d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(zone), info.EffectiveFQDN, info.Value)
123 if err != nil {
124 return fmt.Errorf("ispconfig (DDNS module): add record: %w", err)
125 }
126
127 return nil
128 }
129
130 // CleanUp removes the TXT record matching the specified parameters.
131 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
132 info := dns01.GetChallengeInfo(domain, keyAuth)
133
134 zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
135 if err != nil {
136 return fmt.Errorf("ispconfig (DDNS module): could not find zone for domain %q: %w", domain, err)
137 }
138
139 err = d.client.DeleteTXTRecord(context.Background(), dns01.UnFqdn(zone), info.EffectiveFQDN, info.Value)
140 if err != nil {
141 return fmt.Errorf("ispconfig (DDNS module): delete record: %w", err)
142 }
143
144 return nil
145 }
146