lego/providers/dns/inwx/inwx.go

212 lines
5.7 KiB
Go
Raw Normal View History

2018-11-06 10:49:50 +00:00
// Package inwx implements a DNS provider for solving the DNS-01 challenge using inwx dom robot
2019-03-11 16:56:48 +00:00
package inwx
2018-11-06 10:49:50 +00:00
import (
"errors"
"fmt"
"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-02-04 21:43:10 +00:00
"github.com/nrdcg/goinwx"
"github.com/pquerna/otp/totp"
2018-11-06 10:49:50 +00:00
)
// Environment variables names.
const (
envNamespace = "INWX_"
EnvUsername = envNamespace + "USERNAME"
EnvPassword = envNamespace + "PASSWORD"
EnvSharedSecret = envNamespace + "SHARED_SECRET"
EnvSandbox = envNamespace + "SANDBOX"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
)
2020-05-08 17:35:25 +00:00
// Config is used to configure the creation of the DNSProvider.
2018-11-06 10:49:50 +00:00
type Config struct {
Username string
Password string
SharedSecret string
2018-11-06 10:49:50 +00:00
Sandbox bool
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}
2020-05-08 17:35:25 +00:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
2018-11-06 10:49:50 +00:00
func NewDefaultConfig() *Config {
return &Config{
TTL: env.GetOrDefaultInt(EnvTTL, 300),
// INWX has rather unstable propagation delays, thus using a larger default value
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, 360*time.Second),
PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval),
Sandbox: env.GetOrDefaultBool(EnvSandbox, false),
2018-11-06 10:49:50 +00:00
}
}
2020-05-08 17:35:25 +00:00
// DNSProvider implements the challenge.Provider interface.
2018-11-06 10:49:50 +00:00
type DNSProvider struct {
config *Config
client *goinwx.Client
}
// NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
// Credentials must be passed in the environment variables:
// INWX_USERNAME, INWX_PASSWORD, and INWX_SHARED_SECRET.
2018-11-06 10:49:50 +00:00
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvUsername, EnvPassword)
2018-11-06 10:49:50 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return nil, fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
config := NewDefaultConfig()
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
config.SharedSecret = env.GetOrFile(EnvSharedSecret)
2018-11-06 10:49:50 +00:00
return NewDNSProviderConfig(config)
}
2020-05-08 17:35:25 +00:00
// NewDNSProviderConfig return a DNSProvider instance configured for Dyn DNS.
2018-11-06 10:49:50 +00:00
func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
if config == nil {
return nil, errors.New("inwx: the configuration of the DNS provider is nil")
}
if config.Username == "" || config.Password == "" {
2020-02-27 18:14:46 +00:00
return nil, errors.New("inwx: credentials missing")
2018-11-06 10:49:50 +00:00
}
if config.Sandbox {
log.Infof("inwx: sandbox mode is enabled")
}
client := goinwx.NewClient(config.Username, config.Password, &goinwx.ClientOptions{Sandbox: config.Sandbox})
return &DNSProvider{config: config, client: client}, nil
}
2020-05-08 17:35:25 +00:00
// Present creates a TXT record using the specified parameters.
2018-11-06 10:49:50 +00:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
2018-11-06 10:49:50 +00:00
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
2018-11-06 10:49:50 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
2018-11-06 10:49:50 +00:00
}
info, err := d.client.Account.Login()
2018-11-06 10:49:50 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
defer func() {
errL := d.client.Account.Logout()
if errL != nil {
log.Infof("inwx: failed to log out: %v", errL)
2018-11-06 10:49:50 +00:00
}
}()
err = d.twoFactorAuth(info)
if err != nil {
return fmt.Errorf("inwx: %w", err)
}
2020-07-09 23:48:18 +00:00
request := &goinwx.NameserverRecordRequest{
Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
2018-11-06 10:49:50 +00:00
Type: "TXT",
Content: challengeInfo.Value,
2019-02-04 21:43:10 +00:00
TTL: d.config.TTL,
2018-11-06 10:49:50 +00:00
}
_, err = d.client.Nameservers.CreateRecord(request)
if err != nil {
2020-10-27 11:01:05 +00:00
var er *goinwx.ErrorResponse
if errors.As(err, &er) {
if er.Message == "Object exists" {
2018-11-06 10:49:50 +00:00
return nil
}
2020-02-27 18:14:46 +00:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
2020-10-27 11:01:05 +00:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
return nil
}
2020-05-08 17:35:25 +00:00
// CleanUp removes the TXT record matching the specified parameters.
2018-11-06 10:49:50 +00:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
2018-11-06 10:49:50 +00:00
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
2018-11-06 10:49:50 +00:00
if err != nil {
2023-05-05 07:49:38 +00:00
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
2018-11-06 10:49:50 +00:00
}
info, err := d.client.Account.Login()
2018-11-06 10:49:50 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
defer func() {
errL := d.client.Account.Logout()
if errL != nil {
log.Infof("inwx: failed to log out: %v", errL)
2018-11-06 10:49:50 +00:00
}
}()
err = d.twoFactorAuth(info)
if err != nil {
return fmt.Errorf("inwx: %w", err)
}
2018-11-06 10:49:50 +00:00
response, err := d.client.Nameservers.Info(&goinwx.NameserverInfoRequest{
Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
2018-11-06 10:49:50 +00:00
Type: "TXT",
})
if err != nil {
2020-02-27 18:14:46 +00:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
var lastErr error
for _, record := range response.Records {
2019-02-04 21:43:10 +00:00
err = d.client.Nameservers.DeleteRecord(record.ID)
2018-11-06 10:49:50 +00:00
if err != nil {
2020-02-27 18:14:46 +00:00
lastErr = fmt.Errorf("inwx: %w", err)
2018-11-06 10:49:50 +00:00
}
}
return lastErr
}
// Timeout returns the timeout and interval to use when checking for DNS propagation.
// Adjusting here to cope with spikes in propagation times.
func (d *DNSProvider) Timeout() (timeout, interval time.Duration) {
return d.config.PropagationTimeout, d.config.PollingInterval
}
func (d *DNSProvider) twoFactorAuth(info *goinwx.LoginResponse) error {
if info.TFA != "GOOGLE-AUTH" {
return nil
}
if d.config.SharedSecret == "" {
return errors.New("two-factor authentication but no shared secret is given")
}
tan, err := totp.GenerateCode(d.config.SharedSecret, time.Now())
if err != nil {
return err
}
return d.client.Account.Unlock(tan)
}