2024-07-21 11:32:17 +00:00
|
|
|
package directadmin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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/directadmin/internal"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "DIRECTADMIN_"
|
|
|
|
|
|
|
|
EnvAPIURL = envNamespace + "API_URL"
|
|
|
|
EnvUsername = envNamespace + "USERNAME"
|
|
|
|
EnvPassword = envNamespace + "PASSWORD"
|
|
|
|
EnvZoneName = envNamespace + "ZONE_NAME"
|
|
|
|
|
|
|
|
EnvTTL = envNamespace + "TTL"
|
|
|
|
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
|
|
|
|
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
|
|
|
|
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
2024-07-21 13:06:01 +00:00
|
|
|
BaseURL string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
|
|
|
|
ZoneName string
|
|
|
|
|
2024-07-21 11:32:17 +00:00
|
|
|
TTL int
|
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
HTTPClient *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
2024-07-21 13:06:01 +00:00
|
|
|
ZoneName: env.GetOrFile(EnvZoneName),
|
2024-07-21 11:32:17 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 30),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 60*time.Second),
|
|
|
|
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, 5*time.Second),
|
|
|
|
HTTPClient: &http.Client{
|
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 30*time.Second),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
|
|
|
type DNSProvider struct {
|
|
|
|
client *internal.Client
|
|
|
|
config *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProvider returns a DNSProvider instance configured for DirectAdmin.
|
|
|
|
// Credentials must be passed in the environment variables:
|
|
|
|
// DIRECTADMIN_API_URL, DIRECTADMIN_USERNAME, DIRECTADMIN_PASSWORD.
|
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
|
|
|
values, err := env.Get(EnvAPIURL, EnvUsername, EnvPassword)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directadmin: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
|
|
|
config.BaseURL = values[EnvAPIURL]
|
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.Password = values[EnvPassword]
|
|
|
|
|
|
|
|
return NewDNSProviderConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDNSProviderConfig return a DNSProvider instance configured for DirectAdmin.
|
|
|
|
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
|
|
|
|
if config.BaseURL == "" {
|
|
|
|
return nil, errors.New("directadmin: missing API URL")
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Username == "" || config.Password == "" {
|
|
|
|
return nil, errors.New("directadmin: some credentials information are missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := internal.NewClient(config.BaseURL, config.Username, config.Password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("directadmin: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DNSProvider{client: client, config: config}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Present creates a TXT record using the specified parameters.
|
|
|
|
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
|
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
|
|
|
|
2024-07-21 13:06:01 +00:00
|
|
|
authZone, err := d.getZoneName(info.EffectiveFQDN)
|
2024-07-21 11:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
record := internal.Record{
|
|
|
|
Name: subDomain,
|
|
|
|
Type: "TXT",
|
|
|
|
Value: info.Value,
|
|
|
|
TTL: d.config.TTL,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.client.SetRecord(context.Background(), dns01.UnFqdn(authZone), record)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: set record for zone %s and subdomain %s: %w", authZone, subDomain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanUp removes the TXT record matching the specified parameters.
|
|
|
|
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
|
|
|
|
info := dns01.GetChallengeInfo(domain, keyAuth)
|
|
|
|
|
2024-07-21 13:06:01 +00:00
|
|
|
authZone, err := d.getZoneName(info.EffectiveFQDN)
|
2024-07-21 11:32:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: [domain: %q] %w", domain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, authZone)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
record := internal.Record{
|
|
|
|
Name: subDomain,
|
|
|
|
Type: "TXT",
|
|
|
|
Value: info.Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.client.DeleteRecord(context.Background(), dns01.UnFqdn(authZone), record)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("directadmin: delete record for zone %s and subdomain %s: %w", authZone, subDomain, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-21 13:06:01 +00:00
|
|
|
func (d *DNSProvider) getZoneName(fqdn string) (string, error) {
|
|
|
|
if d.config.ZoneName != "" {
|
|
|
|
return d.config.ZoneName, nil
|
2024-07-21 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("could not find zone for %s: %w", fqdn, err)
|
|
|
|
}
|
|
|
|
|
2024-07-21 13:06:01 +00:00
|
|
|
if authZone == "" {
|
|
|
|
return "", errors.New("empty zone name")
|
|
|
|
}
|
|
|
|
|
2024-07-21 11:32:17 +00:00
|
|
|
return authZone, nil
|
|
|
|
}
|