Skip to content
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
5 changes: 5 additions & 0 deletions dataclients/kubernetes/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"regexp"
"strings"

log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
)

Expand All @@ -15,6 +16,10 @@ func createHostRx(hosts ...string) string {

hrx := make([]string, len(hosts))
for i, host := range hosts {
if strings.HasPrefix(host, "*.") {
log.Debugf("Host %q starts with '*.'; replacing with regex", 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("/")
-> <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
4 changes: 4 additions & 0 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ func applyPredicates(route *Route, proute *parsedRoute) error {
}
case "Host":
if args, err = getStringArgs(p, 1); err == nil {
if strings.HasPrefix(args[0], "*.") {
log.Infof("Host %q starts with '*.'; replacing with regex", args[0])
args[0] = strings.Replace(args[0], "*", "[a-z0-9]+((-[a-z0-9]+)?)*", 1)
}
route.HostRegexps = append(route.HostRegexps, args[0])
}
case "PathRegexp":
Expand Down
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
9 changes: 3 additions & 6 deletions predicates/host/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package host
import (
"net/http"

"slices"

"github.com/zalando/skipper/predicates"
"github.com/zalando/skipper/routing"
)
Expand Down Expand Up @@ -40,10 +42,5 @@ func (*anySpec) Create(args []interface{}) (routing.Predicate, error) {
}

func (ap *anyPredicate) Match(r *http.Request) bool {
for _, host := range ap.hosts {
if host == r.Host {
return true
}
}
return false
return slices.Contains(ap.hosts, r.Host)
}
11 changes: 10 additions & 1 deletion routing/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ func leafWeight(l *leafMatcher) int {
w++
}

w += len(l.hostRxs)
for _, rx := range l.hostRxs {
if strings.HasPrefix(rx.String(), "[a-z0-9]+((-[a-z0-9]+)?)*") {
// this is a free wildcard, skip it from the first matching
w += 0
} else {
w += 1
}
}

// w += len(l.hostRxs)
w += len(l.pathRxs)
w += len(l.headersExact)
w += len(l.headersRegexp)
Expand Down