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