|
| 1 | +// Package abion implements a DNS provider for solving the DNS-01 challenge using Abion. |
| 2 | +package abion |
| 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/abion/internal" |
| 15 | +) |
| 16 | + |
| 17 | +// Environment variables names. |
| 18 | +const ( |
| 19 | + envNamespace = "ABION_" |
| 20 | + |
| 21 | + EnvAPIKey = envNamespace + "API_KEY" |
| 22 | + |
| 23 | + EnvTTL = envNamespace + "TTL" |
| 24 | + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" |
| 25 | + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" |
| 26 | + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" |
| 27 | +) |
| 28 | + |
| 29 | +var _ challenge.ProviderTimeout = (*DNSProvider)(nil) |
| 30 | + |
| 31 | +// Config is used to configure the creation of the DNSProvider. |
| 32 | +type Config struct { |
| 33 | + APIKey string |
| 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, dns01.DefaultPropagationTimeout), |
| 45 | + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), |
| 46 | + HTTPClient: &http.Client{ |
| 47 | + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*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 | + |
| 58 | +// NewDNSProvider returns a DNSProvider instance configured for Abion. |
| 59 | +// Credentials must be passed in the environment variable: ABION_API_KEY. |
| 60 | +func NewDNSProvider() (*DNSProvider, error) { |
| 61 | + values, err := env.Get(EnvAPIKey) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("abion: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + config := NewDefaultConfig() |
| 67 | + config.APIKey = values[EnvAPIKey] |
| 68 | + |
| 69 | + return NewDNSProviderConfig(config) |
| 70 | +} |
| 71 | + |
| 72 | +// NewDNSProviderConfig return a DNSProvider instance configured for Abion. |
| 73 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 74 | + if config == nil { |
| 75 | + return nil, errors.New("abion: the configuration of the DNS provider is nil") |
| 76 | + } |
| 77 | + |
| 78 | + if config.APIKey == "" { |
| 79 | + return nil, errors.New("abion: credentials missing") |
| 80 | + } |
| 81 | + |
| 82 | + client := internal.NewClient(config.APIKey) |
| 83 | + |
| 84 | + if config.HTTPClient != nil { |
| 85 | + client.HTTPClient = config.HTTPClient |
| 86 | + } |
| 87 | + |
| 88 | + return &DNSProvider{ |
| 89 | + config: config, |
| 90 | + client: client, |
| 91 | + }, nil |
| 92 | +} |
| 93 | + |
| 94 | +// Timeout returns the timeout and interval to use when checking for DNS propagation. |
| 95 | +// Adjusting here to cope with spikes in propagation times. |
| 96 | +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
| 97 | + return d.config.PropagationTimeout, d.config.PollingInterval |
| 98 | +} |
| 99 | + |
| 100 | +// Present creates a TXT record to fulfill the dns-01 challenge. |
| 101 | +func (d *DNSProvider) Present(domain, _, keyAuth string) error { |
| 102 | + ctx := context.Background() |
| 103 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 104 | + |
| 105 | + authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("abion: could not find zone for domain %q: %w", domain, err) |
| 108 | + } |
| 109 | + |
| 110 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("abion: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + zones, err := d.client.GetZone(ctx, dns01.UnFqdn(authZone)) |
| 116 | + if err != nil { |
| 117 | + return fmt.Errorf("abion: get zone %w", err) |
| 118 | + } |
| 119 | + |
| 120 | + var data []internal.Record |
| 121 | + if sub, ok := zones.Data.Attributes.Records[subDomain]; ok { |
| 122 | + if records, exist := sub["TXT"]; exist { |
| 123 | + data = append(data, records...) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + data = append(data, internal.Record{ |
| 128 | + TTL: d.config.TTL, |
| 129 | + Data: info.Value, |
| 130 | + Comments: "lego", |
| 131 | + }) |
| 132 | + |
| 133 | + patch := internal.ZoneRequest{ |
| 134 | + Data: internal.Zone{ |
| 135 | + Type: "zone", |
| 136 | + ID: dns01.UnFqdn(authZone), |
| 137 | + Attributes: internal.Attributes{ |
| 138 | + Records: map[string]map[string][]internal.Record{ |
| 139 | + subDomain: {"TXT": data}, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + _, err = d.client.UpdateZone(ctx, dns01.UnFqdn(authZone), patch) |
| 146 | + if err != nil { |
| 147 | + return fmt.Errorf("abion: update zone %w", err) |
| 148 | + } |
| 149 | + |
| 150 | + return nil |
| 151 | +} |
| 152 | + |
| 153 | +// CleanUp removes the TXT record matching the specified parameters. |
| 154 | +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
| 155 | + ctx := context.Background() |
| 156 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 157 | + |
| 158 | + authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 159 | + if err != nil { |
| 160 | + return fmt.Errorf("abion: could not find zone for domain %q: %w", domain, err) |
| 161 | + } |
| 162 | + |
| 163 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone) |
| 164 | + if err != nil { |
| 165 | + return fmt.Errorf("abion: %w", err) |
| 166 | + } |
| 167 | + |
| 168 | + zones, err := d.client.GetZone(ctx, dns01.UnFqdn(authZone)) |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("abion: get zone %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + var data []internal.Record |
| 174 | + if sub, ok := zones.Data.Attributes.Records[subDomain]; ok { |
| 175 | + if records, exist := sub["TXT"]; exist { |
| 176 | + for _, record := range records { |
| 177 | + if record.Data != info.Value { |
| 178 | + data = append(data, record) |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + payload := map[string][]internal.Record{} |
| 185 | + if len(data) == 0 { |
| 186 | + payload["TXT"] = nil |
| 187 | + } else { |
| 188 | + payload["TXT"] = data |
| 189 | + } |
| 190 | + |
| 191 | + patch := internal.ZoneRequest{ |
| 192 | + Data: internal.Zone{ |
| 193 | + Type: "zone", |
| 194 | + ID: dns01.UnFqdn(authZone), |
| 195 | + Attributes: internal.Attributes{ |
| 196 | + Records: map[string]map[string][]internal.Record{ |
| 197 | + subDomain: payload, |
| 198 | + }, |
| 199 | + }, |
| 200 | + }, |
| 201 | + } |
| 202 | + |
| 203 | + _, err = d.client.UpdateZone(ctx, dns01.UnFqdn(authZone), patch) |
| 204 | + if err != nil { |
| 205 | + return fmt.Errorf("abion: update zone %w", err) |
| 206 | + } |
| 207 | + |
| 208 | + return nil |
| 209 | +} |
0 commit comments