private/grpctlsopts: grpc related tlsopts

This moves grpc related tlsopts methods to private/grpctlsopts.
This allows to remove grpc dependency from tlsopts.

Change-Id: I25090b82b1e7a0633417ad600f8587b0c30ace73
This commit is contained in:
Egon Elbre 2020-02-26 22:43:31 +02:00
parent d5540c89a1
commit f85606b5a7
4 changed files with 81 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import (
"storj.io/common/identity" "storj.io/common/identity"
"storj.io/common/peertls/tlsopts" "storj.io/common/peertls/tlsopts"
"storj.io/storj/pkg/cfgstruct" "storj.io/storj/pkg/cfgstruct"
"storj.io/storj/private/grpctlsopts"
) )
var ( var (
@ -37,7 +38,7 @@ func main() {
panic(err) panic(err)
} }
dialOption := clientOptions.DialUnverifiedIDOption() dialOption := grpctlsopts.DialUnverifiedIDOption(clientOptions)
conn, err := grpc.Dial(*targetAddr, dialOption, grpc.WithInsecure()) conn, err := grpc.Dial(*targetAddr, dialOption, grpc.WithInsecure())
if err != nil { if err != nil {

View File

@ -19,6 +19,7 @@ import (
"storj.io/common/rpc" "storj.io/common/rpc"
"storj.io/drpc/drpcserver" "storj.io/drpc/drpcserver"
"storj.io/storj/pkg/listenmux" "storj.io/storj/pkg/listenmux"
"storj.io/storj/private/grpctlsopts"
) )
// Service represents a specific gRPC method collection to be registered // Service represents a specific gRPC method collection to be registered
@ -84,7 +85,7 @@ func New(log *zap.Logger, tlsOptions *tlsopts.Options, publicAddr, privateAddr s
grpc: grpc.NewServer( grpc: grpc.NewServer(
grpc.StreamInterceptor(server.logOnErrorStreamInterceptor), grpc.StreamInterceptor(server.logOnErrorStreamInterceptor),
grpc.UnaryInterceptor(unaryInterceptor), grpc.UnaryInterceptor(unaryInterceptor),
tlsOptions.ServerOption(), grpctlsopts.ServerOption(tlsOptions),
), ),
} }

View File

@ -0,0 +1,35 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package grpctlsopts
import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"storj.io/common/peertls/tlsopts"
"storj.io/common/storj"
)
// ServerOption returns a grpc `ServerOption` for incoming connections
// to the node with this full identity.
func ServerOption(opts *tlsopts.Options) grpc.ServerOption {
tlsConfig := opts.ServerTLSConfig()
return grpc.Creds(credentials.NewTLS(tlsConfig))
}
// DialOption returns a grpc `DialOption` for making outgoing connections
// to the node with this peer identity.
func DialOption(opts *tlsopts.Options, id storj.NodeID) (grpc.DialOption, error) {
if id.IsZero() {
return nil, tlsopts.Error.New("no ID specified for DialOption")
}
tlsConfig := opts.ClientTLSConfig(id)
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), nil
}
// DialUnverifiedIDOption returns a grpc `DialUnverifiedIDOption`
func DialUnverifiedIDOption(opts *tlsopts.Options) grpc.DialOption {
tlsConfig := opts.UnverifiedClientTLSConfig()
return grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
}

View File

@ -0,0 +1,42 @@
// Copyright (C) 2020 Storj Labs, Inc.
// See LICENSE for copying information.
package grpctlsopts_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"storj.io/common/identity"
"storj.io/common/identity/testidentity"
"storj.io/common/peertls/tlsopts"
"storj.io/common/storj"
"storj.io/storj/private/grpctlsopts"
)
func TestOptions_DialOption_error_on_empty_ID(t *testing.T) {
testidentity.CompleteIdentityVersionsTest(t, func(t *testing.T, version storj.IDVersion, ident *identity.FullIdentity) {
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{
PeerIDVersions: "*",
}, nil)
require.NoError(t, err)
dialOption, err := grpctlsopts.DialOption(tlsOptions, storj.NodeID{})
assert.Nil(t, dialOption)
assert.Error(t, err)
})
}
func TestOptions_DialUnverifiedIDOption(t *testing.T) {
testidentity.CompleteIdentityVersionsTest(t, func(t *testing.T, version storj.IDVersion, ident *identity.FullIdentity) {
tlsOptions, err := tlsopts.NewOptions(ident, tlsopts.Config{
PeerIDVersions: "*",
}, nil)
require.NoError(t, err)
dialOption := grpctlsopts.DialUnverifiedIDOption(tlsOptions)
assert.NotNil(t, dialOption)
})
}