-
Notifications
You must be signed in to change notification settings - Fork 110
/
session_web.go
240 lines (216 loc) · 7.08 KB
/
session_web.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
package robot
import (
"context"
"sync"
"github.com/google/uuid"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/dynamic"
"github.com/pkg/errors"
commonpb "go.viam.com/api/common/v1"
"go.viam.com/utils/rpc"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/proto"
"go.viam.com/rdk/protoutils"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/session"
)
func (m *SessionManager) safetyMonitoredTypeAndMethod(method string) (*resource.RPCSubtype, *desc.MethodDescriptor, bool) {
subType, methodDesc, err := TypeAndMethodDescFromMethod(m.robot, method)
if err != nil {
return nil, nil, false
}
opts := methodDesc.AsMethodDescriptorProto().Options
if !proto.HasExtension(opts, commonpb.E_SafetyHeartbeatMonitored) {
return nil, nil, false
}
safetyMonitored := proto.GetExtension(opts, commonpb.E_SafetyHeartbeatMonitored).(bool)
if !safetyMonitored {
return nil, nil, false
}
return subType, methodDesc, true
}
func (m *SessionManager) safetyMonitoredResourceFromUnary(req interface{}, method string) resource.Name {
subType, _, ok := m.safetyMonitoredTypeAndMethod(method)
if !ok {
return resource.Name{}
}
reqMsg := protoutils.MessageToProtoV1(req)
if reqMsg == nil {
return resource.Name{}
}
msg, err := dynamic.AsDynamicMessage(reqMsg)
if err != nil {
m.logger.Errorw("error converting message to dynamic", "error", err, "method", method)
return resource.Name{}
}
_, resName, err := ResourceFromProtoMessage(m.robot, msg, subType.Subtype)
if err != nil {
m.logger.Errorw("unable to find resource", "error", err)
return resource.Name{}
}
return resName
}
type firstMessageServerStreamWrapper struct {
mu sync.Mutex
grpc.ServerStream
firstMsg *dynamic.Message
}
func (w *firstMessageServerStreamWrapper) RecvMsg(m interface{}) error {
w.mu.Lock()
defer w.mu.Unlock()
if w.firstMsg != nil {
msgTarget := protoutils.MessageToProtoV1(m)
if msgTarget == nil {
return errors.Errorf("expected message to be a proto.Message but got %T", m)
}
msg := w.firstMsg
w.firstMsg = nil
return msg.ConvertToDeterministic(msgTarget)
}
return w.ServerStream.RecvMsg(m)
}
func (m *SessionManager) safetyMonitoredResourceFromStream(
stream grpc.ServerStream,
method string,
) (resource.Name, grpc.ServerStream, error) {
subType, methodDesc, ok := m.safetyMonitoredTypeAndMethod(method)
if !ok {
// Note(erd): could maybe cache this in the future but may be subject to a DOS attack
// since method space is unbounded.
return resource.Name{}, nil, nil
}
firstMsg := dynamic.NewMessage(methodDesc.GetInputType())
if err := stream.RecvMsg(firstMsg); err != nil {
// this error counts
return resource.Name{}, nil, err
}
newStream := &firstMessageServerStreamWrapper{ServerStream: stream, firstMsg: firstMsg}
_, resName, err := ResourceFromProtoMessage(m.robot, firstMsg, subType.Subtype)
if err != nil {
m.logger.Errorw("unable to find resource", "error", err)
return resource.Name{}, newStream, nil
}
return resName, newStream, nil
}
var exemptFromSession = map[string]bool{
"/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo": true,
"/proto.rpc.webrtc.v1.SignalingService/Call": true,
"/proto.rpc.webrtc.v1.SignalingService/CallUpdate": true,
"/proto.rpc.webrtc.v1.SignalingService/OptionalWebRTCConfig": true,
"/proto.rpc.v1.AuthService/Authenticate": true,
"/proto.rpc.v1.ExternalAuthService/AuthenticateTo": true,
"/viam.robot.v1.RobotService/ResourceNames": true,
"/viam.robot.v1.RobotService/ResourceRPCSubtypes": true,
"/viam.robot.v1.RobotService/StartSession": true,
"/viam.robot.v1.RobotService/SendSessionHeartbeat": true,
}
// ServerInterceptors returns gRPC interceptors to work with sessions.
func (m *SessionManager) ServerInterceptors() session.ServerInterceptors {
return session.ServerInterceptors{
UnaryServerInterceptor: m.UnaryServerInterceptor,
StreamServerInterceptor: m.StreamServerInterceptor,
}
}
// UnaryServerInterceptor associates the current session (if present) in the current context before
// passing it to the unary response handler.
func (m *SessionManager) UnaryServerInterceptor(
ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (interface{}, error) {
if exemptFromSession[info.FullMethod] {
return handler(ctx, req)
}
safetyMonitoredResourceName := m.safetyMonitoredResourceFromUnary(req, info.FullMethod)
ctx, err := associateSession(ctx, m, safetyMonitoredResourceName, info.FullMethod)
if err != nil {
return nil, err
}
return handler(ctx, req)
}
// StreamServerInterceptor associates the current session (if present) in the current context before
// passing it to the stream response handler.
func (m *SessionManager) StreamServerInterceptor(
srv interface{},
ss grpc.ServerStream,
info *grpc.StreamServerInfo,
handler grpc.StreamHandler,
) error {
if exemptFromSession[info.FullMethod] {
return handler(srv, ss)
}
safetyMonitoredResource, wrappedStream, err := m.safetyMonitoredResourceFromStream(ss, info.FullMethod)
if err != nil {
return err
}
if wrappedStream != nil {
ss = wrappedStream
}
ctx, err := associateSession(ss.Context(), m, safetyMonitoredResource, info.FullMethod)
if err != nil {
return err
}
return handler(srv, &ssStreamContextWrapper{ss, ctx})
}
// associateSession creates a new context associated with the session, if found, from an incoming context.
func associateSession(
ctx context.Context,
m *SessionManager,
safetyMonitoredResourceName resource.Name,
method string,
) (nextCtx context.Context, err error) {
var sessID uuid.UUID
if safetyMonitoredResourceName != (resource.Name{}) {
// defer this because no matter what we want to know that someone was using
// a resource with a monitored method as long as no error happened.
defer func() {
if err == nil {
m.AssociateResource(sessID, safetyMonitoredResourceName)
}
}()
}
meta, ok := metadata.FromIncomingContext(ctx)
if !ok {
m.logger.Warnw("failed to pull metadata from context", "method", method)
return ctx, nil
}
sessID, err = sessionFromMetadata(meta)
if err != nil {
m.logger.Warnw("failed to get session id from metadata", "error", err)
return ctx, err
}
if sessID == uuid.Nil {
return ctx, nil
}
authSubject, _ := rpc.ContextAuthSubject(ctx)
sess, err := m.FindByID(sessID, authSubject)
if err != nil {
return nil, err
}
return session.ToContext(ctx, sess), nil
}
// sessionFromMetadata returns a session id from metadata.
func sessionFromMetadata(meta metadata.MD) (uuid.UUID, error) {
values := meta.Get(session.IDMetadataKey)
switch len(values) {
case 0:
return uuid.UUID{}, nil
case 1:
sessID, err := uuid.Parse(values[0])
if err != nil {
return uuid.UUID{}, err
}
return sessID, nil
default:
return uuid.UUID{}, errors.New("found more than one session id in metadata")
}
}
type ssStreamContextWrapper struct {
grpc.ServerStream
ctx context.Context
}
func (w ssStreamContextWrapper) Context() context.Context {
return w.ctx
}