conohav3.go raw

   1  // Package conohav3 implements a DNS provider for solving the DNS-01 challenge using ConoHa VPS Ver 3.0 DNS.
   2  package conohav3
   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/conohav3/internal"
  15  	"github.com/go-acme/lego/v4/providers/dns/internal/clientdebug"
  16  )
  17  
  18  // Environment variables names.
  19  const (
  20  	envNamespace = "CONOHAV3_"
  21  
  22  	EnvRegion      = envNamespace + "REGION"
  23  	EnvTenantID    = envNamespace + "TENANT_ID"
  24  	EnvAPIUserID   = envNamespace + "API_USER_ID"
  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  	UserID             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, "c3j1"),
  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  // CONOHAV3_TENANT_ID, CONOHAV3_API_USER_ID, CONOHAV3_API_PASSWORD.
  69  func NewDNSProvider() (*DNSProvider, error) {
  70  	values, err := env.Get(EnvTenantID, EnvAPIUserID, EnvAPIPassword)
  71  	if err != nil {
  72  		return nil, fmt.Errorf("conohav3: %w", err)
  73  	}
  74  
  75  	config := NewDefaultConfig()
  76  	config.TenantID = values[EnvTenantID]
  77  	config.UserID = values[EnvAPIUserID]
  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("conohav3: the configuration of the DNS provider is nil")
  87  	}
  88  
  89  	if config.TenantID == "" || config.UserID == "" || config.Password == "" {
  90  		return nil, errors.New("conohav3: some credentials information are missing")
  91  	}
  92  
  93  	identifier, err := internal.NewIdentifier(config.Region)
  94  	if err != nil {
  95  		return nil, fmt.Errorf("conohav3: 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  		Identity: internal.Identity{
 106  			Methods: []string{"password"},
 107  			Password: internal.Password{
 108  				User: internal.User{
 109  					ID:       config.UserID,
 110  					Password: config.Password,
 111  				},
 112  			},
 113  		},
 114  		Scope: internal.Scope{
 115  			Project: internal.Project{
 116  				ID: config.TenantID,
 117  			},
 118  		},
 119  	}
 120  
 121  	token, err := identifier.GetToken(context.Background(), auth)
 122  	if err != nil {
 123  		return nil, fmt.Errorf("conohav3: failed to log in: %w", err)
 124  	}
 125  
 126  	client, err := internal.NewClient(config.Region, token)
 127  	if err != nil {
 128  		return nil, fmt.Errorf("conohav3: failed to create client: %w", err)
 129  	}
 130  
 131  	if config.HTTPClient != nil {
 132  		client.HTTPClient = config.HTTPClient
 133  	}
 134  
 135  	client.HTTPClient = clientdebug.Wrap(client.HTTPClient)
 136  
 137  	return &DNSProvider{config: config, client: client}, nil
 138  }
 139  
 140  // Present creates a TXT record to fulfill the dns-01 challenge.
 141  func (d *DNSProvider) Present(domain, token, keyAuth string) error {
 142  	info := dns01.GetChallengeInfo(domain, keyAuth)
 143  
 144  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 145  	if err != nil {
 146  		return fmt.Errorf("conohav3: could not find zone for domain %q: %w", domain, err)
 147  	}
 148  
 149  	ctx := context.Background()
 150  
 151  	id, err := d.client.GetDomainID(ctx, authZone)
 152  	if err != nil {
 153  		return fmt.Errorf("conohav3: failed to get domain ID: %w", err)
 154  	}
 155  
 156  	record := internal.Record{
 157  		Name: info.EffectiveFQDN,
 158  		Type: "TXT",
 159  		Data: info.Value,
 160  		TTL:  d.config.TTL,
 161  	}
 162  
 163  	err = d.client.CreateRecord(ctx, id, record)
 164  	if err != nil {
 165  		return fmt.Errorf("conohav3: failed to create record: %w", err)
 166  	}
 167  
 168  	return nil
 169  }
 170  
 171  // CleanUp clears ConoHa DNS TXT record.
 172  func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
 173  	info := dns01.GetChallengeInfo(domain, keyAuth)
 174  
 175  	authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
 176  	if err != nil {
 177  		return fmt.Errorf("conohav3: could not find zone for domain %q: %w", domain, err)
 178  	}
 179  
 180  	ctx := context.Background()
 181  
 182  	domID, err := d.client.GetDomainID(ctx, authZone)
 183  	if err != nil {
 184  		return fmt.Errorf("conohav3: failed to get domain ID: %w", err)
 185  	}
 186  
 187  	recID, err := d.client.GetRecordID(ctx, domID, info.EffectiveFQDN, "TXT", info.Value)
 188  	if err != nil {
 189  		return fmt.Errorf("conohav3: failed to get record ID: %w", err)
 190  	}
 191  
 192  	err = d.client.DeleteRecord(ctx, domID, recID)
 193  	if err != nil {
 194  		return fmt.Errorf("conohav3: failed to delete record: %w", err)
 195  	}
 196  
 197  	return nil
 198  }
 199  
 200  // Timeout returns the timeout and interval to use when checking for DNS propagation.
 201  func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
 202  	return d.config.PropagationTimeout, d.config.PollingInterval
 203  }
 204