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:
+16
@@ -80,6 +80,22 @@ class AuditQueryEndpointTest {
|
||||
.contains("500");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldApplySemanticActionFieldSelector() {
|
||||
webClient.get()
|
||||
.uri("/apis/audit.guichumo.com/v1alpha1/auditlogs/-/query"
|
||||
+ "?fieldSelector=spec.action=publish")
|
||||
.exchange()
|
||||
.expectStatus().isOk();
|
||||
|
||||
var captor = ArgumentCaptor.forClass(ListOptions.class);
|
||||
verify(client).listBy(eq(AuditLog.class), captor.capture(), any(PageRequest.class));
|
||||
var condition = captor.getValue().toCondition().toString();
|
||||
assertThat(condition)
|
||||
.contains("spec.action")
|
||||
.contains("publish");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldApplyResultFilter() {
|
||||
webClient.get()
|
||||
|
||||
+15
@@ -60,6 +60,8 @@ class AuditRecorderTest {
|
||||
|
||||
assertThat(auditLog.getSpec().getUsername()).isEqualTo("admin");
|
||||
assertThat(auditLog.getSpec().getMethod()).isEqualTo("DELETE");
|
||||
// 语义动作标注(产品设计 V2):捕获时推导落库
|
||||
assertThat(auditLog.getSpec().getAction()).isEqualTo("delete");
|
||||
assertThat(auditLog.getSpec().getResourceType()).isEqualTo("posts");
|
||||
assertThat(auditLog.getSpec().getResourceName()).isEqualTo("xyz");
|
||||
assertThat(auditLog.getSpec().getPath()).isEqualTo(
|
||||
@@ -80,9 +82,22 @@ class AuditRecorderTest {
|
||||
assertThat(captor.getValue().getSpec().getUsername())
|
||||
.isEqualTo(AuditLogSpec.UNKNOWN_USERNAME);
|
||||
assertThat(captor.getValue().getSpec().getResourceType()).isEqualTo("auth");
|
||||
assertThat(captor.getValue().getSpec().getAction()).isEqualTo("login");
|
||||
assertThat(captor.getValue().getSpec().getStatusCode()).isEqualTo(401);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAnnotateSubActionSemanticAction() {
|
||||
recorder.record(RequestSnapshot.of(MockServerHttpRequest
|
||||
.put("http://localhost/apis/api.console.halo.run/v1alpha1/posts/xyz/publish")
|
||||
.build()), 200, "admin");
|
||||
|
||||
var captor = ArgumentCaptor.forClass(AuditLog.class);
|
||||
verify(client).create(captor.capture());
|
||||
assertThat(captor.getValue().getSpec().getAction()).isEqualTo("publish");
|
||||
assertThat(captor.getValue().getSpec().getResourceType()).isEqualTo("posts");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSkipRecordingWhenAuditDisabled() {
|
||||
when(settingProvider.get()).thenReturn(Mono.just(new AuditSettings(false, 30)));
|
||||
|
||||
+115
@@ -100,4 +100,119 @@ class OperationDescriptionTest {
|
||||
assertThat(OperationDescription.describe("POST", "workflows", null))
|
||||
.isEqualTo("新建workflows");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeriveLoginLogoutAuthActions() {
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/login")).isEqualTo("login");
|
||||
// GET /login?redirect_uri=... 也应识别为登录(path 含查询串)
|
||||
assertThat(OperationDescription.actionOf("GET", "auth",
|
||||
"/login?redirect_uri=https%3A%2F%2Fwww.guichumo.com%2Fconsole")).isEqualTo("login");
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/logout")).isEqualTo("logout");
|
||||
assertThat(OperationDescription.actionOf("POST", "auth", "/auth/other"))
|
||||
.isEqualTo("auth");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeriveMethodActions() {
|
||||
assertThat(OperationDescription.actionOf("POST", "users",
|
||||
"/apis/api.console.halo.run/v1alpha1/users")).isEqualTo("create");
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/content.halo.run/v1alpha1/posts/post-abc")).isEqualTo("update");
|
||||
assertThat(OperationDescription.actionOf("PATCH", "themes",
|
||||
"/apis/api.console.halo.run/v1alpha1/themes/theme-abc")).isEqualTo("update");
|
||||
assertThat(OperationDescription.actionOf("DELETE", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc")).isEqualTo("delete");
|
||||
assertThat(OperationDescription.actionOf("GET", "posts",
|
||||
"/apis/content.halo.run/v1alpha1/posts/post-abc")).isEqualTo("view");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeriveSubActionFromPathSegment() {
|
||||
assertThat(OperationDescription.actionOf("POST", "posts",
|
||||
"/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish")).isEqualTo("publish");
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/api.console.halo.run/v1alpha1/posts/post-abc/unpublish"))
|
||||
.isEqualTo("unpublish");
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/api.console.halo.run/v1alpha1/posts/post-abc/recycle"))
|
||||
.isEqualTo("recycle");
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/api.console.halo.run/v1alpha1/posts/post-abc/restore")).isEqualTo("restore");
|
||||
assertThat(OperationDescription.actionOf("PUT", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/enable"))
|
||||
.isEqualTo("enable");
|
||||
assertThat(OperationDescription.actionOf("PUT", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/disable"))
|
||||
.isEqualTo("disable");
|
||||
assertThat(OperationDescription.actionOf("PUT", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/upgrade"))
|
||||
.isEqualTo("upgrade");
|
||||
assertThat(OperationDescription.actionOf("PUT", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/uninstall"))
|
||||
.isEqualTo("uninstall");
|
||||
assertThat(OperationDescription.actionOf("PUT", "plugins",
|
||||
"/apis/api.console.halo.run/v1alpha1/plugins/plugin-abc/reload"))
|
||||
.isEqualTo("reload");
|
||||
assertThat(OperationDescription.actionOf("POST", "comments",
|
||||
"/apis/api.comment.halo.run/v1alpha1/comments/comment-abc/reply"))
|
||||
.isEqualTo("reply");
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/api.content.halo.run/v1alpha1/posts/post-abc/recalculate"))
|
||||
.isEqualTo("recalculate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNormalizeActivationToEnable() {
|
||||
// 激活与启用语义相同(主题/插件 activation 与 enable),归并为同一动作键便于统一筛选
|
||||
assertThat(OperationDescription.actionOf("PUT", "themes",
|
||||
"/apis/api.console.halo.run/v1alpha1/themes/theme-abc/activation"))
|
||||
.isEqualTo("enable");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDerivePasswordSubActions() {
|
||||
assertThat(OperationDescription.actionOf("PUT", "users",
|
||||
"/apis/api.console.halo.run/v1alpha1/users/admin/password")).isEqualTo("password");
|
||||
assertThat(OperationDescription.actionOf("PUT", "users",
|
||||
"/apis/api.console.halo.run/v1alpha1/users/admin/resetpassword"))
|
||||
.isEqualTo("resetpassword");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldStripQueryBeforeDerivingAction() {
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/api.console.halo.run/v1alpha1/posts/post-abc/publish?async=true"))
|
||||
.isEqualTo("publish");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFallbackToNullActionForUnknownMethod() {
|
||||
assertThat(OperationDescription.actionOf("TRACE", "posts",
|
||||
"/apis/content.halo.run/v1alpha1/posts/post-abc")).isNull();
|
||||
// 未识别的子动作回退到方法推导
|
||||
assertThat(OperationDescription.actionOf("PUT", "posts",
|
||||
"/apis/content.halo.run/v1alpha1/posts/post-abc/content")).isEqualTo("update");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDescribeFromActionKey() {
|
||||
assertThat(OperationDescription.describeAction("login", "auth")).isEqualTo("登录");
|
||||
assertThat(OperationDescription.describeAction("logout", "auth")).isEqualTo("登出");
|
||||
assertThat(OperationDescription.describeAction("auth", "auth")).isEqualTo("认证操作");
|
||||
assertThat(OperationDescription.describeAction("create", "users")).isEqualTo("新建用户");
|
||||
assertThat(OperationDescription.describeAction("update", "posts")).isEqualTo("修改文章");
|
||||
assertThat(OperationDescription.describeAction("delete", "plugins"))
|
||||
.isEqualTo("删除插件");
|
||||
assertThat(OperationDescription.describeAction("publish", "posts"))
|
||||
.isEqualTo("发布文章");
|
||||
assertThat(OperationDescription.describeAction("restore", "posts"))
|
||||
.isEqualTo("恢复文章");
|
||||
assertThat(OperationDescription.describeAction("enable", "themes"))
|
||||
.isEqualTo("启用主题");
|
||||
assertThat(OperationDescription.describeAction("password", "users"))
|
||||
.isEqualTo("修改密码");
|
||||
// 未识别的键回退 null(调用方回退 method 推导)
|
||||
assertThat(OperationDescription.describeAction(null, "posts")).isNull();
|
||||
assertThat(OperationDescription.describeAction("bogus", "posts")).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user