Skip to content

Commit 17c65de

Browse files
marcusgrandoldez
andauthored
azion: improve zone lookup (#2564)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 990f9ac commit 17c65de

File tree

4 files changed

+175
-13
lines changed

4 files changed

+175
-13
lines changed

providers/dns/azion/azion.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/aziontech/azionapi-go-sdk/idns"
1313
"github.com/go-acme/lego/v4/challenge/dns01"
1414
"github.com/go-acme/lego/v4/platform/config/env"
15+
"github.com/miekg/dns"
1516
)
1617

1718
// Environment variables names.
@@ -182,13 +183,12 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
182183
}
183184

184185
defer func() {
185-
// Remove the record ID from our map
186+
// Cleans the record ID.
186187
d.recordIDsMu.Lock()
187188
delete(d.recordIDs, token)
188189
d.recordIDsMu.Unlock()
189190
}()
190191

191-
// Find the existing TXT record
192192
existingRecord, err := d.findExistingTXTRecord(ctxAuth, zone.GetId(), subDomain)
193193
if err != nil {
194194
return fmt.Errorf("azion: find existing record: %w", err)
@@ -198,7 +198,6 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
198198
return nil
199199
}
200200

201-
// Get current answers and remove the specific value
202201
currentAnswers := existingRecord.GetAnswersList()
203202

204203
var updatedAnswers []string
@@ -239,11 +238,6 @@ func (d *DNSProvider) CleanUp(domain, token, keyAuth string) error {
239238
}
240239

