Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: go/src/infra/crimson/server/frontend/handler.go

Issue 2080243005: appengine, crimson: Use updated router API (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/appengine/sheriff-o-matic/main_test.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Package frontend implements HTTP server that handles requests to default 5 // Package frontend implements HTTP server that handles requests to default
6 // module. 6 // module.
7 package frontend 7 package frontend
8 8
9 import ( 9 import (
10 "database/sql" 10 "database/sql"
11 "net/http" 11 "net/http"
12 "strings" 12 "strings"
13 13
14 "github.com/golang/protobuf/proto" 14 "github.com/golang/protobuf/proto"
15 15
16 "github.com/julienschmidt/httprouter"
17 "github.com/luci/gae/service/info" 16 "github.com/luci/gae/service/info"
18 "github.com/luci/luci-go/appengine/gaeauth/server" 17 "github.com/luci/luci-go/appengine/gaeauth/server"
19 "github.com/luci/luci-go/appengine/gaemiddleware" 18 "github.com/luci/luci-go/appengine/gaemiddleware"
20 "github.com/luci/luci-go/common/grpcutil" 19 "github.com/luci/luci-go/common/grpcutil"
21 "github.com/luci/luci-go/common/logging" 20 "github.com/luci/luci-go/common/logging"
22 "github.com/luci/luci-go/server/auth" 21 "github.com/luci/luci-go/server/auth"
23 "github.com/luci/luci-go/server/auth/identity" 22 "github.com/luci/luci-go/server/auth/identity"
24 "github.com/luci/luci-go/server/discovery" 23 "github.com/luci/luci-go/server/discovery"
25 "github.com/luci/luci-go/server/middleware"
26 "github.com/luci/luci-go/server/prpc" 24 "github.com/luci/luci-go/server/prpc"
25 "github.com/luci/luci-go/server/router"
27 "github.com/luci/luci-go/server/templates" 26 "github.com/luci/luci-go/server/templates"
28 "golang.org/x/net/context" 27 "golang.org/x/net/context"
29 "google.golang.org/appengine" 28 "google.golang.org/appengine"
30 "google.golang.org/grpc/codes" 29 "google.golang.org/grpc/codes"
31 30
32 "infra/crimson/proto" // 'crimson' package 31 "infra/crimson/proto" // 'crimson' package
33 "infra/crimson/server/crimsondb" 32 "infra/crimson/server/crimsondb"
34 ) 33 )
35 34
36 // templateBundle is used to render HTML templates. It provides a base args 35 // templateBundle is used to render HTML templates. It provides a base args
(...skipping 25 matching lines...) Expand all
62 "IsAdmin": isAdmin, 61 "IsAdmin": isAdmin,
63 "User": auth.CurrentUser(c), 62 "User": auth.CurrentUser(c),
64 "LoginURL": loginURL, 63 "LoginURL": loginURL,
65 "LogoutURL": logoutURL, 64 "LogoutURL": logoutURL,
66 }, nil 65 }, nil
67 }, 66 },
68 } 67 }
69 ) 68 )
70 69
71 // Auth middleware. Hard fails when user is not authorized. 70 // Auth middleware. Hard fails when user is not authorized.
72 func requireAuthWeb(h middleware.Handler) middleware.Handler { 71 func requireAuthWeb(c *router.Context, next router.Handler) {
73 » return func( 72 » if auth.CurrentIdentity(c.Context) == identity.AnonymousIdentity {
74 » » c context.Context, 73 » » loginURL, err := auth.LoginURL(c.Context, "/")
75 » » rw http.ResponseWriter, 74 » » if err != nil {
76 » » r *http.Request, 75 » » » logging.Errorf(c.Context, "Failed to get login URL")
77 » » p httprouter.Params) { 76 » » » http.Error(c.Writer, err.Error(), http.StatusInternalSer verError)
78
79 » » if auth.CurrentIdentity(c) == identity.AnonymousIdentity {
80 » » » loginURL, err := auth.LoginURL(c, "/")
81 » » » if err != nil {
82 » » » » logging.Errorf(c, "Failed to get login URL")
83 » » » }
84 » » » logging.Infof(c, "Redirecting to %s", loginURL)
85 » » » http.Redirect(rw, r, loginURL, 302)
86 return 77 return
87 } 78 }
79 logging.Infof(c.Context, "Redirecting to %s", loginURL)
80 http.Redirect(c.Writer, c.Request, loginURL, 302)
81 return
82 }
88 83
89 » » isGoogler, err := auth.IsMember(c, rwGroup) 84 » isGoogler, err := auth.IsMember(c.Context, rwGroup)
90 » » if err != nil { 85 » if err != nil {
91 » » » rw.WriteHeader(http.StatusInternalServerError) 86 » » c.Writer.WriteHeader(http.StatusInternalServerError)
92 » » » logging.Errorf(c, "Failed to get group membership.") 87 » » logging.Errorf(c.Context, "Failed to get group membership.")
93 » » » return 88 » » return
94 » » } 89 » }
95 » » if isGoogler { 90 » if isGoogler {
96 » » » h(c, rw, r, p) 91 » » next(c)
97 » » » return 92 » » return
98 » » } 93 » }
99 94
100 » » templates.MustRender(c, rw, "pages/access_denied.html", nil) 95 » templates.MustRender(c.Context, c.Writer, "pages/access_denied.html", ni l)
101 » }
102 } 96 }
103 97
104 func addDbToContext(h middleware.Handler) middleware.Handler { 98 func addDbToContext(c *router.Context, next router.Handler) {
105 » return func( 99 » c.Context = context.WithValue(c.Context, "dbHandle", dbHandle)
106 » » c context.Context, 100 » next(c)
107 » » rw http.ResponseWriter,
108 » » r *http.Request,
109 » » p httprouter.Params) {
110 » » c = context.WithValue(c, "dbHandle", dbHandle)
111 » » h(c, rw, r, p)
112 » }
113 } 101 }
114 102
115 // checkAuthorizationPrpc is a prelude function in the svcdec sense. 103 // checkAuthorizationPrpc is a prelude function in the svcdec sense.
116 // It hard fails when the user is not authorized. 104 // It hard fails when the user is not authorized.
117 func checkAuthorizationPrpc( 105 func checkAuthorizationPrpc(
118 c context.Context, methodName string, req proto.Message) (context.Contex t, error) { 106 c context.Context, methodName string, req proto.Message) (context.Contex t, error) {
119 107
120 identity := auth.CurrentIdentity(c) 108 identity := auth.CurrentIdentity(c)
121 logging.Infof(c, "%s", identity) 109 logging.Infof(c, "%s", identity)
122 hasAccess, err := auth.IsMember(c, rwGroup) 110 hasAccess, err := auth.IsMember(c, rwGroup)
123 if err != nil { 111 if err != nil {
124 return nil, grpcutil.Errf(codes.Internal, "%s", err) 112 return nil, grpcutil.Errf(codes.Internal, "%s", err)
125 } 113 }
126 if hasAccess { 114 if hasAccess {
127 return c, nil 115 return c, nil
128 } 116 }
129 return nil, grpcutil.Errf(codes.PermissionDenied, 117 return nil, grpcutil.Errf(codes.PermissionDenied,
130 "%s is not allowed to call APIs", auth.CurrentIdentity(c)) 118 "%s is not allowed to call APIs", auth.CurrentIdentity(c))
131 } 119 }
132 120
133 func base(h middleware.Handler) httprouter.Handle { 121 func base() router.MiddlewareChain {
134 methods := auth.Authenticator{ 122 methods := auth.Authenticator{
135 server.CookieAuth, 123 server.CookieAuth,
136 } 124 }
137 » h = auth.Authenticate(h) 125 » return append(
138 » h = auth.Use(h, methods) 126 » » gaemiddleware.BaseProd(),
139 » h = templates.WithTemplates(h, templateBundle) 127 » » templates.WithTemplates(templateBundle),
140 » return gaemiddleware.BaseProd(h) 128 » » auth.Use(methods),
129 » » auth.Authenticate,
130 » )
141 } 131 }
142 132
143 // webBase sets up authentication/authorization for http requests. 133 // webBase sets up authentication/authorization for http requests.
144 func webBase(h middleware.Handler) httprouter.Handle { 134 func webBase() router.MiddlewareChain {
145 » return base(requireAuthWeb(h)) 135 » return append(base(), requireAuthWeb)
146 } 136 }
147 137
148 // prpcBase is the middleware for pRPC API handlers. 138 // prpcBase returns the middleware for pRPC API handlers.
149 func prpcBase(h middleware.Handler) httprouter.Handle { 139 func prpcBase() router.MiddlewareChain {
150 // OAuth 2.0 with email scope is registered as a default authenticator 140 // OAuth 2.0 with email scope is registered as a default authenticator
151 // by importing "github.com/luci/luci-go/appengine/gaeauth/server". 141 // by importing "github.com/luci/luci-go/appengine/gaeauth/server".
152 // No need to setup an authenticator here. 142 // No need to setup an authenticator here.
153 // 143 //
154 // Authorization is checked in checkAuthorizationPrpc using a 144 // Authorization is checked in checkAuthorizationPrpc using a
155 // service decorator. 145 // service decorator.
156 » return gaemiddleware.BaseProd(addDbToContext(h)) 146 » return append(gaemiddleware.BaseProd(), addDbToContext)
157 } 147 }
158 148
159 //// Routes. 149 //// Routes.
160 150
161 func init() { 151 func init() {
162 152
163 // Open DB connection. 153 // Open DB connection.
164 // Declare 'err' here otherwise the next line shadows the global 'dbHand le' 154 // Declare 'err' here otherwise the next line shadows the global 'dbHand le'
165 var err error 155 var err error
166 dbHandle, err = crimsondb.GetDBHandle() 156 dbHandle, err = crimsondb.GetDBHandle()
167 if err != nil { 157 if err != nil {
168 logging.Errorf(context.Background(), 158 logging.Errorf(context.Background(),
169 "Failed to connect to CloudSQL: %v", err) 159 "Failed to connect to CloudSQL: %v", err)
170 return 160 return
171 } 161 }
172 162
173 » router := httprouter.New() 163 » r := router.New()
174 » gaemiddleware.InstallHandlers(router, base) 164 » gaemiddleware.InstallHandlers(r, base())
175 » router.GET("/", webBase(indexPage)) 165 » r.GET("/", webBase(), indexPage)
176 166
177 var api prpc.Server 167 var api prpc.Server
178 crimson.RegisterCrimsonServer(&api, &crimson.DecoratedCrimson{ 168 crimson.RegisterCrimsonServer(&api, &crimson.DecoratedCrimson{
179 Service: &crimsonService{}, 169 Service: &crimsonService{},
180 Prelude: checkAuthorizationPrpc, 170 Prelude: checkAuthorizationPrpc,
181 }) 171 })
182 discovery.Enable(&api) 172 discovery.Enable(&api)
183 » api.InstallHandlers(router, prpcBase) 173 » api.InstallHandlers(r, prpcBase())
184 174
185 » http.DefaultServeMux.Handle("/", router) 175 » http.DefaultServeMux.Handle("/", r)
186 } 176 }
187 177
188 //// Handlers. 178 //// Handlers.
189 179
190 func indexPage( 180 func indexPage(c *router.Context) {
191 » c context.Context, 181 » templates.MustRender(c.Context, c.Writer, "pages/index.html", nil)
192 » w http.ResponseWriter,
193 » r *http.Request,
194 » p httprouter.Params) {
195
196 » templates.MustRender(c, w, "pages/index.html", nil)
197 } 182 }
OLDNEW
« no previous file with comments | « go/src/infra/appengine/sheriff-o-matic/main_test.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698