1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-04 19:45:28 +02:00
lego/providers/dns/bindman/bindman.go

110 lines
3.3 KiB
Go
Raw Normal View History

// Package bindman implements a DNS provider for solving the DNS-01 challenge.
package bindman
import (
"errors"
"fmt"
"net/http"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/labbsr0x/bindman-dns-webhook/src/client"
)
// 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 19:35:25 +02:00
// Config is used to configure the creation of the DNSProvider.
type Config struct {
PropagationTimeout time.Duration
PollingInterval time.Duration
BaseURL string
HTTPClient *http.Client
}
2020-05-08 19:35:25 +02:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig() *Config {
return &Config{
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, time.Minute),
},
}
}
2020-05-08 19:35:25 +02:00
// DNSProvider implements the challenge.Provider interface.
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) {
values, err := env.Get(EnvManagerAddress)
if err != nil {
2020-02-27 20:14:46 +02:00
return nil, fmt.Errorf("bindman: %w", err)
}
config := NewDefaultConfig()
config.BaseURL = values[EnvManagerAddress]
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 20:14:46 +02:00
return nil, errors.New("bindman: bindman manager address missing")
}
bClient, err := client.New(config.BaseURL, config.HTTPClient)
if err != nil {
2020-02-27 20:14:46 +02:00
return nil, fmt.Errorf("bindman: %w", err)
}
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 20:14:46 +02:00
return fmt.Errorf("bindman: %w", err)
}
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 20:14:46 +02:00
return fmt.Errorf("bindman: %w", err)
}
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
}