Skip to content

Commit 7095aa6

Browse files
authored
fix: return an error when extracting record name (#1778)
1 parent 9ec5c8a commit 7095aa6

File tree

27 files changed

+207
-116
lines changed

27 files changed

+207
-116
lines changed

providers/dns/allinkl/allinkl.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strings"
98
"sync"
109
"time"
1110

@@ -114,7 +113,10 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
114113
return fmt.Errorf("allinkl: %w", err)
115114
}
116115

117-
subDomain := dns01.UnFqdn(strings.TrimSuffix(fqdn, authZone))
116+
subDomain, err := dns01.ExtractSubDomain(fqdn, authZone)
117+
if err != nil {
118+
return fmt.Errorf("allinkl: %w", err)
119+
}
118120

119121
record := internal.DNSRequest{
120122
ZoneHost: authZone,

providers/dns/azure/azure.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import (
77
"fmt"
88
"io"
99
"net/http"
10-
"strings"
1110
"time"
1211

1312
"github.com/Azure/go-autorest/autorest"
1413
aazure "github.com/Azure/go-autorest/autorest/azure"
1514
"github.com/Azure/go-autorest/autorest/azure/auth"
1615
"github.com/go-acme/lego/v4/challenge"
17-
"github.com/go-acme/lego/v4/challenge/dns01"
1816
"github.com/go-acme/lego/v4/platform/config/env"
1917
)
2018

@@ -179,11 +177,6 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
179177
return d.provider.CleanUp(domain, token, keyAuth)
180178
}
181179

182-
// Returns the relative record to the domain.
183-
func toRelativeRecord(domain, zone string) string {
184-
return dns01.UnFqdn(strings.TrimSuffix(domain, zone))
185-
}
186-
187180
func getAuthorizer(config *Config) (autorest.Authorizer, error) {
188181
if config.ClientID != "" && config.ClientSecret != "" && config.TenantID != "" {
189182
credentialsConfig := auth.ClientCredentialsConfig{

providers/dns/azure/private.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func (d *dnsProviderPrivate) Present(domain, token, keyAuth string) error {
3939
rsc := privatedns.NewRecordSetsClientWithBaseURI(d.config.ResourceManagerEndpoint, d.config.SubscriptionID)
4040
rsc.Authorizer = d.authorizer
4141

42-
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
42+
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
43+
if err != nil {
44+
return fmt.Errorf("azure: %w", err)
45+
}
4346

4447
// Get existing record set
45-
rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, privatedns.TXT, relative)
48+
rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, privatedns.TXT, subDomain)
4649
if err != nil {
4750
var detailed autorest.DetailedError
4851
if !errors.As(err, &detailed) || detailed.StatusCode != http.StatusNotFound {
@@ -68,14 +71,14 @@ func (d *dnsProviderPrivate) Present(domain, token, keyAuth string) error {
6871
}
6972

7073
rec := privatedns.RecordSet{
71-
Name: &relative,
74+
Name: &subDomain,
7275
RecordSetProperties: &privatedns.RecordSetProperties{
7376
TTL: to.Int64Ptr(int64(d.config.TTL)),
7477
TxtRecords: &txtRecords,
7578
},
7679
}
7780

78-
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, privatedns.TXT, relative, rec, "", "")
81+
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, privatedns.TXT, subDomain, rec, "", "")
7982
if err != nil {
8083
return fmt.Errorf("azure: %w", err)
8184
}
@@ -92,12 +95,15 @@ func (d *dnsProviderPrivate) CleanUp(domain, token, keyAuth string) error {
9295
return fmt.Errorf("azure: %w", err)
9396
}
9497

95-
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
98+
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
99+
if err != nil {
100+
return fmt.Errorf("azure: %w", err)
101+
}
96102

97103
rsc := privatedns.NewRecordSetsClientWithBaseURI(d.config.ResourceManagerEndpoint, d.config.SubscriptionID)
98104
rsc.Authorizer = d.authorizer
99105

100-
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, privatedns.TXT, relative, "")
106+
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, privatedns.TXT, subDomain, "")
101107
if err != nil {
102108
return fmt.Errorf("azure: %w", err)
103109
}

