From 4f2dca0b635c90524d838c28d691ccefa50e3593 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Mon, 6 Jul 2026 23:03:25 +0800 Subject: [PATCH] fix(redis): harden TLS tests and surface TLS config at startup Isolate Redis TLS unit tests from parent-process env vars and log REDIS_USE_TLS settings at boot so misconfiguration is easier to spot. --- .env.example | 1 + internal/common/redis_tls_test.go | 12 +++++++++++- internal/runtime/startup.go | 6 ++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index b68398ea..b8aea97a 100644 --- a/.env.example +++ b/.env.example @@ -229,6 +229,7 @@ REDIS_PREFIX=stream: # 可选:用于证书校验与 SNI 的服务器名(当地址是 IP 时有用) # REDIS_TLS_SERVER_NAME= # 跳过服务器证书校验。不安全,仅用于开发或自签名证书环境,生产禁用。 +# 私有 CA 签发的证书目前无法通过自定义 CA 文件校验,只能临时使用 skip-verify(开发环境)。 # REDIS_TLS_INSECURE_SKIP_VERIFY=false # 当使用本地存储时,文件保存的基础目录路径 diff --git a/internal/common/redis_tls_test.go b/internal/common/redis_tls_test.go index 252405b8..12310122 100644 --- a/internal/common/redis_tls_test.go +++ b/internal/common/redis_tls_test.go @@ -5,8 +5,16 @@ import ( "testing" ) +func resetRedisTLSEnv(t *testing.T) { + t.Helper() + t.Setenv("REDIS_USE_TLS", "") + t.Setenv("REDIS_TLS_SERVER_NAME", "") + t.Setenv("REDIS_TLS_INSECURE_SKIP_VERIFY", "") +} + func TestRedisTLSConfig_DisabledByDefault(t *testing.T) { - // REDIS_USE_TLS unset. + resetRedisTLSEnv(t) + if cfg := RedisTLSConfig(); cfg != nil { t.Fatalf("expected nil tls.Config when REDIS_USE_TLS is unset, got %#v", cfg) } @@ -18,6 +26,7 @@ func TestRedisTLSConfig_DisabledByDefault(t *testing.T) { } func TestRedisTLSConfig_EnabledSecureByDefault(t *testing.T) { + resetRedisTLSEnv(t) t.Setenv("REDIS_USE_TLS", "true") cfg := RedisTLSConfig() @@ -36,6 +45,7 @@ func TestRedisTLSConfig_EnabledSecureByDefault(t *testing.T) { } func TestRedisTLSConfig_Options(t *testing.T) { + resetRedisTLSEnv(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") diff --git a/internal/runtime/startup.go b/internal/runtime/startup.go index f9f3b3bd..dc5da747 100644 --- a/internal/runtime/startup.go +++ b/internal/runtime/startup.go @@ -89,6 +89,8 @@ var startupEnvVars = []envVarSpec{ // Cache / queue {name: "REDIS_ADDR"}, {name: "REDIS_PASSWORD", sensitive: true}, + {name: "REDIS_USE_TLS"}, + {name: "REDIS_TLS_SERVER_NAME"}, // Object storage {name: "STORAGE_TYPE"}, {name: "MINIO_ENDPOINT"}, @@ -137,6 +139,10 @@ func LogStartupEnv(ctx context.Context) { "[startup-env] SYSTEM_AES_KEY is set but %d bytes long; AES-256 requires exactly 32 bytes — encryption is DISABLED", len(k)) } + if strings.EqualFold(strings.TrimSpace(os.Getenv("REDIS_TLS_INSECURE_SKIP_VERIFY")), "true") { + logger.Warn(ctx, + "[startup-env] REDIS_TLS_INSECURE_SKIP_VERIFY=true — Redis TLS certificate verification is DISABLED; do not use in production") + } } func formatEnvValue(s envVarSpec, val string) string {