Skip to content

Commit 11724c7

Browse files
authored
bunny: fix zone detection (go-acme#2012)
1 parent 0435924 commit 11724c7

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

providers/dns/bunny/bunny.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/go-acme/lego/v4/challenge/dns01"
1111
"github.com/go-acme/lego/v4/platform/config/env"
12+
"github.com/miekg/dns"
1213
"github.com/nrdcg/bunny-go"
1314
)
1415

@@ -190,7 +191,28 @@ func getZone(fqdn string) (string, error) {
190191
return "", err
191192
}
192193

193-
return dns01.UnFqdn(authZone), nil
194+
zone, _, err := splitDomain(dns01.UnFqdn(authZone))
195+
if err != nil {
196+
return "", err
197+
}
198+
199+
return zone, nil
200+
}
201+
202+
func splitDomain(full string) (string, string, error) {
203+
split := dns.Split(full)
204+
if len(split) < 2 {
205+
return "", "", fmt.Errorf("unsupported domain: %s", full)
206+
}
207+
208+
if len(split) == 2 {
209+
return full, "", nil
210+
}
211+
212+
domain := full[split[len(split)-2]:]
213+
subDomain := full[:split[len(split)-2]-1]
214+
215+
return domain, subDomain, nil
194216
}
195217

196218
func pointer[T string | int | int32 | int64](v T) *T { return &v }

providers/dns/bunny/bunny_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/go-acme/lego/v4/platform/tester"
7+
"github.com/stretchr/testify/assert"
78
"github.com/stretchr/testify/require"
89
)
910

@@ -123,3 +124,83 @@ func TestLiveCleanUp(t *testing.T) {
123124
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
124125
require.NoError(t, err)
125126
}
127+
128+
func Test_splitDomain(t *testing.T) {
129+
type expected struct {
130+
root string
131+
sub string
132+
requireErr require.ErrorAssertionFunc
133+
}
134+
135+
testCases := []struct {
136+
desc string
137+
domain string
138+
expected expected
139+
}{
140+
{
141+
desc: "empty",
142+
domain: "",
143+
expected: expected{
144+
requireErr: require.Error,
145+
},
146+
},
147+
{
148+
desc: "2 levels",
149+
domain: "example.com",
150+
expected: expected{
151+
root: "example.com",
152+
sub: "",
153+
requireErr: require.NoError,
154+
},
155+
},
156+
{
157+
desc: "3 levels",
158+
domain: "_acme-challenge.example.com",
159+
expected: expected{
160+
root: "example.com",
161+
sub: "_acme-challenge",
162+
requireErr: require.NoError,
163+
},
164+
},
165+
{
166+
desc: "4 levels",
167+
domain: "_acme-challenge.sub.example.com",
168+
expected: expected{
169+
root: "example.com",
170+
sub: "_acme-challenge.sub",
171+
requireErr: require.NoError,
172+
},
173+
},
174+
{
175+
desc: "5 levels",
176+
domain: "_acme-challenge.my.sub.example.com",
177+
expected: expected{
178+
root: "example.com",
179+
sub: "_acme-challenge.my.sub",
180+
requireErr: require.NoError,
181+
},
182+
},
183+
{
184+
desc: "6 levels",
185+
domain: "_acme-challenge.my.sub.sub.example.com",
186+
expected: expected{
187+
root: "example.com",
188+
sub: "_acme-challenge.my.sub.sub",
189+
requireErr: require.NoError,
190+
},
191+
},
192+
}
193+
194+
for _, test := range testCases {
195+
test := test
196+
t.Run(test.desc, func(t *testing.T) {
197+
t.Parallel()
198+
199+
root, sub, err := splitDomain(test.domain)
200+
test.expected.requireErr(t, err)
201+
202+
assert.Equal(t, test.expected.root, root)
203+
assert.Equal(t, test.expected.sub, sub)
204+
})
205+
}
206+
}

0 commit comments

Comments
 (0)