2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-06-29 19:28:06 +01:00
|
|
|
// See LICENSE for copying information.
|
2019-02-11 11:17:32 +00:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
package rpc_test
|
2018-06-29 19:28:06 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-12-14 10:44:48 +00:00
|
|
|
"fmt"
|
2018-06-29 19:28:06 +01:00
|
|
|
"testing"
|
2018-12-14 10:44:48 +00:00
|
|
|
"time"
|
2018-06-29 19:28:06 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-26 18:35:16 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-31 13:09:45 +01:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2018-12-14 10:44:48 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-04-08 19:15:19 +01:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2018-12-14 10:44:48 +00:00
|
|
|
"storj.io/storj/internal/testplanet"
|
2018-09-18 05:39:06 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-26 18:35:16 +00:00
|
|
|
"storj.io/storj/pkg/peertls/tlsopts"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc"
|
2018-12-14 10:44:48 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-06-29 19:28:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDialNode(t *testing.T) {
|
2018-12-14 10:44:48 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
2018-08-24 05:01:03 +01:00
|
|
|
|
2018-12-14 10:44:48 +00:00
|
|
|
planet, err := testplanet.New(t, 0, 2, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-08-24 05:01:03 +01:00
|
|
|
}
|
2018-12-14 10:44:48 +00:00
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
whitelistPath, err := planet.WriteWhitelist(storj.LatestIDVersion())
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-12-14 10:44:48 +00:00
|
|
|
planet.Start(ctx)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
unsignedIdent, err := testidentity.PregeneratedIdentity(0, storj.LatestIDVersion())
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
signedIdent, err := testidentity.PregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(signedIdent, tlsopts.Config{
|
2019-02-26 18:35:16 +00:00
|
|
|
UsePeerCAWhitelist: true,
|
|
|
|
PeerCAWhitelistPath: whitelistPath,
|
2019-04-09 18:01:45 +01:00
|
|
|
PeerIDVersions: "*",
|
2019-08-19 23:10:38 +01:00
|
|
|
}, nil)
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
unsignedClientOpts, err := tlsopts.NewOptions(unsignedIdent, tlsopts.Config{
|
2019-04-09 18:01:45 +01:00
|
|
|
PeerIDVersions: "*",
|
2019-08-19 23:10:38 +01:00
|
|
|
}, nil)
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
unsignedDialer := rpc.NewDefaultDialer(unsignedClientOpts)
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
t.Run("DialNode with invalid targets", func(t *testing.T) {
|
2018-12-14 10:44:48 +00:00
|
|
|
targets := []*pb.Node{
|
2019-01-02 18:47:34 +00:00
|
|
|
{
|
|
|
|
Id: storj.NodeID{},
|
|
|
|
Address: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: storj.NodeID{},
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: storj.NodeID{123},
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: "127.0.0.1:100",
|
|
|
|
},
|
|
|
|
},
|
2019-03-04 20:03:33 +00:00
|
|
|
{
|
|
|
|
Id: storj.NodeID{},
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: planet.StorageNodes[1].Addr(),
|
|
|
|
},
|
|
|
|
},
|
2018-12-14 10:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range targets {
|
|
|
|
tag := fmt.Sprintf("%+v", target)
|
|
|
|
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialNode(timedCtx, target)
|
2018-12-14 10:44:48 +00:00
|
|
|
cancel()
|
|
|
|
assert.Error(t, err, tag)
|
|
|
|
assert.Nil(t, conn, tag)
|
|
|
|
}
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2019-03-04 20:03:33 +00:00
|
|
|
t.Run("DialNode with valid signed target", func(t *testing.T) {
|
|
|
|
target := &pb.Node{
|
|
|
|
Id: planet.StorageNodes[1].ID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: planet.StorageNodes[1].Addr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialNode(timedCtx, target)
|
2018-12-14 10:44:48 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NotNil(t, conn)
|
2018-12-14 10:44:48 +00:00
|
|
|
|
|
|
|
assert.NoError(t, conn.Close())
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
t.Run("DialNode with unsigned identity", func(t *testing.T) {
|
2019-02-26 18:35:16 +00:00
|
|
|
target := &pb.Node{
|
|
|
|
Id: planet.StorageNodes[1].ID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: planet.StorageNodes[1].Addr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := unsignedDialer.DialNode(timedCtx, target)
|
2019-02-26 18:35:16 +00:00
|
|
|
cancel()
|
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, conn.Close())
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
t.Run("DialAddress with unsigned identity", func(t *testing.T) {
|
2019-02-26 18:35:16 +00:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := unsignedDialer.DialAddressInsecure(timedCtx, planet.StorageNodes[1].Addr())
|
2019-02-26 18:35:16 +00:00
|
|
|
cancel()
|
|
|
|
|
2019-03-06 14:42:34 +00:00
|
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, conn.Close())
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DialAddress with valid address", func(t *testing.T) {
|
2018-12-14 10:44:48 +00:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialAddressInsecure(timedCtx, planet.StorageNodes[1].Addr())
|
2018-12-14 10:44:48 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NotNil(t, conn)
|
2018-12-14 10:44:48 +00:00
|
|
|
assert.NoError(t, conn.Close())
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialNode_BadServerCertificate(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.NewCustom(
|
2019-07-31 13:09:45 +01:00
|
|
|
zaptest.NewLogger(t),
|
2019-02-26 18:35:16 +00:00
|
|
|
testplanet.Config{
|
|
|
|
SatelliteCount: 0,
|
|
|
|
StorageNodeCount: 2,
|
|
|
|
UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.DisablePeerCAWhitelist,
|
2019-04-08 19:15:19 +01:00
|
|
|
Identities: testidentity.NewPregeneratedIdentities(storj.LatestIDVersion()),
|
2019-02-26 18:35:16 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|
2019-02-26 18:35:16 +00:00
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
whitelistPath, err := planet.WriteWhitelist(storj.LatestIDVersion())
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
ident, err := testidentity.PregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{
|
2019-02-26 18:35:16 +00:00
|
|
|
UsePeerCAWhitelist: true,
|
|
|
|
PeerCAWhitelistPath: whitelistPath,
|
2019-08-19 23:10:38 +01:00
|
|
|
}, nil)
|
2019-02-26 18:35:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
dialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
t.Run("DialNode with bad server certificate", func(t *testing.T) {
|
|
|
|
target := &pb.Node{
|
|
|
|
Id: planet.StorageNodes[1].ID(),
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: planet.StorageNodes[1].Addr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialNode(timedCtx, target)
|
2019-02-26 18:35:16 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
tag := fmt.Sprintf("%+v", target)
|
|
|
|
assert.Nil(t, conn, tag)
|
|
|
|
require.Error(t, err, tag)
|
|
|
|
assert.Contains(t, err.Error(), "not signed by any CA in the whitelist")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DialAddress with bad server certificate", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2019-09-19 05:46:39 +01:00
|
|
|
conn, err := dialer.DialAddressID(timedCtx, planet.StorageNodes[1].Addr(), planet.StorageNodes[1].ID())
|
2019-02-26 18:35:16 +00:00
|
|
|
cancel()
|
|
|
|
|
|
|
|
assert.Nil(t, conn)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "not signed by any CA in the whitelist")
|
|
|
|
})
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|