-
Notifications
You must be signed in to change notification settings - Fork 1
/
dial.go
45 lines (34 loc) · 1.15 KB
/
dial.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package visigrpc
import (
"crypto/tls"
"crypto/x509"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Dialer is interface implemented by types that create a connection to a gRPC server
type Dialer interface {
Dial(target string, options ...grpc.DialOption) (*grpc.ClientConn, error)
}
type securedDialer struct{}
// NewSecuredDialer is factory to create a new gRPC dialer using TLS
func NewSecuredDialer() Dialer {
return &securedDialer{}
}
func (d *securedDialer) Dial(target string, options ...grpc.DialOption) (*grpc.ClientConn, error) {
cert, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
cred := credentials.NewTLS(&tls.Config{
RootCAs: cert,
})
return grpc.Dial(target, append(options, grpc.WithAuthority(target), grpc.WithTransportCredentials(cred))...)
}
type insecuredDialer struct{}
// NewInsecuredDialer is factory to create a new gRPC dialer using insecure dial option
func NewInsecuredDialer() Dialer {
return &insecuredDialer{}
}
func (d *insecuredDialer) Dial(target string, options ...grpc.DialOption) (*grpc.ClientConn, error) {
return grpc.Dial(target, append(options, grpc.WithInsecure())...)
}