directadmin.go raw

   1  package directadmin
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"fmt"
   7  	"net/http"
   8  	"time"
   9  
  10  	"github.com/go-acme/lego/v4/challenge"
  11  	"github.com/go-acme/lego/v4/challenge/dns01"
  12  	"github.com/go-acme/lego/v4/platform/config/env"
  13  	"github.com/go-acme/lego/v4/providers/dns/directadmin/internal"
  14  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  15  )
  16  
  17  // Environment variables names.
  18  const (
  19  	envNamespace = "DIRECTADMIN_"
  20  
  21  	EnvAPIURL   = envNamespace + "API_URL"
  22  	EnvUsername = envNamespace + "USERNAME"
  23  	EnvPassword = envNamespace + "PASSWORD"
  24  	EnvZoneName = envNamespace + "ZONE_NAME"
  25  
  26  	EnvTTL                = envNamespace + "TTL"
  27  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  28  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  29  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  30  )
  31  
  32  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  33  
  34  // Config is used to configure the creation of the DNSProvider.
  35  type Config struct {
  36  	BaseURL  string
  37  	Username string
  38  	Password string
  39  
  40  	ZoneName string
  41  
  42  	TTL                int
  43  	PropagationTimeout time.Duration
  44  	PollingInterval    time.Duration
  45  	HTTPClient         *http.Client
  46  }
  47  
  48  // NewDefaultConfig returns a default configuration for the DNSProvider.
  49  func NewDefaultConfig() *Config {
  50  	return &Config{
  51  		ZoneName:           env.GetOrFile(EnvZoneName),
  52  		TTL:                env.GetOrDefaultInt(EnvTTL, 30),
  53  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second),
  54  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
  55  		HTTPClient: &http.Client{
  56  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  57  		},
  58  	}
  59  }
  60  
  61  // DNSProvider implements the challenge.Provider interface.
  62  type DNSProvider struct {
  63  	client *internal.Client
  64  	config *Config
  65  }
  66  
  67  // NewDNSProvider returns a DNSProvider instance configured for DirectAdmin.
  68  // Credentials must be passed in the environment variables:
  69  // DIRECTADMIN_API_URL, DIRECTADMIN_USERNAME, DIRECTADMIN_PASSWORD.
  70  func NewDNSProvider() (*DNSProvider, error) {
  71  	values, err := env.Get(EnvAPIURL, EnvUsername, EnvPassword)
  72  	if err != nil {
  73  		return nil, fmt.Errorf("directadmin: %w", err)
  74  	}
  75  
  76  	config := NewDefaultConfig()
  77  	config.BaseURL = values[EnvAPIURL]
  78  	config.Username = values[EnvUsername]
  79  	config.Password = values[EnvPassword]
  80  
  81  	return NewDNSProviderConfig(config)
  82  }
  83  
  84  // NewDNSProviderConfig return a DNSProvider instance configured for DirectAdmin.
  85  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  86  	if config.BaseURL == "" {
  87  		return nil, errors.New("directadmin: missing API URL")
  88  	}
  89  
  90  	if config.Username == "" || config.Password == "" {
  91  		return nil, errors.New("directadmin: some credentials information are missing")
  92  	}
  93  
  94  	client, err := internal.NewClient(config.BaseURL, config.Username, config.Password)
  95  	if err != nil {
  96  		return nil, fmt.Errorf("directadmin: %w", err)
  97  	}
  98  
  99  	if config.HTTPClient != nil {
 100  		client.HTTPClient = config.HTTPClient
 101  	}
 102  
 103  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
 104  
 105  	return &DNSProvider{client: client, config: config}, nil
 106  }
 107  
 108  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 109  // Adjusting here to cope with spikes in propagation times.
 110  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 111  	return d.config.PropagationTimeout, d.config.PollingInterval
 112  }
 113  
 114  // Present creates a TXT record using the specified parameters.
 115  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 116  	info := dns01.GetChallengeInfo(domain, keyAuth)
 117  
 118  	authZone, err := d.getZoneName(info.EffectiveFQDN)
 119  	if err != nil {
 120  		return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
 121  	}
 122  
 123  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 124  	if err != nil {
 125  		return fmt.Errorf("directadmin: %w", err)
 126  	}
 127  
 128  	record := internal.Record{
 129  		Name:  subDomain,
 130  		Type:  "TXT",
 131  		Value: info.Value,
 132  		TTL:   d.config.TTL,
 133  	}
 134  
 135  	err = d.client.SetRecord(context.Background(), dns01.UnFqdn(authZone), record)
 136  	if err != nil {
 137  		return fmt.Errorf("directadmin: set record for zone %s and subdomain %s: %w", authZone, subDomain, err)
 138  	}
 139  
 140  	return nil
 141  }
 142  
 143  // CleanUp removes the TXT record matching the specified parameters.
 144  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 145  	info := dns01.GetChallengeInfo(domain, keyAuth)
 146  
 147  	authZone, err := d.getZoneName(info.EffectiveFQDN)
 148  	if err != nil {
 149  		return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
 150  	}
 151  
 152  	subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
 153  	if err != nil {
 154  		return fmt.Errorf("directadmin: %w", err)
 155  	}
 156  
 157  	record := internal.Record{
 158  		Name:  subDomain,
 159  		Type:  "TXT",
 160  		Value: info.Value,
 161  	}
 162  
 163  	err = d.client.DeleteRecord(context.Background(), dns01.UnFqdn(authZone), record)
 164  	if err != nil {
 165  		return fmt.Errorf("directadmin: delete record for zone %s and subdomain %s: %w", authZone, subDomain, err)
 166  	}
 167  
 168  	return nil
 169  }
 170  
 171  func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
 172  	if d.config.ZoneName != "" {
 173  		return d.config.ZoneName, nil
 174  	}
 175  
 176  	authZone, err := dns01.FindZoneByFqdn(fqdn)
 177  	if err != nil {
 178  		return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
 179  	}
 180  
 181  	if authZone == "" {
 182  		return "", errors.New("empty zone name")
 183  	}
 184  
 185  	return authZone, nil
 186  }
 187