conoha.go raw

   1  // Package conoha implements a DNS provider for solving the DNS-01 challenge using ConoHa DNS.
   2  package conoha
   3  
   4  import (
   5  	"context"
   6  	"errors"
   7  	"fmt"
   8  	"net/http"
   9  	"time"
  10  
  11  	"github.com/go-acme/lego/v4/challenge"
  12  	"github.com/go-acme/lego/v4/challenge/dns01"
  13  	"github.com/go-acme/lego/v4/platform/config/env"
  14  	"github.com/go-acme/lego/v4/providers/dns/conoha/internal"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "CONOHA_"
  21  
  22  	EnvRegion      = envNamespace + "REGION"
  23  	EnvTenantID    = envNamespace + "TENANT_ID"
  24  	EnvAPIUsername = envNamespace + "API_USERNAME"
  25  	EnvAPIPassword = envNamespace + "API_PASSWORD"
  26  
  27  	EnvTTL                = envNamespace + "TTL"
  28  	EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
  29  	EnvPollingInterval    = envNamespace + "POLLING_INTERVAL"
  30  	EnvHTTPTimeout        = envNamespace + "HTTP_TIMEOUT"
  31  )
  32  
  33  var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
  34  
  35  // Config is used to configure the creation of the DNSProvider.
  36  type Config struct {
  37  	Region             string
  38  	TenantID           string
  39  	Username           string
  40  	Password           string
  41  	TTL                int
  42  	PropagationTimeout time.Duration
  43  	PollingInterval    time.Duration
  44  	HTTPClient         *http.Client
  45  }
  46  
  47  // NewDefaultConfig returns a default configuration for the DNSProvider.
  48  func NewDefaultConfig() *Config {
  49  	return &Config{
  50  		Region:             env.GetOrDefaultString(EnvRegion, "tyo1"),
  51  		TTL:                env.GetOrDefaultInt(EnvTTL, 60),
  52  		PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
  53  		PollingInterval:    env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
  54  		HTTPClient: &http.Client{
  55  			Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
  56  		},
  57  	}
  58  }
  59  
  60  // DNSProvider implements the challenge.Provider interface.
  61  type DNSProvider struct {
  62  	config *Config
  63  	client *internal.Client
  64  }
  65  
  66  // NewDNSProvider returns a DNSProvider instance configured for ConoHa DNS.
  67  // Credentials must be passed in the environment variables:
  68  // CONOHA_TENANT_ID, CONOHA_API_USERNAME, CONOHA_API_PASSWORD.
  69  func NewDNSProvider() (*DNSProvider, error) {
  70  	values, err := env.Get(EnvTenantID, EnvAPIUsername, EnvAPIPassword)
  71  	if err != nil {
  72  		return nil, fmt.Errorf("conoha: %w", err)
  73  	}
  74  
  75  	config := NewDefaultConfig()
  76  	config.TenantID = values[EnvTenantID]
  77  	config.Username = values[EnvAPIUsername]
  78  	config.Password = values[EnvAPIPassword]
  79  
  80  	return NewDNSProviderConfig(config)
  81  }
  82  
  83  // NewDNSProviderConfig return a DNSProvider instance configured for ConoHa DNS.
  84  func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
  85  	if config == nil {
  86  		return nil, errors.New("conoha: the configuration of the DNS provider is nil")
  87  	}
  88  
  89  	if config.TenantID == "" || config.Username == "" || config.Password == "" {
  90  		return nil, errors.New("conoha: some credentials information are missing")
  91  	}
  92  
  93  	identifier, err := internal.NewIdentifier(config.Region)
  94  	if err != nil {
  95  		return nil, fmt.Errorf("conoha: failed to create identity client: %w", err)
  96  	}
  97  
  98  	if config.HTTPClient != nil {
  99  		identifier.HTTPClient = config.HTTPClient
 100  	}
 101  
 102  	identifier.HTTPClient = clientdebug.Wrap(identifier.HTTPClient)
 103  
 104  	auth := internal.Auth{
 105  		TenantID: config.TenantID,
 106  		PasswordCredentials: internal.PasswordCredentials{
 107  			Username: config.Username,
 108  			Password: config.Password,
 109  		},
 110  	}
 111  
 112  	tokens, err := identifier.GetToken(context.TODO(), auth)
 113  	if err != nil {
 114  		return nil, fmt.Errorf("conoha: failed to log in: %w", err)
 115  	}
 116  
 117  	client, err := internal.NewClient(config.Region, tokens.Access.Token.ID)
 118  	if err != nil {
 119  		return nil, fmt.Errorf("conoha: failed to create client: %w", err)
 120  	}
 121  
 122  	if config.HTTPClient != nil {
 123  		client.HTTPClient = config.HTTPClient
 124  	}
 125  
 126  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
 127  
 128  	return &DNSProvider{config: config, client: client}, nil
 129  }
 130  
 131  // Present creates a TXT record to fulfill the dns-01 challenge.
 132  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 133  	info := dns01.GetChallengeInfo(domain, keyAuth)
 134  
 135  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 136  	if err != nil {
 137  		return fmt.Errorf("conoha: could not find zone for domain %q: %w", domain, err)
 138  	}
 139  
 140  	ctx := context.Background()
 141  
 142  	id, err := d.client.GetDomainID(ctx, authZone)
 143  	if err != nil {
 144  		return fmt.Errorf("conoha: failed to get domain ID: %w", err)
 145  	}
 146  
 147  	record := internal.Record{
 148  		Name: info.EffectiveFQDN,
 149  		Type: "TXT",
 150  		Data: info.Value,
 151  		TTL:  d.config.TTL,
 152  	}
 153  
 154  	err = d.client.CreateRecord(ctx, id, record)
 155  	if err != nil {
 156  		return fmt.Errorf("conoha: failed to create record: %w", err)
 157  	}
 158  
 159  	return nil
 160  }
 161  
 162  // CleanUp clears ConoHa DNS TXT record.
 163  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 164  	info := dns01.GetChallengeInfo(domain, keyAuth)
 165  
 166  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 167  	if err != nil {
 168  		return fmt.Errorf("conoha: could not find zone for domain %q: %w", domain, err)
 169  	}
 170  
 171  	ctx := context.Background()
 172  
 173  	domID, err := d.client.GetDomainID(ctx, authZone)
 174  	if err != nil {
 175  		return fmt.Errorf("conoha: failed to get domain ID: %w", err)
 176  	}
 177  
 178  	recID, err := d.client.GetRecordID(ctx, domID, info.EffectiveFQDN, "TXT", info.Value)
 179  	if err != nil {
 180  		return fmt.Errorf("conoha: failed to get record ID: %w", err)
 181  	}
 182  
 183  	err = d.client.DeleteRecord(ctx, domID, recID)
 184  	if err != nil {
 185  		return fmt.Errorf("conoha: failed to delete record: %w", err)
 186  	}
 187  
 188  	return nil
 189  }
 190  
 191  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 192  // Adjusting here to cope with spikes in propagation times.
 193  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 194  	return d.config.PropagationTimeout, d.config.PollingInterval
 195  }
 196