1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-25 02:55:11 +02:00
lego/providers/dns/joker/joker.go

199 lines
5.4 KiB
Go
Raw Normal View History

2019-04-28 14:33:50 +02:00
// Package joker implements a DNS provider for solving the DNS-01 challenge using joker.com DMAPI.
package joker
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
2020-09-02 03:20:01 +02:00
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/platform/config/env"
2019-04-28 14:33:50 +02:00
)
// Environment variables names.
const (
envNamespace = "JOKER_"
EnvAPIKey = envNamespace + "API_KEY"
EnvUsername = envNamespace + "USERNAME"
EnvPassword = envNamespace + "PASSWORD"
EnvDebug = envNamespace + "DEBUG"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
)
2019-04-28 14:33:50 +02:00
// Config is used to configure the creation of the DNSProvider.
type Config struct {
Debug bool
BaseURL string
APIKey string
Username string
Password string
2019-04-28 14:33:50 +02:00
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
HTTPClient *http.Client
AuthSid string
}
2020-05-08 19:35:25 +02:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
2019-04-28 14:33:50 +02:00
func NewDefaultConfig() *Config {
return &Config{
BaseURL: defaultBaseURL,
Debug: env.GetOrDefaultBool(EnvDebug, false),
TTL: env.GetOrDefaultInt(EnvTTL, dns01.DefaultTTL),
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
2019-04-28 14:33:50 +02:00
HTTPClient: &http.Client{
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second),
2019-04-28 14:33:50 +02:00
},
}
}
2020-05-08 19:35:25 +02:00
// DNSProvider implements the challenge.Provider interface.
2019-04-28 14:33:50 +02:00
type DNSProvider struct {
config *Config
}
// NewDNSProvider returns a DNSProvider instance configured for Joker DMAPI.
// Credentials must be passed in the environment variable JOKER_API_KEY.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvAPIKey)
2019-04-28 14:33:50 +02:00
if err != nil {
var errU error
values, errU = env.Get(EnvUsername, EnvPassword)
if errU != nil {
return nil, fmt.Errorf("joker: %v or %v", errU, err)
}
2019-04-28 14:33:50 +02:00
}
config := NewDefaultConfig()
config.APIKey = values[EnvAPIKey]
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
2019-04-28 14:33:50 +02:00
return NewDNSProviderConfig(config)
}
// NewDNSProviderConfig return a DNSProvider instance configured for Joker DMAPI.
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("joker: the configuration of the DNS provider is nil")
}
if config.APIKey == "" {
if config.Username == "" || config.Password == "" {
2020-02-27 20:14:46 +02:00
return nil, errors.New("joker: credentials missing")
}
2019-04-28 14:33:50 +02:00
}
if !strings.HasSuffix(config.BaseURL, "/") {
config.BaseURL += "/"
}
return &DNSProvider{config: config}, nil
}
// Timeout returns the timeout and interval to use when checking for DNS propagation.
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
return d.config.PropagationTimeout, d.config.PollingInterval
}
// Present installs a TXT record for the DNS challenge.
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
fqdn, value := dns01.GetRecord(domain, keyAuth)
zone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("joker: %w", err)
2019-04-28 14:33:50 +02:00
}
relative := getRelative(fqdn, zone)
if d.config.Debug {
log.Infof("[%s] joker: adding TXT record %q to zone %q with value %q", domain, relative, zone, value)
}
response, err := d.login()
if err != nil {
return formatResponseError(response, err)
}
response, err = d.getZone(zone)
if err != nil || response.StatusCode != 0 {
return formatResponseError(response, err)
}
dnsZone := addTxtEntryToZone(response.Body, relative, value, d.config.TTL)
response, err = d.putZone(zone, dnsZone)
if err != nil || response.StatusCode != 0 {
return formatResponseError(response, err)
}
return nil
}
// CleanUp removes a TXT record used for a previous DNS challenge.
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
fqdn, _ := dns01.GetRecord(domain, keyAuth)
zone, err := dns01.FindZoneByFqdn(fqdn)
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("joker: %w", err)
2019-04-28 14:33:50 +02:00
}
relative := getRelative(fqdn, zone)
if d.config.Debug {
log.Infof("[%s] joker: removing entry %q from zone %q", domain, relative, zone)
}
response, err := d.login()
if err != nil {
return formatResponseError(response, err)
}
defer func() {
// Try to logout in case of errors
_, _ = d.logout()
}()
response, err = d.getZone(zone)
if err != nil || response.StatusCode != 0 {
return formatResponseError(response, err)
}
dnsZone, modified := removeTxtEntryFromZone(response.Body, relative)
if modified {
response, err = d.putZone(zone, dnsZone)
if err != nil || response.StatusCode != 0 {
return formatResponseError(response, err)
}
}
response, err = d.logout()
if err != nil {
return formatResponseError(response, err)
}
return nil
}
func getRelative(fqdn, zone string) string {
return dns01.UnFqdn(strings.TrimSuffix(fqdn, dns01.ToFqdn(zone)))
}
2020-05-08 19:35:25 +02:00
// formatResponseError formats error with optional details from DMAPI response.
2019-04-28 14:33:50 +02:00
func formatResponseError(response *response, err error) error {
if response != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("joker: DMAPI error: %w Response: %v", err, response.Headers)
2019-04-28 14:33:50 +02:00
}
2020-02-27 20:14:46 +02:00
return fmt.Errorf("joker: DMAPI error: %w", err)
2019-04-28 14:33:50 +02:00
}