2020-02-25 20:56:40 +00:00
|
|
|
// Package constellix implements a DNS provider for solving the DNS-01 challenge using Constellix DNS.
|
|
|
|
package constellix
|
|
|
|
|
|
|
|
import (
|
2023-05-05 07:49:38 +00:00
|
|
|
"context"
|
2020-02-25 20:56:40 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-01-23 14:18:54 +00:00
|
|
|
"strconv"
|
2020-02-25 20:56:40 +00:00
|
|
|
"time"
|
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
|
|
|
"github.com/go-acme/lego/v4/providers/dns/constellix/internal"
|
2024-01-23 14:18:54 +00:00
|
|
|
"github.com/hashicorp/go-retryablehttp"
|
2020-02-25 20:56:40 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "CONSTELLIX_"
|
|
|
|
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
EnvSecretKey = envNamespace + "SECRET_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2020-02-25 20:56:40 +00:00
|
|
|
type Config struct {
|
|
|
|
APIKey string
|
|
|
|
SecretKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2020-02-25 20:56:40 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-06-27 19:28:03 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 60),
|
2020-03-11 22:51:10 +00:00
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
2020-06-27 19:28:03 +00:00
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 10*time.Second),
|
2020-02-25 20:56:40 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
2020-02-25 20:56:40 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2020-02-25 20:56:40 +00:00
|
|
|
type DNSProvider struct {
|
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for Constellix.
|
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// CONSTELLIX_API_KEY and CONSTELLIX_SECRET_KEY.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey, EnvSecretKey)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("constellix: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
config.SecretKey = values[EnvSecretKey]
|
2020-02-25 20:56:40 +00:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for Constellix.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("constellix: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SecretKey == "" || config.APIKey == "" {
|
|
|
|
return nil, errors.New("constellix: incomplete credentials, missing secret key and/or API key")
|
|
|
|
}
|
|
|
|
|
|
|
|
tr, err := internal.NewTokenTransport(config.APIKey, config.SecretKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("constellix: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-01-23 14:18:54 +00:00
|
|
|
retryClient := retryablehttp.NewClient()
|
|
|
|
retryClient.RetryMax = 5
|
|
|
|
retryClient.HTTPClient = tr.Wrap(config.HTTPClient)
|
|
|
|
retryClient.Backoff = backoff
|
|
|
|
|
|
|
|
client := internal.NewClient(retryClient.StandardClient())
|
2020-02-25 20:56:40 +00:00
|
|
|
|
|
|
|
return &DNSProvider{config: config, client: client}, 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
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// Present creates a TXT record using the specified parameters.
|
2020-02-25 20:56:40 +00:00
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("constellix: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dom, err := d.client.Domains.GetByName(ctx, dns01.UnFqdn(authZone))
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("constellix: failed to get domain (%s): %w", authZone, err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
recordName, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constellix: %w", err)
|
|
|
|
}
|
2020-04-18 18:39:10 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
records, err := d.client.TxtRecords.Search(ctx, dom.ID, internal.Exact, recordName)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("constellix: failed to search TXT records: %w", err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
if len(records) > 1 {
|
|
|
|
return errors.New("constellix: failed to get TXT records")
|
|
|
|
}
|
2020-02-25 20:56:40 +00:00
|
|
|
|
|
|
|
// TXT record entry already existing
|
2020-04-18 18:39:10 +00:00
|
|
|
if len(records) == 1 {
|
2023-05-05 07:49:38 +00:00
|
|
|
return d.appendRecordValue(ctx, dom, records[0].ID, info.Value)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.createRecord(ctx, dom, info.EffectiveFQDN, recordName, info.Value)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("constellix: %w", err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
2020-02-25 20:56:40 +00:00
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 08:39:05 +00:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2023-05-05 07:49:38 +00:00
|
|
|
return fmt.Errorf("constellix: could not find zone for domain %q (%s): %w", domain, info.EffectiveFQDN, err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dom, err := d.client.Domains.GetByName(ctx, dns01.UnFqdn(authZone))
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("constellix: failed to get domain (%s): %w", authZone, err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
recordName, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 17:53:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constellix: %w", err)
|
|
|
|
}
|
2020-04-18 18:39:10 +00:00
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
records, err := d.client.TxtRecords.Search(ctx, dom.ID, internal.Exact, recordName)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("constellix: failed to search TXT records: %w", err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
if len(records) > 1 {
|
|
|
|
return errors.New("constellix: failed to get TXT records")
|
|
|
|
}
|
2020-02-25 20:56:40 +00:00
|
|
|
|
2020-04-18 18:39:10 +00:00
|
|
|
if len(records) == 0 {
|
2020-02-25 20:56:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
record, err := d.client.TxtRecords.Get(ctx, dom.ID, records[0].ID)
|
2020-06-27 19:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constellix: failed to get TXT records: %w", err)
|
|
|
|
}
|
2020-04-18 18:39:10 +00:00
|
|
|
|
2023-03-07 08:39:05 +00:00
|
|
|
if !containsValue(record, info.Value) {
|
2020-02-25 20:56:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// only 1 record value, the whole record must be deleted.
|
|
|
|
if len(record.Value) == 1 {
|
2023-05-05 07:49:38 +00:00
|
|
|
_, err = d.client.TxtRecords.Delete(ctx, dom.ID, record.ID)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constellix: failed to delete TXT records: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
err = d.removeRecordValue(ctx, dom, record, info.Value)
|
2020-06-27 19:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("constellix: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (d *DNSProvider) createRecord(ctx context.Context, dom internal.Domain, fqdn, recordName, value string) error {
|
2020-06-27 19:28:03 +00:00
|
|
|
request := internal.RecordRequest{
|
|
|
|
Name: recordName,
|
|
|
|
TTL: d.config.TTL,
|
|
|
|
RoundRobin: []internal.RecordValue{
|
2021-11-03 19:39:12 +00:00
|
|
|
{Value: fmt.Sprintf(`%q`, value)},
|
2020-06-27 19:28:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
_, err := d.client.TxtRecords.Create(ctx, dom.ID, request)
|
2020-06-27 19:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create TXT record %s: %w", fqdn, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (d *DNSProvider) appendRecordValue(ctx context.Context, dom internal.Domain, recordID int64, value string) error {
|
|
|
|
record, err := d.client.TxtRecords.Get(ctx, dom.ID, recordID)
|
2020-06-27 19:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get TXT records: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if containsValue(record, value) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
request := internal.RecordRequest{
|
|
|
|
Name: record.Name,
|
|
|
|
TTL: record.TTL,
|
2021-11-03 19:39:12 +00:00
|
|
|
RoundRobin: append(record.RoundRobin, internal.RecordValue{Value: fmt.Sprintf(`%q`, value)}),
|
2020-06-27 19:28:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
_, err = d.client.TxtRecords.Update(ctx, dom.ID, record.ID, request)
|
2020-06-27 19:28:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update TXT records: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
func (d *DNSProvider) removeRecordValue(ctx context.Context, dom internal.Domain, record *internal.Record, value string) error {
|
2020-02-25 20:56:40 +00:00
|
|
|
request := internal.RecordRequest{
|
|
|
|
Name: record.Name,
|
|
|
|
TTL: record.TTL,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range record.Value {
|
2021-11-03 19:39:12 +00:00
|
|
|
if val.Value != fmt.Sprintf(`%q`, value) {
|
2020-02-25 20:56:40 +00:00
|
|
|
request.RoundRobin = append(request.RoundRobin, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 07:49:38 +00:00
|
|
|
_, err := d.client.TxtRecords.Update(ctx, dom.ID, record.ID, request)
|
2020-02-25 20:56:40 +00:00
|
|
|
if err != nil {
|
2020-06-27 19:28:03 +00:00
|
|
|
return fmt.Errorf("failed to update TXT records: %w", err)
|
2020-02-25 20:56:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:28:03 +00:00
|
|
|
func containsValue(record *internal.Record, value string) bool {
|
|
|
|
if record == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-25 20:56:40 +00:00
|
|
|
for _, val := range record.Value {
|
2021-11-03 19:39:12 +00:00
|
|
|
if val.Value == fmt.Sprintf(`%q`, value) {
|
2020-02-25 20:56:40 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2024-01-23 14:18:54 +00:00
|
|
|
|
|
|
|
func backoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
|
|
|
|
if resp != nil {
|
|
|
|
// https://api.dns.constellix.com/v4/docs#section/Using-the-API/Rate-Limiting
|
|
|
|
if resp.StatusCode == http.StatusTooManyRequests {
|
|
|
|
if s, ok := resp.Header["X-Ratelimit-Reset"]; ok {
|
|
|
|
if sleep, err := strconv.ParseInt(s[0], 10, 64); err == nil {
|
|
|
|
return time.Second * time.Duration(sleep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryablehttp.DefaultBackoff(min, max, attemptNum, resp)
|
|
|
|
}
|