Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: align PDNS provider by removing MatchParent #3869

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/tutorials/pdns.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ eg. ```--domain-filter=.example.org``` will allow *only* zones that end in `.exa

The filter can also match parent zones. For example `--domain-filter=a.example.com` will allow for zone `example.com`. If you want to match parent zones, you cannot pre-pend your filter with a ".", eg. `--domain-filter=.example.com` will not attempt to match parent zones.

### Regex Domain Filter (`--regex-domain-filter`)
`--regex-domain-filter` limits possible domains and target zone with a regex. It overrides domain filters and can be specified only once.

## RBAC

If your cluster is RBAC enabled, you also need to setup the following, before you can run external-dns:
Expand Down Expand Up @@ -169,4 +172,3 @@ Once the API shows the record correctly, you can double check your record using:
```bash
$ dig @${PDNS_FQDN} echo.example.com.
```

22 changes: 0 additions & 22 deletions endpoint/domain_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() != "" {
Expand Down
101 changes: 0 additions & 101 deletions endpoint/domain_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion provider/pdns/pdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 11 additions & 7 deletions provider/pdns/pdns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"net/http"
"regexp"
"strings"
"testing"

Expand Down Expand Up @@ -513,6 +514,8 @@ var (
Filters: []string{},
}

RegexDomainFilter = endpoint.NewRegexDomainFilter(regexp.MustCompile("example.com"), nil)

DomainFilterEmptyClient = &PDNSAPIClient{
dryRun: false,
authCtx: context.WithValue(context.Background(), pgo.ContextAPIKey, pgo.APIKey{Key: "TEST-API-KEY"}),
Expand Down Expand Up @@ -547,6 +550,13 @@ var (
client: pgo.NewAPIClient(pgo.NewConfiguration()),
domainFilter: DomainFilterChildListMultiple,
}

RegexDomainFilterClient = &PDNSAPIClient{
dryRun: false,
authCtx: context.WithValue(context.Background(), pgo.ContextAPIKey, pgo.APIKey{Key: "TEST-API-KEY"}),
client: pgo.NewAPIClient(pgo.NewConfiguration()),
domainFilter: RegexDomainFilter,
}
)

/******************************************************************************/
Expand Down Expand Up @@ -1049,15 +1059,9 @@ 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)
filteredZones, residualZones = RegexDomainFilterClient.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)
}

func TestNewPDNSProviderTestSuite(t *testing.T) {
Expand Down