From 7aa9d25067c0d356095cfd2dd0b334a9c7f20a4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=BA=C5=A1=20Ferech?= Date: Mon, 14 Aug 2023 12:49:23 +0200 Subject: [PATCH] Align PDNS provider with other providers by removing MatchParent PNDS provider is the only one which uses MatchParent functionality. The MatchParent functionality breaks domain and regex domain filters. It also makes PDNS provider behave differently than other providers while having the same configuration. MatchParent can be replaced by using multiple domain filters. After discussion with maintainers we concluded that MatchParent should be removed. --- endpoint/domain_filter.go | 22 ------- endpoint/domain_filter_test.go | 101 --------------------------------- provider/pdns/pdns.go | 2 +- provider/pdns/pdns_test.go | 10 ---- 4 files changed, 1 insertion(+), 134 deletions(-) diff --git a/endpoint/domain_filter.go b/endpoint/domain_filter.go index 7bfe1b3e6..0efa3e71b 100644 --- a/endpoint/domain_filter.go +++ b/endpoint/domain_filter.go @@ -140,28 +140,6 @@ func matchRegex(regex *regexp.Regexp, negativeRegex *regexp.Regexp, domain strin return regex.MatchString(strippedDomain) } -// MatchParent checks wether DomainFilter matches a given parent domain. -func (df DomainFilter) MatchParent(domain string) bool { - if matchFilter(df.exclude, domain, false) { - return false - } - if len(df.Filters) == 0 { - return true - } - - strippedDomain := strings.ToLower(strings.TrimSuffix(domain, ".")) - for _, filter := range df.Filters { - if filter == "" || strings.HasPrefix(filter, ".") { - // We don't check parents if the filter is prefixed with "." - continue - } - if strings.HasSuffix(filter, "."+strippedDomain) { - return true - } - } - return false -} - // IsConfigured returns true if any inclusion or exclusion rules have been specified. func (df DomainFilter) IsConfigured() bool { if df.regex != nil && df.regex.String() != "" { diff --git a/endpoint/domain_filter_test.go b/endpoint/domain_filter_test.go index dcd518bab..9fe747ef6 100644 --- a/endpoint/domain_filter_test.go +++ b/endpoint/domain_filter_test.go @@ -439,107 +439,6 @@ func TestDomainFilterMatchWithEmptyFilter(t *testing.T) { } } -func TestDomainFilterMatchParent(t *testing.T) { - parentMatchTests := []domainFilterTest{ - { - []string{"a.example.com."}, - []string{}, - []string{"example.com"}, - true, - map[string][]string{ - "include": {"a.example.com"}, - }, - }, - { - []string{" a.example.com "}, - []string{}, - []string{"example.com"}, - true, - map[string][]string{ - "include": {"a.example.com"}, - }, - }, - { - []string{""}, - []string{}, - []string{"example.com"}, - true, - map[string][]string{}, - }, - { - []string{".a.example.com."}, - []string{}, - []string{"example.com"}, - false, - map[string][]string{ - "include": {".a.example.com"}, - }, - }, - { - []string{"a.example.com.", "b.example.com"}, - []string{}, - []string{"example.com"}, - true, - map[string][]string{ - "include": {"a.example.com", "b.example.com"}, - }, - }, - { - []string{"a.example.com"}, - []string{}, - []string{"b.example.com"}, - false, - map[string][]string{ - "include": {"a.example.com"}, - }, - }, - { - []string{"example.com"}, - []string{}, - []string{"example.com"}, - false, - map[string][]string{ - "include": {"example.com"}, - }, - }, - { - []string{"example.com"}, - []string{}, - []string{"anexample.com"}, - false, - map[string][]string{ - "include": {"example.com"}, - }, - }, - { - []string{""}, - []string{}, - []string{""}, - true, - map[string][]string{}, - }, - } - for i, tt := range parentMatchTests { - t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { - domainFilter := NewDomainFilterWithExclusions(tt.domainFilter, tt.exclusions) - - assertSerializes(t, domainFilter, tt.expectedSerialization) - deserialized := deserialize(t, map[string][]string{ - "include": tt.domainFilter, - "exclude": tt.exclusions, - }) - - for _, domain := range tt.domains { - assert.Equal(t, tt.expected, domainFilter.MatchParent(domain), "%v", domain) - assert.Equal(t, tt.expected, domainFilter.MatchParent(domain+"."), "%v", domain+".") - - assert.Equal(t, tt.expected, deserialized.MatchParent(domain), "deserialized %v", domain) - assert.Equal(t, tt.expected, deserialized.MatchParent(domain+"."), "deserialized %v", domain+".") - } - }) - } -} - func TestRegexDomainFilter(t *testing.T) { for i, tt := range regexDomainFilterTests { t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { diff --git a/provider/pdns/pdns.go b/provider/pdns/pdns.go index 4047fda6e..db9332f86 100644 --- a/provider/pdns/pdns.go +++ b/provider/pdns/pdns.go @@ -166,7 +166,7 @@ func (c *PDNSAPIClient) ListZones() (zones []pgo.Zone, resp *http.Response, err func (c *PDNSAPIClient) PartitionZones(zones []pgo.Zone) (filteredZones []pgo.Zone, residualZones []pgo.Zone) { if c.domainFilter.IsConfigured() { for _, zone := range zones { - if c.domainFilter.Match(zone.Name) || c.domainFilter.MatchParent(zone.Name) { + if c.domainFilter.Match(zone.Name) { filteredZones = append(filteredZones, zone) } else { residualZones = append(residualZones, zone) diff --git a/provider/pdns/pdns_test.go b/provider/pdns/pdns_test.go index 427ed2d11..bd7c7506a 100644 --- a/provider/pdns/pdns_test.go +++ b/provider/pdns/pdns_test.go @@ -1059,16 +1059,6 @@ func (suite *NewPDNSProviderTestSuite) TestPDNSClientPartitionZones() { assert.Equal(suite.T(), partitionResultFilteredMultipleFilter, filteredZones) assert.Equal(suite.T(), partitionResultResidualMultipleFilter, residualZones) - // Check filtered, residual zones when a single child domain filter specified - filteredZones, residualZones = DomainFilterChildSingleClient.PartitionZones(zoneList) - assert.Equal(suite.T(), partitionResultFilteredSingleFilter, filteredZones) - assert.Equal(suite.T(), partitionResultResidualSingleFilter, residualZones) - - // Check filter, residual zones when multiple child domain filters specified - filteredZones, residualZones = DomainFilterChildMultipleClient.PartitionZones(zoneList) - assert.Equal(suite.T(), partitionResultFilteredMultipleFilter, filteredZones) - assert.Equal(suite.T(), partitionResultResidualMultipleFilter, residualZones) - filteredZones, residualZones = RegexDomainFilterClient.PartitionZones(zoneList) assert.Equal(suite.T(), partitionResultFilteredSingleFilter, filteredZones) assert.Equal(suite.T(), partitionResultResidualSingleFilter, residualZones)