| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package gaemiddleware | 5 package gaemiddleware |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "net/http" | 9 "net/http" |
| 10 | 10 |
| 11 "github.com/julienschmidt/httprouter" | |
| 12 "github.com/luci/gae/service/info" | 11 "github.com/luci/gae/service/info" |
| 13 "github.com/luci/luci-go/common/logging" | 12 "github.com/luci/luci-go/common/logging" |
| 14 » "github.com/luci/luci-go/server/middleware" | 13 » "github.com/luci/luci-go/server/router" |
| 15 "golang.org/x/net/context" | 14 "golang.org/x/net/context" |
| 16 ) | 15 ) |
| 17 | 16 |
| 18 var devAppserverBypassFn = func(c context.Context) bool { | 17 var devAppserverBypassFn = func(c context.Context) bool { |
| 19 return info.Get(c).IsDevAppServer() | 18 return info.Get(c).IsDevAppServer() |
| 20 } | 19 } |
| 21 | 20 |
| 22 // RequireCron ensures that this handler was run from the appengine 'cron' | 21 // RequireCron ensures that the request is from the appengine 'cron' |
| 23 // service. Otherwise it aborts the request with a StatusForbidden. | 22 // service. Otherwise it aborts the request with a StatusForbidden. |
| 24 // | 23 // |
| 25 // This middleware has no effect when using 'BaseTest' or when running under | 24 // This middleware has no effect when using 'BaseTest' or when running under |
| 26 // dev_appserver.py | 25 // dev_appserver.py |
| 27 func RequireCron(h middleware.Handler) middleware.Handler { | 26 func RequireCron(c *router.Context, next router.Handler) { |
| 28 » return func(c context.Context, rw http.ResponseWriter, r *http.Request,
p httprouter.Params) { | 27 » if !devAppserverBypassFn(c.Context) { |
| 29 » » if !devAppserverBypassFn(c) { | 28 » » if c.Request.Header.Get("X-Appengine-Cron") != "true" { |
| 30 » » » if r.Header.Get("X-Appengine-Cron") != "true" { | 29 » » » c.Writer.WriteHeader(http.StatusForbidden) |
| 31 » » » » rw.WriteHeader(http.StatusForbidden) | 30 » » » logging.Errorf(c.Context, "request not made from cron") |
| 32 » » » » logging.Errorf(c, "request not made from cron") | 31 » » » fmt.Fprint(c.Writer, "error: must be run from cron") |
| 33 » » » » fmt.Fprint(rw, "error: must be run from cron") | 32 » » » return |
| 34 » » » » return | |
| 35 » » » } | |
| 36 } | 33 } |
| 37 h(c, rw, r, p) | |
| 38 } | 34 } |
| 35 next(c) |
| 39 } | 36 } |
| 40 | 37 |
| 41 // RequireTaskQueue ensures that this handler was run from the specified | 38 // RequireTaskQueue ensures that the request is from the specified |
| 42 // appengine 'taskqueue' queue. Otherwise it aborts the request with | 39 // appengine 'taskqueue' queue. Otherwise it aborts the request with |
| 43 // a StatusForbidden. | 40 // a StatusForbidden. |
| 44 // | 41 // |
| 45 // if `queue` is the empty string, than this simply checks that this handler was | 42 // if `queue` is the empty string, than this simply checks that this handler was |
| 46 // run from ANY appengine taskqueue. | 43 // run from ANY appengine taskqueue. |
| 47 // | 44 // |
| 48 // This middleware has no effect when using 'BaseTest' or when running under | 45 // This middleware has no effect when using 'BaseTest' or when running under |
| 49 // dev_appserver.py | 46 // dev_appserver.py |
| 50 func RequireTaskQueue(queue string, h middleware.Handler) middleware.Handler { | 47 func RequireTaskQueue(queue string) router.Middleware { |
| 51 » return func(c context.Context, rw http.ResponseWriter, r *http.Request,
p httprouter.Params) { | 48 » return func(c *router.Context, next router.Handler) { |
| 52 » » if !devAppserverBypassFn(c) { | 49 » » if !devAppserverBypassFn(c.Context) { |
| 53 » » » qName := r.Header.Get("X-AppEngine-QueueName") | 50 » » » qName := c.Request.Header.Get("X-AppEngine-QueueName") |
| 54 if qName == "" || (queue != "" && queue != qName) { | 51 if qName == "" || (queue != "" && queue != qName) { |
| 55 » » » » rw.WriteHeader(http.StatusForbidden) | 52 » » » » c.Writer.WriteHeader(http.StatusForbidden) |
| 56 » » » » logging.Errorf(c, "request made from wrong taskq
ueue: %q v %q", qName, queue) | 53 » » » » logging.Errorf(c.Context, "request made from wro
ng taskqueue: %q v %q", qName, queue) |
| 57 » » » » fmt.Fprintf(rw, "error: must be run from the cor
rect taskqueue") | 54 » » » » fmt.Fprintf(c.Writer, "error: must be run from t
he correct taskqueue") |
| 58 return | 55 return |
| 59 } | 56 } |
| 60 } | 57 } |
| 61 » » h(c, rw, r, p) | 58 » » next(c) |
| 62 } | 59 } |
| 63 } | 60 } |
| OLD | NEW |