|
| 1 | +/* |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package comm_test |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "crypto/tls" |
| 12 | + "crypto/x509" |
| 13 | + "sync/atomic" |
| 14 | + "testing" |
| 15 | + "time" |
| 16 | + |
| 17 | + "github.com/golang/protobuf/proto" |
| 18 | + "github.com/hyperledger/fabric/common/util" |
| 19 | + "github.com/hyperledger/fabric/core/comm" |
| 20 | + grpc_testdata "github.com/hyperledger/fabric/core/comm/testdata/grpc" |
| 21 | + "github.com/hyperledger/fabric/protos/common" |
| 22 | + "github.com/hyperledger/fabric/protos/utils" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + context2 "golang.org/x/net/context" |
| 25 | + "google.golang.org/grpc" |
| 26 | + "google.golang.org/grpc/credentials" |
| 27 | + "google.golang.org/grpc/peer" |
| 28 | +) |
| 29 | + |
| 30 | +func TestExtractCertificateHashFromContext(t *testing.T) { |
| 31 | + assert.Nil(t, comm.ExtractCertificateHashFromContext(context.Background())) |
| 32 | + |
| 33 | + p := &peer.Peer{} |
| 34 | + ctx := peer.NewContext(context.Background(), p) |
| 35 | + assert.Nil(t, comm.ExtractCertificateHashFromContext(ctx)) |
| 36 | + |
| 37 | + p.AuthInfo = &nonTLSConnection{} |
| 38 | + ctx = peer.NewContext(context.Background(), p) |
| 39 | + assert.Nil(t, comm.ExtractCertificateHashFromContext(ctx)) |
| 40 | + |
| 41 | + p.AuthInfo = credentials.TLSInfo{} |
| 42 | + ctx = peer.NewContext(context.Background(), p) |
| 43 | + assert.Nil(t, comm.ExtractCertificateHashFromContext(ctx)) |
| 44 | + |
| 45 | + p.AuthInfo = credentials.TLSInfo{ |
| 46 | + State: tls.ConnectionState{ |
| 47 | + PeerCertificates: []*x509.Certificate{ |
| 48 | + {}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + ctx = peer.NewContext(context.Background(), p) |
| 53 | + assert.Nil(t, comm.ExtractCertificateHashFromContext(ctx)) |
| 54 | +} |
| 55 | + |
| 56 | +type nonTLSConnection struct { |
| 57 | +} |
| 58 | + |
| 59 | +func (*nonTLSConnection) AuthType() string { |
| 60 | + return "" |
| 61 | +} |
| 62 | + |
| 63 | +func TestNoopBindingInspector(t *testing.T) { |
| 64 | + // A Noop binding inspector always returns nil |
| 65 | + assert.Nil(t, comm.NewBindingInspector(false)(context.Background(), nil)) |
| 66 | +} |
| 67 | + |
| 68 | +func TestBindingInspector(t *testing.T) { |
| 69 | + testAddress := "localhost:25000" |
| 70 | + srv := newInspectingServer(testAddress, comm.NewBindingInspector(true)) |
| 71 | + go srv.Start() |
| 72 | + defer srv.Stop() |
| 73 | + time.Sleep(time.Second) |
| 74 | + |
| 75 | + // Scenario I: Invalid header sent |
| 76 | + err := srv.newInspection(t).inspectBinding(nil) |
| 77 | + assert.Error(t, err) |
| 78 | + assert.Contains(t, err.Error(), "envelope is nil") |
| 79 | + |
| 80 | + // Scenario II: invalid channel header |
| 81 | + ch, _ := proto.Marshal(utils.MakeChannelHeader(common.HeaderType_CONFIG, 0, "test", 0)) |
| 82 | + // Corrupt channel header |
| 83 | + ch = append(ch, 0) |
| 84 | + err = srv.newInspection(t).inspectBinding(envelopeWithChannelHeader(ch)) |
| 85 | + assert.Error(t, err) |
| 86 | + assert.Contains(t, err.Error(), "client didn't send a valid channel header") |
| 87 | + |
| 88 | + // Scenario III: No TLS cert hash in envelope |
| 89 | + chanHdr := utils.MakeChannelHeader(common.HeaderType_CONFIG, 0, "test", 0) |
| 90 | + ch, _ = proto.Marshal(chanHdr) |
| 91 | + err = srv.newInspection(t).inspectBinding(envelopeWithChannelHeader(ch)) |
| 92 | + assert.Error(t, err) |
| 93 | + assert.Contains(t, err.Error(), "client didn't include its TLS cert hash") |
| 94 | + |
| 95 | + // Scenario IV: Client sends its TLS cert hash as needed, but doesn't use mutual TLS |
| 96 | + cert, _ := tls.X509KeyPair([]byte(selfSignedCertPEM), []byte(selfSignedKeyPEM)) |
| 97 | + chanHdr.TlsCertHash = util.ComputeSHA256([]byte(cert.Certificate[0])) |
| 98 | + ch, _ = proto.Marshal(chanHdr) |
| 99 | + err = srv.newInspection(t).inspectBinding(envelopeWithChannelHeader(ch)) |
| 100 | + assert.Error(t, err) |
| 101 | + assert.Contains(t, err.Error(), "client didn't send a TLS certificate") |
| 102 | + |
| 103 | + // Scenario V: Client uses mutual TLS but sends the wrong TLS cert hash |
| 104 | + chanHdr.TlsCertHash = []byte{1, 2, 3} |
| 105 | + chHdrWithWrongTLSCertHash, _ := proto.Marshal(chanHdr) |
| 106 | + err = srv.newInspection(t).withMutualTLS().inspectBinding(envelopeWithChannelHeader(chHdrWithWrongTLSCertHash)) |
| 107 | + assert.Error(t, err) |
| 108 | + assert.Contains(t, err.Error(), "claimed TLS cert hash is [1 2 3] but actual TLS cert hash is") |
| 109 | + |
| 110 | + // Scenario VI: Client uses mutual TLS and also sends the correct TLS cert hash |
| 111 | + err = srv.newInspection(t).withMutualTLS().inspectBinding(envelopeWithChannelHeader(ch)) |
| 112 | + assert.NoError(t, err) |
| 113 | +} |
| 114 | + |
| 115 | +type inspectingServer struct { |
| 116 | + addr string |
| 117 | + comm.GRPCServer |
| 118 | + lastContext atomic.Value |
| 119 | + inspector comm.BindingInspector |
| 120 | +} |
| 121 | + |
| 122 | +func (is *inspectingServer) EmptyCall(ctx context2.Context, _ *grpc_testdata.Empty) (*grpc_testdata.Empty, error) { |
| 123 | + is.lastContext.Store(ctx) |
| 124 | + return &grpc_testdata.Empty{}, nil |
| 125 | +} |
| 126 | + |
| 127 | +func (is *inspectingServer) inspect(envelope *common.Envelope) error { |
| 128 | + return is.inspector(is.lastContext.Load().(context2.Context), envelope) |
| 129 | +} |
| 130 | + |
| 131 | +func newInspectingServer(addr string, inspector comm.BindingInspector) *inspectingServer { |
| 132 | + srv, err := comm.NewGRPCServer(addr, comm.SecureServerConfig{ |
| 133 | + UseTLS: true, |
| 134 | + ServerCertificate: []byte(selfSignedCertPEM), |
| 135 | + ServerKey: []byte(selfSignedKeyPEM), |
| 136 | + }) |
| 137 | + if err != nil { |
| 138 | + panic(err) |
| 139 | + } |
| 140 | + is := &inspectingServer{ |
| 141 | + addr: addr, |
| 142 | + GRPCServer: srv, |
| 143 | + inspector: inspector, |
| 144 | + } |
| 145 | + grpc_testdata.RegisterTestServiceServer(srv.Server(), is) |
| 146 | + return is |
| 147 | +} |
| 148 | + |
| 149 | +type inspection struct { |
| 150 | + tlsConfig *tls.Config |
| 151 | + server *inspectingServer |
| 152 | + creds credentials.TransportCredentials |
| 153 | + t *testing.T |
| 154 | +} |
| 155 | + |
| 156 | +func (is *inspectingServer) newInspection(t *testing.T) *inspection { |
| 157 | + tlsConfig := &tls.Config{ |
| 158 | + RootCAs: x509.NewCertPool(), |
| 159 | + } |
| 160 | + tlsConfig.RootCAs.AppendCertsFromPEM([]byte(selfSignedCertPEM)) |
| 161 | + return &inspection{ |
| 162 | + server: is, |
| 163 | + creds: credentials.NewTLS(tlsConfig), |
| 164 | + t: t, |
| 165 | + tlsConfig: tlsConfig, |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func (ins *inspection) withMutualTLS() *inspection { |
| 170 | + cert, err := tls.X509KeyPair([]byte(selfSignedCertPEM), []byte(selfSignedKeyPEM)) |
| 171 | + assert.NoError(ins.t, err) |
| 172 | + ins.tlsConfig.Certificates = []tls.Certificate{cert} |
| 173 | + ins.creds = credentials.NewTLS(ins.tlsConfig) |
| 174 | + return ins |
| 175 | +} |
| 176 | + |
| 177 | +func (ins *inspection) inspectBinding(envelope *common.Envelope) error { |
| 178 | + ctx := context.Background() |
| 179 | + ctx, c := context.WithTimeout(ctx, time.Second*3) |
| 180 | + defer c() |
| 181 | + conn, err := grpc.DialContext(ctx, ins.server.addr, grpc.WithTransportCredentials(ins.creds), grpc.WithBlock()) |
| 182 | + defer conn.Close() |
| 183 | + assert.NoError(ins.t, err) |
| 184 | + _, err = grpc_testdata.NewTestServiceClient(conn).EmptyCall(context.Background(), &grpc_testdata.Empty{}) |
| 185 | + return ins.server.inspect(envelope) |
| 186 | +} |
| 187 | + |
| 188 | +func envelopeWithChannelHeader(ch []byte) *common.Envelope { |
| 189 | + pl := &common.Payload{ |
| 190 | + Header: &common.Header{ |
| 191 | + ChannelHeader: ch, |
| 192 | + }, |
| 193 | + } |
| 194 | + payload, _ := proto.Marshal(pl) |
| 195 | + return &common.Envelope{ |
| 196 | + Payload: payload, |
| 197 | + } |
| 198 | +} |
0 commit comments