/
redirect.go
145 lines (120 loc) · 3.64 KB
/
redirect.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package kubernetes
import (
"fmt"
"net/http"
"strconv"
log "github.com/sirupsen/logrus"
"github.com/zalando/skipper/eskip"
)
const (
redirectAnnotationKey = "zalando.org/skipper-ingress-redirect"
redirectCodeAnnotationKey = "zalando.org/skipper-ingress-redirect-code"
)
type redirectInfo struct {
defaultEnabled, enable, disable, override bool
defaultCode, code int
setHostCode map[string]int
disableHost map[string]bool
}
func createRedirectInfo(defaultEnabled bool, defaultCode int) *redirectInfo {
return &redirectInfo{
defaultEnabled: defaultEnabled,
defaultCode: defaultCode,
setHostCode: make(map[string]int),
disableHost: make(map[string]bool),
}
}
func (r *redirectInfo) initCurrent(m *metadata) {
r.enable = !r.defaultEnabled && m.Annotations[redirectAnnotationKey] == "true"
r.disable = r.defaultEnabled && m.Annotations[redirectAnnotationKey] == "false"
r.code = r.defaultCode
if annotationCode, ok := m.Annotations[redirectCodeAnnotationKey]; ok {
var err interface{}
if r.code, err = strconv.Atoi(annotationCode); err != nil ||
r.code < http.StatusMultipleChoices ||
r.code >= http.StatusBadRequest {
if err == nil {
err = annotationCode
}
log.Error("invalid redirect code annotation:", err)
r.code = r.defaultCode
}
}
r.override = r.defaultEnabled && !r.disable && r.code != r.defaultCode
}
func (r *redirectInfo) setHost(host string) {
r.setHostCode[host] = r.code
}
func (r *redirectInfo) setHostDisabled(host string) {
r.disableHost[host] = true
}
func (r *redirectInfo) updateHost(host string) {
if r.enable || r.override {
r.setHost(host)
}
if r.disable {
r.setHostDisabled(host)
}
}
func routeIDForRedirectRoute(baseID string, enable bool) string {
f := "%s_https_redirect"
if !enable {
f = "%s_disable_https_redirect"
}
return fmt.Sprintf(f, baseID)
}
func initRedirectRoute(r *eskip.Route, code int) {
if r.Headers == nil {
r.Headers = make(map[string]string)
}
r.Headers["X-Forwarded-Proto"] = "http"
// the below duplicate any-path (.*) is set to make sure that
// the redirect route has a higher priority during matching than
// the normal routes that may have max 2 predicates: path regexp
// and host.
//
// A better solution might be to implement a Weight() predicate
// and apply it here.
//
r.PathRegexps = append(r.PathRegexps, ".*")
r.PathRegexps = append(r.PathRegexps, ".*")
r.Filters = append(r.Filters, &eskip.Filter{
Name: "redirectTo",
Args: []interface{}{float64(code), "https:"},
})
r.BackendType = eskip.ShuntBackend
r.Backend = ""
}
func initDisableRedirectRoute(r *eskip.Route) {
if r.Headers == nil {
r.Headers = make(map[string]string)
}
r.Headers["X-Forwarded-Proto"] = "http"
// the below duplicate any-path (.*) is set to make sure that
// the redirect route has a higher priority during matching than
// the normal routes that may have max 2 predicates: path regexp
// and host.
//
// A better solution might be to implement a Weight() predicate
// and apply it here.
//
r.PathRegexps = append(r.PathRegexps, ".*")
r.PathRegexps = append(r.PathRegexps, ".*")
}
func globalRedirectRoute(code int) *eskip.Route {
r := &eskip.Route{Id: httpRedirectRouteID}
initRedirectRoute(r, code)
return r
}
func createIngressEnableHTTPSRedirect(r *eskip.Route, code int) *eskip.Route {
rr := *r
rr.Id = routeIDForRedirectRoute(rr.Id, true)
initRedirectRoute(&rr, code)
return &rr
}
func createIngressDisableHTTPSRedirect(r *eskip.Route) *eskip.Route {
rr := *r
rr.Id = routeIDForRedirectRoute(rr.Id, false)
initDisableRedirectRoute(&rr)
return &rr
}