ionoscloud.go raw
1 // Package ionoscloud implements a DNS provider for solving the DNS-01 challenge using Ionos Cloud.
2 package ionoscloud
3
4 import (
5 "context"
6 "errors"
7 "fmt"
8 "net/http"
9 "sync"
10 "time"
11
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/ionoscloud/internal"
16 )
17
18 // Environment variables names.
19 const (
20 envNamespace = "IONOSCLOUD_"
21
22 EnvAPIToken = envNamespace + "API_TOKEN"
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 APIToken string
33
34 PropagationTimeout time.Duration
35 PollingInterval time.Duration
36 TTL int
37 HTTPClient *http.Client
38 }
39
40 // NewDefaultConfig returns a default configuration for the DNSProvider.
41 func NewDefaultConfig() *Config {
42 return &Config{
43 TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
44 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 120*time.Second),
45 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
46 HTTPClient: &http.Client{
47 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
48 },
49 }
50 }
51
52 // DNSProvider implements the challenge.Provider interface.
53 type DNSProvider struct {
54 config *Config
55 client *internal.Client
56
57 zoneIDs map[string]string
58 recordIDs map[string]string
59 recordIDsMu sync.Mutex
60 }
61
62 // NewDNSProvider returns a DNSProvider instance configured for Ionos Cloud.
63 func NewDNSProvider() (*DNSProvider, error) {
64 values, err := env.Get(EnvAPIToken)
65 if err != nil {
66 return nil, fmt.Errorf("ionoscloud: %w", err)
67 }
68
69 config := NewDefaultConfig()
70 config.APIToken = values[EnvAPIToken]
71
72 return NewDNSProviderConfig(config)
73 }
74
75 // NewDNSProviderConfig return a DNSProvider instance configured for Ionos Cloud.
76 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
77 if config == nil {
78 return nil, errors.New("ionoscloud: the configuration of the DNS provider is nil")
79 }
80
81 client, err := internal.NewClient(config.APIToken)
82 if err != nil {
83 return nil, fmt.Errorf("ionoscloud: %w", err)
84 }
85
86 if config.HTTPClient != nil {
87 client.HTTPClient = config.HTTPClient
88 }
89
90 client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
91
92 return &DNSProvider{
93 config: config,
94 client: client,
95 zoneIDs: make(map[string]string),
96 recordIDs: make(map[string]string),
97 }, nil
98 }
99
100 // Present creates a TXT record using the specified parameters.
101 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
102 ctx := context.Background()
103
104 info := dns01.GetChallengeInfo(domain, keyAuth)
105
106 authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
107 if err != nil {
108 return fmt.Errorf("ionoscloud: could not find zone for domain %q: %w", domain, err)
109 }
110
111 zones, err := d.client.RetrieveZones(ctx, dns01.UnFqdn(authZone))
112 if err != nil {
113 return fmt.Errorf("ionoscloud: retrieve zones: %w", err)
114 }
115
116 if len(zones) != 1 {
117 return fmt.Errorf("ionoscloud: zone ID not found for domain %q", domain)
118 }
119
120 subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
121 if err != nil {
122 return fmt.Errorf("ionoscloud: %w", err)
123 }
124
125 zoneID := zones[0].ID
126
127 request := internal.RecordProperties{
128 Name: subDomain,
129 Type: "TXT",
130 Content: info.Value,
131 TTL: d.config.TTL,
132 }
133
134 record, err := d.client.CreateRecord(ctx, zoneID, request)
135 if err != nil {
136 return fmt.Errorf("ionoscloud: create record: %w", err)
137 }
138
139 d.recordIDsMu.Lock()
140 d.zoneIDs[token] = zoneID
141 d.recordIDs[token] = record.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 d.recordIDsMu.Lock()
152 zoneID, ok := d.zoneIDs[token]
153 d.recordIDsMu.Unlock()
154
155 if !ok {
156 return fmt.Errorf("ionoscloud: unknown zone ID for '%s' '%s'", info.EffectiveFQDN, token)
157 }
158
159 d.recordIDsMu.Lock()
160 recordID, ok := d.recordIDs[token]
161 d.recordIDsMu.Unlock()
162
163 if !ok {
164 return fmt.Errorf("ionoscloud: unknown record ID for '%s' '%s'", info.EffectiveFQDN, token)
165 }
166
167 err := d.client.DeleteRecord(context.Background(), zoneID, recordID)
168 if err != nil {
169 return fmt.Errorf("ionoscloud: delete record: %w", err)
170 }
171
172 d.recordIDsMu.Lock()
173 delete(d.zoneIDs, token)
174 delete(d.recordIDs, token)
175 d.recordIDsMu.Unlock()
176
177 return nil
178 }
179
180 // Timeout returns the timeout and interval to use when checking for DNS propagation.
181 // Adjusting here to cope with spikes in propagation times.
182 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
183 return d.config.PropagationTimeout, d.config.PollingInterval
184 }
185