hosttech.go raw
1 // Package hosttech implements a DNS provider for solving the DNS-01 challenge using hosttech.
2 package hosttech
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "strconv"
10 "sync"
11 "time"
12
13 "github.com/go-acme/lego/v4/challenge"
14 "github.com/go-acme/lego/v4/challenge/dns01"
15 "github.com/go-acme/lego/v4/platform/config/env"
16 "github.com/go-acme/lego/v4/providers/dns/hosttech/internal"
17 "github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
18 )
19
20 // Environment variables names.
21 const (
22 envNamespace = "HOSTTECH_"
23
24 EnvAPIKey = envNamespace + "API_KEY"
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 APIKey string
37 PropagationTimeout time.Duration
38 PollingInterval time.Duration
39 TTL int
40 HTTPClient *http.Client
41 }
42
43 // NewDefaultConfig returns a default configuration for the DNSProvider.
44 func NewDefaultConfig() *Config {
45 return &Config{
46 TTL: env.GetOrDefaultInt(EnvTTL, 3600),
47 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
48 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
49 HTTPClient: &http.Client{
50 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
51 },
52 }
53 }
54
55 // DNSProvider implements the challenge.Provider interface.
56 type DNSProvider struct {
57 config *Config
58 client *internal.Client
59
60 recordIDs map[string]int
61 recordIDsMu sync.Mutex
62 }
63
64 // NewDNSProvider returns a DNSProvider instance configured for hosttech.
65 // Credentials must be passed in the environment variable: HOSTTECH_API_KEY.
66 func NewDNSProvider() (*DNSProvider, error) {
67 values, err := env.Get(EnvAPIKey)
68 if err != nil {
69 return nil, fmt.Errorf("hosttech: %w", err)
70 }
71
72 config := NewDefaultConfig()
73 config.APIKey = values[EnvAPIKey]
74
75 return NewDNSProviderConfig(config)
76 }
77
78 // NewDNSProviderConfig return a DNSProvider instance configured for hosttech.
79 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
80 if config == nil {
81 return nil, errors.New("hosttech: the configuration of the DNS provider is nil")
82 }
83
84 if config.APIKey == "" {
85 return nil, errors.New("hosttech: missing credentials")
86 }
87
88 client := internal.NewClient(
89 clientdebug.Wrap(
90 internal.OAuthStaticAccessToken(config.HTTPClient, config.APIKey),
91 ),
92 )
93
94 return &DNSProvider{
95 config: config,
96 client: client,
97 recordIDs: map[string]int{},
98 }, nil
99 }
100
101 // Timeout returns the timeout and interval to use when checking for DNS propagation.
102 // Adjusting here to cope with spikes in propagation times.
103 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
104 return d.config.PropagationTimeout, d.config.PollingInterval
105 }
106
107 // Present creates a TXT record using the specified parameters.
108 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
109 info := dns01.GetChallengeInfo(domain, keyAuth)
110
111 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
112 if err != nil {
113 return fmt.Errorf("hosttech: could not find zone for domain %q: %w", domain, err)
114 }
115
116 ctx := context.Background()
117
118 zone, err := d.client.GetZone(ctx, dns01.UnFqdn(authZone))
119 if err != nil {
120 return fmt.Errorf("hosttech: could not find zone for domain %q (%s): %w", domain, authZone, err)
121 }
122
123 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
124 if err != nil {
125 return fmt.Errorf("hosttech: %w", err)
126 }
127
128 record := internal.Record{
129 Type: "TXT",
130 Name: subDomain,
131 Text: info.Value,
132 TTL: d.config.TTL,
133 }
134
135 newRecord, err := d.client.AddRecord(ctx, strconv.Itoa(zone.ID), record)
136 if err != nil {
137 return fmt.Errorf("hosttech: %w", err)
138 }
139
140 d.recordIDsMu.Lock()
141 d.recordIDs[token] = newRecord.ID
142 d.recordIDsMu.Unlock()
143
144 return nil
145 }
146
147 // CleanUp removes the TXT record matching the specified parameters.
148 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
149 info := dns01.GetChallengeInfo(domain, keyAuth)
150
151 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
152 if err != nil {
153 return fmt.Errorf("hosttech: could not find zone for domain %q: %w", domain, err)
154 }
155
156 ctx := context.Background()
157
158 zone, err := d.client.GetZone(ctx, dns01.UnFqdn(authZone))
159 if err != nil {
160 return fmt.Errorf("hosttech: could not find zone for domain %q (%s): %w", domain, authZone, err)
161 }
162
163 // gets the record's unique ID from when we created it
164 d.recordIDsMu.Lock()
165 recordID, ok := d.recordIDs[token]
166 d.recordIDsMu.Unlock()
167
168 if !ok {
169 return fmt.Errorf("hosttech: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
170 }
171
172 err = d.client.DeleteRecord(ctx, strconv.Itoa(zone.ID), strconv.Itoa(recordID))
173 if err != nil {
174 return fmt.Errorf("hosttech: %w", err)
175 }
176
177 d.recordIDsMu.Lock()
178 delete(d.recordIDs, token)
179 d.recordIDsMu.Unlock()
180
181 return nil
182 }
183