forked from SmartBFT-Go/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interceptor.go
79 lines (67 loc) · 1.78 KB
/
interceptor.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package accesscontrol
import (
"fmt"
pb "github.com/hyperledger/fabric/protos/peer"
"google.golang.org/grpc"
)
type interceptor struct {
next pb.ChaincodeSupportServer
auth authorization
}
// ChaincodeStream defines a gRPC stream for sending
// and receiving chaincode messages
type ChaincodeStream interface {
// Send sends a chaincode message
Send(*pb.ChaincodeMessage) error
// Recv receives a chaincode message
Recv() (*pb.ChaincodeMessage, error)
}
type authorization func(message *pb.ChaincodeMessage, stream grpc.ServerStream) error
func newInterceptor(srv pb.ChaincodeSupportServer, auth authorization) pb.ChaincodeSupportServer {
return &interceptor{
next: srv,
auth: auth,
}
}
// Register makes the interceptor implement ChaincodeSupportServer
func (i *interceptor) Register(stream pb.ChaincodeSupport_RegisterServer) error {
is := &interceptedStream{
incMessages: make(chan *pb.ChaincodeMessage, 1),
stream: stream,
ServerStream: stream,
auth: i.auth,
}
msg, err := stream.Recv()
if err != nil {
return fmt.Errorf("Recv() error: %v, closing connection", err)
}
err = is.auth(msg, is.ServerStream)
if err != nil {
return err
}
is.incMessages <- msg
close(is.incMessages)
return i.next.Register(is)
}
type interceptedStream struct {
incMessages chan *pb.ChaincodeMessage
stream ChaincodeStream
grpc.ServerStream
auth authorization
}
// Send sends a chaincode message
func (is *interceptedStream) Send(msg *pb.ChaincodeMessage) error {
return is.stream.Send(msg)
}
// Recv receives a chaincode message
func (is *interceptedStream) Recv() (*pb.ChaincodeMessage, error) {
msg, ok := <-is.incMessages
if !ok {
return is.stream.Recv()
}
return msg, nil
}