storj/private/grpctlsopts/opts.go
Egon Elbre f85606b5a7 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
2020-02-26 22:46:06 +02:00

36 lines
1.1 KiB
Go

// 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))
}