2019-04-27 16:50:40 +00:00
|
|
|
// Package bindman implements a DNS provider for solving the DNS-01 challenge.
|
|
|
|
package bindman
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2019-04-27 16:50:40 +00:00
|
|
|
"github.com/labbsr0x/bindman-dns-webhook/src/client"
|
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "BINDMAN_"
|
|
|
|
|
|
|
|
EnvManagerAddress = envNamespace + "MANAGER_ADDRESS"
|
|
|
|
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2019-04-27 16:50:40 +00:00
|
|
|
type Config struct {
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
BaseURL string
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2019-04-27 16:50:40 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-11 22:51:10 +00:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
|
2019-04-27 16:50:40 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, time.Minute),
|
2019-04-27 16:50:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2019-04-27 16:50:40 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *client.DNSWebhookClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Bindman.
|
|
|
|
// BINDMAN_MANAGER_ADDRESS should have the scheme, hostname, and port (if required) of the authoritative Bindman Manager server.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvManagerAddress)
|
2019-04-27 16:50:40 +00:00
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("bindman: %w", err)
|
2019-04-27 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.BaseURL = values[EnvManagerAddress]
|
2019-04-27 16:50:40 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Bindman.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("bindman: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.BaseURL == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("bindman: bindman manager address missing")
|
2019-04-27 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bClient, err := client.New(config.BaseURL, config.HTTPClient)
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, fmt.Errorf("bindman: %w", err)
|
2019-04-27 16:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: bClient}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
// This will *not* create a subzone to contain the TXT record,
|
|
|
|
// so make sure the FQDN specified is within an extant zone.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
|
|
|
if err := d.client.AddRecord(fqdn, "TXT", value); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("bindman: %w", err)
|
2019-04-27 16:50:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
|
|
|
|
|
|
|
if err := d.client.RemoveRecord(fqdn, "TXT"); err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("bindman: %w", err)
|
2019-04-27 16:50:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the timeout and interval to use when checking for DNS propagation.
|
|
|
|
// Adjusting here to cope with spikes in propagation times.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
|
|
|
}
|