|
| 1 | +package corenetworks |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "time" |
| 9 | + |
| 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/corenetworks/internal" |
| 13 | +) |
| 14 | + |
| 15 | +// Environment variables names. |
| 16 | +const ( |
| 17 | + envNamespace = "CORENETWORKS_" |
| 18 | + |
| 19 | + EnvLogin = envNamespace + "LOGIN" |
| 20 | + EnvPassword = envNamespace + "PASSWORD" |
| 21 | + |
| 22 | + EnvTTL = envNamespace + "TTL" |
| 23 | + EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT" |
| 24 | + EnvPollingInterval = envNamespace + "POLLING_INTERVAL" |
| 25 | + EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT" |
| 26 | +) |
| 27 | + |
| 28 | +// Config is used to configure the creation of the DNSProvider. |
| 29 | +type Config struct { |
| 30 | + Login string |
| 31 | + Password string |
| 32 | + PropagationTimeout time.Duration |
| 33 | + PollingInterval time.Duration |
| 34 | + TTL int |
| 35 | + HTTPClient *http.Client |
| 36 | +} |
| 37 | + |
| 38 | +// NewDefaultConfig returns a default configuration for the DNSProvider. |
| 39 | +func NewDefaultConfig() *Config { |
| 40 | + return &Config{ |
| 41 | + TTL: env.GetOrDefaultInt(EnvTTL, 3600), |
| 42 | + PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), |
| 43 | + PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), |
| 44 | + HTTPClient: &http.Client{ |
| 45 | + Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second), |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// DNSProvider implements the challenge.Provider interface. |
| 51 | +type DNSProvider struct { |
| 52 | + config *Config |
| 53 | + client *internal.Client |
| 54 | +} |
| 55 | + |
| 56 | +// NewDNSProvider returns a DNSProvider instance configured for Core-Networks. |
| 57 | +// Credentials must be passed in the environment variables: CORENETWORKS_LOGIN, CORENETWORKS_PASSWORD. |
| 58 | +func NewDNSProvider() (*DNSProvider, error) { |
| 59 | + values, err := env.Get(EnvLogin, EnvPassword) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("corenetworks: %w", err) |
| 62 | + } |
| 63 | + |
| 64 | + config := NewDefaultConfig() |
| 65 | + config.Login = values[EnvLogin] |
| 66 | + config.Password = values[EnvPassword] |
| 67 | + |
| 68 | + return NewDNSProviderConfig(config) |
| 69 | +} |
| 70 | + |
| 71 | +// NewDNSProviderConfig return a DNSProvider instance configured for Bluecat DNS. |
| 72 | +func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { |
| 73 | + if config == nil { |
| 74 | + return nil, errors.New("corenetworks: the configuration of the DNS provider is nil") |
| 75 | + } |
| 76 | + |
| 77 | + if config.Login == "" || config.Password == "" { |
| 78 | + return nil, errors.New("corenetworks: credentials missing") |
| 79 | + } |
| 80 | + |
| 81 | + client := internal.NewClient(config.Login, config.Password) |
| 82 | + |
| 83 | + if config.HTTPClient != nil { |
| 84 | + client.HTTPClient = config.HTTPClient |
| 85 | + } |
| 86 | + |
| 87 | + return &DNSProvider{config: config, client: client}, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Timeout returns the timeout and interval to use when checking for DNS propagation. |
| 91 | +// Adjusting here to cope with spikes in propagation times. |
| 92 | +func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { |
| 93 | + return d.config.PropagationTimeout, d.config.PollingInterval |
| 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 | + ctx, err := d.client.CreateAuthenticatedContext(context.Background()) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("create authentication token: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 106 | + if err != nil { |
| 107 | + return fmt.Errorf("corenetworks: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) |
| 108 | + } |
| 109 | + |
| 110 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zone) |
| 111 | + if err != nil { |
| 112 | + return fmt.Errorf("corenetworks: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + record := internal.Record{ |
| 116 | + Name: subDomain, |
| 117 | + TTL: d.config.TTL, |
| 118 | + Type: "TXT", |
| 119 | + Data: info.Value, |
| 120 | + } |
| 121 | + |
| 122 | + err = d.client.AddRecord(ctx, zone, record) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("corenetworks: add record:%w", err) |
| 125 | + } |
| 126 | + |
| 127 | + err = d.client.CommitRecords(ctx, zone) |
| 128 | + if err != nil { |
| 129 | + return fmt.Errorf("corenetworks: commit records: %w", err) |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// CleanUp removes the TXT record matching the specified parameters. |
| 136 | +func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { |
| 137 | + info := dns01.GetChallengeInfo(domain, keyAuth) |
| 138 | + |
| 139 | + ctx, err := d.client.CreateAuthenticatedContext(context.Background()) |
| 140 | + if err != nil { |
| 141 | + return fmt.Errorf("create authentication token: %w", err) |
| 142 | + } |
| 143 | + |
| 144 | + zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("corenetworks: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err) |
| 147 | + } |
| 148 | + |
| 149 | + subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zone) |
| 150 | + if err != nil { |
| 151 | + return fmt.Errorf("corenetworks: %w", err) |
| 152 | + } |
| 153 | + |
| 154 | + record := internal.Record{ |
| 155 | + Name: subDomain, |
| 156 | + TTL: d.config.TTL, |
| 157 | + Type: "TXT", |
| 158 | + Data: info.Value, |
| 159 | + } |
| 160 | + |
| 161 | + err = d.client.DeleteRecords(ctx, zone, record) |
| 162 | + if err != nil { |
| 163 | + return fmt.Errorf("corenetworks: delete records: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + err = d.client.CommitRecords(ctx, zone) |
| 167 | + if err != nil { |
| 168 | + return fmt.Errorf("corenetworks: commit records: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
0 commit comments