providers/dns/azure/public.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,13 @@ func (d *dnsProviderPublic) Present(domain, token, keyAuth string) error {
3939
rsc := dns.NewRecordSetsClientWithBaseURI(d.config.ResourceManagerEndpoint, d.config.SubscriptionID)
4040
rsc.Authorizer = d.authorizer
4141

42-
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
42+
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
43+
if err != nil {
44+
return fmt.Errorf("azure: %w", err)
45+
}
4346

4447
// Get existing record set
45-
rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, relative, dns.TXT)
48+
rset, err := rsc.Get(ctx, d.config.ResourceGroup, zone, subDomain, dns.TXT)
4649
if err != nil {
4750
var detailed autorest.DetailedError
4851
if !errors.As(err, &detailed) || detailed.StatusCode != http.StatusNotFound {
@@ -68,14 +71,14 @@ func (d *dnsProviderPublic) Present(domain, token, keyAuth string) error {
6871
}
6972

7073
rec := dns.RecordSet{
71-
Name: &relative,
74+
Name: &subDomain,
7275
RecordSetProperties: &dns.RecordSetProperties{
7376
TTL: to.Int64Ptr(int64(d.config.TTL)),
7477
TxtRecords: &txtRecords,
7578
},
7679
}
7780

78-
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, rec, "", "")
81+
_, err = rsc.CreateOrUpdate(ctx, d.config.ResourceGroup, zone, subDomain, dns.TXT, rec, "", "")
7982
if err != nil {
8083
return fmt.Errorf("azure: %w", err)
8184
}
@@ -92,12 +95,15 @@ func (d *dnsProviderPublic) CleanUp(domain, token, keyAuth string) error {
9295
return fmt.Errorf("azure: %w", err)
9396
}
9497

95-
relative := toRelativeRecord(fqdn, dns01.ToFqdn(zone))
98+
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
99+
if err != nil {
100+
return fmt.Errorf("azure: %w", err)
101+
}
96102

97103
rsc := dns.NewRecordSetsClientWithBaseURI(d.config.ResourceManagerEndpoint, d.config.SubscriptionID)
98104
rsc.Authorizer = d.authorizer
99105

100-
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, relative, dns.TXT, "")
106+
_, err = rsc.Delete(ctx, d.config.ResourceGroup, zone, subDomain, dns.TXT, "")
101107
if err != nil {
102108
return fmt.Errorf("azure: %w", err)
103109
}

providers/dns/cloudns/internal/client.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/url"
1010
"path"
1111
"strconv"
12-
"strings"
1312

