Skip to content

Commit

Permalink
Add TLS connection to redis.
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Mar 15, 2022
1 parent dbce7f7 commit a2ff841
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"crypto/tls"
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -90,10 +91,18 @@ func NewContainer(v *viper.Viper, options ...fx.Option) *fx.App {
case "none":
options = append(options, ledger.NoLockModule())
case "redis":
var tlsConfig *tls.Config
if viper.GetBool(lockStrategyRedisTLSEnabledFlag) {
tlsConfig = &tls.Config{}
if viper.GetBool(lockStrategyRedisTLSInsecureFlag) {
tlsConfig.InsecureSkipVerify = true
}
}
options = append(options, redis.Module(redis.Config{
Url: v.GetString(lockStrategyRedisUrlFlag),
LockDuration: v.GetDuration(lockStrategyRedisDurationFlag),
LockRetry: v.GetDuration(lockStrategyRedisRetryFlag),
TLSConfig: tlsConfig,
}))
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
lockStrategyRedisUrlFlag = "lock-strategy-redis-url"
lockStrategyRedisDurationFlag = "lock-strategy-redis-duration"
lockStrategyRedisRetryFlag = "lock-strategy-redis-retry"
lockStrategyRedisTLSEnabledFlag = "lock-strategy-redis-tls-enabled"
lockStrategyRedisTLSInsecureFlag = "lock-strategy-redis-tls-insecure"
otelTracesFlag = "otel-traces"
otelTracesBatchFlag = "otel-traces-batch"
otelTracesExporterFlag = "otel-traces-exporter"
Expand Down Expand Up @@ -126,6 +128,8 @@ func NewRootCommand() *cobra.Command {
root.PersistentFlags().String(lockStrategyRedisUrlFlag, "", "Redis url when using redis locking strategy")
root.PersistentFlags().Duration(lockStrategyRedisDurationFlag, redis.DefaultLockDuration, "Lock duration")
root.PersistentFlags().Duration(lockStrategyRedisRetryFlag, redis.DefaultRetryInterval, "Retry lock period")
root.PersistentFlags().Bool(lockStrategyRedisTLSEnabledFlag, false, "Use tls on redis")
root.PersistentFlags().Bool(lockStrategyRedisTLSInsecureFlag, false, "Whether redis is trusted or not")

viper.BindPFlags(root.PersistentFlags())
viper.SetConfigName("numary")
Expand Down
3 changes: 3 additions & 0 deletions pkg/redis/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package redis

import (
"crypto/tls"
"github.com/go-redis/redis/v8"
"github.com/numary/ledger/pkg/ledger"
"go.uber.org/fx"
Expand All @@ -18,6 +19,7 @@ type Config struct {
Url string
LockDuration time.Duration
LockRetry time.Duration
TLSConfig *tls.Config
}

func Module(cfg Config) fx.Option {
Expand All @@ -33,6 +35,7 @@ func Module(cfg Config) fx.Option {
if err != nil {
return nil, err
}
options.TLSConfig = cfg.TLSConfig
return redis.NewClient(options), nil
}),
ledger.ProvideResolverOption(func(redisClient Client) ledger.ResolverOption {
Expand Down

0 comments on commit a2ff841

Please sign in to comment.