2019-04-28 12:33:50 +00: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 01:20:01 +00: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 12:33:50 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00: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 12:33:50 +00:00
|
|
|
// Config is used to configure the creation of the DNSProvider.
|
|
|
|
type Config struct {
|
|
|
|
Debug bool
|
|
|
|
BaseURL string
|
|
|
|
APIKey string
|
2019-07-17 00:39:17 +00:00
|
|
|
Username string
|
|
|
|
Password string
|
2019-04-28 12:33:50 +00:00
|
|
|
PropagationTimeout time.Duration
|
|
|
|
PollingInterval time.Duration
|
|
|
|
TTL int
|
|
|
|
HTTPClient *http.Client
|
|
|
|
AuthSid string
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// NewDefaultConfig returns a default configuration for the DNSProvider.
|
2019-04-28 12:33:50 +00:00
|
|
|
func NewDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
BaseURL: defaultBaseURL,
|
2020-03-11 22:51:10 +00:00
|
|
|
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 12:33:50 +00:00
|
|
|
HTTPClient: &http.Client{
|
2020-03-11 22:51:10 +00:00
|
|
|
Timeout: env.GetOrDefaultSecond(EnvHTTPTimeout, 60*time.Second),
|
2019-04-28 12:33:50 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:35:25 +00:00
|
|
|
// DNSProvider implements the challenge.Provider interface.
|
2019-04-28 12:33:50 +00: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) {
|
2020-03-11 22:51:10 +00:00
|
|
|
values, err := env.Get(EnvAPIKey)
|
2019-04-28 12:33:50 +00:00
|
|
|
if err != nil {
|
2019-07-17 00:39:17 +00:00
|
|
|
var errU error
|
2020-03-11 22:51:10 +00:00
|
|
|
values, errU = env.Get(EnvUsername, EnvPassword)
|
2019-07-17 00:39:17 +00:00
|
|
|
if errU != nil {
|
|
|
|
return nil, fmt.Errorf("joker: %v or %v", errU, err)
|
|
|
|
}
|
2019-04-28 12:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config := NewDefaultConfig()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.APIKey = values[EnvAPIKey]
|
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.Password = values[EnvPassword]
|
2019-04-28 12:33:50 +00: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 == "" {
|
2019-07-17 00:39:17 +00:00
|
|
|
if config.Username == "" || config.Password == "" {
|
2020-02-27 18:14:46 +00:00
|
|
|
return nil, errors.New("joker: credentials missing")
|
2019-07-17 00:39:17 +00:00
|
|
|
}
|
2019-04-28 12:33:50 +00: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 18:14:46 +00:00
|
|
|
return fmt.Errorf("joker: %w", err)
|
2019-04-28 12:33:50 +00: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 18:14:46 +00:00
|
|
|
return fmt.Errorf("joker: %w", err)
|
2019-04-28 12:33:50 +00: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 17:35:25 +00:00
|
|
|
// formatResponseError formats error with optional details from DMAPI response.
|
2019-04-28 12:33:50 +00:00
|
|
|
func formatResponseError(response *response, err error) error {
|
|
|
|
if response != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("joker: DMAPI error: %w Response: %v", err, response.Headers)
|
2019-04-28 12:33:50 +00:00
|
|
|
}
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("joker: DMAPI error: %w", err)
|
2019-04-28 12:33:50 +00:00
|
|
|
}
|