Skip to content

Commit

Permalink
adding tls flag to establish secure connection
Browse files Browse the repository at this point in the history
  • Loading branch information
olegfomenko committed Feb 20, 2024
1 parent 072c7df commit d84230a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ core:

cosmos:
addr: 127.0.0.1:9090
enable_tls: false

evm:
chains:
Expand Down
20 changes: 18 additions & 2 deletions internal/config/cosmos.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package config

import (
"crypto/tls"
"time"

"gitlab.com/distributed_lab/figure/v3"
"gitlab.com/distributed_lab/kit/comfig"
"gitlab.com/distributed_lab/kit/kv"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/keepalive"
)

Expand All @@ -29,20 +31,34 @@ func (c *cosmoser) Cosmos() *grpc.ClientConn {
return c.once.Do(func() interface{} {
var config struct {
Addr string `fig:"addr"`
TLS bool `fig:"enable_tls"`
}

if err := figure.Out(&config).From(kv.MustGetStringMap(c.getter, "cosmos")).Please(); err != nil {
panic(err)
}

con, err := grpc.Dial(config.Addr, grpc.WithInsecure(), grpc.WithKeepaliveParams(keepalive.ClientParameters{
var client *grpc.ClientConn
var err error

connectSecurityOptions := grpc.WithInsecure()

if config.TLS {
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS13,
}

connectSecurityOptions = grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig))
}

client, err = grpc.Dial(config.Addr, connectSecurityOptions, grpc.WithKeepaliveParams(keepalive.ClientParameters{
Time: 10 * time.Second, // wait time before ping if no activity
Timeout: 20 * time.Second, // ping timeout
}))
if err != nil {
panic(err)
}

return con
return client
}).(*grpc.ClientConn)
}

0 comments on commit d84230a

Please sign in to comment.