From 4c823ed3c04f7266a6b6b8c5e054c33f837e32fc Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Sat, 17 Sep 2022 11:22:42 +0200 Subject: [PATCH] lightsail: cleanup (#1716) --- providers/dns/lightsail/lightsail.go | 38 +++++++++++++++------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/providers/dns/lightsail/lightsail.go b/providers/dns/lightsail/lightsail.go index b1dab779..8c1334a0 100644 --- a/providers/dns/lightsail/lightsail.go +++ b/providers/dns/lightsail/lightsail.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "math/rand" + "strconv" "time" "github.com/aws/aws-sdk-go/aws" @@ -63,10 +64,8 @@ type Config struct { // NewDefaultConfig returns a default configuration for the DNSProvider. func NewDefaultConfig() *Config { return &Config{ - DNSZone: env.GetOrFile(EnvDNSZone), PropagationTimeout: env.GetOrDefaultSecond(EnvPropagationTimeout, dns01.DefaultPropagationTimeout), PollingInterval: env.GetOrDefaultSecond(EnvPollingInterval, dns01.DefaultPollingInterval), - Region: env.GetOrDefaultString(EnvRegion, "us-east-1"), } } @@ -89,7 +88,12 @@ type DNSProvider struct { // // See also: https://github.com/aws/aws-sdk-go/wiki/configuring-sdk func NewDNSProvider() (*DNSProvider, error) { - return NewDNSProviderConfig(NewDefaultConfig()) + config := NewDefaultConfig() + + config.DNSZone = env.GetOrFile(EnvDNSZone) + config.Region = env.GetOrDefaultString(EnvRegion, "us-east-1") + + return NewDNSProviderConfig(config) } // NewDNSProviderConfig return a DNSProvider instance configured for AWS Lightsail. @@ -117,10 +121,20 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) { func (d *DNSProvider) Present(domain, token, keyAuth string) error { fqdn, value := dns01.GetRecord(domain, keyAuth) - err := d.newTxtRecord(fqdn, `"`+value+`"`) + params := &lightsail.CreateDomainEntryInput{ + DomainName: aws.String(d.config.DNSZone), + DomainEntry: &lightsail.DomainEntry{ + Name: aws.String(fqdn), + Target: aws.String(strconv.Quote(value)), + Type: aws.String("TXT"), + }, + } + + _, err := d.client.CreateDomainEntry(params) if err != nil { return fmt.Errorf("lightsail: %w", err) } + return nil } @@ -133,7 +147,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { DomainEntry: &lightsail.DomainEntry{ Name: aws.String(fqdn), Type: aws.String("TXT"), - Target: aws.String(`"` + value + `"`), + Target: aws.String(strconv.Quote(value)), }, } @@ -141,6 +155,7 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { if err != nil { return fmt.Errorf("lightsail: %w", err) } + return nil } @@ -149,16 +164,3 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error { func (d *DNSProvider) Timeout() (timeout, interval time.Duration) { return d.config.PropagationTimeout, d.config.PollingInterval } - -func (d *DNSProvider) newTxtRecord(fqdn, value string) error { - params := &lightsail.CreateDomainEntryInput{ - DomainName: aws.String(d.config.DNSZone), - DomainEntry: &lightsail.DomainEntry{ - Name: aws.String(fqdn), - Target: aws.String(value), - Type: aws.String("TXT"), - }, - } - _, err := d.client.CreateDomainEntry(params) - return err -}