Kaynağa Gözat

support REDIS_USE_SSL config parameter (#21)

* support REDIS_USE_SSL config parameter

* fix tests

* add tests
Masashi Tomooka 6 ay önce
ebeveyn
işleme
c6c882e86a

+ 1 - 1
internal/cluster/clutser_test.go

@@ -11,7 +11,7 @@ import (
 )
 
 func createSimulationCluster(nums int) ([]*Cluster, error) {
-	err := cache.InitRedisClient("0.0.0.0:6379", "difyai123456")
+	err := cache.InitRedisClient("0.0.0.0:6379", "difyai123456", false)
 	if err != nil {
 		return nil, err
 	}

+ 3 - 3
internal/core/persistence/persistence_test.go

@@ -12,7 +12,7 @@ import (
 )
 
 func TestPersistenceStoreAndLoad(t *testing.T) {
-	err := cache.InitRedisClient("localhost:6379", "difyai123456")
+	err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
 	if err != nil {
 		t.Fatalf("Failed to init redis client: %v", err)
 	}
@@ -72,7 +72,7 @@ func TestPersistenceStoreAndLoad(t *testing.T) {
 }
 
 func TestPersistenceSaveAndLoadWithLongKey(t *testing.T) {
-	err := cache.InitRedisClient("localhost:6379", "difyai123456")
+	err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
 	if err != nil {
 		t.Fatalf("Failed to init redis client: %v", err)
 	}
@@ -100,7 +100,7 @@ func TestPersistenceSaveAndLoadWithLongKey(t *testing.T) {
 }
 
 func TestPersistenceDelete(t *testing.T) {
-	err := cache.InitRedisClient("localhost:6379", "difyai123456")
+	err := cache.InitRedisClient("localhost:6379", "difyai123456", false)
 	if err != nil {
 		t.Fatalf("Failed to init redis client: %v", err)
 	}

+ 1 - 1
internal/core/plugin_manager/debugging_runtime/connection_key_test.go

@@ -8,7 +8,7 @@ import (
 )
 
 func TestConnectionKey(t *testing.T) {
-	err := cache.InitRedisClient("0.0.0.0:6379", "difyai123456")
+	err := cache.InitRedisClient("0.0.0.0:6379", "difyai123456", false)
 	if err != nil {
 		t.Errorf("init redis client failed: %v", err)
 		return

+ 2 - 2
internal/core/plugin_manager/debugging_runtime/server_test.go

@@ -86,7 +86,7 @@ func TestLaunchAndClosePluginServer(t *testing.T) {
 
 // TestAcceptConnection tests the acceptance of the connection
 func TestAcceptConnection(t *testing.T) {
-	if cache.InitRedisClient("0.0.0.0:6379", "difyai123456") != nil {
+	if cache.InitRedisClient("0.0.0.0:6379", "difyai123456", false) != nil {
 		t.Errorf("failed to init redis client")
 		return
 	}
@@ -322,7 +322,7 @@ func TestNoHandleShakeIn10Seconds(t *testing.T) {
 }
 
 func TestIncorrectHandshake(t *testing.T) {
-	if cache.InitRedisClient("0.0.0.0:6379", "difyai123456") != nil {
+	if cache.InitRedisClient("0.0.0.0:6379", "difyai123456", false) != nil {
 		t.Errorf("failed to init redis client")
 		return
 	}

+ 1 - 0
internal/core/plugin_manager/manager.go

@@ -146,6 +146,7 @@ func (p *PluginManager) Launch(configuration *app.Config) {
 	if err := cache.InitRedisClient(
 		fmt.Sprintf("%s:%d", configuration.RedisHost, configuration.RedisPort),
 		configuration.RedisPass,
+		configuration.RedisUseSsl,
 	); err != nil {
 		log.Panic("init redis client failed: %s", err.Error())
 	}

+ 4 - 3
internal/types/app/config.go

@@ -48,9 +48,10 @@ type Config struct {
 	RoutinePoolSize int `envconfig:"ROUTINE_POOL_SIZE" validate:"required"`
 
 	// redis
-	RedisHost string `envconfig:"REDIS_HOST" validate:"required"`
-	RedisPort uint16 `envconfig:"REDIS_PORT" validate:"required"`
-	RedisPass string `envconfig:"REDIS_PASSWORD"`
+	RedisHost   string `envconfig:"REDIS_HOST" validate:"required"`
+	RedisPort   uint16 `envconfig:"REDIS_PORT" validate:"required"`
+	RedisPass   string `envconfig:"REDIS_PASSWORD"`
+	RedisUseSsl bool   `envconfig:"REDIS_USE_SSL"`
 
 	// database
 	DBUsername string `envconfig:"DB_USERNAME" validate:"required"`

+ 13 - 3
internal/utils/cache/redis.go

@@ -2,6 +2,7 @@ package cache
 
 import (
 	"context"
+	"crypto/tls"
 	"errors"
 	"strings"
 	"time"
@@ -18,11 +19,20 @@ var (
 	ErrNotFound  = errors.New("key not found")
 )
 
-func InitRedisClient(addr, password string) error {
-	client = redis.NewClient(&redis.Options{
+func getRedisOptions(addr, password string, useSsl bool) *redis.Options {
+	opts := &redis.Options{
 		Addr:     addr,
 		Password: password,
-	})
+	}
+	if useSsl {
+		opts.TLSConfig = &tls.Config{}
+	}
+	return opts
+}
+
+func InitRedisClient(addr, password string, useSsl bool) error {
+	opts := getRedisOptions(addr, password, useSsl)
+	client = redis.NewClient(opts)
 
 	if _, err := client.Ping(ctx).Result(); err != nil {
 		return err

+ 2 - 2
internal/utils/cache/redis_auto_type_test.go

@@ -7,7 +7,7 @@ type TestAutoTypeStruct struct {
 }
 
 func TestAutoType(t *testing.T) {
-	if err := InitRedisClient("127.0.0.1:6379", "difyai123456"); err != nil {
+	if err := InitRedisClient("127.0.0.1:6379", "difyai123456", false); err != nil {
 		t.Fatal(err)
 	}
 	defer Close()
@@ -32,7 +32,7 @@ func TestAutoType(t *testing.T) {
 }
 
 func TestAutoTypeWithGetter(t *testing.T) {
-	if err := InitRedisClient("127.0.0.1:6379", "difyai123456"); err != nil {
+	if err := InitRedisClient("127.0.0.1:6379", "difyai123456", false); err != nil {
 		t.Fatal(err)
 	}
 	defer Close()

+ 15 - 1
internal/utils/cache/redis_test.go

@@ -15,7 +15,7 @@ const (
 )
 
 func getRedisConnection() error {
-	return InitRedisClient("0.0.0.0:6379", "difyai123456")
+	return InitRedisClient("0.0.0.0:6379", "difyai123456", false)
 }
 
 func TestRedisConnection(t *testing.T) {
@@ -267,3 +267,17 @@ func TestRedisP2ARedis(t *testing.T) {
 
 	wg.Wait()
 }
+
+func TestGetRedisOptions(t *testing.T) {
+	opts := getRedisOptions("dummy:6379", "password", false)
+	if opts.TLSConfig != nil {
+		t.Errorf("TLSConfig should not be set")
+		return
+	}
+
+	opts = getRedisOptions("dummy:6379", "password", true)
+	if opts.TLSConfig == nil {
+		t.Errorf("TLSConfig should be set")
+		return
+	}
+}