1 parent
519e2b4a5f
commit
d7837de561
45 files changed
+1588
-109
No files matched your search
@@ -0,0 +1,173 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
"git.pcux.cn/xuans/Smail/utils/runtimepath"
|
||||
)
|
||||
|
||||
func TestDeploymentStateRecoversEveryCommitPhase(t *testing.T) {
|
||||
for _, phase := range []TransactionPhase{TransactionPrepared, TransactionSectionsCommitted, TransactionRootCommitted, TransactionVerified} {
|
||||
t.Run(string(phase), func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := InitializeDeployment(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current := validRuntimeConfig()
|
||||
writeRuntimeFixture(t, root, current)
|
||||
candidate := cloneRuntimeConfig(current)
|
||||
setCandidateGeneration(&candidate, 2)
|
||||
candidate.Email.Policy.MaxMessageBytes = 9 << 20
|
||||
validation := RuntimeValidationOptions{HasSuperAdmin: true, Production: true}
|
||||
store := NewTransactionStore(root, validation).WithHooks(TransactionHooks{BeforePhase: func(actual TransactionPhase) error {
|
||||
if actual == phase {
|
||||
return errors.New("模拟进程中断")
|
||||
}
|
||||
return nil
|
||||
}})
|
||||
if err := store.Commit(current, candidate); err == nil {
|
||||
t.Fatal("未注入中断")
|
||||
}
|
||||
if err := NewTransactionStore(root, validation).Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := NewConfigLoader(root, ConfigLoaderOptions{Validation: validation}).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := uint64(1)
|
||||
if phase == TransactionRootCommitted || phase == TransactionVerified {
|
||||
want = 2
|
||||
}
|
||||
if loaded.Generation != want {
|
||||
t.Fatalf("恢复代次错误:%d", loaded.Generation)
|
||||
}
|
||||
if err := store.Recover(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
entries, _ := os.ReadDir(filepath.Join(root, "config"))
|
||||
if len(entries) != 5 {
|
||||
t.Fatal("config 生成了额外文件")
|
||||
}
|
||||
entries, _ = os.ReadDir(filepath.Join(root, "data"))
|
||||
for _, entry := range entries {
|
||||
if entry.Name() != "smail-state.db" {
|
||||
t.Fatalf("data 中产生额外文件:%s", entry.Name())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeploymentStateImportsHistoryWithoutChangingSecret(t *testing.T) {
|
||||
source, target := t.TempDir(), t.TempDir()
|
||||
current := validMySQLRuntimeConfig(t, source)
|
||||
writeRuntimeFixture(t, source, current)
|
||||
validation := RuntimeValidationOptions{HasSuperAdmin: true}
|
||||
old := NewTransactionStore(source, validation)
|
||||
backup, err := old.CreateBackup(current)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, _ := os.ReadFile(filepath.Join(backup, "files", "config.json"))
|
||||
if err := InitializeDeployment(target); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := runtimepath.SecureDirectory(target, "ssl"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
key, err := os.ReadFile(filepath.Join(source, "data", "credential.key"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(target, "ssl", "credential.key"), key, 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
writeRuntimeFixture(t, target, current)
|
||||
if err := ImportDeploymentHistory(source, target, validation); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := NewTransactionStore(target, validation)
|
||||
loaded, err := store.LoadBackup(current.Generation)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
vault, err := credential.NewVault(target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := OpenStoredSecret(vault, loaded.Root.Database.Password); err != nil {
|
||||
t.Fatal("迁移后密文无法解密")
|
||||
}
|
||||
items, err := store.stateBackups()
|
||||
if err != nil || len(items) != 1 {
|
||||
t.Fatalf("历史导入失败:%v", err)
|
||||
}
|
||||
if !bytes.Equal(before, items[0].Original["config.json"]) {
|
||||
t.Fatal("历史原始内容被改写")
|
||||
}
|
||||
for index := 0; index < 6; index++ {
|
||||
if _, err := store.createBackupAt(current, time.Now().Add(time.Duration(index)*time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
items, err = store.stateBackups()
|
||||
if err != nil || len(items) != 4 {
|
||||
t.Fatalf("历史保留策略错误:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeploymentStateRejectsTamperAndConcurrentWriter(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := InitializeDeployment(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
current := validRuntimeConfig()
|
||||
writeRuntimeFixture(t, root, current)
|
||||
store := NewTransactionStore(root, RuntimeValidationOptions{HasSuperAdmin: true})
|
||||
if _, err := store.CreateBackup(current); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unlock, err := store.lockState()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if another, err := store.lockState(); err == nil {
|
||||
another()
|
||||
t.Fatal("并行写入没有被拒绝")
|
||||
}
|
||||
unlock()
|
||||
db, err := store.openState()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = db.Exec("UPDATE config_records SET payload=? WHERE kind='backup'", []byte("{}"))
|
||||
db.Close()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := store.LoadBackup(1); !errors.Is(err, ErrConfigDigestMismatch) {
|
||||
t.Fatalf("篡改未被拒绝:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeploymentManagedReferenceUsesArgumentRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := InitializeDeployment(root); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
private, certificate := testManagedCertificate(t, "example.com", time.Now().Add(-time.Hour), time.Now().Add(time.Hour), 2048)
|
||||
ref, err := importManagedSSLAtRoot(root, ManagedSSLModeUpload, private, certificate, nil, []string{"example.com"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ref.CertificateRef != "ssl/managed/upload/current/public.crt" {
|
||||
t.Fatalf("引用错误:%s", ref.CertificateRef)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
"""停服后迁移隔离模拟部署,保留源文件和完整备份,不清空业务数据库。"""
|
||||
|
||||
import argparse
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import sqlite3
|
||||
import subprocess
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
TARGET = ROOT / "yunx-test"
|
||||
CONFIG_NAMES = {f"{name}.json" for name in ("config", "smtp", "email", "ssl", "plugins")}
|
||||
|
||||
|
||||
def safe_tree(directory):
|
||||
for current, directories, files in os.walk(directory, followlinks=False):
|
||||
for name in directories + files:
|
||||
item = Path(current) / name
|
||||
if item.is_symlink() or item.is_junction():
|
||||
raise RuntimeError("迁移集合包含链接,已停止")
|
||||
if directory.is_symlink() or directory.is_junction():
|
||||
raise RuntimeError("迁移目录不能是链接")
|
||||
|
||||
|
||||
def digest(filename):
|
||||
return hashlib.file_digest(filename.open("rb"), "sha256").hexdigest()
|
||||
|
||||
|
||||
def copy_verified(source, target):
|
||||
if target.exists():
|
||||
if target.is_file() and digest(source) == digest(target):
|
||||
return
|
||||
raise RuntimeError(f"目标冲突,拒绝覆盖:{target.relative_to(ROOT)}")
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(source, target)
|
||||
if digest(source) != digest(target):
|
||||
raise RuntimeError("文件复制摘要不一致")
|
||||
|
||||
|
||||
def copy_tree(source, target):
|
||||
safe_tree(source)
|
||||
for item in source.rglob("*"):
|
||||
if item.is_file():
|
||||
copy_verified(item, target / item.relative_to(source))
|
||||
|
||||
|
||||
def sqlite_snapshot(source, target):
|
||||
with sqlite3.connect(source.as_uri() + "?mode=ro", uri=True) as src:
|
||||
if src.execute("PRAGMA integrity_check").fetchone()[0] != "ok":
|
||||
raise RuntimeError("源 SQLite 完整性检查未通过")
|
||||
with sqlite3.connect(target) as dst:
|
||||
src.backup(dst)
|
||||
if dst.execute("PRAGMA integrity_check").fetchone()[0] != "ok":
|
||||
raise RuntimeError("目标 SQLite 完整性检查未通过")
|
||||
tables = [row[0] for row in src.execute("SELECT name FROM sqlite_master WHERE type='table'")]
|
||||
for name in tables:
|
||||
quoted = '"' + name.replace('"', '""') + '"'
|
||||
if src.execute(f"SELECT count(*) FROM {quoted}").fetchone() != dst.execute(f"SELECT count(*) FROM {quoted}").fetchone():
|
||||
raise RuntimeError("SQLite 表记录数量不一致")
|
||||
return len(tables)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--apply", action="store_true")
|
||||
args = parser.parse_args()
|
||||
safe_tree(TARGET)
|
||||
for name in ("config", "data", "ssl"):
|
||||
safe_tree(ROOT / name)
|
||||
if {item.name for item in (ROOT / "config").iterdir()} != CONFIG_NAMES:
|
||||
raise RuntimeError("源配置目录不止五个指定配置")
|
||||
if any((ROOT / "data" / "transaction").iterdir()):
|
||||
raise RuntimeError("源目录存在未恢复配置事务,不能迁移")
|
||||
if any((TARGET / name).exists() for name in ("config", "ssl", "data")):
|
||||
raise RuntimeError("目标已有运行数据,拒绝重复迁移")
|
||||
if not args.apply:
|
||||
print("迁移集合检查通过。先停止前后端,再使用 --apply。")
|
||||
return
|
||||
# 再次核对端口,避免复制期间仍有配置写入。
|
||||
result = subprocess.run([
|
||||
"powershell.exe", "-NoProfile", "-Command",
|
||||
"if (Get-NetTCPConnection -State Listen -LocalPort 5201 -ErrorAction SilentlyContinue) { exit 1 }"
|
||||
], capture_output=True)
|
||||
if result.returncode:
|
||||
raise RuntimeError("5201 后端仍在运行,拒绝迁移")
|
||||
stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
backup = ROOT / "test-log" / f"deployment-migration-{stamp}"
|
||||
backup.mkdir(parents=True, exist_ok=False)
|
||||
for name in ("config", "data", "ssl"):
|
||||
copy_tree(ROOT / name, backup / name)
|
||||
for name in ("config", "ssl", "data", "log"):
|
||||
(TARGET / name).mkdir(parents=True, exist_ok=True)
|
||||
copy_tree(backup / "config", TARGET / "config")
|
||||
copy_tree(backup / "data" / "ssl", TARGET / "ssl")
|
||||
copy_tree(backup / "ssl", TARGET / "ssl")
|
||||
copy_tree(backup / "data" / "dkim", TARGET / "ssl" / "dkim")
|
||||
for name in ("credential.key", "external-email-encryption.key", "external-email-fingerprint.key"):
|
||||
copy_verified(backup / "data" / name, TARGET / "ssl" / name)
|
||||
copy_verified(backup / "data" / "config-audit-v2.jsonl", TARGET / "log" / "config-audit-v2.jsonl")
|
||||
databases = {}
|
||||
for source in (ROOT / "data").glob("*.db"):
|
||||
databases[source.name] = {"tables": sqlite_snapshot(source, TARGET / "data" / source.name), "integrity": "ok"}
|
||||
env = {**os.environ, "SMAIL_ROOT": str(TARGET)}
|
||||
result = subprocess.run([
|
||||
str(ROOT / "out" / "backend" / "smail-config.exe"), "--root", str(TARGET),
|
||||
"--deployment-history", str(ROOT), "--apply"
|
||||
], env=env, capture_output=True)
|
||||
(backup / "import.stdout.log").write_bytes(result.stdout)
|
||||
(backup / "import.stderr.log").write_bytes(result.stderr)
|
||||
if result.returncode:
|
||||
raise RuntimeError("配置历史导入失败;保留原目录、目标和备份供排查,不启动服务")
|
||||
report = {"source": str(ROOT), "target": str(TARGET), "backup": str(backup), "databases": databases,
|
||||
"files": {str(item.relative_to(TARGET)): digest(item) for item in TARGET.rglob("*") if item.is_file()}}
|
||||
(backup / "receipt.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
print(f"迁移完成;一致性验收记录:{backup.relative_to(ROOT)}/receipt.json")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -42,15 +42,15 @@ test('前端入口按需加载 Element Plus,并延迟设置页和写信编辑
|
||||
|
||||
test('开发启动必须固定运行配置根目录并集中保存日志', async () => {
|
||||
const content = await read('deploy/start-dev.ps1')
|
||||
assert.match(content, /\$env:SMAIL_ROOT = \$root\r?\n/)
|
||||
assert.match(content, /\$env:SMAIL_ROOT = \$runtime\r?\n/)
|
||||
assert.match(content, /\$legacyConfig = Join-Path \$root 'backend\\config'/)
|
||||
assert.match(content, /Test-Path -LiteralPath \(Join-Path \$legacyConfig \$name\)/)
|
||||
assert.match(content, /\$workingDirectory = Join-Path \$root 'out'/)
|
||||
assert.match(content, /\$workingDirectory = \$runtime/)
|
||||
for (const name of ['spam_block.exe', 'pmail_telegram_push.exe', 'wechat_push.exe']) assert.ok(content.includes(name))
|
||||
assert.match(content, /out\\backend\\smail-dev\.exe/)
|
||||
assert.match(content, /test-log\\runtime/)
|
||||
assert.match(content, /Join-Path \$runtime 'smail\.exe'/)
|
||||
assert.match(content, /Join-Path \$runtime 'log'/)
|
||||
assert.match(content, /-WindowStyle Hidden/)
|
||||
assert.match(content, /\[switch\]\$SkipBuild/)
|
||||
assert.doesNotMatch(content, /SkipBuild/)
|
||||
assert.match(content, /--dev-disable-admin-reset/)
|
||||
assert.match(content, /SMAIL_HTTP_PORT_OVERRIDE = '5201'/)
|
||||
assert.match(content, /SMAIL_HTTP_TLS_MODE_OVERRIDE = 'proxy'/)
|
||||
@@ -59,7 +59,7 @@ test('开发启动必须固定运行配置根目录并集中保存日志', async
|
||||
assert.match(await read('frontend/.env.development'), /VITE_DEV_BACKEND_URL=http:\/\/127\.0\.0\.1:5201/)
|
||||
assert.match(await read('frontend/vite.config.js'), /port: 443/)
|
||||
assert.match(await read('frontend/vite.config.js'), /smail-development-http-redirect/)
|
||||
assert.match(content, /默认先编译当前工作区/)
|
||||
assert.match(content, /deploy-runtime\.ps1/)
|
||||
assert.ok(content.charCodeAt(0) === 0xfeff, 'Windows PowerShell 5 脚本应有 UTF-8 BOM')
|
||||
})
|
||||
|
||||
@@ -74,8 +74,8 @@ test('六个平台默认包含三个插件,构建和发布名称与后端目
|
||||
assert.ok(catalog.includes(`"${path.basename(item.output)}"`))
|
||||
}
|
||||
const files = packagePlan(target)
|
||||
assert.equal(files.length, 6)
|
||||
assert.equal(files.filter(file => path.basename(path.dirname(file.target)) === 'plugins').length, 3)
|
||||
assert.equal(files.length, 2)
|
||||
assert.equal(files.filter(file => path.basename(path.dirname(file.target)) === 'plugins').length, 0)
|
||||
assert.ok(files.every(file => file.target.startsWith(path.join(root, 'out', 'package', target))))
|
||||
}
|
||||
assert.ok(buildPlan({ development: true }).every(item => path.dirname(item.output) === path.join(root, 'out', 'plugins')))
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { mkdtemp, mkdir, readFile, writeFile } from 'node:fs/promises'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import test from 'node:test'
|
||||
import { readDevelopmentRuntime, resolveRuntimeFile, runtimeRoot, sourceRoot } from '../../deploy/runtime-config.mjs'
|
||||
import { fileDigest, sourceFingerprint } from '../../deploy/build-state.mjs'
|
||||
|
||||
test('开发配置只从模拟部署目录读取且不返回凭据字段', async () => {
|
||||
const root = await mkdtemp(path.join(os.tmpdir(), 'smail-runtime-config-'))
|
||||
await mkdir(path.join(root, 'config'))
|
||||
await mkdir(path.join(root, 'ssl'))
|
||||
await writeFile(path.join(root, 'config/config.json'), JSON.stringify({
|
||||
generation: 9, domains: { web: 'mail.example.test' }, database: { password: 'not-exported' }
|
||||
}))
|
||||
await writeFile(path.join(root, 'config/ssl.json'), JSON.stringify({
|
||||
tls: { certificateRef: 'ssl/public.crt', privateKeyRef: 'ssl/private.key' }
|
||||
}))
|
||||
await writeFile(path.join(root, 'ssl/public.crt'), 'certificate')
|
||||
await writeFile(path.join(root, 'ssl/private.key'), 'key')
|
||||
const runtime = readDevelopmentRuntime(root)
|
||||
assert.equal(runtime.origin, 'https://mail.example.test')
|
||||
assert.equal(runtime.generation, 9)
|
||||
assert.equal(runtime.privateKeyPath, path.join(root, 'ssl/private.key'))
|
||||
assert.equal(JSON.stringify(runtime).includes('not-exported'), false)
|
||||
assert.equal(runtimeRoot, path.join(sourceRoot, 'yunx-test'))
|
||||
assert.throws(() => resolveRuntimeFile(root, '../outside.key'))
|
||||
assert.throws(() => resolveRuntimeFile(root, path.join(root, 'ssl/private.key')))
|
||||
})
|
||||
|
||||
test('源码指纹检测编辑且忽略编译嵌入目录', async () => {
|
||||
const root = await mkdtemp(path.join(os.tmpdir(), 'smail-build-state-'))
|
||||
for (const name of ['backend', 'frontend', 'plugins/pmail_telegram_push', 'deploy']) {
|
||||
await mkdir(path.join(root, name), { recursive: true })
|
||||
}
|
||||
await writeFile(path.join(root, 'backend/main.go'), 'package main')
|
||||
const before = await sourceFingerprint(root)
|
||||
await mkdir(path.join(root, 'backend/dist'))
|
||||
await writeFile(path.join(root, 'backend/dist/index.html'), 'generated')
|
||||
assert.equal(await sourceFingerprint(root), before)
|
||||
await writeFile(path.join(root, 'backend/main.go'), 'package main\n')
|
||||
assert.notEqual(await sourceFingerprint(root), before)
|
||||
assert.equal((await fileDigest(path.join(root, 'backend/main.go'))).length, 64)
|
||||
await writeFile(path.join(root, 'empty.exe'), '')
|
||||
await assert.rejects(fileDigest(path.join(root, 'empty.exe')))
|
||||
})
|
||||
|
||||
test('部署和启动必须完整构建、验证产物且不覆盖运行配置', async () => {
|
||||
const read = name => readFile(path.join(sourceRoot, name), 'utf8')
|
||||
const deploy = await read('deploy/deploy-runtime.ps1')
|
||||
const start = await read('deploy/start-dev.ps1')
|
||||
assert.ok(deploy.indexOf("'build.ps1'") < deploy.indexOf('Copy-Item'))
|
||||
assert.match(deploy, /verify --deployed/)
|
||||
assert.match(deploy, /deployment-history/)
|
||||
assert.doesNotMatch(deploy, /Copy-Item[^\n]*(config|data|ssl)/)
|
||||
assert.doesNotMatch(start, /SkipBuild|SkipFrontend/)
|
||||
assert.match(start, /\$env:SMAIL_ROOT = \$runtime/)
|
||||
assert.match(start, /\$workingDirectory = \$runtime/)
|
||||
assert.match(start, /build-state\.mjs'\) verify --deployed/)
|
||||
assert.match(await read('.gitignore'), /^\/yunx-test\/$/m)
|
||||
})
|
||||
@@ -42,6 +42,7 @@ if (suite !== 'frontend' && result === 0) {
|
||||
const args = extra[0] === '--' ? extra.slice(1) : extra
|
||||
// 独立回归源码集中放在 test/backend,通过 overlay 保留同包测试语义。
|
||||
const replacements = {
|
||||
'backend/config/deployment_state_test.go': 'test/backend/deployment_state_test.go',
|
||||
'backend/utils/runtimepath/layout_test.go': 'test/backend/runtime_layout_test.go',
|
||||
'backend/config/data_paths_test.go': 'test/backend/data_paths_test.go',
|
||||
'backend/config/ssl_custom_regression_test.go': 'test/backend/ssl_custom_test.go',
|
||||
|
||||
Reference in new issue
Block a user