lego/providers/dns/joker/provider_svc.go

95 lines
2.9 KiB
Go
Raw Normal View History

2020-10-08 14:52:50 +00:00
package joker
import (
2023-05-05 07:49:38 +00:00
"context"
2020-10-08 14:52:50 +00:00
"errors"
"fmt"
"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/joker/internal/svc"
)
// svcProvider implements the challenge.Provider interface.
type svcProvider struct {
config *Config
client *svc.Client
}
// newSvcProvider returns a DNSProvider instance configured for Joker.
// Credentials must be passed in the environment variable: JOKER_USERNAME, JOKER_PASSWORD.
func newSvcProvider() (*svcProvider, error) {
values, err := env.Get(EnvUsername, EnvPassword)
if err != nil {
2020-10-27 11:01:05 +00:00
return nil, fmt.Errorf("joker: %w", err)
2020-10-08 14:52:50 +00:00
}
config := NewDefaultConfig()
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
return newSvcProviderConfig(config)
}
// newSvcProviderConfig return a DNSProvider instance configured for Joker.
func newSvcProviderConfig(config *Config) (*svcProvider, error) {
if config == nil {
return nil, errors.New("joker: the configuration of the DNS provider is nil")
}
if config.Username == "" || config.Password == "" {
return nil, errors.New("joker: credentials missing")
}
client := svc.NewClient(config.Username, config.Password)
return &svcProvider{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 *svcProvider) Timeout() (timeout, interval time.Duration) {
return d.config.PropagationTimeout, d.config.PollingInterval
}
// Present creates a TXT record using the specified parameters.
func (d *svcProvider) Present(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2020-10-08 14:52:50 +00:00
zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
2020-10-08 14:52:50 +00:00
if err != nil {
return fmt.Errorf("joker: could not find zone for domain %q: %w", domain, err)
2020-10-08 14:52:50 +00:00
}
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zone)
if err != nil {
return fmt.Errorf("joker: %w", err)
}
2020-10-08 14:52:50 +00:00
2023-05-05 07:49:38 +00:00
return d.client.SendRequest(context.Background(), dns01.UnFqdn(zone), subDomain, info.Value)
2020-10-08 14:52:50 +00:00
}
// CleanUp removes the TXT record matching the specified parameters.
func (d *svcProvider) CleanUp(domain, token, keyAuth string) error {
info := dns01.GetChallengeInfo(domain, keyAuth)
2020-10-08 14:52:50 +00:00
zone, err := dns01.FindZoneByFqdn(info.EffectiveFQDN)
2020-10-08 14:52:50 +00:00
if err != nil {
return fmt.Errorf("joker: could not find zone for domain %q: %w", domain, err)
2020-10-08 14:52:50 +00:00
}
subDomain, err := dns01.ExtractSubDomain(info.EffectiveFQDN, zone)
if err != nil {
return fmt.Errorf("joker: %w", err)
}
2020-10-08 14:52:50 +00:00
2023-05-05 07:49:38 +00:00
return d.client.SendRequest(context.Background(), dns01.UnFqdn(zone), subDomain, "")
2020-10-08 14:52:50 +00:00
}
// Sequential All DNS challenges for this provider will be resolved sequentially.
// Returns the interval between each iteration.
func (d *svcProvider) Sequential() time.Duration {
return d.config.SequenceInterval
}