Verify / verify (push) Canceled after 0s
- 将凭据片段列表提升为全局默认列表,新增 DefaultSensitiveList、NewSensitiveList、Extend 与 SafeWith,支持整体替换或在默认列表之上扩展 - 新增 SetSensitiveDetection 与 SensitiveDetectionEnabled,缺省开启敏感检测,关闭后日志副本不再按名称隐藏凭据字段 - 显式敏感列表、访问日志固定凭据头与 RedactKeys、SQL config_value 与 Redis 认证命令等固定策略不受开关影响 - 同步接入使用指南的能力清单与使用说明
112 lines
3.7 KiB
Go
112 lines
3.7 KiB
Go
package commandlog
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/internal/testutil/redisserver"
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/logger/storagevalue"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap/zaptest/observer"
|
|
)
|
|
|
|
// TestNativePipelineObservationPreservesData 验证 Pipeline 逐条对应记录,日志脱敏不修改执行参数和返回正文。
|
|
func TestNativePipelineObservationPreservesData(t *testing.T) {
|
|
var (
|
|
client = redisserver.New(t, 3)
|
|
core, logs = observer.New(zap.InfoLevel)
|
|
ctx = context.Background()
|
|
value = `{"nested":{"token":"private-token"},"data":"kept"}`
|
|
)
|
|
client.AddHook(New(zap.New(core), "cache"))
|
|
var (
|
|
pipeline = client.Pipeline()
|
|
)
|
|
pipeline.Set(ctx, "safe-key", value, 0)
|
|
var (
|
|
get = pipeline.Get(ctx, "safe-key")
|
|
missing = pipeline.Get(ctx, "missing-key")
|
|
)
|
|
if _, err := pipeline.Exec(ctx); !errors.Is(err, redis.Nil) {
|
|
t.Fatalf("pipeline error changed: %v", err)
|
|
}
|
|
if get.Val() != value || !errors.Is(missing.Err(), redis.Nil) {
|
|
t.Fatal("logging changed actual command result")
|
|
}
|
|
var (
|
|
commands []observer.LoggedEntry
|
|
)
|
|
for _, entry := range logs.All() {
|
|
if name := entry.ContextMap()["command"]; name == "set" || name == "get" {
|
|
commands = append(commands, entry)
|
|
}
|
|
}
|
|
if len(commands) != 3 {
|
|
t.Fatalf("pipeline records=%d, want 3", len(commands))
|
|
}
|
|
for _, entry := range logs.All() {
|
|
body, _ := json.Marshal(entry.ContextMap())
|
|
if strings.Contains(string(body), "private-token") {
|
|
t.Fatal("credential leaked into execution log")
|
|
}
|
|
}
|
|
if commands[2].ContextMap()["result"] != "miss" {
|
|
t.Fatal("Redis nil observation changed")
|
|
}
|
|
}
|
|
|
|
// TestHookObservesReturnedError 验证官方回调返回前尚未 SetErr 时不会误记成功。
|
|
func TestHookObservesReturnedError(t *testing.T) {
|
|
var (
|
|
core, logs = observer.New(zap.InfoLevel)
|
|
hook = New(zap.New(core), "cache")
|
|
cmd = redis.NewStringCmd(context.Background(), "get", "safe-key")
|
|
calls int
|
|
process = hook.ProcessHook(func(context.Context, redis.Cmder) error { calls++; return context.Canceled })
|
|
)
|
|
if err := process(context.Background(), cmd); !errors.Is(err, context.Canceled) {
|
|
t.Fatalf("error changed: %v", err)
|
|
}
|
|
if calls != 1 || cmd.Err() != nil || logs.Len() != 1 || logs.All()[0].ContextMap()["result"] != "failed" {
|
|
t.Fatalf("hook altered command or observed wrong result: %v", logs.All())
|
|
}
|
|
}
|
|
|
|
// TestDisabledSensitiveDetectionKeepsFixedHiddenCommands 验证关闭敏感检测后普通命令保留原始参数,认证管理命令仍然整组隐藏。
|
|
func TestDisabledSensitiveDetectionKeepsFixedHiddenCommands(t *testing.T) {
|
|
storagevalue.SetSensitiveDetection(false)
|
|
defer storagevalue.SetSensitiveDetection(true)
|
|
var (
|
|
core, logs = observer.New(zap.InfoLevel)
|
|
hook = New(zap.New(core), "cache")
|
|
process = hook.ProcessHook(func(context.Context, redis.Cmder) error { return nil })
|
|
)
|
|
if err := process(context.Background(), redis.NewStringCmd(context.Background(), "set", "user:token", "private-value")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := process(context.Background(), redis.NewStringCmd(context.Background(), "auth", "private-password")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var (
|
|
entries = logs.All()
|
|
)
|
|
if len(entries) != 2 {
|
|
t.Fatalf("entries=%d, want 2", len(entries))
|
|
}
|
|
body, err := json.Marshal(entries[0].ContextMap())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(body), "private-value") {
|
|
t.Fatal("disabled detection still hid ordinary command arguments")
|
|
}
|
|
if entries[1].ContextMap()["args"] != "[REDACTED]" || entries[1].ContextMap()["value"] != "[REDACTED]" {
|
|
t.Fatal("fixed hidden command policy was disabled")
|
|
}
|
|
}
|