|
| 1 | +// Package bookmyname implements a DNS provider for solving the DNS-01 challenge using BookMyName. |
| 2 | +package bookmyname |
| 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/bookmyname/internal" |
| 15 | +) |
| 16 | + |
| 17 | +// Environment variables names. |
| 18 | +const ( |
| 19 | + envNamespace = "BOOKMYNAME_" |
| 20 | + |
| 21 | + EnvUsername = envNamespace + "USERNAME" |
| 22 | + EnvPassword = envNamespace + "PASSWORD" |
| 23 | + |
| 24 | + EnvTTL = envNamespace + "TTL" |
| 25 | + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" |
| 26 | + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" |
| 27 | + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" |
| 28 | +) |
| 29 | + |
| 30 | +var _ challenge.ProviderTimeout = (*DNSProvider)(nil) |
| 31 | + |
| 32 | +// Config is used to configure the creation of the DNSProvider. |
| 33 | +type Config struct { |
| 34 | + Username string |
| 35 | + Password string |
| 36 | + |
| 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, dns01.DefaultTTL), |
| 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 | + |
| 61 | +// NewDNSProvider returns a DNSProvider instance configured for BookMyName. |
| 62 | +func NewDNSProvider() (*DNSProvider, error) { |
| 63 | + values, err := env.Get(EnvUsername, EnvPassword) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("bookmyname: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + config := NewDefaultConfig() |
| 69 | + config.Username = values[EnvUsername] |
| 70 | + config.Password = values[EnvPassword] |
| 71 | + |
| 72 | + return NewDNSProviderConfig(config) |
| 73 | +} |
| 74 | + |
| 75 | +// NewDNSProviderConfig return a DNSProvider instance configured for BookMyName. |
| 76 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 77 | + if config == nil { |
| 78 | + return nil, errors.New("bookmyname: the configuration of the DNS provider is nil") |
| 79 | + } |
| 80 | + |
| 81 | + client, err := internal.NewClient(config.Username, config.Password) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("bookmyname: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + if config.HTTPClient != nil { |
| 87 | + client.HTTPClient = config.HTTPClient |
| 88 | + } |
| 89 | + |
| 90 | + return &DNSProvider{ |
| 91 | + config: config, |
| 92 | + client: client, |
| 93 | + }, nil |
| 94 | +} |
| 95 | + |
| 96 | +// Present creates a TXT record using the specified parameters. |
| 97 | +func (d *DNSProvider) Present(domain, token, keyAuth string) error { |
| 98 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 99 | + |
| 100 | + record := internal.Record{ |
| 101 | + Hostname: info.EffectiveFQDN, |
| 102 | + Type: "txt", |
| 103 | + TTL: d.config.TTL, |
| 104 | + Value: info.Value, |
| 105 | + } |
| 106 | + |
| 107 | + err := d.client.AddRecord(context.Background(), record) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("bookmyname: add record: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +// CleanUp removes the TXT record matching the specified parameters. |
| 116 | +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
| 117 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 118 | + |
| 119 | + record := internal.Record{ |
| 120 | + Hostname: info.EffectiveFQDN, |
| 121 | + Type: "txt", |
| 122 | + TTL: d.config.TTL, |
| 123 | + Value: info.Value, |
| 124 | + } |
| 125 | + |
| 126 | + err := d.client.RemoveRecord(context.Background(), record) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("bookmyname: add record: %w", err) |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// Timeout returns the timeout and interval to use when checking for DNS propagation. |
| 135 | +// Adjusting here to cope with spikes in propagation times. |
| 136 | +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
| 137 | + return d.config.PropagationTimeout, d.config.PollingInterval |
| 138 | +} |
0 commit comments