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

host2regex: doesn't take in consideration * #3296

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions dataclients/kubernetes/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to verify this matches behaviour descibed at https://kubernetes.io/docs/concepts/services-networking/ingress/#hostname-wildcards

Makes sense to add this link as a comment.

Subdomain regexp looks strange with two capture groups using ? followed by *.

I guess subdomain shoud start and end with alphanumeric (not dash). Lets check this https://stackoverflow.com/questions/7930751/regexp-for-subdomain and similar answers and reuse/adopt regexp from there.

Copy link
Member

@szuecs szuecs Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like #3296 (comment) would be the one 😀
Limiting the size makes also sense as you linked and non ascii domain support is also great to have!

Copy link
Member Author

@MustafaSaber MustafaSaber Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this one already works for all cases.
https://regex101.com/r/ziI6Sv/1

image

Copy link
Member Author

@MustafaSaber MustafaSaber Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like #3296 (comment) would be the one

yes this also works https://regex101.com/r/dNnhqk/1

}
// trailing dots and port are not allowed in kube
// ingress spec, so we can append optional setting
// without check
Expand Down
31 changes: 31 additions & 0 deletions dataclients/kubernetes/hosts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kubernetes

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestHostsToRegex(t *testing.T) {
for _, ti := range []struct {
msg string
host string
regex string
}{
{
msg: "simple",
host: "simple.example.org",
regex: "^(simple[.]example[.]org[.]?(:[0-9]+)?)$",
},
{
msg: "wildcard",
host: "*.example.org",
regex: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$",
},
} {
t.Run(ti.msg, func(t *testing.T) {
regex := createHostRx(ti.host)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another idea is to update validation webhook to say if the host regex is valid

require.Equal(t, ti.regex, regex)
})
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kube_foo__qux____example_org_____qux:
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">;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: qux
namespace: foo
spec:
rules:
- host: "*.example.org"
http:
paths:
- path: "/"
pathType: Prefix
backend:
service:
name: qux
port:
name: baz
---
apiVersion: v1
kind: Service
metadata:
name: qux
namespace: foo
spec:
clusterIP: 10.3.190.97
ports:
- name: baz
port: 8181
protocol: TCP
targetPort: 8080
selector:
application: myapp
type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
labels:
application: myapp
name: qux
namespace: foo
subsets:
- addresses:
- ip: 10.2.9.103
- ip: 10.2.9.104
ports:
- name: baz
port: 8080
protocol: TCP
46 changes: 45 additions & 1 deletion predicates/forwarded/forwarded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,50 @@ func TestForwardedHost(t *testing.T) {
},
matches: true,
isError: false,
}, {
msg: "wildcard host should 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: true,
isError: false,
}, {
msg: "wildcard 2 host should match",
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
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 should match",
host: "^([a-z0-9]+((-[a-z0-9]+)?)*[.]example[.]org[.]?(:[0-9]+)?)$", // *.example.org
r: request{
url: "https://test-v2-v3.example.org/index.html",
headers: http.Header{
"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,
isError: false,
}}

for _, tc := range testCases {
Expand All @@ -173,7 +217,7 @@ func TestForwardedHost(t *testing.T) {
hasError := err != nil
if hasError || tc.isError {
if !tc.isError {
t.Fatal("Predicate creation failed")
t.Fatalf("Predicate creation failed, %s", err)
}

if !hasError {
Expand Down
Loading