-
Notifications
You must be signed in to change notification settings - Fork 13
/
dial.go
51 lines (41 loc) · 1.45 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
46
47
48
49
50
51
package grpcclient
import (
"context"
"crypto/tls"
"net/url"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/heroku/x/grpc/requestid"
"github.com/heroku/x/tlsconfig"
)
// TLSOption is a function which modifies a TLS configuration.
type TLSOption func(*tls.Config)
// Credentials returns a gRPC DialOption configured for mutual TLS.
func Credentials(serverURL string, caCerts [][]byte, fullCert tls.Certificate, tlsopts ...TLSOption) (grpc.DialOption, error) {
uri, err := url.Parse(serverURL)
if err != nil {
return nil, err
}
cfg, err := tlsconfig.NewMutualTLS(caCerts, fullCert)
if err != nil {
return nil, err
}
cfg.ServerName = uri.Host
for _, o := range tlsopts {
o(cfg)
}
return grpc.WithTransportCredentials(credentials.NewTLS(cfg)), nil
}
// SkipVerify disables verification of server certificates.
func SkipVerify(cfg *tls.Config) {
cfg.InsecureSkipVerify = true
}
// AppendOutgoingRequestID reads the incoming Request-ID from the context and appends it to the
// outgoing context. Forwarding Request-IDs from gRPC service to service allows for request
// tracking across any number of services.
func AppendOutgoingRequestID() grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = requestid.AppendToOutgoingContext(ctx)
return invoker(ctx, method, req, reply, cc, opts...)
}
}