Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 5 | // An implementation of a server for WSPR |
| 6 | |
| 7 | package server |
| 8 | |
| 9 | import ( |
| 10 | "encoding/json" |
Nicolas LaCasse | e3867dc | 2015-02-05 14:44:53 -0800 | [diff] [blame] | 11 | "fmt" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 12 | "sync" |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 13 | "time" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 14 | |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 15 | "v.io/v23" |
| 16 | "v.io/v23/context" |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 17 | "v.io/v23/naming" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 18 | "v.io/v23/rpc" |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 19 | "v.io/v23/security" |
| 20 | "v.io/v23/vdl" |
Todd Wang | ac9e190 | 2015-02-25 01:58:01 -0800 | [diff] [blame] | 21 | "v.io/v23/vdlroot/signature" |
Shyam Jayaraman | 7efedbe | 2015-03-05 12:57:18 -0800 | [diff] [blame] | 22 | vdltime "v.io/v23/vdlroot/time" |
Jiri Simsa | 1f1302c | 2015-02-23 16:18:34 -0800 | [diff] [blame] | 23 | "v.io/v23/verror" |
Matt Rosencrantz | e7bf2bc | 2015-03-13 09:54:27 -0700 | [diff] [blame] | 24 | "v.io/v23/vtrace" |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 25 | "v.io/x/lib/vlog" |
Todd Wang | 5b77a34 | 2015-04-06 18:31:37 -0700 | [diff] [blame] | 26 | "v.io/x/ref/services/wspr/internal/lib" |
| 27 | "v.io/x/ref/services/wspr/internal/principal" |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type Flow struct { |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 31 | ID int32 |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 32 | Writer lib.ClientWriter |
| 33 | } |
| 34 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 35 | type FlowHandler interface { |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 36 | CreateNewFlow(server interface{}, sender rpc.Stream) *Flow |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 37 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 38 | CleanupFlow(id int32) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | type HandleStore interface { |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 42 | GetBlessings(handle principal.BlessingsHandle) security.Blessings |
Benjamin Prosnitz | 9e42399 | 2015-04-10 13:10:35 -0700 | [diff] [blame] | 43 | // Gets or adds blessings to the store and returns handle to the blessings |
| 44 | GetOrAddBlessingsHandle(blessings security.Blessings) principal.BlessingsHandle |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | type ServerHelper interface { |
| 48 | FlowHandler |
| 49 | HandleStore |
| 50 | |
Shyam Jayaraman | c17abda | 2015-04-06 16:49:17 -0700 | [diff] [blame] | 51 | SendLogMessage(level lib.LogLevel, msg string) error |
| 52 | |
Matt Rosencrantz | c90eb7b | 2015-01-09 08:32:01 -0800 | [diff] [blame] | 53 | Context() *context.T |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | type authReply struct { |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 57 | Err *verror.E |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 60 | // AuthRequest is a request for a javascript authorizer to run |
| 61 | // This is exported to make the app test easier. |
| 62 | type AuthRequest struct { |
Benjamin Prosnitz | 2be28dc | 2015-03-11 13:33:22 -0700 | [diff] [blame] | 63 | ServerId uint32 `json:"serverId"` |
Shyam Jayaraman | 250aac3 | 2015-03-05 15:29:11 -0800 | [diff] [blame] | 64 | Handle int32 `json:"handle"` |
| 65 | Call SecurityCall `json:"call"` |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | type Server struct { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 69 | // serverStateLock should be aquired when starting or stopping the server. |
| 70 | // This should be locked before outstandingRequestLock. |
| 71 | serverStateLock sync.Mutex |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 72 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 73 | // The rpc.ListenSpec to use with server.Listen |
| 74 | listenSpec *rpc.ListenSpec |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 75 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 76 | // The server that handles the rpc layer. Listen on this server is |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 77 | // lazily started. |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 78 | server rpc.Server |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 79 | |
| 80 | // The saved dispatcher to reuse when serve is called multiple times. |
| 81 | dispatcher *dispatcher |
| 82 | |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 83 | // Whether the server is listening. |
| 84 | isListening bool |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 85 | |
| 86 | // The server id. |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 87 | id uint32 |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 88 | helper ServerHelper |
| 89 | |
Suharsh Sivakumar | 4cc7c2d | 2015-04-09 15:21:56 -0700 | [diff] [blame] | 90 | // outstandingRequestLock should be acquired only to update the outstanding request maps below. |
| 91 | outstandingRequestLock sync.Mutex |
| 92 | outstandingServerRequests map[int32]chan *lib.ServerRpcReply // GUARDED_BY outstandingRequestLock |
| 93 | outstandingAuthRequests map[int32]chan error // GUARDED_BY outstandingRequestLock |
| 94 | outstandingValidationRequests map[int32]chan []error // GUARDED_BY outstandingRequestLock |
Shyam Jayaraman | c17abda | 2015-04-06 16:49:17 -0700 | [diff] [blame] | 95 | |
| 96 | // statusClose will be closed when the server is shutting down, this will |
| 97 | // cause the status poller to exit. |
| 98 | statusClose chan struct{} |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 101 | type serverContextKey struct{} |
| 102 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 103 | func NewServer(id uint32, listenSpec *rpc.ListenSpec, helper ServerHelper) (*Server, error) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 104 | server := &Server{ |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 105 | id: id, |
| 106 | helper: helper, |
| 107 | listenSpec: listenSpec, |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 108 | outstandingServerRequests: make(map[int32]chan *lib.ServerRpcReply), |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 109 | outstandingAuthRequests: make(map[int32]chan error), |
| 110 | outstandingValidationRequests: make(map[int32]chan []error), |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 111 | } |
| 112 | var err error |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 113 | ctx := helper.Context() |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 114 | ctx = context.WithValue(ctx, serverContextKey{}, server) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 115 | if server.server, err = v23.NewServer(ctx); err != nil { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 116 | return nil, err |
| 117 | } |
| 118 | return server, nil |
| 119 | } |
| 120 | |
| 121 | // remoteInvokeFunc is a type of function that can invoke a remote method and |
| 122 | // communicate the result back via a channel to the caller |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 123 | type remoteInvokeFunc func(ctx *context.T, call rpc.StreamServerCall, methodName string, args []interface{}) <-chan *lib.ServerRpcReply |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 124 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 125 | func (s *Server) createRemoteInvokerFunc(handle int32) remoteInvokeFunc { |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 126 | return func(ctx *context.T, call rpc.StreamServerCall, methodName string, args []interface{}) <-chan *lib.ServerRpcReply { |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 127 | securityCall := ConvertSecurityCall(s.helper, ctx, call.Security(), true) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 128 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 129 | flow := s.helper.CreateNewFlow(s, call) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 130 | replyChan := make(chan *lib.ServerRpcReply, 1) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 131 | s.outstandingRequestLock.Lock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 132 | s.outstandingServerRequests[flow.ID] = replyChan |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 133 | s.outstandingRequestLock.Unlock() |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 134 | |
Shyam Jayaraman | 7efedbe | 2015-03-05 12:57:18 -0800 | [diff] [blame] | 135 | var timeout vdltime.Deadline |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 136 | if deadline, ok := ctx.Deadline(); ok { |
Shyam Jayaraman | 7efedbe | 2015-03-05 12:57:18 -0800 | [diff] [blame] | 137 | timeout.Time = deadline |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 140 | errHandler := func(err error) <-chan *lib.ServerRpcReply { |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 141 | if ch := s.popServerRequest(flow.ID); ch != nil { |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 142 | stdErr := verror.Convert(verror.ErrInternal, ctx, err).(verror.E) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 143 | ch <- &lib.ServerRpcReply{nil, &stdErr, vtrace.Response{}} |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 144 | s.helper.CleanupFlow(flow.ID) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 145 | } |
| 146 | return replyChan |
Shyam Jayaraman | c96e1aa | 2014-11-12 16:42:39 -0800 | [diff] [blame] | 147 | } |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 148 | |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 149 | var grantedBlessings *principal.JsBlessings |
| 150 | if !call.GrantedBlessings().IsZero() { |
| 151 | grantedBlessings = convertBlessingsToHandle(s.helper, call.GrantedBlessings()) |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 152 | } |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 153 | |
| 154 | rpcCall := ServerRpcRequestCall{ |
| 155 | SecurityCall: securityCall, |
| 156 | Deadline: timeout, |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 157 | TraceRequest: vtrace.GetRequest(ctx), |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 158 | GrantedBlessings: grantedBlessings, |
| 159 | } |
| 160 | |
| 161 | var vdlValArgs []*vdl.Value = make([]*vdl.Value, len(args)) |
| 162 | for i, arg := range args { |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 163 | if blessings, ok := arg.(security.Blessings); ok { |
| 164 | arg = principal.ConvertBlessingsToHandle(blessings, s.helper.GetOrAddBlessingsHandle(blessings)) |
| 165 | } |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 166 | vdlValArgs[i] = vdl.ValueOf(arg) |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 167 | } |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 168 | |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 169 | // Send a invocation request to JavaScript |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 170 | message := ServerRpcRequest{ |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 171 | ServerId: s.id, |
| 172 | Handle: handle, |
| 173 | Method: lib.LowercaseFirstCharacter(methodName), |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 174 | Args: vdlValArgs, |
Shyam Jayaraman | 250aac3 | 2015-03-05 15:29:11 -0800 | [diff] [blame] | 175 | Call: rpcCall, |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 176 | } |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 177 | vomMessage, err := lib.VomEncode(message) |
Shyam Jayaraman | c96e1aa | 2014-11-12 16:42:39 -0800 | [diff] [blame] | 178 | if err != nil { |
| 179 | return errHandler(err) |
| 180 | } |
Shyam Jayaraman | 907219d | 2014-11-26 12:14:37 -0800 | [diff] [blame] | 181 | if err := flow.Writer.Send(lib.ResponseServerRequest, vomMessage); err != nil { |
Shyam Jayaraman | c96e1aa | 2014-11-12 16:42:39 -0800 | [diff] [blame] | 182 | return errHandler(err) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 185 | vlog.VI(3).Infof("calling method %q with args %v, MessageID %d assigned\n", methodName, args, flow.ID) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 186 | |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 187 | // Watch for cancellation. |
| 188 | go func() { |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 189 | <-ctx.Done() |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 190 | ch := s.popServerRequest(flow.ID) |
| 191 | if ch == nil { |
| 192 | return |
| 193 | } |
| 194 | |
| 195 | // Send a cancel message to the JS server. |
| 196 | flow.Writer.Send(lib.ResponseCancel, nil) |
| 197 | s.helper.CleanupFlow(flow.ID) |
| 198 | |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 199 | err := verror.Convert(verror.ErrAborted, ctx, ctx.Err()).(verror.E) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 200 | ch <- &lib.ServerRpcReply{nil, &err, vtrace.Response{}} |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 201 | }() |
| 202 | |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 203 | go proxyStream(call, flow.Writer, s.helper) |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 204 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 205 | return replyChan |
| 206 | } |
| 207 | } |
| 208 | |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 209 | type globStream struct { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 210 | ch chan naming.GlobReply |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 211 | ctx *context.T |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | func (g *globStream) Send(item interface{}) error { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 215 | if v, ok := item.(naming.GlobReply); ok { |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 216 | g.ch <- v |
| 217 | return nil |
| 218 | } |
Jiri Simsa | 94f68d0 | 2015-02-17 10:22:08 -0800 | [diff] [blame] | 219 | return verror.New(verror.ErrBadArg, g.ctx, item) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | func (g *globStream) Recv(itemptr interface{}) error { |
Jiri Simsa | 94f68d0 | 2015-02-17 10:22:08 -0800 | [diff] [blame] | 223 | return verror.New(verror.ErrNoExist, g.ctx, "Can't call recieve on glob stream") |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | func (g *globStream) CloseSend() error { |
| 227 | close(g.ch) |
| 228 | return nil |
| 229 | } |
| 230 | |
| 231 | // remoteGlobFunc is a type of function that can invoke a remote glob and |
| 232 | // communicate the result back via the channel returned |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 233 | type remoteGlobFunc func(ctx *context.T, call rpc.ServerCall, pattern string) (<-chan naming.GlobReply, error) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 234 | |
| 235 | func (s *Server) createRemoteGlobFunc(handle int32) remoteGlobFunc { |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 236 | return func(ctx *context.T, call rpc.ServerCall, pattern string) (<-chan naming.GlobReply, error) { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 237 | // Until the tests get fixed, we need to create a security context before creating the flow |
| 238 | // because creating the security context creates a flow and flow ids will be off. |
| 239 | // See https://github.com/veyron/release-issues/issues/1181 |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 240 | securityCall := ConvertSecurityCall(s.helper, ctx, call.Security(), true) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 241 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 242 | globChan := make(chan naming.GlobReply, 1) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 243 | flow := s.helper.CreateNewFlow(s, &globStream{ |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 244 | ch: globChan, |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 245 | ctx: ctx, |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 246 | }) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 247 | replyChan := make(chan *lib.ServerRpcReply, 1) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 248 | s.outstandingRequestLock.Lock() |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 249 | s.outstandingServerRequests[flow.ID] = replyChan |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 250 | s.outstandingRequestLock.Unlock() |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 251 | |
Shyam Jayaraman | 7efedbe | 2015-03-05 12:57:18 -0800 | [diff] [blame] | 252 | var timeout vdltime.Deadline |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 253 | if deadline, ok := ctx.Deadline(); ok { |
Shyam Jayaraman | 7efedbe | 2015-03-05 12:57:18 -0800 | [diff] [blame] | 254 | timeout.Time = deadline |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 257 | errHandler := func(err error) (<-chan naming.GlobReply, error) { |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 258 | if ch := s.popServerRequest(flow.ID); ch != nil { |
| 259 | s.helper.CleanupFlow(flow.ID) |
| 260 | } |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 261 | return nil, verror.Convert(verror.ErrInternal, ctx, err).(verror.E) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 262 | } |
| 263 | |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 264 | var grantedBlessings *principal.JsBlessings |
| 265 | if !call.GrantedBlessings().IsZero() { |
| 266 | grantedBlessings = convertBlessingsToHandle(s.helper, call.GrantedBlessings()) |
| 267 | } |
| 268 | |
| 269 | rpcCall := ServerRpcRequestCall{ |
| 270 | SecurityCall: securityCall, |
| 271 | Deadline: timeout, |
| 272 | GrantedBlessings: grantedBlessings, |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Send a invocation request to JavaScript |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 276 | message := ServerRpcRequest{ |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 277 | ServerId: s.id, |
| 278 | Handle: handle, |
| 279 | Method: "Glob__", |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 280 | Args: []*vdl.Value{vdl.ValueOf(pattern)}, |
Shyam Jayaraman | 250aac3 | 2015-03-05 15:29:11 -0800 | [diff] [blame] | 281 | Call: rpcCall, |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 282 | } |
| 283 | vomMessage, err := lib.VomEncode(message) |
| 284 | if err != nil { |
| 285 | return errHandler(err) |
| 286 | } |
| 287 | if err := flow.Writer.Send(lib.ResponseServerRequest, vomMessage); err != nil { |
| 288 | return errHandler(err) |
| 289 | } |
| 290 | |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 291 | vlog.VI(3).Infof("calling method 'Glob__' with args %v, MessageID %d assigned\n", []interface{}{pattern}, flow.ID) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 292 | |
| 293 | // Watch for cancellation. |
| 294 | go func() { |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 295 | <-ctx.Done() |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 296 | ch := s.popServerRequest(flow.ID) |
| 297 | if ch == nil { |
| 298 | return |
| 299 | } |
| 300 | |
| 301 | // Send a cancel message to the JS server. |
| 302 | flow.Writer.Send(lib.ResponseCancel, nil) |
| 303 | s.helper.CleanupFlow(flow.ID) |
| 304 | |
Todd Wang | 54feabe | 2015-04-15 23:38:26 -0700 | [diff] [blame] | 305 | err := verror.Convert(verror.ErrAborted, ctx, ctx.Err()).(verror.E) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 306 | ch <- &lib.ServerRpcReply{nil, &err, vtrace.Response{}} |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 307 | }() |
| 308 | |
| 309 | return globChan, nil |
| 310 | } |
| 311 | } |
| 312 | |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 313 | func proxyStream(stream rpc.Stream, w lib.ClientWriter, blessingsCache HandleStore) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 314 | var item interface{} |
| 315 | for err := stream.Recv(&item); err == nil; err = stream.Recv(&item) { |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 316 | if blessings, ok := item.(security.Blessings); ok { |
| 317 | item = principal.ConvertBlessingsToHandle(blessings, blessingsCache.GetOrAddBlessingsHandle(blessings)) |
| 318 | |
| 319 | } |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 320 | vomItem, err := lib.VomEncode(item) |
Shyam Jayaraman | c96e1aa | 2014-11-12 16:42:39 -0800 | [diff] [blame] | 321 | if err != nil { |
Jiri Simsa | 94f68d0 | 2015-02-17 10:22:08 -0800 | [diff] [blame] | 322 | w.Error(verror.Convert(verror.ErrInternal, nil, err)) |
Shyam Jayaraman | c96e1aa | 2014-11-12 16:42:39 -0800 | [diff] [blame] | 323 | return |
| 324 | } |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 325 | if err := w.Send(lib.ResponseStream, vomItem); err != nil { |
Jiri Simsa | 94f68d0 | 2015-02-17 10:22:08 -0800 | [diff] [blame] | 326 | w.Error(verror.Convert(verror.ErrInternal, nil, err)) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 327 | return |
| 328 | } |
| 329 | } |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 330 | if err := w.Send(lib.ResponseStreamClose, nil); err != nil { |
Jiri Simsa | 94f68d0 | 2015-02-17 10:22:08 -0800 | [diff] [blame] | 331 | w.Error(verror.Convert(verror.ErrInternal, nil, err)) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 332 | return |
| 333 | } |
| 334 | } |
| 335 | |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 336 | func convertBlessingsToHandle(helper ServerHelper, blessings security.Blessings) *principal.JsBlessings { |
| 337 | return principal.ConvertBlessingsToHandle(blessings, helper.GetOrAddBlessingsHandle(blessings)) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 340 | func makeListOfErrors(numErrors int, err error) []error { |
| 341 | errs := make([]error, numErrors) |
| 342 | for i := 0; i < numErrors; i++ { |
| 343 | errs[i] = err |
Shyam Jayaraman | a42622c | 2015-01-05 16:55:53 -0800 | [diff] [blame] | 344 | } |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 345 | return errs |
| 346 | } |
| 347 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 348 | // caveatValidationInGo validates caveats in Go, using the default logic. |
| 349 | func caveatValidationInGo(ctx *context.T, call security.Call, sets [][]security.Caveat) []error { |
| 350 | results := make([]error, len(sets)) |
| 351 | for i, set := range sets { |
| 352 | for _, cav := range set { |
| 353 | if err := cav.Validate(ctx, call); err != nil { |
| 354 | results[i] = err |
| 355 | break |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | return results |
| 360 | } |
| 361 | |
| 362 | // caveatValidationInJavascript validates caveats in javascript. It resolves |
| 363 | // each []security.Caveat in cavs to an error (or nil) and collects them in a |
| 364 | // slice. |
| 365 | func (s *Server) caveatValidationInJavascript(ctx *context.T, call security.Call, cavs [][]security.Caveat) []error { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 366 | flow := s.helper.CreateNewFlow(s, nil) |
| 367 | req := CaveatValidationRequest{ |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 368 | Call: ConvertSecurityCall(s.helper, ctx, call, false), |
Ankur | ef39ba8 | 2015-03-19 13:34:03 -0700 | [diff] [blame] | 369 | Cavs: cavs, |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | replyChan := make(chan []error, 1) |
| 373 | s.outstandingRequestLock.Lock() |
| 374 | s.outstandingValidationRequests[flow.ID] = replyChan |
| 375 | s.outstandingRequestLock.Unlock() |
| 376 | |
| 377 | defer func() { |
| 378 | s.outstandingRequestLock.Lock() |
| 379 | delete(s.outstandingValidationRequests, flow.ID) |
| 380 | s.outstandingRequestLock.Unlock() |
| 381 | s.cleanupFlow(flow.ID) |
| 382 | }() |
| 383 | |
| 384 | if err := flow.Writer.Send(lib.ResponseValidate, req); err != nil { |
| 385 | vlog.VI(2).Infof("Failed to send validate response: %v", err) |
| 386 | replyChan <- makeListOfErrors(len(cavs), err) |
| 387 | } |
| 388 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 389 | // TODO(bprosnitz) Consider using a different timeout than the standard rpc timeout. |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 390 | var timeoutChan <-chan time.Time |
Matt Rosencrantz | 317b0c5 | 2015-03-24 20:47:01 -0700 | [diff] [blame] | 391 | if deadline, ok := ctx.Deadline(); ok { |
Todd Wang | f6a0688 | 2015-02-27 17:38:01 -0800 | [diff] [blame] | 392 | timeoutChan = time.After(deadline.Sub(time.Now())) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 393 | } |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 394 | |
| 395 | select { |
Shyam Jayaraman | f0a995c | 2015-04-21 17:34:38 -0700 | [diff] [blame^] | 396 | case <-s.statusClose: |
| 397 | return caveatValidationInGo(ctx, call, cavs) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 398 | case <-timeoutChan: |
Matt Rosencrantz | 317b0c5 | 2015-03-24 20:47:01 -0700 | [diff] [blame] | 399 | return makeListOfErrors(len(cavs), NewErrCaveatValidationTimeout(ctx)) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 400 | case reply := <-replyChan: |
| 401 | if len(reply) != len(cavs) { |
| 402 | vlog.VI(2).Infof("Wspr caveat validator received %d results from javascript but expected %d", len(reply), len(cavs)) |
Matt Rosencrantz | 317b0c5 | 2015-03-24 20:47:01 -0700 | [diff] [blame] | 403 | return makeListOfErrors(len(cavs), NewErrInvalidValidationResponseFromJavascript(ctx)) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | return reply |
| 407 | } |
| 408 | } |
| 409 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 410 | // CaveatValidation implements a function suitable for passing to |
| 411 | // security.OverrideCaveatValidation. |
| 412 | // |
| 413 | // Certain caveats (PublicKeyThirdPartyCaveatX) are intercepted and handled in |
| 414 | // go, while all other caveats are evaluated in javascript. |
| 415 | func CaveatValidation(ctx *context.T, call security.Call, cavs [][]security.Caveat) []error { |
| 416 | // If the server isn't set in the context, we just perform validation in Go. |
| 417 | ctxServer := ctx.Value(serverContextKey{}) |
| 418 | if ctxServer == nil { |
| 419 | return caveatValidationInGo(ctx, call, cavs) |
| 420 | } |
| 421 | // Otherwise we run our special logic. |
| 422 | server := ctxServer.(*Server) |
Benjamin Prosnitz | 2c8c237 | 2015-03-13 12:03:08 -0700 | [diff] [blame] | 423 | type validationStatus struct { |
| 424 | err error |
| 425 | isSet bool |
| 426 | } |
| 427 | valStatus := make([]validationStatus, len(cavs)) |
| 428 | |
| 429 | var caveatChainsToValidate [][]security.Caveat |
| 430 | nextCav: |
| 431 | for i, chainCavs := range cavs { |
| 432 | var newChainCavs []security.Caveat |
| 433 | for _, cav := range chainCavs { |
Suharsh Sivakumar | bc74089 | 2015-04-17 10:54:17 -0700 | [diff] [blame] | 434 | // If the server is closed handle all caveats in Go, because Javascript is |
| 435 | // no longer there. |
| 436 | select { |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 437 | case <-server.statusClose: |
Suharsh Sivakumar | bc74089 | 2015-04-17 10:54:17 -0700 | [diff] [blame] | 438 | res := cav.Validate(ctx, call) |
| 439 | if res != nil { |
| 440 | valStatus[i] = validationStatus{ |
| 441 | err: res, |
| 442 | isSet: true, |
| 443 | } |
| 444 | continue nextCav |
| 445 | } |
| 446 | default: |
| 447 | } |
Benjamin Prosnitz | 2c8c237 | 2015-03-13 12:03:08 -0700 | [diff] [blame] | 448 | switch cav.Id { |
| 449 | case security.PublicKeyThirdPartyCaveatX.Id: |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 450 | res := cav.Validate(ctx, call) |
Benjamin Prosnitz | 2c8c237 | 2015-03-13 12:03:08 -0700 | [diff] [blame] | 451 | if res != nil { |
| 452 | valStatus[i] = validationStatus{ |
| 453 | err: res, |
| 454 | isSet: true, |
| 455 | } |
| 456 | continue nextCav |
| 457 | } |
| 458 | default: |
| 459 | newChainCavs = append(newChainCavs, cav) |
| 460 | } |
| 461 | } |
| 462 | if len(newChainCavs) == 0 { |
| 463 | valStatus[i] = validationStatus{ |
| 464 | err: nil, |
| 465 | isSet: true, |
| 466 | } |
| 467 | } else { |
| 468 | caveatChainsToValidate = append(caveatChainsToValidate, newChainCavs) |
| 469 | } |
| 470 | } |
| 471 | |
Todd Wang | 555097f | 2015-04-21 10:49:06 -0700 | [diff] [blame] | 472 | jsRes := server.caveatValidationInJavascript(ctx, call, caveatChainsToValidate) |
Benjamin Prosnitz | 2c8c237 | 2015-03-13 12:03:08 -0700 | [diff] [blame] | 473 | |
| 474 | outResults := make([]error, len(cavs)) |
| 475 | jsIndex := 0 |
| 476 | for i, status := range valStatus { |
| 477 | if status.isSet { |
| 478 | outResults[i] = status.err |
| 479 | } else { |
| 480 | outResults[i] = jsRes[jsIndex] |
| 481 | jsIndex++ |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return outResults |
| 486 | } |
| 487 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 488 | func ConvertSecurityCall(helper ServerHelper, ctx *context.T, call security.Call, includeBlessingStrings bool) SecurityCall { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 489 | var localEndpoint string |
Matt Rosencrantz | 9dce9b2 | 2015-03-02 10:48:37 -0800 | [diff] [blame] | 490 | if call.LocalEndpoint() != nil { |
| 491 | localEndpoint = call.LocalEndpoint().String() |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 492 | } |
| 493 | var remoteEndpoint string |
Matt Rosencrantz | 9dce9b2 | 2015-03-02 10:48:37 -0800 | [diff] [blame] | 494 | if call.RemoteEndpoint() != nil { |
| 495 | remoteEndpoint = call.RemoteEndpoint().String() |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 496 | } |
Benjamin Prosnitz | 23bf1a0 | 2015-03-30 16:17:04 -0700 | [diff] [blame] | 497 | var localBlessings principal.JsBlessings |
Matt Rosencrantz | 9dce9b2 | 2015-03-02 10:48:37 -0800 | [diff] [blame] | 498 | if !call.LocalBlessings().IsZero() { |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 499 | localBlessings = *convertBlessingsToHandle(helper, call.LocalBlessings()) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 500 | } |
Matt Rosencrantz | 9dce9b2 | 2015-03-02 10:48:37 -0800 | [diff] [blame] | 501 | anymtags := make([]*vdl.Value, len(call.MethodTags())) |
| 502 | for i, mtag := range call.MethodTags() { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 503 | anymtags[i] = mtag |
| 504 | } |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 505 | secCall := SecurityCall{ |
Matt Rosencrantz | 9dce9b2 | 2015-03-02 10:48:37 -0800 | [diff] [blame] | 506 | Method: lib.LowercaseFirstCharacter(call.Method()), |
| 507 | Suffix: call.Suffix(), |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 508 | MethodTags: anymtags, |
| 509 | LocalEndpoint: localEndpoint, |
| 510 | RemoteEndpoint: remoteEndpoint, |
| 511 | LocalBlessings: localBlessings, |
Benjamin Prosnitz | bf71e96 | 2015-04-14 16:43:07 -0700 | [diff] [blame] | 512 | RemoteBlessings: *convertBlessingsToHandle(helper, call.RemoteBlessings()), |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 513 | } |
| 514 | if includeBlessingStrings { |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 515 | secCall.LocalBlessingStrings = security.LocalBlessingNames(ctx, call) |
| 516 | secCall.RemoteBlessingStrings, _ = security.RemoteBlessingNames(ctx, call) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 517 | } |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 518 | return secCall |
Shyam Jayaraman | a42622c | 2015-01-05 16:55:53 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 521 | type remoteAuth struct { |
| 522 | Func func(*context.T, security.Call, int32) error |
| 523 | Handle int32 |
| 524 | } |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 525 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 526 | func (r remoteAuth) Authorize(ctx *context.T, call security.Call) error { |
| 527 | return r.Func(ctx, call, r.Handle) |
| 528 | } |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 529 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 530 | func (s *Server) createRemoteAuthorizer(handle int32) security.Authorizer { |
| 531 | return remoteAuth{s.authorizeRemote, handle} |
| 532 | } |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 533 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 534 | func (s *Server) authorizeRemote(ctx *context.T, call security.Call, handle int32) error { |
| 535 | // Until the tests get fixed, we need to create a security context before |
| 536 | // creating the flow because creating the security context creates a flow and |
| 537 | // flow ids will be off. |
| 538 | securityCall := ConvertSecurityCall(s.helper, ctx, call, true) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 539 | |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 540 | flow := s.helper.CreateNewFlow(s, nil) |
| 541 | replyChan := make(chan error, 1) |
| 542 | s.outstandingRequestLock.Lock() |
| 543 | s.outstandingAuthRequests[flow.ID] = replyChan |
| 544 | s.outstandingRequestLock.Unlock() |
| 545 | message := AuthRequest{ |
| 546 | ServerId: s.id, |
| 547 | Handle: handle, |
| 548 | Call: securityCall, |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 549 | } |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 550 | vlog.VI(0).Infof("Sending out auth request for %v, %v", flow.ID, message) |
| 551 | |
| 552 | vomMessage, err := lib.VomEncode(message) |
| 553 | if err != nil { |
| 554 | replyChan <- verror.Convert(verror.ErrInternal, nil, err) |
| 555 | } else if err := flow.Writer.Send(lib.ResponseAuthRequest, vomMessage); err != nil { |
| 556 | replyChan <- verror.Convert(verror.ErrInternal, nil, err) |
| 557 | } |
| 558 | |
| 559 | err = <-replyChan |
| 560 | vlog.VI(0).Infof("going to respond with %v", err) |
| 561 | s.outstandingRequestLock.Lock() |
| 562 | delete(s.outstandingAuthRequests, flow.ID) |
| 563 | s.outstandingRequestLock.Unlock() |
| 564 | s.helper.CleanupFlow(flow.ID) |
| 565 | return err |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Shyam Jayaraman | c17abda | 2015-04-06 16:49:17 -0700 | [diff] [blame] | 568 | func (s *Server) readStatus() { |
| 569 | // A map of names to the last error message sent. |
| 570 | lastErrors := map[string]string{} |
| 571 | for { |
| 572 | status := s.server.Status() |
| 573 | for _, mountStatus := range status.Mounts { |
| 574 | var errMsg string |
| 575 | if mountStatus.LastMountErr != nil { |
| 576 | errMsg = mountStatus.LastMountErr.Error() |
| 577 | } |
| 578 | mountName := mountStatus.Name |
| 579 | if lastMessage, ok := lastErrors[mountName]; !ok || errMsg != lastMessage { |
| 580 | if errMsg == "" { |
| 581 | s.helper.SendLogMessage( |
| 582 | lib.LogLevelInfo, "serve: "+mountName+" successfully mounted ") |
| 583 | } else { |
| 584 | s.helper.SendLogMessage( |
| 585 | lib.LogLevelError, "serve: "+mountName+" failed with: "+errMsg) |
| 586 | } |
| 587 | } |
| 588 | lastErrors[mountName] = errMsg |
| 589 | } |
| 590 | select { |
| 591 | case <-time.After(10 * time.Second): |
| 592 | continue |
| 593 | case <-s.statusClose: |
| 594 | return |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 599 | func (s *Server) Serve(name string) error { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 600 | s.serverStateLock.Lock() |
| 601 | defer s.serverStateLock.Unlock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 602 | |
| 603 | if s.dispatcher == nil { |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 604 | s.dispatcher = newDispatcher(s.id, s, s, s) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 607 | if !s.isListening { |
| 608 | _, err := s.server.Listen(*s.listenSpec) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 609 | if err != nil { |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 610 | return err |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 611 | } |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 612 | s.isListening = true |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 613 | } |
Cosmos Nicolaou | 89303d6 | 2014-11-02 12:58:11 -0800 | [diff] [blame] | 614 | if err := s.server.ServeDispatcher(name, s.dispatcher); err != nil { |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 615 | return err |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 616 | } |
Shyam Jayaraman | c17abda | 2015-04-06 16:49:17 -0700 | [diff] [blame] | 617 | s.statusClose = make(chan struct{}, 1) |
| 618 | go s.readStatus() |
Nicolas LaCasse | 27b57c7 | 2014-11-19 13:40:20 -0800 | [diff] [blame] | 619 | return nil |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 622 | func (s *Server) popServerRequest(id int32) chan *lib.ServerRpcReply { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 623 | s.outstandingRequestLock.Lock() |
| 624 | defer s.outstandingRequestLock.Unlock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 625 | ch := s.outstandingServerRequests[id] |
| 626 | delete(s.outstandingServerRequests, id) |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 627 | |
| 628 | return ch |
| 629 | } |
| 630 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 631 | func (s *Server) HandleServerResponse(id int32, data string) { |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 632 | ch := s.popServerRequest(id) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 633 | if ch == nil { |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 634 | vlog.Errorf("unexpected result from JavaScript. No channel "+ |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 635 | "for MessageId: %d exists. Ignoring the results.", id) |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 636 | // Ignore unknown responses that don't belong to any channel |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 637 | return |
| 638 | } |
Matt Rosencrantz | 4aabe57 | 2014-10-22 09:25:50 -0700 | [diff] [blame] | 639 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 640 | // Decode the result and send it through the channel |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 641 | var reply lib.ServerRpcReply |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 642 | if err := lib.VomDecode(data, &reply); err != nil { |
| 643 | reply.Err = err |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 646 | vlog.VI(0).Infof("response received from JavaScript server for "+ |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 647 | "MessageId %d with result %v", id, reply) |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 648 | s.helper.CleanupFlow(id) |
Shyam Jayaraman | 07136a7 | 2015-04-13 13:34:41 -0700 | [diff] [blame] | 649 | if reply.Err != nil { |
| 650 | ch <- &reply |
| 651 | return |
| 652 | } |
| 653 | jsBlessingsType := vdl.TypeOf(principal.JsBlessings{}) |
| 654 | for i, val := range reply.Results { |
| 655 | if val.Type() == jsBlessingsType { |
| 656 | var jsBlessings principal.JsBlessings |
| 657 | if err := vdl.Convert(&jsBlessings, val); err != nil { |
| 658 | reply.Err = err |
| 659 | break |
| 660 | } |
| 661 | reply.Results[i] = vdl.ValueOf( |
| 662 | s.helper.GetBlessings(jsBlessings.Handle)) |
| 663 | } |
| 664 | } |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 665 | ch <- &reply |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 668 | func (s *Server) HandleLookupResponse(id int32, data string) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 669 | s.dispatcher.handleLookupResponse(id, data) |
| 670 | } |
| 671 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 672 | func (s *Server) HandleAuthResponse(id int32, data string) { |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 673 | s.outstandingRequestLock.Lock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 674 | ch := s.outstandingAuthRequests[id] |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 675 | s.outstandingRequestLock.Unlock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 676 | if ch == nil { |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 677 | vlog.Errorf("unexpected result from JavaScript. No channel "+ |
Shyam Jayaraman | e56df9a | 2014-11-20 17:38:54 -0800 | [diff] [blame] | 678 | "for MessageId: %d exists. Ignoring the results(%s)", id, data) |
Suharsh Sivakumar | 4cc7c2d | 2015-04-09 15:21:56 -0700 | [diff] [blame] | 679 | // Ignore unknown responses that don't belong to any channel |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 680 | return |
| 681 | } |
| 682 | // Decode the result and send it through the channel |
| 683 | var reply authReply |
| 684 | if decoderErr := json.Unmarshal([]byte(data), &reply); decoderErr != nil { |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 685 | err := verror.Convert(verror.ErrInternal, nil, decoderErr).(verror.E) |
Mike Burrows | b6689c2 | 2014-10-08 11:14:15 -0700 | [diff] [blame] | 686 | reply = authReply{Err: &err} |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Matt Rosencrantz | 99ed678 | 2015-01-27 19:58:34 -0800 | [diff] [blame] | 689 | vlog.VI(0).Infof("response received from JavaScript server for "+ |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 690 | "MessageId %d with result %v", id, reply) |
| 691 | s.helper.CleanupFlow(id) |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 692 | // A nil verror.E does not result in an nil error. Instead, we have create |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 693 | // a variable for the error interface and only set it's value if the struct is non- |
| 694 | // nil. |
| 695 | var err error |
| 696 | if reply.Err != nil { |
| 697 | err = reply.Err |
| 698 | } |
| 699 | ch <- err |
| 700 | } |
| 701 | |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 702 | func (s *Server) HandleCaveatValidationResponse(id int32, data string) { |
| 703 | s.outstandingRequestLock.Lock() |
| 704 | ch := s.outstandingValidationRequests[id] |
| 705 | s.outstandingRequestLock.Unlock() |
| 706 | if ch == nil { |
| 707 | vlog.Errorf("unexpected result from JavaScript. No channel "+ |
| 708 | "for validation response with MessageId: %d exists. Ignoring the results(%s)", id, data) |
Suharsh Sivakumar | 4cc7c2d | 2015-04-09 15:21:56 -0700 | [diff] [blame] | 709 | // Ignore unknown responses that don't belong to any channel |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 710 | return |
| 711 | } |
| 712 | |
| 713 | var reply CaveatValidationResponse |
| 714 | if err := lib.VomDecode(data, &reply); err != nil { |
| 715 | vlog.Errorf("failed to decode validation response %q: error %v", data, err) |
| 716 | ch <- []error{} |
| 717 | return |
| 718 | } |
| 719 | |
| 720 | ch <- reply.Results |
| 721 | } |
| 722 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 723 | func (s *Server) createFlow() *Flow { |
| 724 | return s.helper.CreateNewFlow(s, nil) |
| 725 | } |
| 726 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 727 | func (s *Server) cleanupFlow(id int32) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 728 | s.helper.CleanupFlow(id) |
| 729 | } |
| 730 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 731 | func (s *Server) createInvoker(handle int32, sig []signature.Interface, hasGlobber bool) (rpc.Invoker, error) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 732 | remoteInvokeFunc := s.createRemoteInvokerFunc(handle) |
Shyam Jayaraman | eb10b9b | 2015-01-12 12:23:20 -0800 | [diff] [blame] | 733 | var globFunc remoteGlobFunc |
| 734 | if hasGlobber { |
| 735 | globFunc = s.createRemoteGlobFunc(handle) |
| 736 | } |
| 737 | return newInvoker(sig, remoteInvokeFunc, globFunc), nil |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Benjamin Prosnitz | 86d5228 | 2014-12-19 15:48:38 -0800 | [diff] [blame] | 740 | func (s *Server) createAuthorizer(handle int32, hasAuthorizer bool) (security.Authorizer, error) { |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 741 | if hasAuthorizer { |
Todd Wang | 4264e4b | 2015-04-16 22:43:40 -0700 | [diff] [blame] | 742 | return s.createRemoteAuthorizer(handle), nil |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 743 | } |
Asim Shankar | 8572f6c | 2014-10-28 15:24:17 -0700 | [diff] [blame] | 744 | return nil, nil |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | func (s *Server) Stop() { |
Mike Burrows | 2ec2bb3 | 2015-02-26 15:14:43 -0800 | [diff] [blame] | 748 | stdErr := verror.New(verror.ErrTimeout, nil).(verror.E) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 749 | result := lib.ServerRpcReply{ |
Benjamin Prosnitz | a2ac3b3 | 2014-12-12 11:40:31 -0800 | [diff] [blame] | 750 | Results: nil, |
Mike Burrows | b6689c2 | 2014-10-08 11:14:15 -0700 | [diff] [blame] | 751 | Err: &stdErr, |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 752 | } |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 753 | s.serverStateLock.Lock() |
Nicolas LaCasse | e3867dc | 2015-02-05 14:44:53 -0800 | [diff] [blame] | 754 | |
Shyam Jayaraman | c17abda | 2015-04-06 16:49:17 -0700 | [diff] [blame] | 755 | if s.statusClose != nil { |
| 756 | close(s.statusClose) |
| 757 | } |
Nicolas LaCasse | e3867dc | 2015-02-05 14:44:53 -0800 | [diff] [blame] | 758 | if s.dispatcher != nil { |
| 759 | s.dispatcher.Cleanup() |
| 760 | } |
| 761 | |
| 762 | for _, ch := range s.outstandingAuthRequests { |
| 763 | ch <- fmt.Errorf("Cleaning up server") |
| 764 | } |
Nicolas LaCasse | e3867dc | 2015-02-05 14:44:53 -0800 | [diff] [blame] | 765 | |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 766 | for _, ch := range s.outstandingServerRequests { |
| 767 | select { |
| 768 | case ch <- &result: |
| 769 | default: |
| 770 | } |
| 771 | } |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 772 | s.outstandingRequestLock.Lock() |
| 773 | s.outstandingAuthRequests = make(map[int32]chan error) |
Todd Wang | baf1684 | 2015-03-16 14:12:29 -0700 | [diff] [blame] | 774 | s.outstandingServerRequests = make(map[int32]chan *lib.ServerRpcReply) |
Benjamin Prosnitz | e8e2b9b | 2015-02-24 12:55:25 -0800 | [diff] [blame] | 775 | s.outstandingRequestLock.Unlock() |
| 776 | s.serverStateLock.Unlock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 777 | s.server.Stop() |
Matt Rosencrantz | 317b0c5 | 2015-03-24 20:47:01 -0700 | [diff] [blame] | 778 | |
Suharsh Sivakumar | 4cc7c2d | 2015-04-09 15:21:56 -0700 | [diff] [blame] | 779 | // Only clear the validation requests map after stopping. Clearing them before |
| 780 | // can cause the publisher to get stuck waiting for a caveat validation that |
| 781 | // will never be answered, which prevents the server from stopping. |
Matt Rosencrantz | 317b0c5 | 2015-03-24 20:47:01 -0700 | [diff] [blame] | 782 | s.serverStateLock.Lock() |
| 783 | s.outstandingRequestLock.Lock() |
| 784 | s.outstandingValidationRequests = make(map[int32]chan []error) |
| 785 | s.outstandingRequestLock.Unlock() |
| 786 | s.serverStateLock.Unlock() |
Jiri Simsa | 78b646f | 2014-10-08 10:23:05 -0700 | [diff] [blame] | 787 | } |
Ali Ghassemi | 1008bbe | 2014-11-07 16:36:08 -0800 | [diff] [blame] | 788 | |
| 789 | func (s *Server) AddName(name string) error { |
| 790 | return s.server.AddName(name) |
| 791 | } |
| 792 | |
Cosmos Nicolaou | b1a41af | 2015-01-25 22:13:40 -0800 | [diff] [blame] | 793 | func (s *Server) RemoveName(name string) { |
| 794 | s.server.RemoveName(name) |
Ali Ghassemi | 1008bbe | 2014-11-07 16:36:08 -0800 | [diff] [blame] | 795 | } |