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 3 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)
}
// 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("/")
Copy link
Member

Choose a reason for hiding this comment

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

I think you want
^([a-z0-9][a-z0-9-]*)?[.]example....

Better also to write down in the issue what kind of match or regexp you want to create.

Copy link
Member Author

Choose a reason for hiding this comment

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

I want to match a valid hostname, it can't end or start with - I think this ([a-z0-9][a-z0-9-]*) can end with -

Copy link
Member

@szuecs szuecs Nov 5, 2024

Choose a reason for hiding this comment

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

What about foo-bar-qux.example , that your regexp doesn't match?

maybe:

^[a-z0-9]([a-z0-9-]*[a-z0-9])?[.]example....

Not sure if we need the capture group for the full first part of the hostname.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated the PR to match it, the new regex [a-z0-9]+((-[a-z0-9]+)?)*

-> <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
35 changes: 34 additions & 1 deletion predicates/forwarded/forwarded_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,39 @@ 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.com/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.com/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
r: request{
url: "https://test-.example.com/index.html",
headers: http.Header{
"Forwarded": []string{`host="test-.example.com"`},
},
},
matches: false,
isError: false,
}}

for _, tc := range testCases {
Expand All @@ -173,7 +206,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