rimuhosting.go raw

   1  // Package rimuhosting implements a DNS provider for solving the DNS-01 challenge using RimuHosting DNS.
   2  package rimuhosting
   3  
   4  import (
   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/internal/rimuhosting"
  14  )
  15  
  16  // Environment variables names.
  17  const (
  18  	envNamespace = "RIMUHOSTING_"
  19  
  20  	EnvAPIKey = envNamespace + "API_KEY"
  21  
  22  	EnvTTL                = envNamespace + "TTL"
  23  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  24  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  25  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  26  )
  27  
  28  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  29  
  30  // Config is used to configure the creation of the DNSProvider.
  31  type Config = rimuhosting.Config
  32  
  33  // NewDefaultConfig returns a default configuration for the DNSProvider.
  34  func NewDefaultConfig() *Config {
  35  	return &Config{
  36  		TTL:                env.GetOrDefaultInt(EnvTTL, rimuhosting.DefaultTTL),
  37  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  38  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  39  		HTTPClient: &http.Client{
  40  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  41  		},
  42  	}
  43  }
  44  
  45  // DNSProvider implements the challenge.Provider interface.
  46  type DNSProvider struct {
  47  	prv challenge.ProviderTimeout
  48  }
  49  
  50  // NewDNSProvider returns a DNSProvider instance configured for RimuHosting.
  51  // Credentials must be passed in the environment variables.
  52  func NewDNSProvider() (*DNSProvider, error) {
  53  	values, err := env.Get(EnvAPIKey)
  54  	if err != nil {
  55  		return nil, fmt.Errorf("rimuhosting: %w", err)
  56  	}
  57  
  58  	config := NewDefaultConfig()
  59  	config.APIKey = values[EnvAPIKey]
  60  
  61  	return NewDNSProviderConfig(config)
  62  }
  63  
  64  // NewDNSProviderConfig return a DNSProvider instance configured for RimuHosting.
  65  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  66  	if config == nil {
  67  		return nil, errors.New("rimuhosting: the configuration of the DNS provider is nil")
  68  	}
  69  
  70  	provider, err := rimuhosting.NewDNSProviderConfig(config, "")
  71  	if err != nil {
  72  		return nil, fmt.Errorf("rimuhosting: %w", err)
  73  	}
  74  
  75  	return &DNSProvider{prv: provider}, nil
  76  }
  77  
  78  // Present creates a TXT record using the specified parameters.
  79  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
  80  	err := d.prv.Present(domain, token, keyAuth)
  81  	if err != nil {
  82  		return fmt.Errorf("rimuhosting: %w", err)
  83  	}
  84  
  85  	return nil
  86  }
  87  
  88  // CleanUp removes the TXT record matching the specified parameters.
  89  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
  90  	err := d.prv.CleanUp(domain, token, keyAuth)
  91  	if err != nil {
  92  		return fmt.Errorf("rimuhosting: %w", err)
  93  	}
  94  
  95  	return nil
  96  }
  97  
  98  // Timeout returns the timeout and interval to use when checking for DNS propagation.
  99  // Adjusting here to cope with spikes in propagation times.
 100  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 101  	return d.prv.Timeout()
 102  }
 103