2021-01-14 19:42:12 +00:00
// Package ionos implements a DNS provider for solving the DNS-01 challenge using Ionos/1&1.
package ionos
import (
"context"
"errors"
"fmt"
"net/http"
2024-01-17 21:18:17 +00:00
"strconv"
2021-01-14 19:42:12 +00:00
"strings"
"time"
"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/platform/config/env"
"github.com/go-acme/lego/v4/providers/dns/ionos/internal"
)
const minTTL = 300
// Environment variables names.
const (
envNamespace = "IONOS_"
EnvAPIKey = envNamespace + "API_KEY"
EnvTTL = envNamespace + "TTL"
EnvPropagationTimeout = envNamespace + "PROPAGATION_TIMEOUT"
EnvPollingInterval = envNamespace + "POLLING_INTERVAL"
EnvHTTPTimeout = envNamespace + "HTTP_TIMEOUT"
)
// Config is used to configure the creation of the DNSProvider.
type Config struct {
APIKey string
PropagationTimeout time . Duration
PollingInterval time . Duration
TTL int
HTTPClient * http . Client
}
// NewDefaultConfig returns a default configuration for the DNSProvider.
func NewDefaultConfig ( ) * Config {
return & Config {
TTL : env . GetOrDefaultInt ( EnvTTL , minTTL ) ,
PropagationTimeout : env . GetOrDefaultSecond ( EnvPropagationTimeout , dns01 . DefaultPropagationTimeout ) ,
PollingInterval : env . GetOrDefaultSecond ( EnvPollingInterval , dns01 . DefaultPollingInterval ) ,
HTTPClient : & http . Client {
Timeout : env . GetOrDefaultSecond ( EnvHTTPTimeout , 30 * time . Second ) ,
} ,
}
}
// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
config * Config
client * internal . Client
}
// NewDNSProvider returns a DNSProvider instance configured for Ionos.
// Credentials must be passed in the environment variables: IONOS_API_KEY.
func NewDNSProvider ( ) ( * DNSProvider , error ) {
values , err := env . Get ( EnvAPIKey )
if err != nil {
return nil , fmt . Errorf ( "ionos: %w" , err )
}
config := NewDefaultConfig ( )
config . APIKey = values [ EnvAPIKey ]
return NewDNSProviderConfig ( config )
}
// NewDNSProviderConfig return a DNSProvider instance configured for Ionos.
func NewDNSProviderConfig ( config * Config ) ( * DNSProvider , error ) {
if config == nil {
return nil , errors . New ( "ionos: the configuration of the DNS provider is nil" )
}
if config . APIKey == "" {
return nil , errors . New ( "ionos: credentials missing" )
}
if config . TTL < minTTL {
return nil , fmt . Errorf ( "ionos: invalid TTL, TTL (%d) must be greater than %d" , config . TTL , minTTL )
}
client , err := internal . NewClient ( config . APIKey )
if err != nil {
return nil , fmt . Errorf ( "ionos: %w" , err )
}
if config . HTTPClient != nil {
client . HTTPClient = config . HTTPClient
}
2023-05-05 07:49:38 +00:00
return & DNSProvider { config : config , client : client } , nil
2021-01-14 19:42:12 +00:00
}
// 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
}
// Present creates a TXT record using the specified parameters.
func ( d * DNSProvider ) Present ( domain , _ , keyAuth string ) error {
2023-03-07 08:39:05 +00:00
info := dns01 . GetChallengeInfo ( domain , keyAuth )
2021-01-14 19:42:12 +00:00
ctx := context . Background ( )
zones , err := d . client . ListZones ( ctx )
if err != nil {
return fmt . Errorf ( "ionos: failed to get zones: %w" , err )
}
2024-09-19 13:29:20 +00:00
name := dns01 . UnFqdn ( info . EffectiveFQDN )
zone := findZone ( zones , name )
2021-01-14 19:42:12 +00:00
if zone == nil {
return errors . New ( "ionos: no matching zone found for domain" )
}
filter := & internal . RecordsFilter {
2024-09-19 13:29:20 +00:00
Suffix : name ,
2021-01-14 19:42:12 +00:00
RecordType : "TXT" ,
}
records , err := d . client . GetRecords ( ctx , zone . ID , filter )
if err != nil {
return fmt . Errorf ( "ionos: failed to get records (zone=%s): %w" , zone . ID , err )
}
records = append ( records , internal . Record {
2024-09-19 13:29:20 +00:00
Name : name ,
2023-03-07 08:39:05 +00:00
Content : info . Value ,
2021-01-14 19:42:12 +00:00
TTL : d . config . TTL ,
Type : "TXT" ,
} )
err = d . client . ReplaceRecords ( ctx , zone . ID , records )
if err != nil {
return fmt . Errorf ( "ionos: failed to create/update records (zone=%s): %w" , zone . ID , err )
}
return nil
}
// CleanUp removes the TXT record matching the specified parameters.
func ( d * DNSProvider ) CleanUp ( domain , _ , keyAuth string ) error {
2023-03-07 08:39:05 +00:00
info := dns01 . GetChallengeInfo ( domain , keyAuth )
2021-01-14 19:42:12 +00:00
ctx := context . Background ( )
zones , err := d . client . ListZones ( ctx )
if err != nil {
return fmt . Errorf ( "ionos: failed to get zones: %w" , err )
}
2024-09-19 13:29:20 +00:00
name := dns01 . UnFqdn ( info . EffectiveFQDN )
zone := findZone ( zones , name )
2021-01-14 19:42:12 +00:00
if zone == nil {
return errors . New ( "ionos: no matching zone found for domain" )
}
filter := & internal . RecordsFilter {
2024-09-19 13:29:20 +00:00
Suffix : name ,
2021-01-14 19:42:12 +00:00
RecordType : "TXT" ,
}
records , err := d . client . GetRecords ( ctx , zone . ID , filter )
if err != nil {
return fmt . Errorf ( "ionos: failed to get records (zone=%s): %w" , zone . ID , err )
}
for _ , record := range records {
2024-09-19 13:29:20 +00:00
if record . Name == name && record . Content == strconv . Quote ( info . Value ) {
2024-01-17 21:18:17 +00:00
err = d . client . RemoveRecord ( ctx , zone . ID , record . ID )
2021-01-14 19:42:12 +00:00
if err != nil {
return fmt . Errorf ( "ionos: failed to remove record (zone=%s, record=%s): %w" , zone . ID , record . ID , err )
}
return nil
}
}
2024-01-17 21:18:17 +00:00
return fmt . Errorf ( "ionos: failed to remove record, record not found (zone=%s, domain=%s, fqdn=%s, value=%s)" , zone . ID , domain , info . EffectiveFQDN , info . Value )
2021-01-14 19:42:12 +00:00
}
func findZone ( zones [ ] internal . Zone , domain string ) * internal . Zone {
var result * internal . Zone
for _ , zone := range zones {
if zone . Name != "" && strings . HasSuffix ( domain , zone . Name ) {
if result == nil || len ( zone . Name ) > len ( result . Name ) {
result = & zone
}
}
}
return result
}