1413
"github.com/go-acme/lego/v4/challenge/dns01"
1514
)
@@ -89,7 +88,10 @@ func (c *Client) GetZone(authFQDN string) (*Zone, error) {
8988

9089
// FindTxtRecord returns the TXT record a zone ID and a FQDN.
9190
func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
92-
host := dns01.UnFqdn(strings.TrimSuffix(dns01.UnFqdn(fqdn), zoneName))
91+
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
92+
if err != nil {
93+
return nil, err
94+
}
9395

9496
reqURL, err := c.BaseURL.Parse(path.Join(c.BaseURL.Path, "records.json"))
9597
if err != nil {
@@ -98,7 +100,7 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
98100

99101
q := reqURL.Query()
100102
q.Set("domain-name", zoneName)
101-
q.Set("host", host)
103+
q.Set("host", subDomain)
102104
q.Set("type", "TXT")
103105
reqURL.RawQuery = q.Encode()
104106

@@ -118,7 +120,7 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
118120
}
119121

120122
for _, record := range records {
121-
if record.Host == host && record.Type == "TXT" {
123+
if record.Host == subDomain && record.Type == "TXT" {
122124
return &record, nil
123125
}
124126
}
@@ -128,7 +130,10 @@ func (c *Client) FindTxtRecord(zoneName, fqdn string) (*TXTRecord, error) {
128130

129131
// ListTxtRecords returns the TXT records a zone ID and a FQDN.
130132
func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
131-
host := dns01.UnFqdn(strings.TrimSuffix(dns01.UnFqdn(fqdn), zoneName))
133+
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
134+
if err != nil {
135+
return nil, err
136+
}
132137

133138
reqURL, err := c.BaseURL.Parse(path.Join(c.BaseURL.Path, "records.json"))
134139
if err != nil {
@@ -137,7 +142,7 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
137142

138143
q := reqURL.Query()
139144
q.Set("domain-name", zoneName)
140-
q.Set("host", host)
145+
q.Set("host", subDomain)
141146
q.Set("type", "TXT")
142147
reqURL.RawQuery = q.Encode()
143148

@@ -158,7 +163,7 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
158163

159164
var records []TXTRecord
160165
for _, record := range raw {
161-
if record.Host == host && record.Type == "TXT" {
166+
if record.Host == subDomain && record.Type == "TXT" {
162167
records = append(records, record)
163168
}
164169
}
@@ -168,7 +173,10 @@ func (c *Client) ListTxtRecords(zoneName, fqdn string) ([]TXTRecord, error) {
168173

169174
// AddTxtRecord adds a TXT record.
170175
func (c *Client) AddTxtRecord(zoneName, fqdn, value string, ttl int) error {
171-
host := dns01.UnFqdn(strings.TrimSuffix(dns01.UnFqdn(fqdn), zoneName))
176+
subDomain, err := dns01.ExtractSubDomain(fqdn, zoneName)
177+
if err != nil {
178+
return err
179+
}
172180

173181
reqURL, err := c.BaseURL.Parse(path.Join(c.BaseURL.Path, "add-record.json"))
174182
if err != nil {
@@ -177,7 +185,7 @@ func (c *Client) AddTxtRecord(zoneName, fqdn, value string, ttl int) error {
177185

178186
q := reqURL.Query()
179187
q.Set("domain-name", zoneName)
180-
q.Set("host", host)
188+
q.Set("host", subDomain)
181189
q.Set("record", value)
182190
q.Set("ttl", strconv.Itoa(ttlRounder(ttl)))
183191
q.Set("record-type", "TXT")

providers/dns/cloudns/internal/client_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,14 @@ func TestClient_FindTxtRecord(t *testing.T) {
212212
},
213213
{
214214
desc: "zero records",
215-
authFQDN: "_acme-challenge.foo.com.",
216-
zoneName: "test-zone",
215+
authFQDN: "_acme-challenge.example.com.",
216+
zoneName: "example.com",
217217
apiResponse: `[]`,
218218
},
219219
{
220220
desc: "invalid json response",
221-
authFQDN: "_acme-challenge.foo.com.",
222-
zoneName: "test-zone",
221+
authFQDN: "_acme-challenge.example.com.",
222+
zoneName: "example.com",
223223
apiResponse: `[{}]`,
224224
expected: expected{
225225
errorMsg: "failed to unmarshall TXT records: json: cannot unmarshal array into Go value of type map[string]internal.TXTRecord: [{}]",
@@ -327,14 +327,14 @@ func TestClient_ListTxtRecord(t *testing.T) {
327327
},
328328
{
329329
desc: "zero records",
330-
authFQDN: "_acme-challenge.foo.com.",
331-
zoneName: "test-zone",
330+
authFQDN: "_acme-challenge.example.com.",
331+
zoneName: "example.com",
332332
apiResponse: `[]`,
333333
},
334334
{
335335
desc: "invalid json response",
336-
authFQDN: "_acme-challenge.foo.com.",
337-
zoneName: "test-zone",
336+
authFQDN: "_acme-challenge.example.com.",
337+
zoneName: "example.com",
338338
apiResponse: `[{}]`,
339339
expected: expected{
340340
errorMsg: "failed to unmarshall TXT records: json: cannot unmarshal array into Go value of type map[string]internal.TXTRecord: [{}]",

providers/dns/cloudxns/internal/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"io"
1111
"net/http"
1212
"strconv"
13-
"strings"
1413
"time"
1514

1615
"github.com/go-acme/lego/v4/challenge/dns01"
@@ -127,9 +126,14 @@ func (c *Client) AddTxtRecord(info *Data, fqdn, value string, ttl int) error {
127126
return fmt.Errorf("CloudXNS: invalid zone ID: %w", err)
128127
}
129128

129+
subDomain, err := dns01.ExtractSubDomain(fqdn, info.Domain)
130+
if err != nil {
131+
return fmt.Errorf("CloudXNS: %w", err)
132+
}
133+
130134
payload := TXTRecord{
131135
ID: id,
132-
Host: dns01.UnFqdn(strings.TrimSuffix(fqdn, info.Domain)),
136+
Host: subDomain,
133137
Value: value,
134138
Type: "TXT",
135139
LineID: 1,

providers/dns/domeneshop/domeneshop.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strings"
98
"time"
109

1110
"github.com/go-acme/lego/v4/challenge/dns01"
@@ -142,8 +141,10 @@ func (d *DNSProvider) splitDomain(fqdn string) (string, string, error) {
142141
return "", "", err
143142
}
144143

145-
host := dns01.UnFqdn(strings.TrimSuffix(fqdn, zone))
146-
zone = dns01.UnFqdn(zone)
144+
subDomain, err := dns01.ExtractSubDomain(fqdn, zone)
145+
if err != nil {
146+
return "", "", err
147+
}
147148

148-
return zone, host, nil
149+
return dns01.UnFqdn(zone), subDomain, nil
149150
}

providers/dns/dynu/dynu.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8-
"strings"
98
"time"
109

1110
"github.com/go-acme/lego/v4/challenge/dns01"
1211
"github.com/go-acme/lego/v4/platform/config/env"
1312
"github.com/go-acme/lego/v4/providers/dns/dynu/internal"
14-
"github.com/miekg/dns"
1513
)
1614

1715
// Environment variables names.
@@ -117,11 +115,16 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
117115
}
118116
}
119117

118+
subDomain, err := dns01.ExtractSubDomain(fqdn, domain)
119+
if err != nil {
120+
return fmt.Errorf("dynu: %w", err)
121+
}
122+
120123
record := internal.DNSRecord{
121124
Type: "TXT",
122125
DomainName: rootDomain.DomainName,
123126
Hostname: dns01.UnFqdn(fqdn),
124-
NodeName: dns01.UnFqdn(strings.TrimSuffix(fqdn, dns.Fqdn(domain))),
127+
NodeName: subDomain,
125128
TextData: value,
126129
State: true,
127130
TTL: d.config.TTL,

providers/dns/epik/epik.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
101101
return fmt.Errorf("epik: %w", err)
102102
}
103103

104+
subDomain, err := dns01.ExtractSubDomain(fqdn, authZone)
105+
if err != nil {
106+
return fmt.Errorf("epik: %w", err)
107+
}
108+
104109
record := internal.RecordRequest{
105-
Host: dns01.UnFqdn(strings.TrimSuffix(fqdn, authZone)),
110+
Host: subDomain,
106111
Type: "TXT",
107112
Data: value,
108113
TTL: d.config.TTL,
@@ -127,15 +132,19 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
127132
}
128133

129134
dom := dns01.UnFqdn(authZone)
130-
host := dns01.UnFqdn(strings.TrimSuffix(fqdn, authZone))
131135

132136
records, err := d.client.GetDNSRecords(dom)
133137
if err != nil {
134138
return fmt.Errorf("epik: %w", err)
135139
}
136140

141+
subDomain, err := dns01.ExtractSubDomain(fqdn, authZone)
142+
if err != nil {
143+
return fmt.Errorf("epik: %w", err)
144+
}
145+
137146
for _, record := range records {
138-
if strings.EqualFold(record.Type, "TXT") && record.Data == value && record.Name == host {
147+
if strings.EqualFold(record.Type, "TXT") && record.Data == value && record.Name == subDomain {
139148
_, err = d.client.RemoveHostRecord(dom, record.ID)
140149
if err != nil {
141150
return fmt.Errorf("epik: %w", err)

0 commit comments

Comments
 (0)