1
0
mirror of https://github.com/go-acme/lego.git synced 2024-12-23 17:21:50 +02:00
lego/providers/dns/inwx/inwx.go

238 lines
6.3 KiB
Go
Raw Normal View History

2018-11-06 12:49:50 +02:00
// Package inwx implements a DNS provider for solving the DNS-01 challenge using inwx dom robot
2019-03-11 18:56:48 +02:00
package inwx
2018-11-06 12:49:50 +02:00
import (
"errors"
"fmt"
"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-02-04 23:43:10 +02:00
"github.com/nrdcg/goinwx"
"github.com/pquerna/otp/totp"
2018-11-06 12:49:50 +02: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 19:35:25 +02:00
// Config is used to configure the creation of the DNSProvider.
2018-11-06 12:49:50 +02:00
type Config struct {
Username string
Password string
SharedSecret string
2018-11-06 12:49:50 +02:00
Sandbox bool
PropagationTimeout time.Duration
PollingInterval time.Duration
TTL int
}
2020-05-08 19:35:25 +02:00
// NewDefaultConfig returns a default configuration for the DNSProvider.
2018-11-06 12:49:50 +02: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 12:49:50 +02:00
}
}
2020-05-08 19:35:25 +02:00
// DNSProvider implements the challenge.Provider interface.
2018-11-06 12:49:50 +02:00
type DNSProvider struct {
config *Config
client *goinwx.Client
previousUnlock time.Time
2018-11-06 12:49:50 +02:00
}
// 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 12:49:50 +02:00
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvUsername, EnvPassword)
2018-11-06 12:49:50 +02:00
if err != nil {
2020-02-27 20:14:46 +02:00
return nil, fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
config := NewDefaultConfig()
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
config.SharedSecret = env.GetOrFile(EnvSharedSecret)
2018-11-06 12:49:50 +02:00
return NewDNSProviderConfig(config)
}
2020-05-08 19:35:25 +02:00
// NewDNSProviderConfig return a DNSProvider instance configured for Dyn DNS.
2018-11-06 12:49:50 +02: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 20:14:46 +02:00
return nil, errors.New("inwx: credentials missing")
2018-11-06 12:49:50 +02: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 19:35:25 +02:00
// Present creates a TXT record using the specified parameters.
2018-11-06 12:49:50 +02:00
func (d *DNSProvider) Present(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
2018-11-06 12:49:50 +02:00
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
2018-11-06 12:49:50 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
2018-11-06 12:49:50 +02:00
}
info, err := d.client.Account.Login()
2018-11-06 12:49:50 +02:00
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
defer func() {
errL := d.client.Account.Logout()
if errL != nil {
log.Infof("inwx: failed to log out: %v", errL)
2018-11-06 12:49:50 +02:00
}
}()
err = d.twoFactorAuth(info)
if err != nil {
return fmt.Errorf("inwx: %w", err)
}
2020-07-10 01:48:18 +02:00
request := &goinwx.NameserverRecordRequest{
Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
2018-11-06 12:49:50 +02:00
Type: "TXT",
Content: challengeInfo.Value,
2019-02-04 23:43:10 +02:00
TTL: d.config.TTL,
2018-11-06 12:49:50 +02:00
}
_, err = d.client.Nameservers.CreateRecord(request)
if err != nil {
2020-10-27 13:01:05 +02:00
var er *goinwx.ErrorResponse
if errors.As(err, &er) {
if er.Message == "Object exists" {
2018-11-06 12:49:50 +02:00
return nil
}
2020-02-27 20:14:46 +02:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
2020-10-27 13:01:05 +02:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
return nil
}
2020-05-08 19:35:25 +02:00
// CleanUp removes the TXT record matching the specified parameters.
2018-11-06 12:49:50 +02:00
func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
challengeInfo := dns01.GetChallengeInfo(domain, keyAuth)
2018-11-06 12:49:50 +02:00
authZone, err := dns01.FindZoneByFqdn(challengeInfo.EffectiveFQDN)
2018-11-06 12:49:50 +02:00
if err != nil {
2023-05-05 09:49:38 +02:00
return fmt.Errorf("inwx: could not find zone for domain %q (%s): %w", domain, challengeInfo.EffectiveFQDN, err)
2018-11-06 12:49:50 +02:00
}
info, err := d.client.Account.Login()
2018-11-06 12:49:50 +02:00
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
defer func() {
errL := d.client.Account.Logout()
if errL != nil {
log.Infof("inwx: failed to log out: %v", errL)
2018-11-06 12:49:50 +02:00
}
}()
err = d.twoFactorAuth(info)
if err != nil {
return fmt.Errorf("inwx: %w", err)
}
2018-11-06 12:49:50 +02:00
response, err := d.client.Nameservers.Info(&goinwx.NameserverInfoRequest{
Domain: dns01.UnFqdn(authZone),
Name: dns01.UnFqdn(challengeInfo.EffectiveFQDN),
2018-11-06 12:49:50 +02:00
Type: "TXT",
})
if err != nil {
2020-02-27 20:14:46 +02:00
return fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02:00
}
var lastErr error
for _, record := range response.Records {
2019-02-04 23:43:10 +02:00
err = d.client.Nameservers.DeleteRecord(record.ID)
2018-11-06 12:49:50 +02:00
if err != nil {
2020-02-27 20:14:46 +02:00
lastErr = fmt.Errorf("inwx: %w", err)
2018-11-06 12:49:50 +02: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")
}
// INWX forbids re-authentication with a previously used TAN.
// To avoid using the same TAN twice, we wait until the next TOTP period.
sleep := d.computeSleep(time.Now())
if sleep != 0 {
log.Infof("inwx: waiting %s for next TOTP token", sleep)
time.Sleep(sleep)
}
now := time.Now()
tan, err := totp.GenerateCode(d.config.SharedSecret, now)
if err != nil {
return err
}
d.previousUnlock = now.Truncate(30 * time.Second)
return d.client.Account.Unlock(tan)
}
func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
if d.previousUnlock.IsZero() {
return 0
}
endPeriod := d.previousUnlock.Add(30 * time.Second)
if endPeriod.After(now) {
return endPeriod.Sub(now)
}
return 0
}