diff --git a/pkg/transport/client.go b/pkg/transport/client.go new file mode 100644 index 000000000..d6a5f9e6f --- /dev/null +++ b/pkg/transport/client.go @@ -0,0 +1,26 @@ +// Copyright (C) 2018 Storj Labs, Inc. +// See LICENSE for copying information. + +package transport + +import ( + "context" + + "github.com/zeebo/errs" + "google.golang.org/grpc" + monkit "gopkg.in/spacemonkeygo/monkit.v2" + + proto "storj.io/storj/protos/overlay" +) + +var ( + mon = monkit.Package() + //Error is the errs class of standard Transport Client errors + Error = errs.Class("transport error") +) + +// Client defines the interface to an transport client. +type Client interface { + DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (*grpc.ClientConn, error) + DialNode(ctx context.Context, node proto.Node) (*grpc.ClientConn, error) +} diff --git a/pkg/transport/transport.go b/pkg/transport/transport.go new file mode 100644 index 000000000..365a46b0f --- /dev/null +++ b/pkg/transport/transport.go @@ -0,0 +1,38 @@ +// Copyright (C) 2018 Storj Labs, Inc. +// See LICENSE for copying information. + +package transport + +import ( + "context" + + "google.golang.org/grpc" + + proto "storj.io/storj/protos/overlay" +) + +// Transport interface structure +type Transport struct { +} + +// DialNode using the authenticated mode +func (o *Transport) DialNode(ctx context.Context, node proto.Node) (conn *grpc.ClientConn, err error) { + defer mon.Task()(&ctx)(&err) + + if node.Address == nil { + return nil, Error.New("no address") + } + /* TODO@ASK security feature under development */ + return o.DialUnauthenticated(ctx, *node.Address) +} + +// DialUnauthenticated using unauthenticated mode +func (o *Transport) DialUnauthenticated(ctx context.Context, addr proto.NodeAddress) (conn *grpc.ClientConn, err error) { + defer mon.Task()(&ctx)(&err) + + if addr.Address == "" { + return nil, Error.New("no address") + } + + return grpc.Dial(addr.Address, grpc.WithInsecure()) +} diff --git a/pkg/transport/transport_test.go b/pkg/transport/transport_test.go new file mode 100644 index 000000000..15277a48d --- /dev/null +++ b/pkg/transport/transport_test.go @@ -0,0 +1,48 @@ +// Copyright (C) 2018 Storj Labs, Inc. +// See LICENSE for copying information. +package transport + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + proto "storj.io/storj/protos/overlay" +) + +func TestDialNode(t *testing.T) { + oc := Transport{} + + // node.Address.Address == "" condition test + node := proto.Node{ + Id: "DUMMYID1", + Address: &proto.NodeAddress{ + Transport: proto.NodeTransport_TCP, + Address: "", + }, + } + conn, err := oc.DialNode(context.Background(), node) + assert.Error(t, err) + assert.Nil(t, conn) + + // node.Address == nil condition test + node = proto.Node{ + Id: "DUMMYID2", + Address: nil, + } + conn, err = oc.DialNode(context.Background(), node) + assert.Error(t, err) + assert.Nil(t, conn) + + // node is valid argument condition test + node = proto.Node{ + Id: "DUMMYID3", + Address: &proto.NodeAddress{ + Transport: proto.NodeTransport_TCP, + Address: "127.0.0.0:9000", + }, + } + conn, err = oc.DialNode(context.Background(), node) + assert.NoError(t, err) + assert.NotNil(t, conn) +}