-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.go
More file actions
301 lines (284 loc) · 10.5 KB
/
handler.go
File metadata and controls
301 lines (284 loc) · 10.5 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package unidler
import (
"context"
"fmt"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"text/template"
"time"
"github.com/go-logr/logr"
"github.com/uselagoon/aergia-controller/internal/handlers/metrics"
corev1 "k8s.io/api/core/v1"
networkv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/types"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
)
func (h *Unidler) ingressHandler(path string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := context.Background()
opLog := h.Log.WithValues("custom-default-backend", "request")
start := time.Now()
// if debug is enabled, then set the headers in the response too
if os.Getenv("DEBUG") == "true" {
w.Header().Set(FormatHeader, r.Header.Get(FormatHeader))
w.Header().Set(CodeHeader, r.Header.Get(CodeHeader))
w.Header().Set(ContentType, r.Header.Get(ContentType))
w.Header().Set(OriginalURI, r.Header.Get(OriginalURI))
w.Header().Set(Namespace, r.Header.Get(Namespace))
w.Header().Set(IngressName, r.Header.Get(IngressName))
w.Header().Set(ServiceName, r.Header.Get(ServiceName))
w.Header().Set(ServicePort, r.Header.Get(ServicePort))
w.Header().Set(RequestID, r.Header.Get(RequestID))
}
format := r.Header.Get(FormatHeader)
if format == "" {
format = "text/html"
}
// Browser may send a list of accepted formats, but we only want the first reported content type for the response
if strings.Contains(format, ",") {
format = strings.Split(format, ",")[0]
}
w.Header().Set(ContentType, format)
w.Header().Set(AergiaHeader, "true")
w.Header().Set(CacheControl, "private,no-store")
errCode := r.Header.Get(CodeHeader)
code, err := strconv.Atoi(errCode)
if err != nil {
code = h.DefaultHTTPResponseCode
}
w.WriteHeader(code)
ns := r.Header.Get(Namespace)
originalURI := r.Header.Get(OriginalURI)
serviceName := r.Header.Get(ServiceName)
ingressName := r.Header.Get(IngressName)
hostname := ""
// haproxy requests will set query param `unidle=true`
unidleReq := r.URL.Query().Get("unidle")
if unidleReq != "" {
opLog.Info(fmt.Sprintf("UnidleReq Param: %s", unidleReq))
// and haproxy requests include the `Referer` header for which url was requested by the client
urlParam := r.Header.Get(Referer)
if urlParam != "" {
// if unidle and referrer are set
opLog.Info(fmt.Sprintf("Referer: %s", urlParam))
url, err := url.Parse(urlParam)
if err != nil {
opLog.Info(fmt.Sprintf("URL err: %v", err))
}
originalURI = urlParam
hostname = url.Hostname()
// look for an idled ingress that matches this hostname
i, svcName, err := h.getIngressByHostname(ctx, hostname)
if err == nil {
ns = i.Namespace
serviceName = svcName
}
}
}
// traefik requests will set the `namespace` and `url` query params
nsParam := r.URL.Query().Get("namespace")
if nsParam != "" {
opLog.Info(fmt.Sprintf("Namespace Param: %s", nsParam))
ns = nsParam
}
urlParam := r.URL.Query().Get("url")
if urlParam != "" {
url, err := url.Parse(urlParam)
if err != nil {
opLog.Info(fmt.Sprintf("URL Param err: %v", err))
}
opLog.Info(fmt.Sprintf("URL Param: %s", urlParam))
hostname = url.Hostname()
}
// check if the namespace exists so we know this is somewhat legitimate request
if ns != "" {
namespace := &corev1.Namespace{}
if err := h.Client.Get(ctx, types.NamespacedName{
Name: ns,
}, namespace); err != nil {
opLog.Info(fmt.Sprintf("unable to get any namespaces: %v", err))
return
}
ingress := &networkv1.Ingress{}
if ingressName != "" {
if err := h.Client.Get(ctx, types.NamespacedName{
Namespace: ns,
Name: ingressName,
}, ingress); err != nil {
opLog.Info(fmt.Sprintf("Unable to get the ingress %s in %s", ingressName, ns))
h.genericError(w, r, opLog, format, path, 400)
h.setMetrics(r, start)
return
}
} else {
listOption := (&ctrlClient.ListOptions{}).ApplyOptions([]ctrlClient.ListOption{
ctrlClient.InNamespace(ns),
})
ingresses := &networkv1.IngressList{}
if err := h.Client.List(ctx, ingresses, listOption); err != nil {
opLog.Info(fmt.Sprintf("Unable to get any ingress - %s", ns))
return
}
for _, ingressss := range ingresses.Items {
for _, rule := range ingressss.Spec.Rules {
for _, host := range rule.Host {
if string(host) == hostname {
ingress = ingressss.DeepCopy()
}
}
}
}
}
// if hmac verification is enabled, perform the verification of the request
signedNamespace, verfied := h.verifyRequest(r, namespace, ingress)
xForwardedFor := strings.Split(r.Header.Get("X-Forwarded-For"), ",")
trueClientIP := r.Header.Get("True-Client-IP")
requestUserAgent := r.Header.Get("User-Agent")
allowUnidle := h.checkAccess(namespace.Annotations, ingress.Annotations, requestUserAgent, trueClientIP, xForwardedFor)
// then run checks to start to unidle the environment
if allowUnidle {
// if a namespace exists, it means that the custom-http-errors code is defined in the ingress object
// so do something with that here, like kickstart the idler process to unidle targets
if h.Debug {
opLog.Info(fmt.Sprintf("Request for %s verfied: %t from xff:%s; tcip:%s; ua: %s, ", ns, verfied, xForwardedFor, trueClientIP, requestUserAgent))
}
file := fmt.Sprintf("%v/unidle.html", path)
forceScaled := h.checkForceScaled(ctx, ns, opLog)
if forceScaled {
// if this has been force scaled, return the force scaled landing page
file = fmt.Sprintf("%v/forced.html", path)
} else {
// only unidle environments that aren't force scaled
// actually do the unidling here, lock to prevent multiple unidle operations from running
if verfied {
if h.Debug {
opLog.Info(fmt.Sprintf("Request for %s verfied", ns))
}
metrics.AllowedRequests.Inc()
w.Header().Set("X-Aergia-Allowed", "true")
_, ok := h.Locks.Load(ns)
if !ok {
_, _ = h.Locks.LoadOrStore(ns, ns)
if h.Debug {
opLog.Info(fmt.Sprintf("Unidle request for %s verfied", ns))
}
go h.Unidle(ctx, namespace, opLog)
}
} else {
metrics.VerificationRequired.Inc()
w.Header().Set("X-Aergia-Verification-Required", "true")
}
}
if h.Debug {
opLog.Info(fmt.Sprintf("Serving custom error response for code %v and format %v from file %v", code, format, file))
}
// then return the unidle template to the user
tmpl := template.Must(template.ParseFiles(file))
_ = tmpl.ExecuteTemplate(w, "base", pageData{
ErrorCode: strconv.Itoa(code),
FormatHeader: r.Header.Get(FormatHeader),
CodeHeader: r.Header.Get(CodeHeader),
ContentType: r.Header.Get(ContentType),
OriginalURI: originalURI,
Namespace: ns,
IngressName: ingress.Name,
ServiceName: serviceName,
ServicePort: r.Header.Get(ServicePort),
RequestID: r.Header.Get(RequestID),
RefreshInterval: h.RefreshInterval,
Verifier: signedNamespace,
})
} else {
// respond with forbidden
w.Header().Set("X-Aergia-Denied", "true")
metrics.BlockedRequests.Inc()
h.genericError(w, r, opLog, format, path, 403)
}
} else {
w.Header().Set("X-Aergia-Denied", "true")
w.Header().Set("X-Aergia-No-Namespace", "true")
metrics.NoNamespaceRequests.Inc()
h.genericError(w, r, opLog, format, path, code)
}
h.setMetrics(r, start)
}
}
func (h *Unidler) genericError(w http.ResponseWriter, r *http.Request, opLog logr.Logger, format, path string, code int) {
file := fmt.Sprintf("%v/error.html", path)
if h.Debug {
opLog.Info(fmt.Sprintf("Serving custom error response for code %v and format %v from file %v", code, format, file))
}
tmpl := template.Must(template.ParseFiles(file))
_ = tmpl.ExecuteTemplate(w, "base", pageData{
ErrorCode: strconv.Itoa(code),
ErrorMessage: http.StatusText(code),
FormatHeader: r.Header.Get(FormatHeader),
CodeHeader: r.Header.Get(CodeHeader),
ContentType: r.Header.Get(ContentType),
OriginalURI: r.Header.Get(OriginalURI),
Namespace: r.Header.Get(Namespace),
IngressName: r.Header.Get(IngressName),
ServiceName: r.Header.Get(ServiceName),
ServicePort: r.Header.Get(ServicePort),
RequestID: r.Header.Get(RequestID),
RefreshInterval: h.RefreshInterval,
})
}
// handle verifying the namespace name is signed by our secret
func (h *Unidler) verifyRequest(r *http.Request, ns *corev1.Namespace, ingress *networkv1.Ingress) (string, bool) {
if h.VerifiedUnidling {
if val, ok := ingress.Annotations["idling.amazee.io/disable-request-verification"]; ok {
t, _ := strconv.ParseBool(val)
if t {
return "", true
}
// otherwise fall through to namespace check
}
if val, ok := ns.Annotations["idling.amazee.io/disable-request-verification"]; ok {
t, _ := strconv.ParseBool(val)
if t {
return "", true
}
// fall through to verify the request
}
// if hmac verification is enabled, perform the verification of the request
signedNamespace := hmacSigner(ns.Name, []byte(h.VerifiedSecret))
verifier := r.URL.Query().Get("verifier")
metrics.VerificationRequests.Inc()
return signedNamespace, hmacVerifier(ns.Name, verifier, []byte(h.VerifiedSecret))
}
return "", true
}
func (h *Unidler) setMetrics(r *http.Request, start time.Time) {
duration := time.Since(start).Seconds()
proto := strconv.Itoa(r.ProtoMajor)
proto = fmt.Sprintf("%s.%s", proto, strconv.Itoa(r.ProtoMinor))
metrics.RequestCount.WithLabelValues(proto).Inc()
metrics.RequestDuration.WithLabelValues(proto).Observe(duration)
}
func (h *Unidler) getIngressByHostname(ctx context.Context, targetHost string) (*networkv1.Ingress, string, error) {
ingressList := &networkv1.IngressList{}
labelRequirements, _ := labels.NewRequirement("idling.amazee.io/idled", selection.Equals, []string{"true"})
listOption := (&ctrlClient.ListOptions{}).ApplyOptions([]ctrlClient.ListOption{
ctrlClient.MatchingLabelsSelector{
Selector: labels.NewSelector().Add(*labelRequirements),
},
})
err := h.Client.List(ctx, ingressList, listOption)
if err != nil {
return nil, "", err
}
for _, ingress := range ingressList.Items {
for _, rule := range ingress.Spec.Rules {
if rule.Host == targetHost {
return &ingress, rule.HTTP.Paths[0].Backend.Service.Name, nil
}
}
}
return nil, "", fmt.Errorf("ingress with host %s not found", targetHost)
}