Skip to content

Commit 31d4922

Browse files
committed
support wildcard for the first segment only in ingress
Signed-off-by: Mustafa Abdelrahman <[email protected]>
1 parent 8027b99 commit 31d4922

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

dataclients/kubernetes/hosts.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func createHostRx(hosts ...string) string {
1515

1616
hrx := make([]string, len(hosts))
1717
for i, host := range hosts {
18+
if strings.HasPrefix(host, "*.") {
19+
host = strings.Replace(host, "*", "[a-z0-9]+(-[a-z0-9]+)?", 1)
20+
}
1821
// trailing dots and port are not allowed in kube
1922
// ingress spec, so we can append optional setting
2023
// without check

dataclients/kubernetes/hosts_test.go

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package kubernetes
22

33
import (
4-
"regexp"
54
"testing"
65

76
"github.com/stretchr/testify/require"
@@ -21,15 +20,12 @@ func TestHostsToRegex(t *testing.T) {
2120
{
2221
msg: "wildcard",
2322
host: "*.example.org",
24-
regex: "^(*[.]example[.]org[.]?(:[0-9]+)?)$",
23+
regex: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$",
2524
},
2625
} {
2726
t.Run(ti.msg, func(t *testing.T) {
2827
regex := createHostRx(ti.host)
2928
require.Equal(t, ti.regex, regex)
30-
// maybe we should validate the generated regex and maybe add
31-
_, err := regexp.Compile(regex)
32-
require.NoError(t, err)
3329
})
3430
}
3531
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
kube_foo__qux____example_org_____qux:
2-
Host("^(*[.]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

+23-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestForwardedHost(t *testing.T) {
164164
isError: false,
165165
}, {
166166
msg: "wildcard host should match",
167-
host: "^(*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
167+
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
168168
r: request{
169169
url: "https://test.example.com/index.html",
170170
headers: http.Header{
@@ -173,6 +173,28 @@ func TestForwardedHost(t *testing.T) {
173173
},
174174
matches: true,
175175
isError: false,
176+
}, {
177+
msg: "wildcard 2 host should match",
178+
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
179+
r: request{
180+
url: "https://test-v2.example.com/index.html",
181+
headers: http.Header{
182+
"Forwarded": []string{`host="test-v2.example.org"`},
183+
},
184+
},
185+
matches: true,
186+
isError: false,
187+
}, {
188+
msg: "wildcard 3 host shouldn't match",
189+
host: "^([a-z0-9]+(-[a-z0-9]+)?[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
190+
r: request{
191+
url: "https://test-.example.com/index.html",
192+
headers: http.Header{
193+
"Forwarded": []string{`host="test-.example.com"`},
194+
},
195+
},
196+
matches: false,
197+
isError: false,
176198
}}
177199

178200
for _, tc := range testCases {

0 commit comments

Comments
 (0)