feat: AuditLog 模型与索引注册(里程碑 2)
- AuditLog Extension(group audit.example.com 占位)+ AuditLogSpec,字段与产品设计 4.1 一一对应 - 操作时间复用 metadata.creationTimestamp(D3) - start() 注册 6 个业务索引(username/resourceType/method/statusCode/resourceName/path),stop() 注销 - 资源名称/路径索引用于关键字模糊筛选(QueryFactory.contains 需索引字段) - 生命周期测试覆盖注册索引清单与注销
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
plugins {
|
plugins {
|
||||||
id 'java'
|
id 'java'
|
||||||
|
id 'io.freefair.lombok' version '9.2.0'
|
||||||
id "run.halo.plugin.devtools" version "0.8.0"
|
id "run.halo.plugin.devtools" version "0.8.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+43
-2
@@ -1,18 +1,59 @@
|
|||||||
package run.halo.operationaudit;
|
package run.halo.operationaudit;
|
||||||
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
import run.halo.app.extension.GroupVersionKind;
|
||||||
|
import run.halo.app.extension.Scheme;
|
||||||
|
import run.halo.app.extension.SchemeManager;
|
||||||
|
import run.halo.app.extension.index.IndexSpecs;
|
||||||
|
import run.halo.operationaudit.audit.AuditLog;
|
||||||
import run.halo.app.plugin.BasePlugin;
|
import run.halo.app.plugin.BasePlugin;
|
||||||
import run.halo.app.plugin.PluginContext;
|
import run.halo.app.plugin.PluginContext;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 操作审计插件入口。
|
* 操作审计插件入口。
|
||||||
*
|
*
|
||||||
* <p>负责插件生命周期:启动时注册 {@code AuditLog} 模型与索引(里程碑 2),停止时注销。
|
* <p>负责插件生命周期(技术方案 D3):
|
||||||
|
* <ul>
|
||||||
|
* <li>启动时注册 {@code AuditLog} 模型与索引(操作者/资源类型/动作/结果/资源名称/路径,
|
||||||
|
* 时间索引由 Halo 为所有模型自动注册);</li>
|
||||||
|
* <li>停止时注销模型。</li>
|
||||||
|
* </ul>
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class OperationAuditPlugin extends BasePlugin {
|
public class OperationAuditPlugin extends BasePlugin {
|
||||||
|
|
||||||
public OperationAuditPlugin(PluginContext pluginContext) {
|
private final SchemeManager schemeManager;
|
||||||
|
|
||||||
|
public OperationAuditPlugin(PluginContext pluginContext, SchemeManager schemeManager) {
|
||||||
super(pluginContext);
|
super(pluginContext);
|
||||||
|
this.schemeManager = schemeManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
schemeManager.register(AuditLog.class, indexSpecs -> {
|
||||||
|
addIndex(indexSpecs, "spec.username", String.class, log -> log.getSpec().getUsername());
|
||||||
|
addIndex(indexSpecs, "spec.resourceType", String.class, log -> log.getSpec().getResourceType());
|
||||||
|
addIndex(indexSpecs, "spec.method", String.class, log -> log.getSpec().getMethod());
|
||||||
|
addIndex(indexSpecs, "spec.statusCode", Integer.class, log -> log.getSpec().getStatusCode());
|
||||||
|
addIndex(indexSpecs, "spec.resourceName", String.class, log -> log.getSpec().getResourceName());
|
||||||
|
addIndex(indexSpecs, "spec.path", String.class, log -> log.getSpec().getPath());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
var gvk = gvkOf();
|
||||||
|
schemeManager.fetch(gvk).ifPresent(schemeManager::unregister);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T extends Comparable<T>> void addIndex(IndexSpecs<AuditLog> specs, String name,
|
||||||
|
Class<T> keyType, java.util.function.Function<AuditLog, T> valueFunc) {
|
||||||
|
specs.add(IndexSpecs.<AuditLog, T>single(name, keyType).indexFunc(valueFunc).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static GroupVersionKind gvkOf() {
|
||||||
|
var gvk = Scheme.getGvkFromType(AuditLog.class);
|
||||||
|
return new GroupVersionKind(gvk.group(), gvk.version(), gvk.kind());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package run.halo.operationaudit.audit;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
import run.halo.app.extension.AbstractExtension;
|
||||||
|
import run.halo.app.extension.GVK;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 审计日志自定义模型(Extension)。
|
||||||
|
*
|
||||||
|
* <p>操作时间直接使用 {@code metadata.creationTimestamp},不重复建模(技术方案 D3)。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@GVK(group = AuditLog.GROUP, version = "v1alpha1", kind = "AuditLog",
|
||||||
|
plural = "auditlogs", singular = "auditlog")
|
||||||
|
public class AuditLog extends AbstractExtension {
|
||||||
|
|
||||||
|
/** 模型 group(占位域名反写,落地时替换为自有域名,见 README)。 */
|
||||||
|
public static final String GROUP = "audit.example.com";
|
||||||
|
|
||||||
|
private AuditLogSpec spec;
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package run.halo.operationaudit.audit;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 审计日志业务字段,与产品设计文档 4.1 表格一一对应。
|
||||||
|
*
|
||||||
|
* <p>操作时间使用 {@code metadata.creationTimestamp}(技术方案 D3)。
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AuditLogSpec {
|
||||||
|
|
||||||
|
/** 操作者用户名;登录失败记 {@link #UNKNOWN_USERNAME}。 */
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
/** HTTP 方法(动作),如 POST/PUT/DELETE/PATCH。 */
|
||||||
|
private String method;
|
||||||
|
|
||||||
|
/** 资源类型(复数形式,如 posts/comments/users/auth)。 */
|
||||||
|
private String resourceType;
|
||||||
|
|
||||||
|
/** 资源名称;集合级操作或登录端点为空。 */
|
||||||
|
private String resourceName;
|
||||||
|
|
||||||
|
/** 完整请求路径(资源语义解析的兜底,保证完整性)。 */
|
||||||
|
private String path;
|
||||||
|
|
||||||
|
/** 响应状态码(结果:2xx 成功,其余失败)。 */
|
||||||
|
private Integer statusCode;
|
||||||
|
|
||||||
|
/** 来源 IP。 */
|
||||||
|
private String remoteIp;
|
||||||
|
|
||||||
|
/** User-Agent。 */
|
||||||
|
private String userAgent;
|
||||||
|
|
||||||
|
/** 登录失败等无法确定操作者时的占位用户名。 */
|
||||||
|
public static final String UNKNOWN_USERNAME = "unknown";
|
||||||
|
}
|
||||||
+95
@@ -0,0 +1,95 @@
|
|||||||
|
package run.halo.operationaudit;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.pf4j.RuntimeMode;
|
||||||
|
import run.halo.app.extension.GroupVersionKind;
|
||||||
|
import run.halo.app.extension.Scheme;
|
||||||
|
import run.halo.app.extension.SchemeManager;
|
||||||
|
import run.halo.app.extension.index.IndexSpecs;
|
||||||
|
import run.halo.app.extension.index.ValueIndexSpec;
|
||||||
|
import run.halo.app.plugin.PluginContext;
|
||||||
|
import run.halo.operationaudit.audit.AuditLog;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插件生命周期测试:启动注册 {@link AuditLog} 模型与全部索引,停止注销。
|
||||||
|
*/
|
||||||
|
class OperationAuditPluginTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldRegisterAuditLogWithExpectedIndexesOnStart() {
|
||||||
|
var schemeManager = mock(SchemeManager.class);
|
||||||
|
var registeredSpecs = new AtomicReference<Consumer<IndexSpecs<AuditLog>>>();
|
||||||
|
doAnswer(invocation -> {
|
||||||
|
registeredSpecs.set(invocation.getArgument(1));
|
||||||
|
return null;
|
||||||
|
}).when(schemeManager).register(eq(AuditLog.class), any());
|
||||||
|
|
||||||
|
var plugin = new OperationAuditPlugin(pluginContext(), schemeManager);
|
||||||
|
plugin.start();
|
||||||
|
|
||||||
|
var recordingSpecs = new RecordingIndexSpecs();
|
||||||
|
registeredSpecs.get().accept(recordingSpecs);
|
||||||
|
|
||||||
|
assertThat(recordingSpecs.names()).containsExactlyInAnyOrder(
|
||||||
|
"spec.username",
|
||||||
|
"spec.resourceType",
|
||||||
|
"spec.method",
|
||||||
|
"spec.statusCode",
|
||||||
|
"spec.resourceName",
|
||||||
|
"spec.path");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldUnregisterSchemeOnStop() {
|
||||||
|
var schemeManager = mock(SchemeManager.class);
|
||||||
|
var scheme = mock(Scheme.class);
|
||||||
|
var gvk = new GroupVersionKind("audit.example.com", "v1alpha1", "AuditLog");
|
||||||
|
org.mockito.Mockito.when(schemeManager.fetch(gvk))
|
||||||
|
.thenReturn(java.util.Optional.of(scheme));
|
||||||
|
|
||||||
|
var plugin = new OperationAuditPlugin(pluginContext(), schemeManager);
|
||||||
|
plugin.stop();
|
||||||
|
|
||||||
|
verify(schemeManager).unregister(scheme);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PluginContext pluginContext() {
|
||||||
|
return PluginContext.builder()
|
||||||
|
.name("operation-audit")
|
||||||
|
.version("0.0.1")
|
||||||
|
.configMapName("operation-audit-configmap")
|
||||||
|
.runtimeMode(RuntimeMode.DEVELOPMENT)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 记录型 IndexSpecs 桩,捕获注册的索引名。 */
|
||||||
|
private static final class RecordingIndexSpecs implements IndexSpecs<AuditLog> {
|
||||||
|
|
||||||
|
private final List<ValueIndexSpec<AuditLog, ?>> specs = new ArrayList<>();
|
||||||
|
|
||||||
|
List<String> names() {
|
||||||
|
return specs.stream().map(ValueIndexSpec::getName).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <K extends Comparable<K>> void add(ValueIndexSpec<AuditLog, K> indexSpec) {
|
||||||
|
specs.add(indexSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ValueIndexSpec<AuditLog, ?>> getIndexSpecs() {
|
||||||
|
return specs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user