2018-12-06 23:50:17 +02:00
|
|
|
// Package glesys implements a DNS provider for solving the DNS-01 challenge using GleSYS api.
|
2018-03-31 16:33:48 +02:00
|
|
|
package glesys
|
|
|
|
|
|
|
|
import (
|
2023-05-05 09:49:38 +02:00
|
|
|
"context"
|
2018-09-15 19:07:24 +02:00
|
|
|
"errors"
|
2018-03-31 16:33:48 +02:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2024-11-16 00:21:21 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge"
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/challenge/dns01"
|
|
|
|
"github.com/go-acme/lego/v4/platform/config/env"
|
2023-05-05 09:49:38 +02:00
|
|
|
"github.com/go-acme/lego/v4/providers/dns/glesys/internal"
|
2018-03-31 16:33:48 +02:00
|
|
|
)
|
|
|
|
|
2020-03-12 00:51:10 +02:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "GLESYS_"
|
|
|
|
|
|
|
|
EnvAPIUser = envNamespace + "API_USER"
|
|
|
|
EnvAPIKey = envNamespace + "API_KEY"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
2024-11-16 00:21:21 +02:00
|
|
|
const minTTL = 60
|
|
|
|
|
|
|
|
var _ challenge.ProviderTimeout = (*DNSProvider)(nil)
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
type Config struct {
|
|
|
|
APIUser string
|
|
|
|
APIKey string
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2018-09-15 19:07:24 +02:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2020-03-12 00:51:10 +02:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, minTTL),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 20*time.Minute),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 20*time.Second),
|
2018-09-15 19:07:24 +02:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-12 00:51:10 +02:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 10*time.Second),
|
2018-09-15 19:07:24 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 16:33:48 +02:00
|
|
|
|
2020-05-08 19:35:25 +02:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2018-03-31 16:33:48 +02:00
|
|
|
type DNSProvider struct {
|
2023-05-05 09:49:38 +02:00
|
|
|
config *Config
|
|
|
|
client *internal.Client
|
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
activeRecords map[string]int
|
|
|
|
inProgressMu sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for GleSYS.
|
2018-09-15 19:07:24 +02:00
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// GLESYS_API_USER and GLESYS_API_KEY.
|
2018-03-31 16:33:48 +02:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-12 00:51:10 +02:00
|
|
|
values, err := env.Get(EnvAPIUser, EnvAPIKey)
|
2018-06-11 17:32:50 +02:00
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, fmt.Errorf("glesys: %w", err)
|
2018-06-11 17:32:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
config := NewDefaultConfig()
|
2020-03-12 00:51:10 +02:00
|
|
|
config.APIUser = values[EnvAPIUser]
|
|
|
|
config.APIKey = values[EnvAPIKey]
|
2018-09-15 19:07:24 +02:00
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:07:24 +02:00
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for GleSYS.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config == nil {
|
|
|
|
return nil, errors.New("glesys: the configuration of the DNS provider is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.APIUser == "" || config.APIKey == "" {
|
2020-02-27 20:14:46 +02:00
|
|
|
return nil, errors.New("glesys: incomplete credentials provided")
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|
2018-06-21 19:06:16 +02:00
|
|
|
|
2018-10-12 19:29:18 +02:00
|
|
|
if config.TTL < minTTL {
|
|
|
|
return nil, fmt.Errorf("glesys: invalid TTL, TTL (%d) must be greater than %d", config.TTL, minTTL)
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
client := internal.NewClient(config.APIUser, config.APIKey)
|
|
|
|
|
|
|
|
if config.HTTPClient != nil {
|
|
|
|
client.HTTPClient = config.HTTPClient
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
return &DNSProvider{
|
2018-10-11 15:49:33 +02:00
|
|
|
config: config,
|
2023-05-05 09:49:38 +02:00
|
|
|
client: client,
|
2018-03-31 16:33:48 +02:00
|
|
|
activeRecords: make(map[string]int),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-09-15 19:07:24 +02:00
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
// find authZone
|
2023-03-07 10:39:05 +02:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
|
2018-03-31 16:33:48 +02:00
|
|
|
if err != nil {
|
2024-02-11 15:37:09 +02:00
|
|
|
return fmt.Errorf("glesys: could not find zone for domain %q: %w", domain, err)
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
2022-11-28 19:53:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("glesys: %w", err)
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2023-05-05 09:49:38 +02:00
|
|
|
// acquire lock and check there is not a challenge already in progress for this value of authZone
|
2018-03-31 16:33:48 +02:00
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
// add TXT record into authZone
|
2023-05-05 09:49:38 +02:00
|
|
|
recordID, err := d.client.AddTXTRecord(context.Background(), dns01.UnFqdn(authZone), subDomain, info.Value, d.config.TTL)
|
2018-03-31 16:33:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
// save data necessary for CleanUp
|
2023-03-07 10:39:05 +02:00
|
|
|
d.activeRecords[info.EffectiveFQDN] = recordID
|
2018-03-31 16:33:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
2023-03-07 10:39:05 +02:00
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
// acquire lock and retrieve authZone
|
|
|
|
d.inProgressMu.Lock()
|
|
|
|
defer d.inProgressMu.Unlock()
|
2023-03-07 10:39:05 +02:00
|
|
|
if _, ok := d.activeRecords[info.EffectiveFQDN]; !ok {
|
2018-03-31 16:33:48 +02:00
|
|
|
// if there is no cleanup information then just return
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2023-03-07 10:39:05 +02:00
|
|
|
recordID := d.activeRecords[info.EffectiveFQDN]
|
|
|
|
delete(d.activeRecords, info.EffectiveFQDN)
|
2018-05-30 19:53:04 +02:00
|
|
|
|
2018-03-31 16:33:48 +02:00
|
|
|
// delete TXT record from authZone
|
2023-05-05 09:49:38 +02:00
|
|
|
return d.client.DeleteTXTRecord(context.Background(), recordID)
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Timeout returns the values (20*time.Minute, 20*time.Second) which
|
|
|
|
// are used by the acme package as timeout and check interval values
|
|
|
|
// when checking for DNS record propagation with GleSYS.
|
|
|
|
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
|
2018-09-15 19:07:24 +02:00
|
|
|
return d.config.PropagationTimeout, d.config.PollingInterval
|
2018-03-31 16:33:48 +02:00
|
|
|
}
|