feat(redis): support TLS for Redis connections (#1930)
[Feature]: Support TLS for Redis connections
This commit is contained in:
@@ -223,6 +223,14 @@ REDIS_DB=0
|
|||||||
# Redis key的前缀,用于命名空间隔离
|
# Redis key的前缀,用于命名空间隔离
|
||||||
REDIS_PREFIX=stream:
|
REDIS_PREFIX=stream:
|
||||||
|
|
||||||
|
# 是否为 Redis 连接启用 TLS(适用于要求传输层加密的托管 Redis,
|
||||||
|
# 如 AWS ElastiCache in-transit encryption、Azure Cache 等)。默认关闭。
|
||||||
|
# REDIS_USE_TLS=false
|
||||||
|
# 可选:用于证书校验与 SNI 的服务器名(当地址是 IP 时有用)
|
||||||
|
# REDIS_TLS_SERVER_NAME=
|
||||||
|
# 跳过服务器证书校验。不安全,仅用于开发或自签名证书环境,生产禁用。
|
||||||
|
# REDIS_TLS_INSECURE_SKIP_VERIFY=false
|
||||||
|
|
||||||
# 当使用本地存储时,文件保存的基础目录路径
|
# 当使用本地存储时,文件保存的基础目录路径
|
||||||
LOCAL_STORAGE_BASE_DIR=/data/files
|
LOCAL_STORAGE_BASE_DIR=/data/files
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RedisTLSConfig builds a *tls.Config for Redis connections from environment
|
||||||
|
// variables. It returns nil when TLS is disabled, so the result can be assigned
|
||||||
|
// directly to redis.Options.TLSConfig or asynq.RedisClientOpt.TLSConfig (a nil
|
||||||
|
// value keeps the existing plaintext behavior).
|
||||||
|
//
|
||||||
|
// Environment variables:
|
||||||
|
//
|
||||||
|
// REDIS_USE_TLS Enable TLS when "true" (default: disabled).
|
||||||
|
// REDIS_TLS_SERVER_NAME Optional server name for certificate
|
||||||
|
// verification and SNI (useful when the address
|
||||||
|
// is an IP rather than a hostname).
|
||||||
|
// REDIS_TLS_INSECURE_SKIP_VERIFY Skip server certificate verification when
|
||||||
|
// "true". INSECURE — intended for development or
|
||||||
|
// self-signed setups only; do not use in production.
|
||||||
|
//
|
||||||
|
// The server certificate is verified by default; verification is only relaxed
|
||||||
|
// when REDIS_TLS_INSECURE_SKIP_VERIFY is explicitly set to "true".
|
||||||
|
func RedisTLSConfig() *tls.Config {
|
||||||
|
if !envTrue("REDIS_USE_TLS") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
cfg := &tls.Config{MinVersion: tls.VersionTLS12}
|
||||||
|
|
||||||
|
if serverName := strings.TrimSpace(os.Getenv("REDIS_TLS_SERVER_NAME")); serverName != "" {
|
||||||
|
cfg.ServerName = serverName
|
||||||
|
}
|
||||||
|
if envTrue("REDIS_TLS_INSECURE_SKIP_VERIFY") {
|
||||||
|
cfg.InsecureSkipVerify = true
|
||||||
|
}
|
||||||
|
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
// envTrue reports whether the named environment variable is set to "true"
|
||||||
|
// (case-insensitive, surrounding whitespace ignored).
|
||||||
|
func envTrue(name string) bool {
|
||||||
|
return strings.EqualFold(strings.TrimSpace(os.Getenv(name)), "true")
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/tls"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRedisTLSConfig_DisabledByDefault(t *testing.T) {
|
||||||
|
// REDIS_USE_TLS unset.
|
||||||
|
if cfg := RedisTLSConfig(); cfg != nil {
|
||||||
|
t.Fatalf("expected nil tls.Config when REDIS_USE_TLS is unset, got %#v", cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Setenv("REDIS_USE_TLS", "false")
|
||||||
|
if cfg := RedisTLSConfig(); cfg != nil {
|
||||||
|
t.Fatalf("expected nil tls.Config when REDIS_USE_TLS=false, got %#v", cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisTLSConfig_EnabledSecureByDefault(t *testing.T) {
|
||||||
|
t.Setenv("REDIS_USE_TLS", "true")
|
||||||
|
|
||||||
|
cfg := RedisTLSConfig()
|
||||||
|
if cfg == nil {
|
||||||
|
t.Fatal("expected non-nil tls.Config when REDIS_USE_TLS=true")
|
||||||
|
}
|
||||||
|
if cfg.MinVersion != tls.VersionTLS12 {
|
||||||
|
t.Errorf("expected MinVersion TLS 1.2, got %x", cfg.MinVersion)
|
||||||
|
}
|
||||||
|
if cfg.InsecureSkipVerify {
|
||||||
|
t.Error("expected certificate verification to be enabled by default")
|
||||||
|
}
|
||||||
|
if cfg.ServerName != "" {
|
||||||
|
t.Errorf("expected empty ServerName by default, got %q", cfg.ServerName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRedisTLSConfig_Options(t *testing.T) {
|
||||||
|
t.Setenv("REDIS_USE_TLS", "TRUE") // case-insensitive
|
||||||
|
t.Setenv("REDIS_TLS_SERVER_NAME", "redis.example.com")
|
||||||
|
t.Setenv("REDIS_TLS_INSECURE_SKIP_VERIFY", "true")
|
||||||
|
|
||||||
|
cfg := RedisTLSConfig()
|
||||||
|
if cfg == nil {
|
||||||
|
t.Fatal("expected non-nil tls.Config")
|
||||||
|
}
|
||||||
|
if !cfg.InsecureSkipVerify {
|
||||||
|
t.Error("expected InsecureSkipVerify=true when opted in")
|
||||||
|
}
|
||||||
|
if cfg.ServerName != "redis.example.com" {
|
||||||
|
t.Errorf("expected ServerName redis.example.com, got %q", cfg.ServerName)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,7 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/application/service/file"
|
"github.com/Tencent/WeKnora/internal/application/service/file"
|
||||||
memoryService "github.com/Tencent/WeKnora/internal/application/service/memory"
|
memoryService "github.com/Tencent/WeKnora/internal/application/service/memory"
|
||||||
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||||
|
"github.com/Tencent/WeKnora/internal/common"
|
||||||
"github.com/Tencent/WeKnora/internal/config"
|
"github.com/Tencent/WeKnora/internal/config"
|
||||||
"github.com/Tencent/WeKnora/internal/database"
|
"github.com/Tencent/WeKnora/internal/database"
|
||||||
"github.com/Tencent/WeKnora/internal/datasource"
|
"github.com/Tencent/WeKnora/internal/datasource"
|
||||||
@@ -439,10 +440,11 @@ func initRedisClient() (*redis.Client, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: redisAddr,
|
Addr: redisAddr,
|
||||||
Username: os.Getenv("REDIS_USERNAME"),
|
Username: os.Getenv("REDIS_USERNAME"),
|
||||||
Password: os.Getenv("REDIS_PASSWORD"),
|
Password: os.Getenv("REDIS_PASSWORD"),
|
||||||
DB: db,
|
DB: db,
|
||||||
|
TLSConfig: common.RedisTLSConfig(),
|
||||||
})
|
})
|
||||||
|
|
||||||
_, err = client.Ping(context.Background()).Result()
|
_, err = client.Ping(context.Background()).Result()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/application/service"
|
"github.com/Tencent/WeKnora/internal/application/service"
|
||||||
|
"github.com/Tencent/WeKnora/internal/common"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/middleware/asynqdl"
|
"github.com/Tencent/WeKnora/internal/middleware/asynqdl"
|
||||||
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
||||||
@@ -73,6 +74,7 @@ func getAsynqRedisClientOpt() *asynq.RedisClientOpt {
|
|||||||
// WriteTimeout slightly larger to absorb head-of-line stalls.
|
// WriteTimeout slightly larger to absorb head-of-line stalls.
|
||||||
WriteTimeout: time.Duration(timeoutMs*2) * time.Millisecond,
|
WriteTimeout: time.Duration(timeoutMs*2) * time.Millisecond,
|
||||||
DB: db,
|
DB: db,
|
||||||
|
TLSConfig: common.RedisTLSConfig(),
|
||||||
}
|
}
|
||||||
return opt
|
return opt
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/common"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
@@ -22,10 +23,11 @@ func NewRedisStreamManager(redisAddr, redisUsername, redisPassword string,
|
|||||||
redisDB int, prefix string, ttl time.Duration,
|
redisDB int, prefix string, ttl time.Duration,
|
||||||
) (*RedisStreamManager, error) {
|
) (*RedisStreamManager, error) {
|
||||||
client := redis.NewClient(&redis.Options{
|
client := redis.NewClient(&redis.Options{
|
||||||
Addr: redisAddr,
|
Addr: redisAddr,
|
||||||
Username: redisUsername,
|
Username: redisUsername,
|
||||||
Password: redisPassword,
|
Password: redisPassword,
|
||||||
DB: redisDB,
|
DB: redisDB,
|
||||||
|
TLSConfig: common.RedisTLSConfig(),
|
||||||
})
|
})
|
||||||
|
|
||||||
// Verify connection
|
// Verify connection
|
||||||
|
|||||||
Reference in New Issue
Block a user