From 0dfbc7e8ac39cf2c8e92231012c8cd31c2f826f4 Mon Sep 17 00:00:00 2001 From: eeno Date: Sun, 18 May 2025 22:24:08 +0200 Subject: [PATCH] fix: only create record from first of multiple targets with pihole V6 --- provider/pihole/clientV6.go | 6 +++--- provider/pihole/clientV6_test.go | 33 +++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/provider/pihole/clientV6.go b/provider/pihole/clientV6.go index ebaaabd6a2..25a1fd42b7 100644 --- a/provider/pihole/clientV6.go +++ b/provider/pihole/clientV6.go @@ -286,12 +286,12 @@ func (p *piholeClientV6) apply(ctx context.Context, action string, ep *endpoint. switch ep.RecordType { case endpoint.RecordTypeA, endpoint.RecordTypeAAAA: - apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s %s", ep.Targets, ep.DNSName)) + apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s %s", ep.Targets[0], ep.DNSName)) case endpoint.RecordTypeCNAME: if ep.RecordTTL.IsConfigured() { - apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s,%d", ep.DNSName, ep.Targets, ep.RecordTTL)) + apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s,%d", ep.DNSName, ep.Targets[0], ep.RecordTTL)) } else { - apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s", ep.DNSName, ep.Targets)) + apiUrl = p.generateApiUrl(apiUrl, fmt.Sprintf("%s,%s", ep.DNSName, ep.Targets[0])) } } diff --git a/provider/pihole/clientV6_test.go b/provider/pihole/clientV6_test.go index ba14038215..741128bf73 100644 --- a/provider/pihole/clientV6_test.go +++ b/provider/pihole/clientV6_test.go @@ -716,7 +716,8 @@ func TestCreateRecordV6(t *testing.T) { if r.Method == "PUT" && (r.URL.Path == "/api/config/dns/hosts/192.168.1.1 test.example.com" || r.URL.Path == "/api/config/dns/hosts/fc00::1:192:168:1:1 test.example.com" || r.URL.Path == "/api/config/dns/cnameRecords/source1.example.com,target1.domain.com" || - r.URL.Path == "/api/config/dns/cnameRecords/source2.example.com,target2.domain.com,500") { + r.URL.Path == "/api/config/dns/cnameRecords/source2.example.com,target2.domain.com,500" || + r.URL.Path == "/api/config/dns/cnameRecords/source3.example.com,target3.domain.com") { // Return A records w.WriteHeader(http.StatusCreated) @@ -747,6 +748,16 @@ func TestCreateRecordV6(t *testing.T) { t.Fatal(err) } + // Test create A record with multiple targets present and ensure only the first is added + ep = &endpoint.Endpoint{ + DNSName: "test.example.com", + Targets: []string{"192.168.1.1", "192.168.1.2"}, + RecordType: endpoint.RecordTypeA, + } + if err := cl.createRecord(context.Background(), ep); err != nil { + t.Fatal(err) + } + // Test create AAAA record ep = &endpoint.Endpoint{ DNSName: "test.example.com", @@ -757,6 +768,16 @@ func TestCreateRecordV6(t *testing.T) { t.Fatal(err) } + // Test create AAAA record with multiple targets present and ensure only the first is added + ep = &endpoint.Endpoint{ + DNSName: "test.example.com", + Targets: []string{"fc00::1:192:168:1:1", "fc00::1:192:168:1:2"}, + RecordType: endpoint.RecordTypeAAAA, + } + if err := cl.createRecord(context.Background(), ep); err != nil { + t.Fatal(err) + } + // Test create CNAME record ep = &endpoint.Endpoint{ DNSName: "source1.example.com", @@ -778,6 +799,16 @@ func TestCreateRecordV6(t *testing.T) { t.Fatal(err) } + // Test create CNAME record with multiple targets present and ensure only the first is added + ep = &endpoint.Endpoint{ + DNSName: "source3.example.com", + Targets: []string{"target3.domain.com", "target4.domain.com"}, + RecordType: endpoint.RecordTypeCNAME, + } + if err := cl.createRecord(context.Background(), ep); err != nil { + t.Fatal(err) + } + // Test create a wildcard record and ensure it fails ep = &endpoint.Endpoint{ DNSName: "*.example.com",