forked from perkeep/perkeep
/
serverconfig.go
416 lines (367 loc) · 11.7 KB
/
serverconfig.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package serverconfig is responsible for mapping from a Camlistore
// configuration file and instantiating HTTP Handlers for all the
// necessary endpoints.
package serverconfig
import (
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"camlistore.org/pkg/auth"
"camlistore.org/pkg/blobserver"
"camlistore.org/pkg/blobserver/handlers"
"camlistore.org/pkg/httputil"
"camlistore.org/pkg/jsonconfig"
)
const camliPrefix = "/camli/"
var ErrCamliPath = errors.New("Invalid Camlistore request path")
type handlerConfig struct {
prefix string // "/foo/"
htype string // "localdisk", etc
conf jsonconfig.Obj // never nil
settingUp, setupDone bool
}
type handlerLoader struct {
installer HandlerInstaller
baseURL string
config map[string]*handlerConfig // prefix -> config
handler map[string]interface{} // prefix -> http.Handler / func / blobserver.Storage
curPrefix string
// optional context (for App Engine, the first request that
// started up the process). we may need this if setting up
// handlers involves doing datastore/memcache/blobstore
// lookups.
context *http.Request
}
// A HandlerInstaller is anything that can register an HTTP Handler at
// a prefix path. Both *http.ServeMux and camlistore.org/pkg/webserver.Server
// implement HandlerInstaller.
type HandlerInstaller interface {
Handle(path string, h http.Handler)
}
type storageAndConfig struct {
blobserver.Storage
config *blobserver.Config
}
var _ blobserver.ContextWrapper = (*storageAndConfig)(nil)
func (sc *storageAndConfig) WrapContext(req *http.Request) blobserver.Storage {
if w, ok := sc.Storage.(blobserver.ContextWrapper); ok {
return &storageAndConfig{w.WrapContext(req), sc.config}
}
return sc
}
func parseCamliPath(path string) (action string, err error) {
camIdx := strings.Index(path, camliPrefix)
if camIdx == -1 {
return "", ErrCamliPath
}
action = path[camIdx+len(camliPrefix):]
return
}
func unsupportedHandler(conn http.ResponseWriter, req *http.Request) {
httputil.BadRequestError(conn, "Unsupported camlistore path or method.")
}
func (s *storageAndConfig) Config() *blobserver.Config {
return s.config
}
// GetStorage returns the unwrapped blobserver.Storage interface value for
// callers to type-assert optional interface implementations on. (e.g. EnumeratorConfig)
func (s *storageAndConfig) GetStorage() blobserver.Storage {
return s.Storage
}
func camliHandlerUsingStorage(req *http.Request, action string, storage blobserver.StorageConfiger) (func(http.ResponseWriter, *http.Request), auth.Operation) {
handler := unsupportedHandler
op := auth.OpAll
switch req.Method {
case "GET":
switch action {
case "enumerate-blobs":
handler = handlers.CreateEnumerateHandler(storage).ServeHTTP
op = auth.OpGet
case "stat":
handler = handlers.CreateStatHandler(storage).ServeHTTP
default:
handler = handlers.CreateGetHandler(storage).ServeHTTP
op = auth.OpGet
}
case "POST":
switch action {
case "stat":
handler = handlers.CreateStatHandler(storage).ServeHTTP
op = auth.OpStat
case "upload":
handler = handlers.CreateUploadHandler(storage).ServeHTTP
op = auth.OpUpload
case "remove":
handler = handlers.CreateRemoveHandler(storage).ServeHTTP
}
}
return handler, op
}
// where prefix is like "/" or "/s3/" for e.g. "/camli/" or "/s3/camli/*"
func makeCamliHandler(prefix, baseURL string, storage blobserver.Storage, hf blobserver.FindHandlerByTyper) http.Handler {
if !strings.HasSuffix(prefix, "/") {
panic("expected prefix to end in slash")
}
baseURL = strings.TrimRight(baseURL, "/")
canLongPoll := true
// TODO(bradfitz): set to false if this is App Engine, or provide some way to disable
storageConfig := &storageAndConfig{
storage,
&blobserver.Config{
Writable: true,
Readable: true,
IsQueue: false,
URLBase: baseURL + prefix[:len(prefix)-1],
CanLongPoll: canLongPoll,
HandlerFinder: hf,
},
}
return http.HandlerFunc(func(conn http.ResponseWriter, req *http.Request) {
action, err := parseCamliPath(req.URL.Path[len(prefix)-1:])
if err != nil {
log.Printf("Invalid request for method %q, path %q",
req.Method, req.URL.Path)
unsupportedHandler(conn, req)
return
}
handler := auth.RequireAuth(camliHandlerUsingStorage(req, action, storageConfig))
handler(conn, req)
})
}
func (hl *handlerLoader) GetRequestContext() (req *http.Request, ok bool) {
return hl.context, hl.context != nil
}
func (hl *handlerLoader) FindHandlerByType(htype string) (prefix string, handler interface{}, err error) {
for prefix, config := range hl.config {
if config.htype == htype {
return prefix, hl.handler[prefix], nil
}
}
return "", nil, blobserver.ErrHandlerTypeNotFound
}
func (hl *handlerLoader) setupAll() {
for prefix := range hl.config {
hl.setupHandler(prefix)
}
}
func (hl *handlerLoader) configType(prefix string) string {
if h, ok := hl.config[prefix]; ok {
return h.htype
}
return ""
}
func (hl *handlerLoader) getOrSetup(prefix string) interface{} {
hl.setupHandler(prefix)
return hl.handler[prefix]
}
func (hl *handlerLoader) MyPrefix() string {
return hl.curPrefix
}
func (hl *handlerLoader) GetStorage(prefix string) (blobserver.Storage, error) {
hl.setupHandler(prefix)
if s, ok := hl.handler[prefix].(blobserver.Storage); ok {
return s, nil
}
return nil, fmt.Errorf("bogus storage handler referenced as %q", prefix)
}
func (hl *handlerLoader) GetHandler(prefix string) (interface{}, error) {
hl.setupHandler(prefix)
if s, ok := hl.handler[prefix].(blobserver.Storage); ok {
return s, nil
}
if h, ok := hl.handler[prefix].(http.Handler); ok {
return h, nil
}
return nil, fmt.Errorf("bogus http or storage handler referenced as %q", prefix)
}
func (hl *handlerLoader) GetHandlerType(prefix string) string {
return hl.configType(prefix)
}
func exitFailure(pattern string, args ...interface{}) {
if !strings.HasSuffix(pattern, "\n") {
pattern = pattern + "\n"
}
panic(fmt.Sprintf(pattern, args...))
}
func (hl *handlerLoader) setupHandler(prefix string) {
h, ok := hl.config[prefix]
if !ok {
exitFailure("invalid reference to undefined handler %q", prefix)
}
if h.setupDone {
// Already setup by something else reference it and forcing it to be
// setup before the bottom loop got to it.
return
}
if h.settingUp {
buf := make([]byte, 1024)
buf = buf[:runtime.Stack(buf, false)]
exitFailure("loop in configuration graph; %q tried to load itself indirectly. Stack:\n%s", prefix, buf)
}
h.settingUp = true
defer func() {
// log.Printf("Configured handler %q", prefix)
h.setupDone = true
r := recover()
if r == nil {
if hl.handler[prefix] == nil {
panic(fmt.Sprintf("setupHandler for %q didn't install a handler", prefix))
}
} else {
panic(r)
}
}()
hl.curPrefix = prefix
if strings.HasPrefix(h.htype, "storage-") {
stype := h.htype[len("storage-"):]
// Assume a storage interface
pstorage, err := blobserver.CreateStorage(stype, hl, h.conf)
if err != nil {
exitFailure("error instantiating storage for prefix %q, type %q: %v",
h.prefix, stype, err)
}
hl.handler[h.prefix] = pstorage
hl.installer.Handle(prefix+"camli/", makeCamliHandler(prefix, hl.baseURL, pstorage, hl))
return
}
hh, err := blobserver.CreateHandler(h.htype, hl, h.conf)
if err != nil {
exitFailure("error instantiating handler for prefix %q, type %q: %v",
h.prefix, h.htype, err)
}
hl.handler[prefix] = hh
var wrappedHandler http.Handler = &httputil.PrefixHandler{prefix, hh}
if handerTypeWantsAuth(h.htype) {
wrappedHandler = auth.Handler{wrappedHandler}
}
hl.installer.Handle(prefix, wrappedHandler)
}
func handerTypeWantsAuth(handlerType string) bool {
// TODO(bradfitz): ask the handler instead? This is a bit of a
// weird spot for this policy maybe?
switch handlerType {
case "ui", "search", "jsonsign", "sync", "status":
return true
}
return false
}
// A Config is the wrapper around a Camlistore JSON configuration file.
// Files on disk can be in either high-level or low-level format, but
// the Load function always returns the Config in its low-level format.
type Config struct {
jsonconfig.Obj
UIPath string // Not valid until after InstallHandlers
configPath string // Filesystem path
}
// Load returns a low-level "handler config" from the provided filename.
// If the config file doesn't contain a top-level JSON key of "handlerConfig"
// with boolean value true, the configuration is assumed to be a high-level
// "user config" file, and transformed into a low-level config.
func Load(filename string) (*Config, error) {
obj, err := jsonconfig.ReadFile(filename)
if err != nil {
return nil, err
}
conf := &Config{
Obj: obj,
configPath: filename,
}
if lowLevel := obj.OptionalBool("handlerConfig", false); !lowLevel {
conf, err = genLowLevelConfig(conf)
if err != nil {
return nil, fmt.Errorf(
"Failed to transform user config file %q into internal handler configuration: %v",
filename, err)
}
if v, _ := strconv.ParseBool(os.Getenv("CAMLI_DEBUG_CONFIG")); v {
jsconf, _ := json.MarshalIndent(conf.Obj, "", " ")
log.Printf("From high-level config, generated low-level config: %s", jsconf)
}
}
return conf, nil
}
func (config *Config) checkValidAuth() error {
authConfig := config.OptionalString("auth", "")
mode, err := auth.FromConfig(authConfig)
if err == nil {
auth.SetMode(mode)
}
return err
}
// InstallHandlers creates and registers all the HTTP Handlers needed by config
// into the provided HandlerInstaller.
//
// baseURL is required and specifies the root of this webserver, without trailing slash.
// context may be nil (used and required by App Engine only)
func (config *Config) InstallHandlers(hi HandlerInstaller, baseURL string, context *http.Request) (outerr error) {
defer func() {
if err := recover(); err != nil {
outerr = fmt.Errorf("%v", err)
}
}()
if err := config.checkValidAuth(); err != nil {
return fmt.Errorf("error while configuring auth: %v", err)
}
prefixes := config.RequiredObject("prefixes")
if err := config.Validate(); err != nil {
return fmt.Errorf("configuration error in root object's keys: %v", err)
}
hl := &handlerLoader{
installer: hi,
baseURL: baseURL,
config: make(map[string]*handlerConfig),
handler: make(map[string]interface{}),
context: context,
}
for prefix, vei := range prefixes {
if !strings.HasPrefix(prefix, "/") {
exitFailure("prefix %q doesn't start with /", prefix)
}
if !strings.HasSuffix(prefix, "/") {
exitFailure("prefix %q doesn't end with /", prefix)
}
pmap, ok := vei.(map[string]interface{})
if !ok {
exitFailure("prefix %q value is a %T, not an object", prefix, vei)
}
pconf := jsonconfig.Obj(pmap)
enabled := pconf.OptionalBool("enabled", true)
if !enabled {
continue
}
handlerType := pconf.RequiredString("handler")
handlerArgs := pconf.OptionalObject("handlerArgs")
if err := pconf.Validate(); err != nil {
exitFailure("configuration error in prefix %s: %v", prefix, err)
}
h := &handlerConfig{
prefix: prefix,
htype: handlerType,
conf: handlerArgs,
}
hl.config[prefix] = h
if handlerType == "ui" {
config.UIPath = prefix
}
}
hl.setupAll()
return nil
}