refactor: 移除 load_app_audit 启动期建表/插角色,表迁 create_tables.py、角色迁 init/data.json(部署期导入)

This commit is contained in:
yumoqing 2026-08-18 16:23:26 +08:00
parent cb96d72c75
commit d2e6c93aa9
3 changed files with 16 additions and 33 deletions

View File

@ -1,8 +1,11 @@
"""
app_audit/init.py - 审计模块加载入口
独立审计模块sd_audit_logs + owner.audit 角色审计独立性
独立审计模块提供审计日志读写服务append-only+ owner.audit 角色审计独立性
owner.superuser 也无权访问审计日志宿主应用调用 load_app_audit() 即可
铁律运行期不做任何 schema / 数据初始化sd_audit_logs 表建表在部署期
scripts/create_tables.pyowner.audit 角色数据在 init/data.jsonbuild.sh import_init.py 导入
"""
MODULE_NAME = "app_audit"
@ -10,35 +13,5 @@ MODULE_VERSION = "1.0.0"
def load_app_audit():
"""注册审计模块的建表 + owner.audit 角色到启动流程。"""
from ahserver.configuredServer import add_startup
async def _init_audit(app):
from sqlor.dbpools import DBPools
from appPublic.log import debug
from ahserver.serverenv import ServerEnv
try:
db = DBPools()
dbname = ServerEnv().get_module_dbname('app_audit')
async with db.sqlorContext(dbname) as sor:
# 审计日志表append-only全局
await sor.sqlExe(
"CREATE TABLE IF NOT EXISTS sd_audit_logs ("
"id varchar(32) NOT NULL, user_id varchar(32), username varchar(100),"
"action varchar(50) NOT NULL, target varchar(200), detail text,"
"result varchar(10), client_ip varchar(64),"
"created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,"
"PRIMARY KEY (id), KEY idx_user (user_id), KEY idx_action (action),"
"KEY idx_created (created_at)"
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4", {})
debug("app_audit: sd_audit_logs table ready")
# owner.audit 角色(审计独立角色)
await sor.sqlExe(
"INSERT IGNORE INTO role (id, orgtypeid, name) "
"VALUES ('owner.audit', 'owner', 'audit')", {})
debug("app_audit: owner.audit role ready")
except Exception as e:
debug("app_audit init: " + str(e))
add_startup(_init_audit)
"""审计模块加载入口。表/角色初始化已迁到部署期,此处无需启动时建表/插数据。"""
return True

View File

@ -1,6 +1,7 @@
#!/bin/bash
# app_audit module build script
# 链接 wwwroot 到宿主应用Sage 或 pipeline。审计表由 load_app_audit() 启动时自动建。
# 链接 wwwroot 到宿主应用Sage 或 pipeline
# 审计表在宿主部署期由 create_tables.py 建owner.audit 角色由 import_init.py 从 init/data.json 导入。
set -e

9
init/data.json Normal file
View File

@ -0,0 +1,9 @@
{
"roles": [
{
"id": "owner.audit",
"orgtypeid": "owner",
"name": "audit"
}
]
}