/
middleware.go
253 lines (215 loc) · 6.45 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
package http
import (
"context"
"fmt"
"net/http"
"path"
"regexp"
"strings"
"time"
"github.com/go-chi/chi"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/kit/platform/errors"
"github.com/influxdata/influxdb/v2/kit/tracing"
ua "github.com/mileusna/useragent"
"github.com/prometheus/client_golang/prometheus"
)
// Middleware constructor.
type Middleware func(http.Handler) http.Handler
func SetCORS(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
if origin := r.Header.Get("Origin"); origin != "" {
// Access-Control-Allow-Origin must be present in every response
w.Header().Set("Access-Control-Allow-Origin", origin)
}
if r.Method == http.MethodOptions {
// allow and stop processing in pre-flight requests
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization, User-Agent")
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func Metrics(name string, reqMetric *prometheus.CounterVec, durMetric *prometheus.HistogramVec) Middleware {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
statusW := NewStatusResponseWriter(w)
defer func(start time.Time) {
statusCode := statusW.Code()
// only log metrics for 2XX or 5XX requests
if !reportFromCode(statusCode) {
return
}
label := prometheus.Labels{
"handler": name,
"method": r.Method,
"path": normalizePath(r.URL.Path),
"status": statusW.StatusCodeClass(),
"response_code": fmt.Sprintf("%d", statusCode),
"user_agent": UserAgent(r),
}
durMetric.With(label).Observe(time.Since(start).Seconds())
reqMetric.With(label).Inc()
}(time.Now())
next.ServeHTTP(statusW, r)
}
return http.HandlerFunc(fn)
}
}
func SkipOptions(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
// Preflight CORS requests from the browser will send an options request,
// so we need to make sure we satisfy them
if origin := r.Header.Get("Origin"); origin == "" && r.Method == http.MethodOptions {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func Trace(name string) Middleware {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
span, r := tracing.ExtractFromHTTPRequest(r, name)
defer span.Finish()
span.LogKV("user_agent", UserAgent(r))
for k, v := range r.Header {
if len(v) == 0 {
continue
}
if k == "Authorization" || k == "User-Agent" {
continue
}
// If header has multiple values, only the first value will be logged on the trace.
span.LogKV(k, v[0])
}
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
}
func UserAgent(r *http.Request) string {
header := r.Header.Get("User-Agent")
if header == "" {
return "unknown"
}
return ua.Parse(header).Name
}
// Constants used for normalizing paths
const (
fileSlug = ":file_name"
shardSlug = ":shard_id"
)
func normalizePath(p string) string {
// Normalize any paths used during backup or restore processes
p = normalizeBackupAndRestore(p)
// Go through each part of the path and normalize IDs and UI assets
var parts []string
for head, tail := shiftPath(p); ; head, tail = shiftPath(tail) {
piece := head
// Normalize any ID's in the path as the ":id" slug
if len(piece) == platform.IDLength {
if _, err := platform.IDFromString(head); err == nil {
piece = ":id"
}
}
parts = append(parts, piece)
if tail == "/" {
// Normalize UI asset file names. The UI asset file is the last part of the path.
parts[len(parts)-1] = normalizeAssetFile(parts[len(parts)-1])
break
}
}
return "/" + path.Join(parts...)
}
func shiftPath(p string) (head, tail string) {
p = path.Clean("/" + p)
i := strings.Index(p[1:], "/") + 1
if i <= 0 {
return p[1:], "/"
}
return p[1:i], p[i:]
}
// Normalize the file name for a UI asset
// For example: 838442d56d.svg will return as :file_id.svg
// Files names that do not have one of the listed extensions will be returned unchanged
func normalizeAssetFile(f string) string {
exts := []string{
".js",
".svg",
".woff2",
".wasm",
".map",
".LICENSE",
".ttf",
".woff",
".eot",
}
for _, ext := range exts {
if strings.HasSuffix(f, ext) {
return fileSlug + ext
}
}
return f
}
// Normalize paths used during the backup and restore process.
// Paths not matching any of the patterns will be returned unchanged.
func normalizeBackupAndRestore(pth string) string {
patterns := map[string]string{
`restore/shards/\d+`: path.Join("restore/shards", shardSlug),
`backup/shards/\d+`: path.Join("backup/shards", shardSlug),
}
for p, s := range patterns {
re := regexp.MustCompile(p)
if re.MatchString(pth) {
return re.ReplaceAllString(pth, s)
}
}
return pth
}
type OrgContext string
const CtxOrgKey OrgContext = "orgID"
// ValidResource make sure a resource exists when a sub system needs to be mounted to an api
func ValidResource(api *API, lookupOrgByResourceID func(context.Context, platform.ID) (platform.ID, error)) Middleware {
return func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
statusW := NewStatusResponseWriter(w)
id, err := platform.IDFromString(chi.URLParam(r, "id"))
if err != nil {
api.Err(w, r, platform.ErrCorruptID(err))
return
}
ctx := r.Context()
orgID, err := lookupOrgByResourceID(ctx, *id)
if err != nil {
// if this function returns an error we will squash the error message and replace it with a not found error
api.Err(w, r, &errors.Error{
Code: errors.ENotFound,
Msg: "404 page not found",
})
return
}
// embed OrgID into context
next.ServeHTTP(statusW, r.WithContext(context.WithValue(ctx, CtxOrgKey, orgID)))
}
return http.HandlerFunc(fn)
}
}
// OrgIDFromContext ....
func OrgIDFromContext(ctx context.Context) *platform.ID {
v := ctx.Value(CtxOrgKey)
if v == nil {
return nil
}
id := v.(platform.ID)
return &id
}
// reportFromCode is a helper function to determine if telemetry data should be
// reported for this response.
func reportFromCode(c int) bool {
return (c >= 200 && c <= 299) || (c >= 500 && c <= 599)
}