fix(auth): persist workbench state without cross-device renames
This commit is contained in:
1 parent
b477737b0f
commit
6c2cc526e6
6 files changed
+58
-9
No files matched your search
@@ -22,7 +22,7 @@
|
||||
|
||||
界面复用 [Tabler](https://tabler.io/admin-template) 免费开源版(@tabler/core 1.6.1),CSS/JS 全部在本机提供;MIT 原文见 licenses/Tabler-MIT.txt。青龙已有独立部署准备和 CLI,尚未接通业务,本次不启动它或复制其内部源码。
|
||||
|
||||
工作台账号、群组设置和原 PDF 记录保存在 `%LOCALAPPDATA%\OrderNotificationAutomation\dashboard\state.json`;业务日历、异常、连接与监管草稿保存在同级 `workspace` 目录的 SQLite 数据库中。邮箱、模板版本、原邮件、收信游标和测试发送记录保存在 `mail\mail.sqlite`,密码、原邮件和完整正文附件加密保存。BPM 会话加密保存在 `bpm-sessions`,主密钥经 Windows 当前运行用户 DPAPI 保护。数据留在运行机本地磁盘,不放项目所在的 Z: 共享盘。备份前停止服务并保护整个本地状态目录。工作台登录有效期为 30 天,每日活动自动续期;令牌哈希保存在 `dashboard\auth.sqlite`,服务重启后继续有效,退出登录立即撤销。已保存的 BPM 会话在同一 Windows 运行账号下恢复。
|
||||
工作台账号、群组设置和原 PDF 记录保存在 `%LOCALAPPDATA%\OrderNotificationAutomation\dashboard\state.sqlite`;业务日历、异常、连接与监管草稿保存在同级 `workspace` 目录的 SQLite 数据库中。邮箱、模板版本、原邮件、收信游标和测试发送记录保存在 `mail\mail.sqlite`,密码、原邮件和完整正文附件加密保存。BPM 会话加密保存在 `bpm-sessions`,主密钥经 Windows 当前运行用户 DPAPI 保护。数据留在运行机本地磁盘,不放项目所在的 Z: 共享盘。备份前停止服务并保护整个本地状态目录。旧版 `dashboard\state.json` 会首次导入 SQLite 并原样保留为迁移备份;后续以 `state.sqlite` 为准。工作台登录有效期为 30 天,每日活动自动续期;令牌哈希保存在 `dashboard\auth.sqlite`,服务重启后继续有效,退出登录立即撤销。已保存的 BPM 会话在同一 Windows 运行账号下恢复。
|
||||
|
||||
## 邮箱与邮件模板
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# 首次创建管理员保存失败修复
|
||||
|
||||
日期:2026-10-17。
|
||||
|
||||
现场现象:`POST /api/bootstrap` 在保存账号时失败,日志为同目录临时文件重命名到 `dashboard/state.json` 时出现 EXDEV。随机无业务数据探针在实际状态目录可复现 rename/link 失败,同一进程在 Windows 临时目录正常;实际目录带 Encrypted 属性,不据此认定具体系统加密或过滤驱动为唯一成因。
|
||||
|
||||
工作台状态改存为 `dashboard/state.sqlite` 的单行快照,通过 SQLite 单语句原子更新,启用 FULL 同步和 5 秒忙等待;提交成功后才更新内存。首次启动导入原有 schema 1 JSON,原文件保留不变;已有 SQLite 快照优先。其他业务数据库及 auth.sqlite 路径不变。应用仍要求单进程运行。
|
||||
|
||||
验证:
|
||||
|
||||
- `node --test --test-concurrency=2 tests/dashboard-store.test.cjs tests/dashboard-http.test.cjs tests/dashboard-sessions.test.cjs tests/server.test.cjs`:11 项通过。
|
||||
- 新增 EXDEV 故障注入及 JSON 一次迁移/重启测试;覆盖创建账号、后续保存、密码哈希及旧数据保留。
|
||||
- 实际出错目录下的隔离子目录:创建账号、保存设置、重新加载均成功。
|
||||
- 同一实际目录下的隔离浏览器流程:创建管理员返回 201、工作台进入成功、重新打开 Store 可验证密码。测试账号未加入正式工作台。
|
||||
- 正式本地进程已重启并保持运行;正式管理员继续由用户创建。
|
||||
|
||||
独立只读审查未发现阻塞问题。本次没有修复 BPM 密文文件写入等其他模块的 rename 行为,后续若遇到相同系统错误需单独处理。
|
||||
@@ -22,6 +22,8 @@ Web 默认监听 `0.0.0.0:18766`,PDF 扩展桥接仅监听 `127.0.0.1:18765`
|
||||
|
||||
状态目录:`%LOCALAPPDATA%\OrderNotificationAutomation`,由实际运行账号决定。数据库清单见 [设计要求](design-requirements.md#8-数据保密与部署)。PDF 输出默认在项目 `output/pdf`,若需跨版本保存建议在 config.json 配置独立输出目录。
|
||||
|
||||
工作台账号、设置及 PDF 记录当前保存在 `dashboard/state.sqlite`,登录会话位于 `dashboard/auth.sqlite`。旧版 `dashboard/state.json` 首次导入后原样保留,仅作为迁移时快照,不再持续更新;备份与恢复以 SQLite 为准,不应直接回退使用旧 JSON。
|
||||
|
||||
备份先停止服务,再备份整个状态目录及业务输出目录;SQLite 的 WAL 文件与数据库必须保持一致。备份包含敏感数据,需限制访问及加密保存。密钥由 Windows DPAPI 绑定运行身份,单独复制 SQLite 或加密文件无法保证跨机器恢复;迁移前验证恢复条件,必要时重新登录及录入渠道凭据。
|
||||
|
||||
升级前备份,安装锁定依赖后再启动。Git 仓库不含运行数据库、凭据、Cookie、日志或业务输出,克隆源码不会同步这些数据,也不会恢复已有登录状态。`scripts/preview.cjs` 仅供隔离开发验证,正式启动使用 `npm start`。
|
||||
|
||||
@@ -108,7 +108,7 @@ ZCode 后续用于有限诊断与修复建议;任务状态、重试上限和
|
||||
|
||||
| 位置 | 数据 |
|
||||
|---|---|
|
||||
| `dashboard/state.json`、`dashboard/auth.sqlite` | 工作台账号哈希、配置、PDF 记录、登录令牌哈希 |
|
||||
| `dashboard/state.sqlite`、`dashboard/auth.sqlite` | 工作台账号哈希、配置、PDF 记录、登录令牌哈希 |
|
||||
| `workspace/workspace.sqlite` | 连接、日历、异常、审计与监管配置 |
|
||||
| `bpm-sessions/accounts.sqlite`、加密会话文件 | 托管账号、密码、冷却信息及会话 |
|
||||
| `bpm-keys/` | Windows DPAPI 保护的主密钥 |
|
||||
|
||||
+19
-6
@@ -1,6 +1,7 @@
|
||||
const fs=require('node:fs');
|
||||
const path=require('node:path');
|
||||
const crypto=require('node:crypto');
|
||||
const {DatabaseSync}=require('node:sqlite');
|
||||
const copy=value=>structuredClone(value);
|
||||
const safeUser=({id,username,role,createdAt})=>({id,username,role,createdAt});
|
||||
const fail=(message,status=400)=>{throw Object.assign(new Error(message),{status});};
|
||||
@@ -26,15 +27,27 @@ function validateSettings(settings) {
|
||||
|
||||
class Store {
|
||||
constructor(dir) {
|
||||
fs.mkdirSync(dir,{recursive:true});this.file=path.join(dir,'state.json');
|
||||
try {this.data=JSON.parse(fs.readFileSync(this.file,'utf8'));if(this.data.schema!==1)fail('状态文件版本不支持。',500);}
|
||||
catch(error) {if(error.code!=='ENOENT')throw error;this.data={schema:1,users:[],settings:{version:1,schedule:{times:['09:00','17:00'],timezone:'Asia/Shanghai',weekends:false,mode:'product_line',missed:'next_window'},templates:{subject:'',body:''},groups:[]},jobs:[],audit:[]};}
|
||||
fs.mkdirSync(dir,{recursive:true});this.file=path.join(dir,'state.sqlite');
|
||||
this.data=this.database(db=>{
|
||||
db.exec('CREATE TABLE IF NOT EXISTS dashboard_state(id INTEGER PRIMARY KEY CHECK(id=1),value TEXT NOT NULL)');
|
||||
const stored=db.prepare('SELECT value FROM dashboard_state WHERE id=1').get();
|
||||
if(stored){const data=JSON.parse(stored.value);if(data.schema!==1)fail('状态文件版本不支持。',500);return data;}
|
||||
let data;
|
||||
try{data=JSON.parse(fs.readFileSync(path.join(dir,'state.json'),'utf8'));if(data.schema!==1)fail('状态文件版本不支持。',500);}
|
||||
catch(error){if(error.code!=='ENOENT')throw error;data={schema:1,users:[],settings:{version:1,schedule:{times:['09:00','17:00'],timezone:'Asia/Shanghai',weekends:false,mode:'product_line',missed:'next_window'},templates:{subject:'',body:''},groups:[]},jobs:[],audit:[]};}
|
||||
// A single SQLite statement publishes the imported snapshot atomically.
|
||||
// Retain legacy JSON unchanged for backups; SQLite is authoritative thereafter.
|
||||
db.prepare('INSERT OR IGNORE INTO dashboard_state(id,value) VALUES(1,?)').run(JSON.stringify(data));
|
||||
return JSON.parse(db.prepare('SELECT value FROM dashboard_state WHERE id=1').get().value);
|
||||
});
|
||||
}
|
||||
database(work){
|
||||
const db=new DatabaseSync(this.file);
|
||||
try{db.exec('PRAGMA busy_timeout=5000; PRAGMA synchronous=FULL;');return work(db);}finally{db.close();}
|
||||
}
|
||||
commit(mutator) {
|
||||
const draft=copy(this.data);const result=mutator(draft);
|
||||
const temp=this.file+'.'+crypto.randomBytes(6).toString('hex')+'.tmp';
|
||||
try {fs.writeFileSync(temp,JSON.stringify(draft,null,2),{mode:0o600});fs.renameSync(temp,this.file);}
|
||||
catch(error){try{fs.unlinkSync(temp);}catch{}throw error;}
|
||||
this.database(db=>db.prepare('UPDATE dashboard_state SET value=? WHERE id=1').run(JSON.stringify(draft)));
|
||||
this.data=draft;return copy(result);
|
||||
}
|
||||
addAudit(draft,actor,action,detail='') {draft.audit.unshift({id:crypto.randomUUID(),at:new Date().toISOString(),actor,action,detail});draft.audit=draft.audit.slice(0,2000);}
|
||||
|
||||
@@ -26,7 +26,7 @@ test('admin bootstrap is one-time and credentials are hashed with usable roles',
|
||||
const reader=store.addUser({username:'reader',password:'Reader-password-123',role:'viewer'},admin.username);
|
||||
assert.equal(reader.role,'viewer');
|
||||
assert.ok(!JSON.stringify(reader).includes('passwordHash'));
|
||||
assert.ok(!fs.readFileSync(path.join(dir,'state.json'),'utf8').includes('Test-password-123'));
|
||||
assert.ok(!fs.readFileSync(path.join(dir,'state.sqlite')).includes(Buffer.from('Test-password-123')));
|
||||
});
|
||||
test('export job records update in place and remain visible after restart',t=>{
|
||||
const {dir,store}=fixture(t);
|
||||
@@ -43,3 +43,20 @@ test('next slots follow Shanghai time, weekend policy and exact slot boundary',(
|
||||
assert.deepEqual(nextSlots(schedule,new Date('2026-10-16T09:00:00Z'),2),['2026-10-19T01:00:00.000Z','2026-10-19T09:00:00.000Z']);
|
||||
assert.deepEqual(nextSlots({...schedule,weekends:true},new Date('2026-10-16T08:59:00Z'),2),['2026-10-16T09:00:00.000Z','2026-10-17T01:00:00.000Z']);
|
||||
});
|
||||
|
||||
test('admin creation and subsequent settings saves survive EXDEV renames',t=>{
|
||||
const {dir,store}=fixture(t);
|
||||
t.mock.method(fs,'renameSync',()=>{throw Object.assign(new Error('cross-device rename'),{code:'EXDEV'});});
|
||||
const admin=store.bootstrap('admin','Test-password-123');
|
||||
const settings=store.settings();store.saveSettings({templates:{subject:'Saved subject',body:'Saved body'}},settings.version,admin.username);
|
||||
const restarted=new Store(dir);assert.equal(restarted.authenticate('admin','Test-password-123').id,admin.id);assert.equal(restarted.settings().templates.subject,'Saved subject');
|
||||
});
|
||||
|
||||
test('legacy JSON state imports once and remains intact as a backup',t=>{
|
||||
const {dir,store}=fixture(t);store.bootstrap('legacy','Test-password-123');
|
||||
const legacy=JSON.stringify(store.data);
|
||||
const importedDir=fs.mkdtempSync(path.join(os.tmpdir(),'dashboard-import-'));t.after(()=>fs.rmSync(importedDir,{recursive:true,force:true}));fs.writeFileSync(path.join(importedDir,'state.json'),legacy);
|
||||
const imported=new Store(importedDir);assert.equal(imported.authenticate('legacy','Test-password-123').role,'admin');
|
||||
imported.addUser({username:'reader',password:'Reader-password-123',role:'viewer'},'legacy');
|
||||
assert.equal(new Store(importedDir).users().length,2);assert.equal(fs.readFileSync(path.join(importedDir,'state.json'),'utf8'),legacy);
|
||||
});
|
||||
Reference in new issue
Block a user