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"
|
2020-05-31 13:18:43 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2018-11-06 10:49:50 +00:00
|
|
|
)
|
|
|
|
|
2020-03-11 22:51:10 +00:00
|
|
|
// Environment variables names.
|
|
|
|
const (
|
|
|
|
envNamespace = "INWX_"
|
|
|
|
|
2020-05-31 13:18:43 +00:00
|
|
|
EnvUsername = envNamespace + "USERNAME"
|
|
|
|
EnvPassword = envNamespace + "PASSWORD"
|
|
|
|
EnvSharedSecret = envNamespace + "SHARED_SECRET"
|
|
|
|
EnvSandbox = envNamespace + "SANDBOX"
|
2020-03-11 22:51:10 +00:00
|
|
|
|
|
|
|
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
|
2020-05-31 13:18:43 +00:00
|
|
|
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{
|
2020-03-11 22:51:10 +00:00
|
|
|
TTL: env.GetOrDefaultInt(EnvTTL, 300),
|
|
|
|
PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout),
|
|
|
|
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:
|
2020-05-31 13:18:43 +00:00
|
|
|
// INWX_USERNAME, INWX_PASSWORD, and INWX_SHARED_SECRET.
|
2018-11-06 10:49:50 +00:00
|
|
|
func NewDNSProvider() (*DNSProvider, error) {
|
2020-03-11 22:51:10 +00:00
|
|
|
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()
|
2020-03-11 22:51:10 +00:00
|
|
|
config.Username = values[EnvUsername]
|
|
|
|
config.Password = values[EnvPassword]
|
2020-05-31 13:18:43 +00:00
|
|
|
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 {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, value := dns01.GetRecord(domain, keyAuth)
|
2018-11-06 10:49:50 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
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
|
|
|
}
|
|
|
|
|
2020-05-31 13:18:43 +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 logout: %v", errL)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-31 13:18:43 +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{
|
2018-12-06 21:50:17 +00:00
|
|
|
Domain: dns01.UnFqdn(authZone),
|
|
|
|
Name: dns01.UnFqdn(fqdn),
|
2018-11-06 10:49:50 +00:00
|
|
|
Type: "TXT",
|
|
|
|
Content: 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) {
|
2018-12-06 21:50:17 +00:00
|
|
|
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 {
|
2018-12-06 21:50:17 +00:00
|
|
|
fqdn, _ := dns01.GetRecord(domain, keyAuth)
|
2018-11-06 10:49:50 +00:00
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
authZone, err := dns01.FindZoneByFqdn(fqdn)
|
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
|
|
|
}
|
|
|
|
|
2020-05-31 13:18:43 +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 logout: %v", errL)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-31 13:18:43 +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{
|
2018-12-06 21:50:17 +00:00
|
|
|
Domain: dns01.UnFqdn(authZone),
|
|
|
|
Name: dns01.UnFqdn(fqdn),
|
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
|
|
|
|
}
|
2020-05-31 13:18:43 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|