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 PowerDNS regex domain filter #3828

Closed
Closed
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.
```

9 changes: 7 additions & 2 deletions endpoint/domain_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,15 @@ func NewRegexDomainFilter(regexDomainFilter *regexp.Regexp, regexDomainExclusion
return DomainFilter{regex: regexDomainFilter, regexExclusion: regexDomainExclusion}
}

// IsRegexFilterConfigured checks if regex filter is set
func (df *DomainFilter) IsRegexFilterConfigured() bool {
return df.regex != nil && df.regex.String() != ""
}

// Match checks whether a domain can be found in the DomainFilter.
// RegexFilter takes precedence over Filters
func (df DomainFilter) Match(domain string) bool {
if df.regex != nil && df.regex.String() != "" || df.regexExclusion != nil && df.regexExclusion.String() != "" {
if df.IsRegexFilterConfigured() || df.regexExclusion != nil && df.regexExclusion.String() != "" {
return matchRegex(df.regex, df.regexExclusion, domain)
}

Expand Down Expand Up @@ -164,7 +169,7 @@ func (df DomainFilter) MatchParent(domain string) bool {

// IsConfigured returns true if any inclusion or exclusion rules have been specified.
func (df DomainFilter) IsConfigured() bool {
if df.regex != nil && df.regex.String() != "" {
if df.IsRegexFilterConfigured() {
return true
} else if df.regexExclusion != nil && df.regexExclusion.String() != "" {
return true
Expand Down
3 changes: 2 additions & 1 deletion provider/pdns/pdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ 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) {
isMatch := c.domainFilter.Match(zone.Name)
if isMatch || (!c.domainFilter.IsRegexFilterConfigured() && c.domainFilter.MatchParent(zone.Name)) {
filteredZones = append(filteredZones, zone)
} else {
residualZones = append(residualZones, zone)
Expand Down
14 changes: 14 additions & 0 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 @@ -1058,6 +1068,10 @@ func (suite *NewPDNSProviderTestSuite) TestPDNSClientPartitionZones() {
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)
}

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