-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_router.go
364 lines (298 loc) · 12.7 KB
/
api_router.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
Package apirouter is a lightweight API router middleware for CORS, logging, and standardized error handling.
This package is intended to be used with Julien Schmidt's httprouter and uses MrZ's go-logger package.
*/
package apirouter
import (
"net/http"
"runtime/debug"
"strings"
"time"
"github.com/gofrs/uuid"
"github.com/julienschmidt/httprouter"
"github.com/mrz1836/go-logger"
"github.com/mrz1836/go-parameters"
"github.com/newrelic/go-agent/v3/integrations/nrhttprouter"
"github.com/newrelic/go-agent/v3/newrelic"
)
// Headers for CORs and Authentication
const (
// connectionHeader string = "Connection"
allowCredentialsHeader string = "Access-Control-Allow-Credentials"
allowHeadersHeader string = "Access-Control-Allow-Headers"
allowMethodsHeader string = "Access-Control-Allow-Methods"
allowOriginHeader string = "Access-Control-Allow-Origin"
authenticateHeader string = "WWW-Authenticate"
contentTypeHeader string = "Content-Type"
defaultHeaders string = "Accept, Content-Type, Content-Length, Cache-Control, Pragma, Accept-Encoding, X-CSRF-Token, Authorization, X-Auth-Cookie"
defaultMethods string = "POST, GET, OPTIONS, PUT, DELETE, HEAD"
exposeHeader string = "Access-Control-Expose-Headers"
forwardedHost string = "x-forwarded-host"
forwardedProtocol string = "x-forwarded-proto"
origin string = "Origin"
varyHeaderString string = "Vary"
)
// Log formats for the request
const (
LogErrorFormat string = "request_id=\"%s\" ip_address=\"%s\" type=\"%s\" internal_message=\"%s\" code=%d\n"
LogPanicFormat string = "request_id=\"%s\" method=\"%s\" path=\"%s\" type=\"%s\" error_message=\"%s\" stack_trace=\"%s\"\n"
LogParamsFormat string = "request_id=\"%s\" method=\"%s\" path=\"%s\" ip_address=\"%s\" user_agent=\"%s\" params=\"%v\"\n"
LogTimeFormat string = "request_id=\"%s\" method=\"%s\" path=\"%s\" ip_address=\"%s\" user_agent=\"%s\" service=%dms status=%d\n"
)
// Package variables
var (
authTokenKey paramRequestKey = "auth_token"
customDataKey paramRequestKey = "custom_data"
ipAddressKey paramRequestKey = "ip_address"
requestIDKey paramRequestKey = "request_id"
// defaultFilterFields is the fields to filter from logs
defaultFilterFields = []string{
"api_key",
"jwt",
"new_password",
"new_password_confirmation",
"oauth",
"oauth_token",
"password",
"password_check",
"password_confirm",
"password_confirmation",
"social_security_number",
"ssn",
"token",
}
)
// paramRequestKey for context key
type paramRequestKey string
// Router is the configuration for the middleware service
type Router struct {
AccessControlExposeHeaders string `json:"access_control_expose_headers" url:"access_control_expose_headers"` // Allow specific headers for cors
CrossOriginAllowCredentials bool `json:"cross_origin_allow_credentials" url:"cross_origin_allow_credentials"` // Allow credentials for BasicAuth()
CrossOriginAllowHeaders string `json:"cross_origin_allow_headers" url:"cross_origin_allow_headers"` // Allowed headers
CrossOriginAllowMethods string `json:"cross_origin_allow_methods" url:"cross_origin_allow_methods"` // Allowed methods
CrossOriginAllowOrigin string `json:"cross_origin_allow_origin" url:"cross_origin_allow_origin"` // Custom value for allow origin
CrossOriginAllowOriginAll bool `json:"cross_origin_allow_origin_all" url:"cross_origin_allow_origin_all"` // Allow all origins
CrossOriginEnabled bool `json:"cross_origin_enabled" url:"cross_origin_enabled"` // Enable or Disable CrossOrigin
FilterFields []string `json:"filter_fields" url:"filter_fields"` // Filter out protected fields from logging
HTTPRouter *nrhttprouter.Router `json:"-" url:"-"` // NewRelic wrapper for J Schmidt's httprouter
Logger LoggerInterface `json:"-" url:"-"` // Logger interface
SkipLoggingPaths []string `json:"skip_logging_paths" url:"skip_logging_paths"` // Skip logging on these paths (IE: /health)
loadedNewRelic bool
}
// NewWithNewRelic returns a router middleware configuration with NewRelic enabled
func NewWithNewRelic(app *newrelic.Application) *Router {
return defaultRouter(app)
}
// defaultRouter is the default settings of the Router/Config
func defaultRouter(app *newrelic.Application) (r *Router) {
// Create new configuration
r = new(Router)
// Default is cross_origin = enabled
r.CrossOriginEnabled = true
// Default is to allow credentials for BasicAuth()
r.CrossOriginAllowCredentials = true
// Default is to allow all (easier to get started)
r.CrossOriginAllowOriginAll = true
// Default is defaultHeaders
r.CrossOriginAllowHeaders = defaultHeaders
// Default is for the common request methods
r.CrossOriginAllowMethods = defaultMethods
// Create the router (nil if app is not set)
r.HTTPRouter = nrhttprouter.New(app)
r.loadedNewRelic = app != nil
// Set the defaults
r.setDefaults()
// Set the filter fields to default
r.FilterFields = defaultFilterFields
// Set the default implementation (which can now be overridden)
r.Logger = logger.GetImplementation()
return
}
// setDefaults will set the router defaults
func (r *Router) setDefaults() {
// Turn on trailing slash redirect
r.HTTPRouter.RedirectTrailingSlash = true
r.HTTPRouter.RedirectFixedPath = true
// Turn on default CORs options handler
r.HTTPRouter.HandleOPTIONS = true
r.HTTPRouter.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// Turned cross_origin off?
if !r.CrossOriginEnabled {
return
}
// Set the header
header := w.Header()
// If we're using NewRelic - ignore options requests (default)
if r.loadedNewRelic {
txn := newrelic.FromContext(req.Context())
txn.Ignore()
}
// On for all origins?
if r.CrossOriginAllowOriginAll {
// Normal requests use the Origin header
originDomain := req.Header.Get(origin)
if len(originDomain) == 0 {
// Maybe it's behind a proxy?
originDomain = req.Header.Get(forwardedHost)
if len(originDomain) > 0 {
originDomain = req.Header.Get(forwardedProtocol) + "//" + originDomain
}
}
header.Set(allowOriginHeader, originDomain)
header.Set(varyHeaderString, origin)
} else { // Only the origin set by config
header.Set(allowOriginHeader, r.CrossOriginAllowOrigin)
}
// Allow credentials (used for BasicAuth)
if r.CrossOriginAllowCredentials {
header.Set(allowCredentialsHeader, "true")
}
// Set access control
header.Set(allowMethodsHeader, r.CrossOriginAllowMethods)
header.Set(allowHeadersHeader, r.CrossOriginAllowHeaders)
// Adjust status code to 204
w.WriteHeader(http.StatusNoContent)
})
}
// New returns a router middleware configuration to use for all future requests
func New() *Router {
return defaultRouter(nil)
}
// Request will write the request to the logs before and after calling the handler
func (r *Router) Request(h httprouter.Handle) httprouter.Handle {
return parameters.MakeHTTPRouterParsedReq(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
// Get the params from parameters.GetParams(req)
params := GetParams(req)
// Start the custom response writer
// var writer *APIResponseWriter
guid, _ := uuid.NewV4()
writer := &APIResponseWriter{
IPAddress: GetClientIPAddress(req),
Method: req.Method,
RequestID: guid.String(),
ResponseWriter: w,
Status: 0, // future use with E-tags
URL: req.URL.String(),
UserAgent: req.UserAgent(),
}
// Store key information into the request that can be used by other methods
req = SetOnRequest(req, ipAddressKey, writer.IPAddress)
req = SetOnRequest(req, requestIDKey, writer.RequestID)
// Set cross-origin on each request that goes through logging
r.SetCrossOriginHeaders(writer, req, ps)
// Set access control headers
if len(r.AccessControlExposeHeaders) > 0 {
w.Header().Set(exposeHeader, r.AccessControlExposeHeaders)
}
// Do we have paths to skip?
// todo: this was added because some requests are confidential or "health-checks" and they can't be split apart from the router
var skipLogging bool
if len(r.SkipLoggingPaths) > 0 {
for _, path := range r.SkipLoggingPaths {
if path == req.URL.Path {
skipLogging = true
break
}
}
}
// Skip logging this specific request
if !skipLogging {
// Capture the panics and log
defer func() {
if err := recover(); err != nil {
r.Logger.Printf(LogPanicFormat, writer.RequestID, writer.Method, writer.URL, "error", err.(error).Error(), strings.Replace(string(debug.Stack()), "\n", ";", -1))
}
}()
// Start the log (timer)
r.Logger.Printf(LogParamsFormat, writer.RequestID, writer.Method, writer.URL, writer.IPAddress, writer.UserAgent, FilterMap(params, r.FilterFields).Values)
start := time.Now()
// Fire the request
h(writer, req, ps)
// Complete the timer and final log
elapsed := time.Since(start)
r.Logger.Printf(LogTimeFormat, writer.RequestID, writer.Method, writer.URL, writer.IPAddress, writer.UserAgent, int64(elapsed/time.Millisecond), writer.Status)
} else {
// Fire the request (no logging)
h(writer, req, ps)
}
})
}
// RequestNoLogging will just call the handler without any logging
// Used for API calls that do not require any logging overhead
func (r *Router) RequestNoLogging(h httprouter.Handle) httprouter.Handle {
return parameters.MakeHTTPRouterParsedReq(func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
// Start the custom response writer
guid, _ := uuid.NewV4()
writer := &APIResponseWriter{
IPAddress: GetClientIPAddress(req),
Method: req.Method,
RequestID: guid.String(),
ResponseWriter: w,
Status: 0, // future use with E-tags
URL: req.URL.String(),
UserAgent: req.UserAgent(),
}
// Store key information into the request that can be used by other methods
req = SetOnRequest(req, ipAddressKey, writer.IPAddress)
req = SetOnRequest(req, requestIDKey, writer.RequestID)
// Set cross-origin on each request that goes through logging
r.SetCrossOriginHeaders(writer, req, ps)
// Set access control headers
if len(r.AccessControlExposeHeaders) > 0 {
w.Header().Set(exposeHeader, r.AccessControlExposeHeaders)
}
// Fire the request
h(writer, req, ps)
})
}
// BasicAuth wraps a request for Basic Authentication (RFC 2617)
func (r *Router) BasicAuth(h httprouter.Handle, requiredUser, requiredPassword string, errorResponse interface{}) httprouter.Handle {
// Return the function up the chain
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
// Get the Basic Authentication credentials
user, password, hasAuth := req.BasicAuth()
if hasAuth && user == requiredUser && password == requiredPassword {
// Delegate request to the given handle
h(w, req, ps)
} else {
// Request Basic Authentication otherwise
w.Header().Set(authenticateHeader, "Basic realm=Restricted")
ReturnResponse(w, req, http.StatusUnauthorized, errorResponse)
}
}
}
// SetCrossOriginHeaders sets the cross-origin headers if enabled
// todo: combine this method and the GlobalOPTIONS http.HandlerFunc() method (@mrz had an issue combining)
func (r *Router) SetCrossOriginHeaders(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
// Turned cross_origin off?
if !r.CrossOriginEnabled {
return
}
// Set the header
header := w.Header()
// On for all origins?
if r.CrossOriginAllowOriginAll {
// Normal requests use the Origin header
originDomain := req.Header.Get(origin)
if len(originDomain) == 0 {
// Maybe it's behind a proxy?
originDomain = req.Header.Get(forwardedHost)
if len(originDomain) > 0 {
originDomain = req.Header.Get(forwardedProtocol) + "//" + originDomain
}
}
header.Set(allowOriginHeader, originDomain)
header.Set(varyHeaderString, origin)
} else { // Only the origin set by config
header.Set(allowOriginHeader, r.CrossOriginAllowOrigin)
}
// Allow credentials (used for BasicAuth)
if r.CrossOriginAllowCredentials {
header.Set(allowCredentialsHeader, "true")
}
// Set access control
header.Set(allowMethodsHeader, r.CrossOriginAllowMethods)
header.Set(allowHeadersHeader, r.CrossOriginAllowHeaders)
// Adjust status code to 204 (Leaving this out, allowing customized response)
// w.WriteHeader(http.StatusNoContent)
}