ionos.go raw
1 // Package ionos implements a DNS provider for solving the DNS-01 challenge using Ionos/1&1.
2 package ionos
3
4 import (
5 "errors"
6 "fmt"
7 "net/http"
8 "time"
9
10 "github.com/go-acme/lego/v4/challenge"
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/ionos"
14 )
15
16 // Environment variables names.
17 const (
18 envNamespace = "IONOS_"
19
20 EnvAPIKey = envNamespace + "API_KEY"
21
22 EnvTTL = envNamespace + "TTL"
23 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
24 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
25 EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
26 )
27
28 const minTTL = 300
29
30 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
31
32 // Config is used to configure the creation of the DNSProvider.
33 type Config = ionos.Config
34
35 // NewDefaultConfig returns a default configuration for the DNSProvider.
36 func NewDefaultConfig() *Config {
37 return &Config{
38 TTL: env.GetOrDefaultInt(EnvTTL, ionos.MinTTL),
39 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 15*time.Minute),
40 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
41 HTTPClient: &http.Client{
42 Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
43 },
44 }
45 }
46
47 // DNSProvider implements the challenge.Provider interface.
48 type DNSProvider struct {
49 prv challenge.ProviderTimeout
50 }
51
52 // NewDNSProvider returns a DNSProvider instance configured for Ionos.
53 // Credentials must be passed in the environment variables: IONOS_API_KEY.
54 func NewDNSProvider() (*DNSProvider, error) {
55 values, err := env.Get(EnvAPIKey)
56 if err != nil {
57 return nil, fmt.Errorf("ionos: %w", err)
58 }
59
60 config := NewDefaultConfig()
61 config.APIKey = values[EnvAPIKey]
62
63 return NewDNSProviderConfig(config)
64 }
65
66 // NewDNSProviderConfig return a DNSProvider instance configured for Ionos.
67 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
68 if config == nil {
69 return nil, errors.New("ionos: the configuration of the DNS provider is nil")
70 }
71
72 provider, err := ionos.NewDNSProviderConfig(config, "")
73 if err != nil {
74 return nil, fmt.Errorf("ionos: %w", err)
75 }
76
77 return &DNSProvider{prv: provider}, nil
78 }
79
80 // Timeout returns the timeout and interval to use when checking for DNS propagation.
81 // Adjusting here to cope with spikes in propagation times.
82 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
83 return d.prv.Timeout()
84 }
85
86 // Present creates a TXT record using the specified parameters.
87 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
88 err := d.prv.Present(domain, token, keyAuth)
89 if err != nil {
90 return fmt.Errorf("ionos: %w", err)
91 }
92
93 return nil
94 }
95
96 // CleanUp removes the TXT record matching the specified parameters.
97 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
98 err := d.prv.CleanUp(domain, token, keyAuth)
99 if err != nil {
100 return fmt.Errorf("ionos: %w", err)
101 }
102
103 return nil
104 }
105