Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Open sidebar
origosys
External Dns
Commits
e23513ee
Commit
e23513ee
authored
6 years ago
by
Curtis Mattoon
Browse files
Options
Download
Email Patches
Plain Diff
Adds DomainFilter.exclusions
parent
63816deb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
102 additions
and
14 deletions
+102
-14
provider/domain_filter.go
provider/domain_filter.go
+26
-14
provider/domain_filter_test.go
provider/domain_filter_test.go
+76
-0
No files found.
provider/domain_filter.go
View file @
e23513ee
...
...
@@ -23,29 +23,42 @@ import (
// DomainFilter holds a lists of valid domain names
type
DomainFilter
struct
{
filters
[]
string
exclude
[]
string
}
// NewDomainFilter returns a new DomainFilter given a comma separated list of domains
func
NewDomainFilter
(
domainFilters
[]
string
)
DomainFilter
{
filters
:=
make
([]
string
,
len
(
domainFilters
))
// user can define filter domains either with trailing dot or without, we remove all trailing periods from
// the internal representation
for
i
,
domain
:=
range
domainFilters
{
filters
[
i
]
=
strings
.
TrimSuffix
(
strings
.
TrimSpace
(
domain
),
"."
)
// prepareFilters provides consistent trimming for filters/exclude params
func
prepareFilters
(
filters
[]
string
)
[]
string
{
fs
:=
make
([]
string
,
len
(
filters
))
for
i
,
domain
:=
range
filters
{
fs
[
i
]
=
strings
.
TrimSuffix
(
strings
.
TrimSpace
(
domain
),
"."
)
}
return
fs
}
return
DomainFilter
{
filters
}
// NewDomainFilterWithExclusions returns a new DomainFilter, given a list of matches and exclusions
func
NewDomainFilterWithExclusions
(
domainFilters
[]
string
,
excludeDomains
[]
string
)
DomainFilter
{
return
DomainFilter
{
prepareFilters
(
domainFilters
),
prepareFilters
(
excludeDomains
)}
}
// NewDomainFilter returns a new DomainFilter given a comma separated list of domains
func
NewDomainFilter
(
domainFilters
[]
string
)
DomainFilter
{
return
DomainFilter
{
prepareFilters
(
domainFilters
),
[]
string
{}}
}
// Match checks whether a domain can be found in the DomainFilter.
func
(
df
DomainFilter
)
Match
(
domain
string
)
bool
{
// return always true, if not filter is specified
if
len
(
df
.
filters
)
==
0
{
return
true
return
df
.
matchFilter
(
df
.
filters
,
domain
,
true
)
&&
!
df
.
matchFilter
(
df
.
exclude
,
domain
,
false
)
}
// matchFilter determines if any `filters` match `domain`.
// If no `filters` are provided, behavior depends on `emptyval`
// (empty `df.filters` matches everything, while empty `df.exclude` excludes nothing)
func
(
df
DomainFilter
)
matchFilter
(
filters
[]
string
,
domain
string
,
emptyval
bool
)
bool
{
if
len
(
filters
)
==
0
{
return
emptyval
}
for
_
,
filter
:=
range
df
.
filters
{
for
_
,
filter
:=
range
filters
{
strippedDomain
:=
strings
.
TrimSuffix
(
domain
,
"."
)
if
filter
==
""
{
...
...
@@ -60,7 +73,6 @@ func (df DomainFilter) Match(domain string) bool {
return
true
}
}
return
false
}
...
...
This diff is collapsed.
Click to expand it.
provider/domain_filter_test.go
View file @
e23513ee
...
...
@@ -24,6 +24,7 @@ import (
type
domainFilterTest
struct
{
domainFilter
[]
string
exclusions
[]
string
domains
[]
string
expected
bool
}
...
...
@@ -31,108 +32,173 @@ type domainFilterTest struct {
var
domainFilterTests
=
[]
domainFilterTest
{
{
[]
string
{
"google.com."
,
"exaring.de"
,
"inovex.de"
},
[]
string
{},
[]
string
{
"google.com"
,
"exaring.de"
,
"inovex.de"
},
true
,
},
{
[]
string
{
"google.com."
,
"exaring.de"
,
"inovex.de"
},
[]
string
{},
[]
string
{
"google.com"
,
"exaring.de"
,
"inovex.de"
},
true
,
},
{
[]
string
{
"google.com."
,
"exaring.de."
,
"inovex.de"
},
[]
string
{},
[]
string
{
"google.com"
,
"exaring.de"
,
"inovex.de"
},
true
,
},
{
[]
string
{
"foo.org. "
},
[]
string
{},
[]
string
{
"foo.org"
},
true
,
},
{
[]
string
{
" foo.org"
},
[]
string
{},
[]
string
{
"foo.org"
},
true
,
},
{
[]
string
{
"foo.org."
},
[]
string
{},
[]
string
{
"foo.org"
},
true
,
},
{
[]
string
{
"foo.org."
},
[]
string
{},
[]
string
{
"baz.org"
},
false
,
},
{
[]
string
{
"baz.foo.org."
},
[]
string
{},
[]
string
{
"foo.org"
},
false
,
},
{
[]
string
{
""
,
"foo.org."
},
[]
string
{},
[]
string
{
"foo.org"
},
true
,
},
{
[]
string
{
""
,
"foo.org."
},
[]
string
{},
[]
string
{},
true
,
},
{
[]
string
{
""
},
[]
string
{},
[]
string
{
"foo.org"
},
true
,
},
{
[]
string
{
""
},
[]
string
{},
[]
string
{},
true
,
},
{
[]
string
{
" "
},
[]
string
{},
[]
string
{},
true
,
},
{
[]
string
{
"bar.sub.example.org"
},
[]
string
{},
[]
string
{
"foo.bar.sub.example.org"
},
true
,
},
{
[]
string
{
"example.org"
},
[]
string
{},
[]
string
{
"anexample.org"
,
"test.anexample.org"
},
false
,
},
{
[]
string
{
".example.org"
},
[]
string
{},
[]
string
{
"anexample.org"
,
"test.anexample.org"
},
false
,
},
{
[]
string
{
".example.org"
},
[]
string
{},
[]
string
{
"example.org"
},
false
,
},
{
[]
string
{
".example.org"
},
[]
string
{},
[]
string
{
"test.example.org"
},
true
,
},
{
[]
string
{
"anexample.org"
},
[]
string
{},
[]
string
{
"example.org"
,
"test.example.org"
},
false
,
},
{
[]
string
{
".org"
},
[]
string
{},
[]
string
{
"example.org"
,
"test.example.org"
,
"foo.test.example.org"
},
true
,
},
{
[]
string
{
"example.org"
},
[]
string
{
"api.example.org"
},
[]
string
{
"example.org"
,
"test.example.org"
,
"foo.test.example.org"
},
true
,
},
{
[]
string
{
"example.org"
},
[]
string
{
"api.example.org"
},
[]
string
{
"foo.api.example.org"
,
"api.example.org"
},
false
,
},
{
[]
string
{
" example.org. "
},
[]
string
{
" .api.example.org "
},
[]
string
{
"foo.api.example.org"
,
"bar.baz.api.example.org."
},
false
,
},
{
[]
string
{
"example.org."
},
[]
string
{
"api.example.org"
},
[]
string
{
"dev-api.example.org"
,
"qa-api.example.org"
},
true
,
},
{
[]
string
{
"example.org."
},
[]
string
{
"api.example.org"
},
[]
string
{
"dev.api.example.org"
,
"qa.api.example.org"
},
false
,
},
{
[]
string
{
"example.org"
,
"api.example.org"
},
[]
string
{
"internal.api.example.org"
},
[]
string
{
"foo.api.example.org"
},
true
,
},
{
[]
string
{
"example.org"
,
"api.example.org"
},
[]
string
{
"internal.api.example.org"
},
[]
string
{
"foo.internal.api.example.org"
},
false
,
},
}
func
TestDomainFilterMatch
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
domainFilterTests
{
if
len
(
tt
.
exclusions
)
>
0
{
t
.
Skip
(
"NewDomainFilter() doesn't support exclusions"
)
}
domainFilter
:=
NewDomainFilter
(
tt
.
domainFilter
)
for
_
,
domain
:=
range
tt
.
domains
{
assert
.
Equal
(
t
,
tt
.
expected
,
domainFilter
.
Match
(
domain
),
"should not fail: %v in test-case #%v"
,
domain
,
i
)
...
...
@@ -141,6 +207,16 @@ func TestDomainFilterMatch(t *testing.T) {
}
}
func
TestDomainFilterWithExclusions
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
domainFilterTests
{
domainFilter
:=
NewDomainFilterWithExclusions
(
tt
.
domainFilter
,
tt
.
exclusions
)
for
_
,
domain
:=
range
tt
.
domains
{
assert
.
Equal
(
t
,
tt
.
expected
,
domainFilter
.
Match
(
domain
),
"should not fail: %v in test-case #%v"
,
domain
,
i
)
assert
.
Equal
(
t
,
tt
.
expected
,
domainFilter
.
Match
(
domain
+
"."
),
"should not fail: %v in test-case #%v"
,
domain
+
"."
,
i
)
}
}
}
func
TestDomainFilterMatchWithEmptyFilter
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
domainFilterTests
{
domainFilter
:=
DomainFilter
{}
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment