-
Notifications
You must be signed in to change notification settings - Fork 107
/
grpc.go
112 lines (96 loc) · 3.14 KB
/
grpc.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
package api
import (
"context"
"fmt"
"sync"
"google.golang.org/grpc"
"github.com/oasisprotocol/oasis-core/go/common"
cmnGrpc "github.com/oasisprotocol/oasis-core/go/common/grpc"
)
var (
errInvalidRequestType = fmt.Errorf("invalid request type")
// ServiceName is the gRPC service name.
ServiceName = cmnGrpc.NewServiceName("EnclaveRPC")
// MethodCallEnclave is the CallEnclave method.
MethodCallEnclave = ServiceName.NewMethod("CallEnclave", CallEnclaveRequest{}).
WithNamespaceExtractor(func(ctx context.Context, req interface{}) (common.Namespace, error) {
r, ok := req.(*CallEnclaveRequest)
if !ok {
return common.Namespace{}, errInvalidRequestType
}
return r.RuntimeID, nil
}).
WithAccessControl(func(ctx context.Context, req interface{}) (bool, error) {
r, ok := req.(*CallEnclaveRequest)
if !ok {
return false, errInvalidRequestType
}
endpoint, ok := registeredEndpoints.Load(r.Endpoint)
if !ok {
return false, fmt.Errorf("enclaverpc: unsupported endpoint: %s", r.Endpoint)
}
return endpoint.(Endpoint).AccessControlRequired(ctx, r)
})
// serviceDesc is the gRPC service descriptor.
serviceDesc = grpc.ServiceDesc{
ServiceName: string(ServiceName),
HandlerType: (*Transport)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: MethodCallEnclave.ShortName(),
Handler: handlerCallEnclave,
},
},
Streams: []grpc.StreamDesc{},
}
// registeredEndpoints is a map of registered EnclaveRPC endpoints. It maps endpoint names
// to instances of the Endpoint interface.
registeredEndpoints sync.Map
)
func handlerCallEnclave( // nolint: golint
srv interface{},
ctx context.Context,
dec func(interface{}) error,
interceptor grpc.UnaryServerInterceptor,
) (interface{}, error) {
var req CallEnclaveRequest
if err := dec(&req); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(Transport).CallEnclave(ctx, &req)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: MethodCallEnclave.FullName(),
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(Transport).CallEnclave(ctx, req.(*CallEnclaveRequest))
}
return interceptor(ctx, &req, info, handler)
}
// RegisterService registers a new EnclaveRPC transport service with the given gRPC server.
func RegisterService(server *grpc.Server, service Transport) {
server.RegisterService(&serviceDesc, service)
}
// NewEndpoint registers a new EnclaveRPC endpoint.
func NewEndpoint(name string, endpoint Endpoint) {
if _, isRegistered := registeredEndpoints.Load(name); isRegistered {
panic(fmt.Errorf("enclaverpc: endpoint already registered: %s", name))
}
registeredEndpoints.Store(name, endpoint)
}
type transportClient struct {
conn *grpc.ClientConn
}
func (c *transportClient) CallEnclave(ctx context.Context, request *CallEnclaveRequest) ([]byte, error) {
var rsp []byte
if err := c.conn.Invoke(ctx, MethodCallEnclave.FullName(), request, &rsp); err != nil {
return nil, err
}
return rsp, nil
}
// NewTransportClient creates a new EnclaveRPC gRPC transport client service.
func NewTransportClient(c *grpc.ClientConn) Transport {
return &transportClient{c}
}