feat: 操作审计语义动作标注落库并支持按语义动作筛选
- OperationDescription 新增 actionOf:捕获时由 method/resourceType/path 推导 语义动作键(login/logout/auth + create/update/delete/view + 子动作段 publish/restore/enable 等,activation 归并为 enable),AuditRecorder 落库 spec.action(AuditLogSpec 新增字段) - OperationAuditPlugin 注册 spec.action 索引,AuditLogFilters 白名单放行 fieldSelector=spec.action=...,查询与导出端点共用 - 前端 audit.ts:describeOperation 优先用落库 action(历史记录无标注仍由 method/path 现场推导,同样适用),导出 deriveAction 与 SEMANTIC_ACTIONS (键→中文标签,与后端同一套映射),展示短语统一由动作键推导 - 列表筛选栏新增「语义动作」下拉(7→8 列),buildQuery/重置/watch 接线 - 测试:后端 +10 例(actionOf 全分支、describeAction、落库断言、索引注册、 查询筛选)共 81 例;前端 +8 例(deriveAction、标注优先、选项完备性)共 39 例; gradle build 全绿 - 文档同步:设计文档 V2 行标记已实现(4.1 字段表/4.2 筛选/5 索引/7 局限)、 技术方案 D3 索引表与 5.2 节、两级 README 功能一览与已知局限
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { AuditLogSpec } from './audit'
|
||||
import { describeOperation } from './audit'
|
||||
import { deriveAction, describeOperation, SEMANTIC_ACTIONS } from './audit'
|
||||
|
||||
// 操作描述推导(这条操作真正干了什么):由 method / resourceType / path 推导中文短语。
|
||||
// 历史记录不落库新字段,描述纯展示层推导,因此必须覆盖所有回退路径,防止出现英文原始值。
|
||||
// 操作描述推导(这条操作真正干了什么):由落库的语义动作标注或
|
||||
// method / resourceType / path 推导中文短语。
|
||||
// 描述必须覆盖所有回退路径,防止出现英文原始值。
|
||||
function spec(overrides: Partial<AuditLogSpec>): AuditLogSpec {
|
||||
return {
|
||||
username: 'admin',
|
||||
@@ -133,3 +134,107 @@ describe('describeOperation', () => {
|
||||
}))).toBe('删除other')
|
||||
})
|
||||
})
|
||||
|
||||
describe('deriveAction 语义动作推导', () => {
|
||||
it('登录/登出/认证操作由路径判定(含查询串剥离)', () => {
|
||||
expect(deriveAction('POST', 'auth', '/login')).toBe('login')
|
||||
expect(deriveAction('GET', 'auth', '/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com'))
|
||||
.toBe('login')
|
||||
expect(deriveAction('POST', 'auth', '/logout')).toBe('logout')
|
||||
expect(deriveAction('POST', 'auth', '/auth/other')).toBe('auth')
|
||||
})
|
||||
|
||||
it('子动作段优先于 HTTP 方法', () => {
|
||||
expect(deriveAction('PUT', 'posts',
|
||||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish')).toBe('publish')
|
||||
expect(deriveAction('POST', 'posts',
|
||||
'/apis/api.console.halo.run/v1alpha1/posts/post-abc/restore')).toBe('restore')
|
||||
// 激活与启用归并为同一动作键
|
||||
expect(deriveAction('PUT', 'themes',
|
||||
'/apis/api.console.halo.run/v1alpha1/themes/theme-abc/activation')).toBe('enable')
|
||||
expect(deriveAction('PUT', 'users',
|
||||
'/apis/api.console.halo.run/v1alpha1/users/admin/password')).toBe('password')
|
||||
})
|
||||
|
||||
it('无子动作段时由 HTTP 方法归为 create/update/delete/view', () => {
|
||||
expect(deriveAction('POST', 'users', '/apis/api.console.halo.run/v1alpha1/users'))
|
||||
.toBe('create')
|
||||
expect(deriveAction('PUT', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBe('update')
|
||||
expect(deriveAction('DELETE', 'plugins',
|
||||
'/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc')).toBe('delete')
|
||||
expect(deriveAction('GET', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBe('view')
|
||||
})
|
||||
|
||||
it('未知方法与未识别子动作回退为方法键或 null', () => {
|
||||
expect(deriveAction('TRACE', 'posts', '/apis/content.halo.run/v1alpha1/posts/post-abc'))
|
||||
.toBeNull()
|
||||
expect(deriveAction('PUT', 'posts',
|
||||
'/apis/content.halo.run/v1alpha1/posts/post-abc/content')).toBe('update')
|
||||
})
|
||||
})
|
||||
|
||||
describe('describeOperation 落库标注优先', () => {
|
||||
it('spec.action 存在时直接由动作键推导(历史记录无 action 仍走 method/path 推导)', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/content.halo.run/v1alpha1/posts/post-abc',
|
||||
action: 'publish',
|
||||
}))).toBe('发布文章')
|
||||
expect(describeOperation(spec({
|
||||
method: 'POST',
|
||||
resourceType: 'auth',
|
||||
path: '/login',
|
||||
action: 'login',
|
||||
}))).toBe('登录')
|
||||
// 与 method/path 现场推导结果一致(同一套推导规则)
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||||
action: 'publish',
|
||||
}))).toBe(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish',
|
||||
})))
|
||||
})
|
||||
|
||||
it('动作键未知时回退为 method 动词 + 资源名', () => {
|
||||
expect(describeOperation(spec({
|
||||
method: 'PUT',
|
||||
resourceType: 'posts',
|
||||
path: '/somewhere/odd',
|
||||
action: 'bogus',
|
||||
}))).toBe('修改文章')
|
||||
})
|
||||
})
|
||||
|
||||
describe('SEMANTIC_ACTIONS 筛选选项', () => {
|
||||
it('值唯一且标签非空', () => {
|
||||
const values = SEMANTIC_ACTIONS.map((a) => a.value)
|
||||
expect(new Set(values).size).toBe(values.length)
|
||||
for (const a of SEMANTIC_ACTIONS) {
|
||||
expect(a.label).not.toBe('')
|
||||
}
|
||||
})
|
||||
|
||||
it('覆盖全部可落库的语义动作键(view 不会被捕获规则记录,除外)', () => {
|
||||
// 全量枚举:登录/登出/认证 + 方法动作 + 全部子动作段(含 activation 归并)
|
||||
const storable = new Set([
|
||||
'login', 'logout', 'auth',
|
||||
'create', 'update', 'delete',
|
||||
'publish', 'unpublish', 'recycle', 'restore', 'enable', 'disable',
|
||||
'upgrade', 'uninstall', 'reload', 'reply', 'recalculate',
|
||||
'password', 'resetpassword',
|
||||
])
|
||||
expect(new Set(SEMANTIC_ACTIONS.map((a) => a.value))).toEqual(storable)
|
||||
// 每个选项都能由 describeOperation 从动作键推出非空短语(下拉与描述保持一致)
|
||||
for (const a of SEMANTIC_ACTIONS) {
|
||||
expect(describeOperation(spec({ resourceType: 'posts', action: a.value })))
|
||||
.not.toBe('')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -8,6 +8,8 @@ export interface AuditLogSpec {
|
||||
resourceType: string
|
||||
resourceName?: string | null
|
||||
path: string
|
||||
/** 语义动作键(语义动作标注,如 login/create/publish);旧版本写入的历史记录可能缺失。 */
|
||||
action?: string | null
|
||||
statusCode: number
|
||||
remoteIp: string
|
||||
userAgent?: string | null
|
||||
@@ -78,7 +80,7 @@ const RESOURCE_LABELS: Record<string, string> = {
|
||||
tags: '标签',
|
||||
}
|
||||
|
||||
/** 动作方法的基础动词(未知方法回退原始值)。 */
|
||||
/** 动作方法的基础动词(未知方法回退原始值,用于未知动作键的展示回退)。 */
|
||||
const METHOD_VERBS: Record<string, string> = {
|
||||
POST: '新建',
|
||||
PUT: '修改',
|
||||
@@ -87,11 +89,51 @@ const METHOD_VERBS: Record<string, string> = {
|
||||
GET: '查看',
|
||||
}
|
||||
|
||||
/** 动作方法 → 语义动作键(未知方法无键)。 */
|
||||
const METHOD_ACTIONS: Record<string, string> = {
|
||||
POST: 'create',
|
||||
PUT: 'update',
|
||||
PATCH: 'update',
|
||||
DELETE: 'delete',
|
||||
GET: 'view',
|
||||
}
|
||||
|
||||
/**
|
||||
* 子资源动作段({@code /apis/.../{plural}/{name}/{action}} 的 action)→ 动词,
|
||||
* 与资源名拼接成短语,如 publish + 文章 = 发布文章。
|
||||
* 子资源动作段({@code /apis/.../{plural}/{name}/{action}} 的 action)→ 语义动作键。
|
||||
* activation 与 enable 语义相同(激活/启用),归并为同一动作键便于统一筛选。
|
||||
*/
|
||||
const SUB_ACTION_VERBS: Record<string, string> = {
|
||||
const SEGMENT_ACTIONS: Record<string, string> = {
|
||||
publish: 'publish',
|
||||
unpublish: 'unpublish',
|
||||
recycle: 'recycle',
|
||||
restore: 'restore',
|
||||
enable: 'enable',
|
||||
activation: 'enable',
|
||||
disable: 'disable',
|
||||
upgrade: 'upgrade',
|
||||
uninstall: 'uninstall',
|
||||
reload: 'reload',
|
||||
reply: 'reply',
|
||||
recalculate: 'recalculate',
|
||||
password: 'password',
|
||||
resetpassword: 'resetpassword',
|
||||
}
|
||||
|
||||
/** 语义动作键 → 完整短语(不拼资源名)。 */
|
||||
const ACTION_PHRASES: Record<string, string> = {
|
||||
login: '登录',
|
||||
logout: '登出',
|
||||
auth: '认证操作',
|
||||
password: '修改密码',
|
||||
resetpassword: '重置密码',
|
||||
}
|
||||
|
||||
/** 语义动作键 → 动词(与资源中文名拼接成短语,如 publish + 文章 = 发布文章)。 */
|
||||
const ACTION_VERBS: Record<string, string> = {
|
||||
create: '新建',
|
||||
update: '修改',
|
||||
delete: '删除',
|
||||
view: '查看',
|
||||
publish: '发布',
|
||||
unpublish: '取消发布',
|
||||
recycle: '移入回收站',
|
||||
@@ -101,16 +143,35 @@ const SUB_ACTION_VERBS: Record<string, string> = {
|
||||
upgrade: '升级',
|
||||
uninstall: '卸载',
|
||||
reload: '重载',
|
||||
activation: '启用',
|
||||
reply: '回复',
|
||||
recalculate: '重新统计',
|
||||
}
|
||||
|
||||
/** 子资源动作段 → 完整短语(不拼接资源名)。 */
|
||||
const SUB_ACTION_PHRASES: Record<string, string> = {
|
||||
password: '修改密码',
|
||||
resetpassword: '重置密码',
|
||||
}
|
||||
/**
|
||||
* 语义动作筛选选项(键 → 中文标签,与后端 OperationDescription 保持一致):
|
||||
* view(查看)不会被捕获规则记录,不列入。
|
||||
*/
|
||||
export const SEMANTIC_ACTIONS: Array<{ value: string; label: string }> = [
|
||||
{ value: 'login', label: '登录' },
|
||||
{ value: 'logout', label: '登出' },
|
||||
{ value: 'auth', label: '认证操作' },
|
||||
{ value: 'create', label: '新建' },
|
||||
{ value: 'update', label: '修改' },
|
||||
{ value: 'delete', label: '删除' },
|
||||
{ value: 'publish', label: '发布' },
|
||||
{ value: 'unpublish', label: '取消发布' },
|
||||
{ value: 'recycle', label: '移入回收站' },
|
||||
{ value: 'restore', label: '恢复' },
|
||||
{ value: 'enable', label: '启用' },
|
||||
{ value: 'disable', label: '停用' },
|
||||
{ value: 'upgrade', label: '升级' },
|
||||
{ value: 'uninstall', label: '卸载' },
|
||||
{ value: 'reload', label: '重载' },
|
||||
{ value: 'reply', label: '回复' },
|
||||
{ value: 'recalculate', label: '重新统计' },
|
||||
{ value: 'password', label: '修改密码' },
|
||||
{ value: 'resetpassword', label: '重置密码' },
|
||||
]
|
||||
|
||||
/** 剥离路径的查询串(spec.path 存的是含查询串的完整路径)。 */
|
||||
function pathWithoutQuery(path?: string | null): string {
|
||||
@@ -122,42 +183,69 @@ function pathWithoutQuery(path?: string | null): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作描述(这条操作真正干了什么):由 method / resourceType / path 推导中文短语,
|
||||
* 如 登录、登出、新建用户、修改文章、发布文章、启用插件、修改密码。
|
||||
*
|
||||
* <p>纯展示层推导,不落库:历史记录同样适用;未识别的子动作回退为「动词 + 资源名」,
|
||||
* 未识别的资源类型直接使用原始 resourceType。
|
||||
* 推导语义动作键(语义动作标注,与后端 OperationDescription.actionOf 保持一致):
|
||||
* 登录/登出/认证操作由路径判定,子动作段优先于 HTTP 方法,其余方法归为
|
||||
* create/update/delete/view;无法识别返回 null。
|
||||
*/
|
||||
export function describeOperation(
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path'>,
|
||||
): string {
|
||||
const cleanPath = pathWithoutQuery(spec.path)
|
||||
if (spec.resourceType === 'auth' || cleanPath === '/login' || cleanPath === '/logout') {
|
||||
if (cleanPath === '/login') {
|
||||
return '登录'
|
||||
}
|
||||
if (cleanPath === '/logout') {
|
||||
return '登出'
|
||||
}
|
||||
return '认证操作'
|
||||
export function deriveAction(
|
||||
method: string,
|
||||
resourceType: string,
|
||||
path?: string | null,
|
||||
): string | null {
|
||||
const cleanPath = pathWithoutQuery(path)
|
||||
if (cleanPath === '/login') {
|
||||
return 'login'
|
||||
}
|
||||
if (cleanPath === '/logout') {
|
||||
return 'logout'
|
||||
}
|
||||
if (resourceType === 'auth') {
|
||||
return 'auth'
|
||||
}
|
||||
const label = RESOURCE_LABELS[spec.resourceType] ?? spec.resourceType
|
||||
const verb = METHOD_VERBS[spec.method] ?? spec.method
|
||||
// /apis/{group}/{version}/{plural}/{name}/{action}:
|
||||
// filter(Boolean) 去掉首段空串后,segments 为 [apis, group, version, plural, name, action],
|
||||
// 子动作段为 segments[5]
|
||||
const segments = cleanPath.split('/').filter(Boolean)
|
||||
const action = segments.length >= 6 ? segments[5].toLowerCase() : null
|
||||
if (action) {
|
||||
const phrase = SUB_ACTION_PHRASES[action]
|
||||
if (phrase) {
|
||||
return phrase
|
||||
}
|
||||
const subVerb = SUB_ACTION_VERBS[action]
|
||||
if (subVerb) {
|
||||
return `${subVerb}${label}`
|
||||
if (segments.length >= 6) {
|
||||
const action = SEGMENT_ACTIONS[segments[5].toLowerCase()]
|
||||
if (action) {
|
||||
return action
|
||||
}
|
||||
}
|
||||
return METHOD_ACTIONS[method] ?? null
|
||||
}
|
||||
|
||||
/** 由语义动作键推导中文短语;键为 null 或未识别返回 null(调用方回退 method 推导)。 */
|
||||
function describeFromAction(action: string | null, resourceType: string): string | null {
|
||||
if (!action) {
|
||||
return null
|
||||
}
|
||||
const phrase = ACTION_PHRASES[action]
|
||||
if (phrase) {
|
||||
return phrase
|
||||
}
|
||||
const verb = ACTION_VERBS[action]
|
||||
return verb ? `${verb}${RESOURCE_LABELS[resourceType] ?? resourceType}` : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 操作描述(这条操作真正干了什么):推导中文短语,
|
||||
* 如 登录、登出、新建用户、修改文章、发布文章、启用插件、修改密码。
|
||||
*
|
||||
* <p>优先使用落库的语义动作标注(spec.action);历史记录(无 action 字段)由
|
||||
* method/path 现场推导,同样适用。识别失败回退为「method 动词 + 资源名」,
|
||||
* 未识别的资源类型直接使用原始 resourceType。
|
||||
*/
|
||||
export function describeOperation(
|
||||
spec: Pick<AuditLogSpec, 'method' | 'resourceType' | 'path' | 'action'>,
|
||||
): string {
|
||||
const action = spec.action ?? deriveAction(spec.method, spec.resourceType, spec.path)
|
||||
const fromAction = describeFromAction(action, spec.resourceType)
|
||||
if (fromAction) {
|
||||
return fromAction
|
||||
}
|
||||
const label = RESOURCE_LABELS[spec.resourceType] ?? spec.resourceType
|
||||
const verb = METHOD_VERBS[spec.method] ?? spec.method
|
||||
return `${verb}${label}`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user