241240
func (d *DNSProvider) findZone(ctx context.Context, fqdn string) (*idns.Zone, error) {
242-
authZone, err := dns01.FindZoneByFqdn(fqdn)
243-
if err != nil {
244-
return nil, fmt.Errorf("could not find a zone for domain %q: %w", fqdn, err)
245-
}
246-
247241
resp, _, err := d.client.ZonesAPI.GetZones(ctx).Execute()
248242
if err != nil {
249243
return nil, fmt.Errorf("get zones: %w", err)
@@ -253,14 +247,19 @@ func (d *DNSProvider) findZone(ctx context.Context, fqdn string) (*idns.Zone, er
253247
return nil, errors.New("get zones: no results")
254248
}
255249

256-
targetZone := dns01.UnFqdn(authZone)
257-
for _, zone := range resp.GetResults() {
258-
if zone.GetName() == targetZone {
259-
return &zone, nil
250+
labelIndexes := dns.Split(fqdn)
251+
252+
for _, index := range labelIndexes {
253+
domain := dns01.UnFqdn(fqdn[index:])
254+
255+
for _, zone := range resp.GetResults() {
256+
if zone.GetDomain() == domain {
257+
return &zone, nil
258+
}
260259
}
261260
}
262261

263-
return nil, fmt.Errorf("zone %q not found (fqdn: %q)", authZone, fqdn)
262+
return nil, fmt.Errorf("zone not found (fqdn: %q)", fqdn)
264263
}
265264

266265
// findExistingTXTRecord searches for an existing TXT record with the given name in the specified zone.

providers/dns/azion/azion_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
package azion
22

33
import (
4+
"context"
5+
"io"
6+
"net/http"
7+
"net/http/httptest"
8+
"os"
9+
"path/filepath"
410
"testing"
511

12+
"github.com/aziontech/azionapi-go-sdk/idns"
613
"github.com/go-acme/lego/v4/platform/tester"
14+
"github.com/stretchr/testify/assert"
715
"github.com/stretchr/testify/require"
816
)
917

@@ -113,3 +121,129 @@ func TestLiveCleanUp(t *testing.T) {
113121
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
114122
require.NoError(t, err)
115123
}
124+
125+
func TestDNSProvider_findZone(t *testing.T) {
126+
provider, mux := setupTest(t)
127+
mux.HandleFunc("GET /intelligent_dns", writeFixtureHandler("zones.json"))
128+
129+
testCases := []struct {
130+
desc string
131+
fqdn string
132+
expected *idns.Zone
133+
}{
134+
{
135+
desc: "apex",
136+
fqdn: "example.com.",
137+
expected: &idns.Zone{
138+
Id: idns.PtrInt32(1),
139+
Domain: idns.PtrString("example.com"),
140+
},
141+
},
142+
{
143+
desc: "sub domain",
144+
fqdn: "sub.example.com.",
145+
expected: &idns.Zone{
146+
Id: idns.PtrInt32(2),
147+
Domain: idns.PtrString("sub.example.com"),
148+
},
149+
},
150+
{
151+
desc: "long sub domain",
152+
fqdn: "_acme-challenge.api.sub.example.com.",
153+
expected: &idns.Zone{
154+
Id: idns.PtrInt32(2),
155+
Domain: idns.PtrString("sub.example.com"),
156+
},
157+
},
158+
{
159+
desc: "long sub domain, apex",
160+
fqdn: "_acme-challenge.test.example.com.",
161+
expected: &idns.Zone{
162+
Id: idns.PtrInt32(1),
163+
Domain: idns.PtrString("example.com"),
164+
},
165+
},
166+
}
167+
168+
for _, test := range testCases {
169+
t.Run(test.desc, func(t *testing.T) {
170+
zone, err := provider.findZone(context.Background(), test.fqdn)
171+
require.NoError(t, err)
172+
173+
assert.Equal(t, test.expected, zone)
174+
})
175+
}
176+
}
177+
178+
func TestDNSProvider_findZone_error(t *testing.T) {
179+
testCases := []struct {
180+
desc string
181+
fqdn string
182+
response string
183+
expected string
184+
}{
185+
{
186+
desc: "no parent zone found",
187+
fqdn: "_acme-challenge.example.org.",
188+
response: "zones.json",
189+
expected: `zone not found (fqdn: "_acme-challenge.example.org.")`,
190+
},
191+
{
192+
desc: "empty zones list",
193+
fqdn: "example.com.",
194+
response: "zones_empty.json",
195+
expected: `zone not found (fqdn: "example.com.")`,
196+
},
197+
}
198+
199+
for _, test := range testCases {
200+
t.Run(test.desc, func(t *testing.T) {
201+
provider, mux := setupTest(t)
202+
mux.HandleFunc("GET /intelligent_dns", writeFixtureHandler(test.response))
203+
204+
zone, err := provider.findZone(context.Background(), test.fqdn)
205+
require.EqualError(t, err, test.expected)
206+
207+
assert.Nil(t, zone)
208+
})
209+
}
210+
}
211+
212+
func setupTest(t *testing.T) (*DNSProvider, *http.ServeMux) {
213+
t.Helper()
214+
215+
mux := http.NewServeMux()
216+
server := httptest.NewServer(mux)
217+
218+
config := NewDefaultConfig()
219+
config.PersonalToken = "secret"
220+
221+
provider, err := NewDNSProviderConfig(config)
222+
require.NoError(t, err)
223+
224+
clientConfig := provider.client.GetConfig()
225+
clientConfig.HTTPClient = server.Client()
226+
clientConfig.Servers = idns.ServerConfigurations{
227+
{
228+
URL: server.URL,
229+
Description: "Production",
230+
},
231+
}
232+
233+
return provider, mux
234+
}
235+
236+
func writeFixtureHandler(filename string) http.HandlerFunc {
237+
return func(rw http.ResponseWriter, req *http.Request) {
238+
rw.Header().Set("Content-Type", "application/json")
239+
240+
file, err := os.Open(filepath.Join("fixtures", filename))
241+
if err != nil {
242+
http.Error(rw, err.Error(), http.StatusInternalServerError)
243+
return
244+
}
245+
defer func() { _ = file.Close() }()
246+
247+
_, _ = io.Copy(rw, file)
248+
}
249+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"count": 2,
3+
"links": {
4+
"previous": null,
5+
"next": null
6+
},
7+
"total_pages": 1,
8+
"results": [
9+
{
10+
"id": 1,
11+
"domain": "example.com"
12+
},
13+
{
14+
"id": 2,
15+
"domain": "sub.example.com"
16+
}
17+
],
18+
"schema_version": 3
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"count": 0,
3+
"links": {
4+
"previous": null,
5+
"next": null
6+
},
7+
"total_pages": 0,
8+
"results": null,
9+
"schema_version": 3
10+
}

0 commit comments

Comments
 (0)