-
Notifications
You must be signed in to change notification settings - Fork 28
/
rest.go
241 lines (198 loc) · 5.97 KB
/
rest.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
// Copyright 2015-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"fmt"
"net/http"
"runtime"
"github.com/gorilla/mux"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
audit "github.com/couchbase/goutils/go-cbaudit"
)
// MapRESTPathStats is keyed by path spec strings.
var MapRESTPathStats map[string]*rest.RESTPathStats
var indexPaths = map[string]bool{
"/api/index/{indexName}": true,
"/api/bucket/{bucketName}/scope/{scopeName}/index/{indexName}": true,
}
var queryPaths = map[string]bool{
"/api/index/{indexName}/query": true,
"/api/bucket/{bucketName}/scope/{scopeName}/index/{indexName}/query": true,
"/Search": true, // grpc requests from n1fty
}
func isIndexPath(path string) bool {
return indexPaths[path]
}
func isQueryPath(path string) bool {
return queryPaths[path]
}
// -----------------------------------------------------------------------------
func InitRESTPathStats() {
MapRESTPathStats = make(map[string]*rest.RESTPathStats)
for k := range queryPaths {
MapRESTPathStats[k] = &rest.RESTPathStats{}
}
}
func InitStaticRouter(staticDir, staticETag string,
mgr *cbgt.Manager) *mux.Router {
router := mux.NewRouter()
router.StrictSlash(true)
prefix := ""
if mgr != nil {
prefix = mgr.GetOption("urlPrefix")
}
hfsStaticX := http.FileServer(assetFS())
router.Handle(prefix+"/",
http.RedirectHandler(prefix+"/index.html", 302))
router.Handle(prefix+"/index.html",
http.RedirectHandler(prefix+"/staticx/index.html", 302))
router.Handle(prefix+"/static/partials/index/start.html",
http.RedirectHandler(prefix+"/staticx/partials/index/start.html", 302))
router.Handle(prefix+"/static/partials/index/new.html",
http.RedirectHandler(prefix+"/staticx/partials/index/ft/new.html", 302))
router.Handle(prefix+"/static/partials/index/list.html",
http.RedirectHandler(prefix+"/staticx/partials/index/ft/list.html", 302))
router = rest.InitStaticRouterEx(router,
staticDir, staticETag, []string{
prefix + "/indexes",
prefix + "/nodes",
prefix + "/monitor",
prefix + "/manage",
prefix + "/logs",
prefix + "/debug",
}, http.RedirectHandler(prefix+"/staticx/index.html", 302), mgr)
staticxRoutes := []string{
"/staticx/",
"/staticx/css/cbft.css",
"/staticx/index.html",
"/staticx/index-ft.html",
"/staticx/js/debug.js",
"/staticx/partials/debug-rows.html",
"/staticx/partials/debug.html",
"/staticx/partials/index/ft/list.html",
"/staticx/partials/index/ft/new.html",
"/staticx/partials/index/start.html",
}
for _, route := range staticxRoutes {
router.Handle(prefix+route, http.StripPrefix(prefix+"/staticx/", hfsStaticX))
}
return router
}
func myAssetDir(name string) ([]string, error) {
a, err := AssetDir(name)
if err == nil {
return a, err
}
return rest.AssetDir(name)
}
func myAsset(name string) ([]byte, error) {
b, err := Asset(name)
if err == nil {
return b, err
}
return rest.Asset(name)
}
// NewRESTRouter creates a mux.Router initialized with the REST
// API and web UI routes. See also InitStaticRouter if you need finer
// control of the router initialization.
func NewRESTRouter(versionMain string, mgr *cbgt.Manager,
staticDir, staticETag string, mr *cbgt.MsgRing,
adtSvc *audit.AuditSvc) (
*mux.Router, map[string]rest.RESTMeta, error) {
wrapAuthVersionHandler := func(h http.Handler) http.Handler {
return &AuthVersionHandler{mgr: mgr, H: h, adtSvc: adtSvc}
}
var options = map[string]interface{}{
"auth": wrapAuthVersionHandler,
"mapRESTPathStats": MapRESTPathStats,
}
return rest.InitRESTRouterEx(
InitStaticRouter(staticDir, staticETag, mgr),
versionMain, mgr, staticDir, staticETag, mr,
myAssetDir, myAsset, options)
}
// --------------------------------------------------
type AuthVersionHandler struct {
mgr *cbgt.Manager
H http.Handler
adtSvc *audit.AuditSvc
}
func NewAuthVersionHandler(mgr *cbgt.Manager, adtSvc *audit.AuditSvc,
h http.Handler) *AuthVersionHandler {
return &AuthVersionHandler{
mgr: mgr,
H: h,
adtSvc: adtSvc,
}
}
func (c *AuthVersionHandler) ServeHTTP(
w http.ResponseWriter, req *http.Request) {
defer func() {
if err := recover(); err != nil {
buf := make([]byte, 2048)
n := runtime.Stack(buf, false)
cbgt.PublishCrashEvent(map[string]interface{}{
"stackTrace": string(buf[:n]),
"crashError": fmt.Sprintf("%v", err),
})
}
}()
if err := CheckAPIVersion(w, req); err != nil {
return
}
path := ""
if c.H != nil {
hwrm, ok := c.H.(*rest.HandlerWithRESTMeta)
if ok && hwrm.RESTMeta != nil {
path = hwrm.RESTMeta.Opts["_path"]
}
}
c.doAudit(req, path)
wEx := wrapResponseWriter(w)
ok, username := checkAPIAuth(c, wEx, req, path)
if !ok {
return
}
if c.H != nil {
c.H.ServeHTTP(wEx, req)
}
completeRequest(username, path, req, wEx.Length())
}
func (c *AuthVersionHandler) doAudit(req *http.Request, path string) {
if c.adtSvc == nil {
return
}
eventId, ok := restAuditMap[req.Method+":"+path]
if ok {
d := GetAuditEventData(eventId, req)
go c.adtSvc.Write(eventId, d)
}
}
// -----------------------------------------------------------------------------
func wrapResponseWriter(w http.ResponseWriter) *ResponseWriterEx {
return &ResponseWriterEx{w, 0}
}
// Wrapper for ResponseWriter to capture the number of bytes written to it
type ResponseWriterEx struct {
http.ResponseWriter
length int64
}
func (w *ResponseWriterEx) Write(b []byte) (n int, err error) {
n, err = w.ResponseWriter.Write(b)
w.length += int64(n)
return
}
func (w *ResponseWriterEx) Length() int64 {
return w.length
}
func (w *ResponseWriterEx) Flush() {
if flusher, ok := w.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}