-
Notifications
You must be signed in to change notification settings - Fork 345
/
hosts.go
83 lines (70 loc) · 1.79 KB
/
hosts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package kubernetes
import (
"net/url"
"regexp"
"strings"
"github.com/zalando/skipper/eskip"
)
func createHostRx(hosts ...string) string {
if len(hosts) == 0 {
return ""
}
hrx := make([]string, len(hosts))
for i, host := range hosts {
// trailing dots and port are not allowed in kube
// ingress spec, so we can append optional setting
// without check
hrx[i] = strings.Replace(host, ".", "[.]", -1) + "[.]?(:[0-9]+)?"
}
return "^(" + strings.Join(hrx, "|") + ")$"
}
// hostCatchAllRoutes creates catch-all routes for those hosts that only have routes with
// a Host predicate and at least one additional predicate.
//
// currently only used for RouteGroups
func hostCatchAllRoutes(hostRoutes map[string][]*eskip.Route, createID func(string) string) []*eskip.Route {
var catchAll []*eskip.Route
for h, r := range hostRoutes {
var hasHostOnlyRoute bool
for _, ri := range r {
ct := eskip.Canonical(ri)
var hasNonHostPredicate bool
for _, p := range ct.Predicates {
if p.Name != "Host" {
hasNonHostPredicate = true
break
}
}
if !hasNonHostPredicate {
hasHostOnlyRoute = true
break
}
}
if !hasHostOnlyRoute {
catchAll = append(catchAll, &eskip.Route{
Id: createID(h),
Predicates: []*eskip.Predicate{{
Name: "Host",
Args: []interface{}{createHostRx(h)},
}},
BackendType: eskip.ShuntBackend,
})
}
}
return catchAll
}
func isExternalDomainAllowed(allowedDomains []*regexp.Regexp, domain string) bool {
for _, a := range allowedDomains {
if a.MatchString(domain) {
return true
}
}
return false
}
func isExternalAddressAllowed(allowedDomains []*regexp.Regexp, address string) bool {
u, err := url.Parse(address)
if err != nil {
return false
}
return isExternalDomainAllowed(allowedDomains, u.Hostname())
}