Skip to content

Commit 3d30978

Browse files
Feature: grpc cert option
1 parent 791a286 commit 3d30978

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

pkg/modules/grpc/client.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/rs/zerolog/log"
1212
"google.golang.org/grpc"
1313
gogrpc "google.golang.org/grpc"
14-
"google.golang.org/grpc/credentials/insecure"
1514
"google.golang.org/grpc/keepalive"
1615
)
1716

@@ -35,14 +34,22 @@ func (client *Client) Name() string {
3534
}
3635

3736
// Connect - connects to server
38-
func (client *Client) Connect(ctx context.Context) error {
39-
conn, err := gogrpc.Dial(
40-
client.serverAddress,
41-
gogrpc.WithTransportCredentials(insecure.NewCredentials()),
37+
func (client *Client) Connect(ctx context.Context, opts ...ConnectOption) error {
38+
dialOpts := []gogrpc.DialOption{
4239
gogrpc.WithKeepaliveParams(keepalive.ClientParameters{
4340
Time: 20 * time.Second,
4441
Timeout: 10 * time.Second,
4542
}),
43+
}
44+
connectOpts := newConnectOptions()
45+
for i := range opts {
46+
opts[i](&connectOpts)
47+
}
48+
dialOpts = append(dialOpts, gogrpc.WithTransportCredentials(connectOpts.creds))
49+
50+
conn, err := gogrpc.Dial(
51+
client.serverAddress,
52+
dialOpts...,
4653
)
4754
if err != nil {
4855
return errors.Wrap(err, "dial connection")
@@ -53,15 +60,23 @@ func (client *Client) Connect(ctx context.Context) error {
5360
}
5461

5562
// WaitConnect - trying to connect to server
56-
func (client *Client) WaitConnect(ctx context.Context) error {
57-
conn, err := gogrpc.Dial(
58-
client.serverAddress,
63+
func (client *Client) WaitConnect(ctx context.Context, opts ...ConnectOption) error {
64+
dialOpts := []gogrpc.DialOption{
5965
gogrpc.WithBlock(),
60-
gogrpc.WithTransportCredentials(insecure.NewCredentials()),
6166
gogrpc.WithKeepaliveParams(keepalive.ClientParameters{
6267
Time: 20 * time.Second,
6368
Timeout: 10 * time.Second,
6469
}),
70+
}
71+
connectOpts := newConnectOptions()
72+
for i := range opts {
73+
opts[i](&connectOpts)
74+
}
75+
dialOpts = append(dialOpts, gogrpc.WithTransportCredentials(connectOpts.creds))
76+
77+
conn, err := gogrpc.Dial(
78+
client.serverAddress,
79+
dialOpts...,
6580
)
6681
if err != nil {
6782
return errors.Wrap(err, "dial connection")

pkg/modules/grpc/options.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package grpc
2+
3+
import (
4+
"google.golang.org/grpc/credentials"
5+
"google.golang.org/grpc/credentials/insecure"
6+
)
7+
8+
// ConnectOptions -
9+
type ConnectOptions struct {
10+
creds credentials.TransportCredentials
11+
}
12+
13+
func newConnectOptions() ConnectOptions {
14+
return ConnectOptions{
15+
creds: insecure.NewCredentials(),
16+
}
17+
}
18+
19+
// ConnectOption -
20+
type ConnectOption func(opts *ConnectOptions)
21+
22+
// WithTlsFromCert -
23+
func WithTlsFromCert(domain string) ConnectOption {
24+
return func(opts *ConnectOptions) {
25+
if domain != "" {
26+
opts.creds = credentials.NewClientTLSFromCert(nil, domain)
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)