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-12-17 12:15:32 +00:00
|
|
|
package testplanet_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-12-10 16:32:54 +00:00
|
|
|
"go.uber.org/zap"
|
2018-07-03 09:35:01 +01:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity/testidentity"
|
|
|
|
"storj.io/common/peertls/tlsopts"
|
|
|
|
"storj.io/common/rpc"
|
2021-08-03 04:25:41 +01:00
|
|
|
"storj.io/common/rpc/quic"
|
2022-10-19 14:32:24 +01:00
|
|
|
"storj.io/common/rpc/rpcpool"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/testplanet"
|
2019-12-10 16:32:54 +00:00
|
|
|
"storj.io/storj/satellite"
|
|
|
|
"storj.io/storj/storagenode"
|
2018-06-29 19:28:06 +01:00
|
|
|
)
|
|
|
|
|
2020-05-19 16:49:13 +01:00
|
|
|
func TestDialNodeURL(t *testing.T) {
|
2019-12-06 18:03:22 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 0, StorageNodeCount: 2, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
whitelistPath, err := planet.WriteWhitelist(storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
unsignedIdent, err := testidentity.PregeneratedIdentity(0, storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
signedIdent, err := testidentity.PregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(signedIdent, tlsopts.Config{
|
|
|
|
UsePeerCAWhitelist: true,
|
|
|
|
PeerCAWhitelistPath: whitelistPath,
|
|
|
|
PeerIDVersions: "*",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
tcpDialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
quicDialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
quicDialer.Connector = quic.NewDefaultConnector(nil)
|
2019-09-19 05:46:39 +01:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
unsignedClientOpts, err := tlsopts.NewOptions(unsignedIdent, tlsopts.Config{
|
|
|
|
PeerIDVersions: "*",
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
unsignedTCPDialer := rpc.NewDefaultDialer(unsignedClientOpts)
|
|
|
|
unsignedQUICDialer := rpc.NewDefaultDialer(unsignedClientOpts)
|
|
|
|
unsignedQUICDialer.Connector = quic.NewDefaultConnector(nil)
|
|
|
|
|
|
|
|
test := func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, dialer rpc.Dialer, unsignedDialer rpc.Dialer) {
|
|
|
|
t.Run("DialNodeURL with invalid targets", func(t *testing.T) {
|
|
|
|
targets := []storj.NodeURL{
|
|
|
|
{
|
|
|
|
ID: storj.NodeID{},
|
|
|
|
Address: "",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: storj.NodeID{123},
|
|
|
|
Address: "127.0.0.1:100",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: storj.NodeID{},
|
|
|
|
Address: planet.StorageNodes[1].Addr(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range targets {
|
|
|
|
tag := fmt.Sprintf("%+v", target)
|
|
|
|
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := dialer.DialNodeURL(rpcpool.WithForceDial(timedCtx), target)
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
|
|
|
assert.Error(t, err, tag)
|
|
|
|
assert.Nil(t, conn, tag)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("DialNode with valid signed target", func(t *testing.T) {
|
2019-12-06 18:03:22 +00:00
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := dialer.DialNodeURL(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].NodeURL())
|
2019-12-06 18:03:22 +00:00
|
|
|
cancel()
|
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, conn)
|
2019-03-04 20:03:33 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.NoError(t, conn.Close())
|
|
|
|
})
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
t.Run("DialNode with unsigned identity", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := unsignedDialer.DialNodeURL(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].NodeURL())
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, conn.Close())
|
|
|
|
})
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
t.Run("DialAddress with unsigned identity", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := unsignedDialer.DialAddressInsecure(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].Addr())
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.NotNil(t, conn)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NoError(t, conn.Close())
|
|
|
|
})
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
t.Run("DialAddress with valid address", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := dialer.DialAddressInsecure(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].Addr())
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
require.NotNil(t, conn)
|
|
|
|
assert.NoError(t, conn.Close())
|
|
|
|
})
|
2018-12-14 10:44:48 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// test with tcp
|
|
|
|
t.Run("TCP", func(t *testing.T) {
|
|
|
|
test(t, ctx, planet, tcpDialer, unsignedTCPDialer)
|
|
|
|
})
|
|
|
|
// test with quic
|
|
|
|
t.Run("QUIC", func(t *testing.T) {
|
|
|
|
test(t, ctx, planet, quicDialer, unsignedQUICDialer)
|
2019-12-06 18:03:22 +00:00
|
|
|
})
|
2021-01-19 16:33:50 +00:00
|
|
|
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDialNode_BadServerCertificate(t *testing.T) {
|
2019-12-06 18:03:22 +00:00
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 0, StorageNodeCount: 2, UplinkCount: 0,
|
2019-12-10 16:32:54 +00:00
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Server.UsePeerCAWhitelist = false
|
|
|
|
},
|
|
|
|
StorageNode: func(index int, config *storagenode.Config) {
|
|
|
|
config.Server.UsePeerCAWhitelist = false
|
|
|
|
},
|
|
|
|
Identities: func(log *zap.Logger, version storj.IDVersion) *testidentity.Identities {
|
|
|
|
return testidentity.NewPregeneratedIdentities(version)
|
|
|
|
},
|
|
|
|
},
|
2019-12-06 18:03:22 +00:00
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
whitelistPath, err := planet.WriteWhitelist(storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
ident, err := testidentity.PregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2019-12-06 18:03:22 +00:00
|
|
|
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{
|
|
|
|
UsePeerCAWhitelist: true,
|
|
|
|
PeerCAWhitelistPath: whitelistPath,
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
tcpDialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
quicDialer := rpc.NewDefaultDialer(tlsOptions)
|
|
|
|
quicDialer.Connector = quic.NewDefaultConnector(nil)
|
2019-09-19 05:46:39 +01:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
test := func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet, dialer rpc.Dialer) {
|
|
|
|
t.Run("DialNodeURL with bad server certificate", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := dialer.DialNodeURL(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].NodeURL())
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
tag := fmt.Sprintf("%+v", planet.StorageNodes[1].NodeURL())
|
|
|
|
assert.Nil(t, conn, tag)
|
|
|
|
require.Error(t, err, tag)
|
|
|
|
assert.Contains(t, err.Error(), "not signed by any CA in the whitelist")
|
|
|
|
})
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
t.Run("DialAddress with bad server certificate", func(t *testing.T) {
|
|
|
|
timedCtx, cancel := context.WithTimeout(ctx, time.Second)
|
2022-10-19 14:32:24 +01:00
|
|
|
conn, err := dialer.DialNodeURL(rpcpool.WithForceDial(timedCtx), planet.StorageNodes[1].NodeURL())
|
2021-01-19 16:33:50 +00:00
|
|
|
cancel()
|
2019-02-26 18:35:16 +00:00
|
|
|
|
2021-01-19 16:33:50 +00:00
|
|
|
assert.Nil(t, conn)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "not signed by any CA in the whitelist")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// test with tcp
|
|
|
|
t.Run("TCP", func(t *testing.T) {
|
|
|
|
test(t, ctx, planet, tcpDialer)
|
|
|
|
})
|
|
|
|
// test with quic
|
|
|
|
t.Run("QUIC", func(t *testing.T) {
|
|
|
|
test(t, ctx, planet, quicDialer)
|
2019-12-06 18:03:22 +00:00
|
|
|
})
|
2019-02-26 18:35:16 +00:00
|
|
|
})
|
2018-06-29 19:28:06 +01:00
|
|
|
}
|