-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.go
More file actions
212 lines (198 loc) · 7.66 KB
/
handler.go
File metadata and controls
212 lines (198 loc) · 7.66 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
package unidler
import (
"context"
"fmt"
"net/http"
"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/types"
)
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)
ingressName := r.Header.Get(IngressName)
// 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 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
}
// 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: 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,
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)
}