ibmcloud.go raw
1 // Package ibmcloud implements a DNS provider for solving the DNS-01 challenge using IBM Cloud (SoftLayer).
2 package ibmcloud
3
4 import (
5 "errors"
6 "fmt"
7 "time"
8
9 "github.com/go-acme/lego/v4/challenge"
10 "github.com/go-acme/lego/v4/challenge/dns01"
11 "github.com/go-acme/lego/v4/platform/config/env"
12 "github.com/go-acme/lego/v4/providers/dns/ibmcloud/internal"
13 "github.com/softlayer/softlayer-go/session"
14 )
15
16 // Environment variables names.
17 const (
18 envNamespace = "SOFTLAYER_"
19
20 // EnvUsername the name must be the same as here:
21 // https://github.com/softlayer/softlayer-go/blob/534185047ea683dd1e29fd23e445598295d94be4/session/session.go#L171
22 EnvUsername = envNamespace + "USERNAME"
23 // EnvAPIKey the name must be the same as here:
24 // https://github.com/softlayer/softlayer-go/blob/534185047ea683dd1e29fd23e445598295d94be4/session/session.go#L175
25 EnvAPIKey = envNamespace + "API_KEY"
26 // EnvHTTPTimeout the name must be the same as here:
27 // https://github.com/softlayer/softlayer-go/blob/534185047ea683dd1e29fd23e445598295d94be4/session/session.go#L182
28 EnvHTTPTimeout = envNamespace + "TIMEOUT"
29 EnvDebug = envNamespace + "DEBUG"
30
31 EnvTTL = envNamespace + "TTL"
32 EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
33 EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
34 )
35
36 var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
37
38 // Config is used to configure the creation of the DNSProvider.
39 type Config struct {
40 Username string
41 APIKey string
42 PropagationTimeout time.Duration
43 PollingInterval time.Duration
44 TTL int
45 HTTPTimeout time.Duration
46 Debug bool
47 }
48
49 // NewDefaultConfig returns a default configuration for the DNSProvider.
50 func NewDefaultConfig() *Config {
51 return &Config{
52 TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
53 PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
54 PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
55 HTTPTimeout: env.GetOrDefaultSecond(EnvHTTPTimeout, session.DefaultTimeout),
56 }
57 }
58
59 // DNSProvider implements the challenge.Provider interface.
60 type DNSProvider struct {
61 config *Config
62 wrapper *internal.Wrapper
63 }
64
65 // NewDNSProvider returns a DNSProvider instance configured for IBM Cloud (SoftLayer).
66 // Credentials must be passed in the environment variables:
67 // SOFTLAYER_USERNAME, SOFTLAYER_API_KEY.
68 func NewDNSProvider() (*DNSProvider, error) {
69 values, err := env.Get(EnvUsername, EnvAPIKey)
70 if err != nil {
71 return nil, fmt.Errorf("ibmcloud: %w", err)
72 }
73
74 config := NewDefaultConfig()
75 config.Username = values[EnvUsername]
76 config.APIKey = values[EnvAPIKey]
77 config.Debug = env.GetOrDefaultBool(EnvDebug, false)
78
79 return NewDNSProviderConfig(config)
80 }
81
82 // NewDNSProviderConfig return a DNSProvider instance configured for IBM Cloud (SoftLayer).
83 func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
84 if config == nil {
85 return nil, errors.New("ibmcloud: the configuration of the DNS provider is nil")
86 }
87
88 if config.Username == "" {
89 return nil, errors.New("ibmcloud: username is missing")
90 }
91
92 if config.APIKey == "" {
93 return nil, errors.New("ibmcloud: API key is missing")
94 }
95
96 sess := session.New(config.Username, config.APIKey)
97
98 sess.Timeout = config.HTTPTimeout
99 sess.Debug = config.Debug
100
101 return &DNSProvider{wrapper: internal.NewWrapper(sess), config: config}, nil
102 }
103
104 // Timeout returns the timeout and interval to use when checking for DNS propagation.
105 // Adjusting here to cope with spikes in propagation times.
106 func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
107 return d.config.PropagationTimeout, d.config.PollingInterval
108 }
109
110 // Present creates a TXT record to fulfill the dns-01 challenge.
111 func (d *DNSProvider) Present(domain, token, keyAuth string) error {
112 info := dns01.GetChallengeInfo(domain, keyAuth)
113
114 // TODO(ldez) replace domain by FQDN to follow CNAME.
115 err := d.wrapper.AddTXTRecord(info.EffectiveFQDN, domain, info.Value, d.config.TTL)
116 if err != nil {
117 return fmt.Errorf("ibmcloud: %w", err)
118 }
119
120 return nil
121 }
122
123 // CleanUp removes the TXT record matching the specified parameters.
124 func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
125 info := dns01.GetChallengeInfo(domain, keyAuth)
126
127 // TODO(ldez) replace domain by FQDN to follow CNAME.
128 err := d.wrapper.CleanupTXTRecord(info.EffectiveFQDN, domain)
129 if err != nil {
130 return fmt.Errorf("ibmcloud: %w", err)
131 }
132
133 return nil
134 }
135