-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.go
316 lines (268 loc) · 8.93 KB
/
middleware.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package proxy
import (
"context"
"crypto/rsa"
"encoding/base64"
"fmt"
"github.com/SermoDigital/jose/crypto"
"github.com/SermoDigital/jose/jws"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/square/go-jose.v2/jwt"
"log"
"math/big"
"net/http"
"net/url"
"strings"
)
type ctxKey string
var (
contextKey = ctxKey("Context")
subjectKey = ctxKey("Subject")
)
var frontendGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "multikube_frontend_live_requests",
Help: "A gauge of live requests currently in flight from clients",
})
var frontendCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "multikube_frontend_requests_total",
Help: "A counter for requests from clients",
},
[]string{"code", "method"},
)
var frontendHistogram = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "multikube_frontend_request_duration_seconds",
Help: "A histogram of request latencies from clients.",
Buckets: prometheus.DefBuckets,
},
[]string{"handler", "method"},
)
// responseSize has no labels, making it a zero-dimensional
// ObserverVec.
var responseSize = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "multikube_frontend_response_size_bytes",
Help: "A histogram of response sizes for client requests",
Buckets: []float64{200, 500, 900, 1500},
},
[]string{},
)
// Middleware represents a multikube middleware
type Middleware func(*Config, http.Handler) http.Handler
// responseWriter implements http.ResponseWriter and adds status code
// so that WithLogging middleware can log response status codes
type responseWriter struct {
http.ResponseWriter
status int
}
// responseError satisfies the error interface
type responseError struct {
Status int `json:"status"`
Errs []string `json:"errors"`
}
func init() {
prometheus.MustRegister(frontendGauge, frontendCounter, frontendHistogram, responseSize)
}
// getTokenFromRequest returns a []byte representation of JWT from an HTTP Authorization Bearer header
func getTokenFromRequest(req *http.Request) []byte {
if ah := req.Header.Get("Authorization"); len(ah) > 7 && strings.EqualFold(ah[0:7], "BEARER ") {
return []byte(ah[7:])
}
return nil
}
// WriteHeader sends and sets an HTTP response header with the provided
// status code.
func (r *responseWriter) WriteHeader(statusCode int) {
r.status = statusCode
r.ResponseWriter.WriteHeader(statusCode)
}
// WithEmpty is an empty handler that does nothing
func WithEmpty(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r)
})
}
// WithMetrics is an empty handler that does nothing
func WithMetrics(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
pushChain := promhttp.InstrumentHandlerInFlight(frontendGauge,
promhttp.InstrumentHandlerDuration(frontendHistogram.MustCurryWith(prometheus.Labels{"handler": "push"}),
promhttp.InstrumentHandlerCounter(frontendCounter,
promhttp.InstrumentHandlerResponseSize(responseSize, next),
),
),
)
pushChain.ServeHTTP(w, r)
})
}
// WithTracing is a middleware that starts a new span and populates the context
func WithTracing(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span := opentracing.GlobalTracer().StartSpan("hello")
ctx := opentracing.ContextWithSpan(r.Context(), span)
defer span.Finish()
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// WithLogging applies access log style logging to the HTTP server
func WithLogging(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lrw := &responseWriter{w, http.StatusOK}
next.ServeHTTP(w, r)
log.Printf("%s %s %s %s %s %d", r.Method, r.URL.Path, r.URL.RawQuery, r.RemoteAddr, r.Proto, lrw.status)
})
}
// WithJWT is a middleware that parses a JWT token from the requests and propagates
// the request context with a claim value.
func WithJWT(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Get the JWT from the request
t, err := jws.ParseJWTFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Check if request has empty credentials
if t == nil {
http.Error(w, "No valid access token", http.StatusUnauthorized)
return
}
// Set context
username, ok := t.Claims().Get(c.OIDCUsernameClaim).(string)
if !ok {
username = ""
}
ctx := context.WithValue(r.Context(), subjectKey, username)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// WithX509Validation is a middleware that validates a JWT token in the http request using RS256 signing method.
// It will do so using a x509 certificate provided in c
func WithX509Validation(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t, err := jws.ParseJWTFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if t == nil {
http.Error(w, "No token in request", http.StatusUnauthorized)
return
}
err = t.Validate(c.RS256PublicKey.PublicKey, crypto.SigningMethodRS256)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Set context
username, ok := t.Claims().Get(c.OIDCUsernameClaim).(string)
if !ok {
username = ""
}
ctx := context.WithValue(r.Context(), subjectKey, username)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// WithJWKValidation is a middleware that validates a JWT token in the http request using RS256 signing method.
// It will do so using a JWK (Json Web Key) provided in c
func WithJWKValidation(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t, err := jws.ParseJWTFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
raw := string(getTokenFromRequest(r))
tok, err := jwt.ParseSigned(raw)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Try to find a JWK using the kid
kid := tok.Headers[0].KeyID
jwk := c.JWKS.Find(kid)
if jwk == nil {
http.Error(w, "key id invalid", http.StatusUnauthorized)
return
}
if jwk.Kty != "RSA" {
http.Error(w, fmt.Sprintf("Invalid key type. Expected 'RSA' got '%s'", jwk.Kty), http.StatusUnauthorized)
return
}
// decode the base64 bytes for n
nb, err := base64.RawURLEncoding.DecodeString(jwk.N)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
// Check if E is big-endian int
if jwk.E != "AQAB" && jwk.E != "AAEAAQ" {
http.Error(w, fmt.Sprintf("Expected E to be one of 'AQAB' and 'AAEAAQ' but got '%s'", jwk.E), http.StatusUnauthorized)
return
}
pk := &rsa.PublicKey{
N: new(big.Int).SetBytes(nb),
E: 65537,
}
err = t.Validate(pk, crypto.SigningMethodRS256)
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
username, ok := t.Claims().Get(c.OIDCUsernameClaim).(string)
if !ok {
username = ""
}
ctx := context.WithValue(r.Context(), subjectKey, username)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
// WithHeader is a middleware that reads the value of the HTTP header "Multikube-Context"
// in the request and, if found, sets it's value in the request context.
func WithHeader(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := r
header := r.Header.Get("Multikube-Context")
if header != "" {
ctx := context.WithValue(r.Context(), contextKey, header)
req = r.WithContext(ctx)
}
next.ServeHTTP(w, req)
})
}
// WithCtxRoot is a middleware that reads the url path params in the request and
// tries to determine which kubeconfig context to use for upstream api server requests.
// If a context is found in the URL path params, the request-context is populated with the value
// so that other handlers and middlewares may use the information
func WithCtxRoot(c *Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req := r
c, rem := getCtxFromURL(r.URL)
if c != "" {
ctx := context.WithValue(r.Context(), contextKey, c)
req = r.WithContext(ctx)
if rem != "" {
req.URL.Path = rem
}
}
next.ServeHTTP(w, req)
})
}
// getCtxFromURL reads path params from u and returns the kubeconfig context
// as well as the path params used for upstream communication
func getCtxFromURL(u *url.URL) (string, string) {
val := ""
rem := []string{}
if vals := strings.Split(u.Path, "/"); len(vals) > 1 {
val = vals[1]
rem = vals[2:]
}
return val, fmt.Sprintf("/%s", strings.Join(rem, "/"))
}