Skip to content

Commit 8e6c365

Browse files
committed
allow multiple - through the hostname regex
Signed-off-by: Mustafa Abdelrahman <[email protected]>
1 parent 31d4922 commit 8e6c365

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

dataclients/kubernetes/hosts.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func createHostRx(hosts ...string) string {
1616
hrx := make([]string, len(hosts))
1717
for i, host := range hosts {
1818
if strings.HasPrefix(host, "*.") {
19-
host = strings.Replace(host, "*", "[a-z0-9]+(-[a-z0-9]+)?", 1)
19+
host = strings.Replace(host, "*", "[a-z0-9]+((-[a-z0-9]+)?)*", 1)
2020
}
2121
// trailing dots and port are not allowed in kube
2222
// ingress spec, so we can append optional setting

dataclients/kubernetes/hosts_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestHostsToRegex(t *testing.T) {
2020
{
2121
msg: "wildcard",
2222
host: "*.example.org",
23-
regex: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$",
23+
regex: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$",
2424
},
2525
} {
2626
t.Run(ti.msg, func(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
kube_foo__qux____example_org_____qux:
2-
Host("^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$") && PathSubtree("/")
2+
Host("^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$") && PathSubtree("/")
33
-> <roundRobin, "http://10.2.9.103:8080", "http://10.2.9.104:8080">;

predicates/forwarded/forwarded_test.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ func TestForwardedHost(t *testing.T) {
164164
isError: false,
165165
}, {
166166
msg: "wildcard host should match",
167-
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
167+
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
168168
r: request{
169-
url: "https://test.example.com/index.html",
169+
url: "https://test.example.org/index.html",
170170
headers: http.Header{
171171
"Forwarded": []string{`host="test.example.org"`},
172172
},
@@ -175,22 +175,33 @@ func TestForwardedHost(t *testing.T) {
175175
isError: false,
176176
}, {
177177
msg: "wildcard 2 host should match",
178-
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
178+
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
179179
r: request{
180-
url: "https://test-v2.example.com/index.html",
180+
url: "https://test-v2.example.org/index.html",
181181
headers: http.Header{
182182
"Forwarded": []string{`host="test-v2.example.org"`},
183183
},
184184
},
185185
matches: true,
186186
isError: false,
187187
}, {
188-
msg: "wildcard 3 host shouldn't match",
189-
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
188+
msg: "wildcard 3 host should match",
189+
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
190190
r: request{
191-
url: "https://test-.example.com/index.html",
191+
url: "https://test-v2-v3.example.org/index.html",
192192
headers: http.Header{
193-
"Forwarded": []string{`host="test-.example.com"`},
193+
"Forwarded": []string{`host="test-v2-v3.example.org"`},
194+
},
195+
},
196+
matches: true,
197+
isError: false,
198+
}, {
199+
msg: "wildcard 4 host shouldn't match",
200+
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
201+
r: request{
202+
url: "https://test-.example.org/index.html",
203+
headers: http.Header{
204+
"Forwarded": []string{`host="test-.example.org"`},
194205
},
195206
},
196207
matches: false,

0 commit comments

Comments
 (0)