b32755b35f
- scripts/backup_layout.sh:定义 $KB_BACKUP_ROOT/{target-kb-snapshots,rds-logical,es-snapshots,logs}
与 /etc/knowledge-governance/backup.env,任意主机跑同一份(测试云主机 → 未来生产云主机)
- scripts/es_kb_export.py:只读 scroll 导出目标库 ES 文档(gzip JSONL,_id/_source,可按 _id 重建)
- scripts/target_kb_snapshot.sh:留档改为 PG 行 + ES 文档两层;PULL_ES 默认 0(692 MB / 家用上行 19m40s,
留在目标主机,清单记 location=cloud:<host>:<path>);挂载源与落点统一走 KB_BACKUP_ROOT
- scripts/logical-backup.sh:自管整库逻辑备份(pg_dump -Fd -j4 -Z6,按天轮转)
- internal/releasesnapshot:门禁要求 PG 与 ES 两层齐备(ES 计数 >0);云侧留档核声明与哈希、
声明本地却缺失则 fail-closed;新增 3 条用例(共 10)
- cmd/guardcheck:用真实产物复核门禁判断
- docs:PRODUCTION_BACKUP_RECOVERY.md 重写(契约/RDS 控制台/恢复路径/边界);P6 同步留档语义
245 lines
9.2 KiB
Go
245 lines
9.2 KiB
Go
package releasesnapshot
|
||
|
||
import (
|
||
"crypto/sha256"
|
||
"encoding/hex"
|
||
"encoding/json"
|
||
"errors"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
func writeSnapshot(t *testing.T, root, name, createdAt string, kbIDs []string, wikiContent string, hashOverride string) string {
|
||
t.Helper()
|
||
directory := filepath.Join(root, name)
|
||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if wikiContent != "" {
|
||
if err := os.WriteFile(filepath.Join(directory, "wiki_pages.jsonl"), []byte(wikiContent), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
digest := sha256.Sum256([]byte(wikiContent))
|
||
sha := hex.EncodeToString(digest[:])
|
||
if hashOverride != "" {
|
||
sha = hashOverride
|
||
}
|
||
files := []map[string]any{}
|
||
if wikiContent != "" {
|
||
files = append(files, map[string]any{
|
||
"file": "wiki_pages.jsonl", "rows": 1, "bytes": len(wikiContent), "sha256": sha, "location": "local",
|
||
})
|
||
}
|
||
esContent := "{\"_id\":\"chunk-1\",\"_source\":{}}\n"
|
||
if err := os.WriteFile(filepath.Join(directory, "es_documents.jsonl.gz"), []byte(esContent), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
esDigest := sha256.Sum256([]byte(esContent))
|
||
files = append(files, map[string]any{
|
||
"file": "es_documents.jsonl.gz", "bytes": len(esContent),
|
||
"sha256": hex.EncodeToString(esDigest[:]), "location": "local",
|
||
})
|
||
counts := map[string]int{}
|
||
for _, kb := range kbIDs {
|
||
counts[kb] = 1
|
||
}
|
||
manifestPayload, err := json.Marshal(map[string]any{
|
||
"kind": manifestKind, "created_at": createdAt, "knowledge_base_ids": kbIDs, "files": files,
|
||
"es_documents_file": "es_documents.jsonl.gz", "es_document_counts": counts,
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(directory, "MANIFEST.json"), manifestPayload, 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return directory
|
||
}
|
||
|
||
func fixedNow() func() time.Time {
|
||
return func() time.Time { return time.Date(2026, time.October, 9, 3, 0, 0, 0, time.UTC) }
|
||
}
|
||
|
||
func TestGuardAcceptsFreshSnapshotCoveringTargetKnowledgeBase(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-a", "kb-b"}, "{}\n", "")
|
||
guard := Guard{Root: root, MaxAge: 24 * time.Hour, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); err != nil {
|
||
t.Fatalf("fresh snapshot must pass: %v", err)
|
||
}
|
||
}
|
||
|
||
// 过期快照不算数:门禁的语义是"发布前刚留过档"。
|
||
func TestGuardRejectsExpiredSnapshot(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261006T025017Z-0b217e8a", "20261006T025017Z", []string{"kb-a"}, "{}\n", "")
|
||
guard := Guard{Root: root, MaxAge: 24 * time.Hour, Now: fixedNow(), VerifyLocalFiles: true}
|
||
err := guard.Check("kb-a")
|
||
if !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("expired snapshot must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 不覆盖目标库的快照不能用(另一个库的快照救不了这个库)。
|
||
func TestGuardRejectsSnapshotOfAnotherKnowledgeBase(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-other"}, "{}\n", "")
|
||
guard := Guard{Root: root, Now: fixedNow()}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("snapshot of another base must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 关键文件哈希对不上 = 这份快照不可恢复,必须拒绝。
|
||
func TestGuardRejectsTamperedSnapshot(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-a"}, "{}\n",
|
||
strings.Repeat("0", 64))
|
||
guard := Guard{Root: root, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("tampered snapshot must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 没有 wiki_pages.jsonl 的快照不算"现场留档"。
|
||
func TestGuardRejectsSnapshotWithoutWikiPages(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-a"}, "", "")
|
||
guard := Guard{Root: root, Now: fixedNow()}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("snapshot without wiki pages must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 根目录未配置(门禁未接线)必须拒绝,而不是放行。
|
||
func TestGuardFailsClosedWithoutRoot(t *testing.T) {
|
||
guard := Guard{Root: "", Now: fixedNow()}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("unconfigured guard must fail closed, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 半成品目录(清单写了一半/缺失)要被跳过,最新那份可用即可。
|
||
func TestGuardSkipsHalfWrittenSnapshotAndPicksNewestValid(t *testing.T) {
|
||
root := t.TempDir()
|
||
if err := os.MkdirAll(filepath.Join(root, "20261009T030000Z-0b217e8a"), 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-a"}, "{}\n", "")
|
||
guard := Guard{Root: root, MaxAge: 24 * time.Hour, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); err != nil {
|
||
t.Fatalf("valid snapshot behind a half-written dir must still pass: %v", err)
|
||
}
|
||
}
|
||
|
||
// 只有 PG 行的快照不算完整留档:恢复后检索不到东西,必须拒绝。
|
||
func TestGuardRejectsSnapshotWithoutESDocuments(t *testing.T) {
|
||
root := t.TempDir()
|
||
directory := filepath.Join(root, "20261009T025017Z-0b217e8a")
|
||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
wiki := "{}\n"
|
||
if err := os.WriteFile(filepath.Join(directory, "wiki_pages.jsonl"), []byte(wiki), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
digest := sha256.Sum256([]byte(wiki))
|
||
manifestPayload, err := json.Marshal(map[string]any{
|
||
"kind": manifestKind, "created_at": "20261009T025017Z", "knowledge_base_ids": []string{"kb-a"},
|
||
"files": []map[string]any{{"file": "wiki_pages.jsonl", "sha256": hex.EncodeToString(digest[:]), "location": "local"}},
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(directory, "MANIFEST.json"), manifestPayload, 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
guard := Guard{Root: root, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("snapshot without ES layer must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// ES 层存在但目标库文档数为 0(导出失败/库写错)也必须拒绝。
|
||
func TestGuardRejectsSnapshotWithZeroESDocumentsForTarget(t *testing.T) {
|
||
root := t.TempDir()
|
||
writeSnapshot(t, root, "20261009T025017Z-0b217e8a", "20261009T025017Z", []string{"kb-a"}, "{}\n", "")
|
||
// 把目标库的 ES 计数改成 0
|
||
path := filepath.Join(root, "20261009T025017Z-0b217e8a", "MANIFEST.json")
|
||
payload, err := os.ReadFile(path)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
var parsed map[string]any
|
||
if err := json.Unmarshal(payload, &parsed); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
parsed["es_document_counts"] = map[string]int{"kb-a": 0}
|
||
rewritten, err := json.Marshal(parsed)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(path, rewritten, 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
guard := Guard{Root: root, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("zero ES documents for the target must be refused, got %v", err)
|
||
}
|
||
}
|
||
|
||
// 云侧留档(家用带宽不拉大文件):清单声明 + 哈希字符串即放行;声明本地却读不到 → 拒绝。
|
||
func TestGuardAcceptsCloudLocatedESArtifacts(t *testing.T) {
|
||
root := t.TempDir()
|
||
directory := filepath.Join(root, "20261009T040000Z-0b217e8a")
|
||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
wiki := "{}\n"
|
||
if err := os.WriteFile(filepath.Join(directory, "wiki_pages.jsonl"), []byte(wiki), 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
digest := sha256.Sum256([]byte(wiki))
|
||
manifestPayload, err := json.Marshal(map[string]any{
|
||
"kind": manifestKind, "created_at": "20261009T040000Z", "knowledge_base_ids": []string{"kb-a"},
|
||
"es_documents_file": "es_documents.jsonl.gz", "es_document_counts": map[string]int{"kb-a": 61613},
|
||
"files": []map[string]any{
|
||
{"file": "wiki_pages.jsonl", "sha256": hex.EncodeToString(digest[:]), "location": "local"},
|
||
{"file": "es_documents.jsonl.gz", "sha256": "deadbeef", "bytes": 692023258,
|
||
"location": "cloud:weknora-test:/srv/kb-backups/target-kb-snapshots/20261009T040000Z-0b217e8a/es_documents.jsonl.gz"},
|
||
},
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(directory, "MANIFEST.json"), manifestPayload, 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
guard := Guard{Root: root, Now: fixedNow(), VerifyLocalFiles: true}
|
||
if err := guard.Check("kb-a"); err != nil {
|
||
t.Fatalf("cloud-located ES artifact must be accepted, got %v", err)
|
||
}
|
||
|
||
// 同一份清单改成"声明为本地",文件却不在 → 必须拒绝(fail-closed)
|
||
var parsed map[string]any
|
||
if err := json.Unmarshal(manifestPayload, &parsed); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
files := parsed["files"].([]any)
|
||
files[1].(map[string]any)["location"] = "local"
|
||
rewritten, err := json.Marshal(parsed)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := os.WriteFile(filepath.Join(directory, "MANIFEST.json"), rewritten, 0o644); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := guard.Check("kb-a"); !errors.Is(err, ErrNoFreshSnapshot) {
|
||
t.Fatalf("local-declared but missing ES artifact must be refused, got %v", err)
|
||
}
|
||
}
|