Skip to content

Commit

Permalink
allow multiple - through the hostname regex
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Abdelrahman <[email protected]>
  • Loading branch information
MustafaSaber committed Nov 4, 2024
1 parent 31d4922 commit 8e6c365
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dataclients/kubernetes/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func createHostRx(hosts ...string) string {
hrx := make([]string, len(hosts))
for i, host := range hosts {
if strings.HasPrefix(host, "*.") {
host = strings.Replace(host, "*", "[a-z0-9]+(-[a-z0-9]+)?", 1)
host = strings.Replace(host, "*", "[a-z0-9]+((-[a-z0-9]+)?)*", 1)
}
// trailing dots and port are not allowed in kube
// ingress spec, so we can append optional setting
Expand Down
2 changes: 1 addition & 1 deletion dataclients/kubernetes/hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestHostsToRegex(t *testing.T) {
{
msg: "wildcard",
host: "*.example.org",
regex: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$",
regex: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$",
},
} {
t.Run(ti.msg, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
kube_foo__qux____example_org_____qux:
Host("^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$") && PathSubtree("/")
Host("^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$") && PathSubtree("/")
-> <roundRobin, "http://10.2.9.103:8080", "http://10.2.9.104:8080">;
27 changes: 19 additions & 8 deletions predicates/forwarded/forwarded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ func TestForwardedHost(t *testing.T) {
isError: false,
}, {
msg: "wildcard host should match",
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
url: "https://test.example.com/index.html",
url: "https://test.example.org/index.html",
headers: http.Header{
"Forwarded": []string{`host="test.example.org"`},
},
Expand All @@ -175,22 +175,33 @@ func TestForwardedHost(t *testing.T) {
isError: false,
}, {
msg: "wildcard 2 host should match",
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
url: "https://test-v2.example.com/index.html",
url: "https://test-v2.example.org/index.html",
headers: http.Header{
"Forwarded": []string{`host="test-v2.example.org"`},
},
},
matches: true,
isError: false,
}, {
msg: "wildcard 3 host shouldn't match",
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
msg: "wildcard 3 host should match",
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
url: "https://test-.example.com/index.html",
url: "https://test-v2-v3.example.org/index.html",
headers: http.Header{
"Forwarded": []string{`host="test-.example.com"`},
"Forwarded": []string{`host="test-v2-v3.example.org"`},
},
},
matches: true,
isError: false,
}, {
msg: "wildcard 4 host shouldn't match",
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
url: "https://test-.example.org/index.html",
headers: http.Header{
"Forwarded": []string{`host="test-.example.org"`},
},
},
matches: false,
Expand Down

0 comments on commit 8e6c365

Please sign in to comment.