From 71b19e4f69781df155e97169dc2459d1ded7e8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Tue, 17 Nov 2026 01:18:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(evidence):=20=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=AF=81=E6=8D=AE=E7=89=B9=E6=80=A7=E6=95=B4=E4=BD=93=E4=B8=8B?= =?UTF-8?q?=E7=BA=BF=EF=BC=882026-11-17=20=E7=94=A8=E6=88=B7=E6=8B=8D?= =?UTF-8?q?=E6=9D=BF=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删 modules/evidence 10 件+EvidenceProperties 注册+application*.yml evidence 块+V205 迁移(DELETE 菜单行+DROP biz_task_evidence);后管 TaskEvidencePage/task-evidence/路由/菜单键/操作指南同步摘除;chunk 分片通道、崩溃记录器本体与设备日志链保留(BACKLOG B115)。 --- admin-frontend-vue/src/api/task-evidence.ts | 61 --- .../src/config/company-scope.ts | 2 +- .../src/layout/operation-guides.ts | 4 - .../src/pages/records/TaskEvidencePage.vue | 305 ------------- admin-frontend-vue/src/router/routes.ts | 1 - .../tests/task-evidence-page.test.ts | 32 -- .../aiimage/config/EvidenceProperties.java | 51 --- .../aiimage/config/PropertiesConfig.java | 2 +- .../AdminTaskEvidenceController.java | 125 ------ .../InternalTaskEvidenceController.java | 59 --- .../evidence/mapper/TaskEvidenceMapper.java | 9 - .../dto/TaskEvidenceRegisterRequest.java | 24 - .../model/entity/TaskEvidenceEntity.java | 45 -- .../evidence/service/TaskEvidenceService.java | 325 -------------- .../storage/TaskEvidenceStorageService.java | 122 ----- .../controller/InternalJdImageController.java | 2 +- .../service/PermissionMenuService.java | 2 +- .../resources/application-local.example.yml | 7 - .../src/main/resources/application.yml | 9 - .../resources/db/V205__drop_task_evidence.sql | 23 + .../common/api/PageDefaultsLedgerTest.java | 18 +- .../SuperAdminPredicateSingleOutletTest.java | 4 +- .../evidence/EvidencePhase3ContractTest.java | 419 ------------------ ...TaskEvidenceOwnershipAndRetentionTest.java | 189 -------- .../ChunkedUploadReclaimContractTest.java | 3 +- 25 files changed, 40 insertions(+), 1803 deletions(-) delete mode 100644 admin-frontend-vue/src/api/task-evidence.ts delete mode 100644 admin-frontend-vue/src/pages/records/TaskEvidencePage.vue delete mode 100644 admin-frontend-vue/tests/task-evidence-page.test.ts delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java delete mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java create mode 100644 backend-java/src/main/resources/db/V205__drop_task_evidence.sql delete mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java delete mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceOwnershipAndRetentionTest.java diff --git a/admin-frontend-vue/src/api/task-evidence.ts b/admin-frontend-vue/src/api/task-evidence.ts deleted file mode 100644 index a8aec8d8e..000000000 --- a/admin-frontend-vue/src/api/task-evidence.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { http } from './http' -import { unwrap } from './envelope' - -/** 任务证据件列表行(AC-13:后管只读面,仅超级管理员)。 */ -export interface TaskEvidenceRow { - id: number - taskId: string - uid: number | null - evidenceType: string - source: string - objectKey: string - byteSize: number - contentType: string - sha256: string - createdAt: string | null -} - -export interface TaskEvidencePage { - items: TaskEvidenceRow[] - total: number - page: number - pageSize: number -} - -export interface TaskEvidenceQuery { - taskId?: string - uid?: number - page: number - pageSize: number -} - -export interface TaskEvidenceContent { - id: number - taskId: string - contentType: string - binary: boolean - content: string -} - -export async function fetchTaskEvidenceFiles(params: TaskEvidenceQuery): Promise { - const query: Record = { page: params.page, pageSize: params.pageSize } - if (params.taskId) query.taskId = params.taskId - if (params.uid) query.uid = params.uid - const { data } = await http.get('/api/admin/task-evidence/files', { params: query }) - return unwrap(data) -} - -export async function fetchTaskEvidenceContent(fileId: number): Promise { - const { data } = await http.get('/api/admin/task-evidence/content', { params: { fileId } }) - return unwrap(data) -} - -/** 下载走服务端流,不把对象直链/预签名 URL 交给浏览器(AC-13 的不入日志口径)。 */ -export function taskEvidenceDownloadUrl(fileId: number): string { - return `/api/admin/task-evidence/download?fileId=${fileId}` -} - -export async function deleteTaskEvidence(ids: number[]): Promise { - const { data } = await http.post('/api/admin/task-evidence/delete', ids) - return unwrap<{ deleted: number }>(data).deleted -} diff --git a/admin-frontend-vue/src/config/company-scope.ts b/admin-frontend-vue/src/config/company-scope.ts index 25d494c45..71d7ec1bd 100644 --- a/admin-frontend-vue/src/config/company-scope.ts +++ b/admin-frontend-vue/src/config/company-scope.ts @@ -3,7 +3,7 @@ * 而护栏用例要从这里反过来读注册表,放在一起会成环。 */ /** 角色内在闸门:只有超管可见可读写,与 user_column_permission 无关。 */ -export const SUPER_ADMIN_ONLY_MENU_KEYS: readonly string[] = ['admin_companies', 'admin_task_evidence'] +export const SUPER_ADMIN_ONLY_MENU_KEYS: readonly string[] = ['admin_companies'] export interface CompanyScopedPage { /** 与 routes.ts 的 menuKey 逐字一致。 */ diff --git a/admin-frontend-vue/src/layout/operation-guides.ts b/admin-frontend-vue/src/layout/operation-guides.ts index ed8fa2781..938075810 100644 --- a/admin-frontend-vue/src/layout/operation-guides.ts +++ b/admin-frontend-vue/src/layout/operation-guides.ts @@ -109,10 +109,6 @@ export const OPERATION_GUIDES: Record = { text: '桌面客户端与麦象采集机上报的日志按来源、日期和关键字筛选;可查看日志尾部、下载完整文件或删除云端记录,云端日志按保留期自动清理。', steps: ['选择来源与日期', '查看或下载日志', '必要时删除'], }, - admin_task_evidence: { - text: '任务异常现场件与窗口图按任务号关联;只有超级管理员能读取与下载,对象直链不出服务端,证据件按保留期自动清理。', - steps: ['按任务号或 uid 查询', '查看现场件或窗口图', '下载或清理过期证据'], - }, admin_user_secret_usage: { text: '按日期范围统计各用户的密钥调用次数(货源查询与外观专利按 LLM 请求次数、代理按提取次数),用于评估用户资源损耗。', steps: ['选择日期范围', '筛选用户或分组', '核对各模块次数'], diff --git a/admin-frontend-vue/src/pages/records/TaskEvidencePage.vue b/admin-frontend-vue/src/pages/records/TaskEvidencePage.vue deleted file mode 100644 index 2c37d90c3..000000000 --- a/admin-frontend-vue/src/pages/records/TaskEvidencePage.vue +++ /dev/null @@ -1,305 +0,0 @@ - - - - - diff --git a/admin-frontend-vue/src/router/routes.ts b/admin-frontend-vue/src/router/routes.ts index 2cb87d2c9..e76cd8d34 100644 --- a/admin-frontend-vue/src/router/routes.ts +++ b/admin-frontend-vue/src/router/routes.ts @@ -34,7 +34,6 @@ export const adminPages: AdminPageDef[] = [ { path: 'records/tutorial', menuKey: 'admin_tutorial', title: '教程管理', companyScoped: false, load: () => import('@/pages/records/RecordsTutorialPage.vue') }, { path: 'records/workbench-client-package', menuKey: 'admin_workbench_client_package', title: '数富AI工作台客户端包管理', companyScoped: false, load: () => import('@/pages/records/WorkbenchClientPackagePage.vue') }, { path: 'records/device-logs', menuKey: 'admin_device_logs', title: '日志管理', companyScoped: true, load: () => import('@/pages/records/DeviceLogsPage.vue') }, - { path: 'records/task-evidence', menuKey: 'admin_task_evidence', title: '任务证据', superAdminOnly: true, companyScoped: false, load: () => import('@/pages/records/TaskEvidencePage.vue') }, { path: 'task-center/board', menuKey: 'admin_task_center', title: '客户端任务管理', companyScoped: true, load: () => import('@/pages/task-center/TaskCenterBoardPage.vue') }, { path: 'records/digital-human-version', menuKey: 'digital_human_version', title: '数字人版本管理', load: () => import('@/pages/records/RecordsDigitalHumanVersionPage.vue') }, { path: 'records/image-video-tasks', menuKey: 'admin_image_video_tasks', title: '视频任务记录', companyScoped: true, load: () => import('@/pages/tasks/ImageVideoTasksPage.vue') }, diff --git a/admin-frontend-vue/tests/task-evidence-page.test.ts b/admin-frontend-vue/tests/task-evidence-page.test.ts deleted file mode 100644 index d558c8736..000000000 --- a/admin-frontend-vue/tests/task-evidence-page.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import test from 'node:test' -import assert from 'node:assert/strict' -import { readSource } from './helpers.ts' - -/** P1-9:任务证据读取面的后台入口必须存在,且只给超级管理员。 */ - -test('task evidence menu is super-admin only', () => { - const routes = readSource('src/router/routes.ts') - assert.match(routes, /path: 'records\/task-evidence'[\s\S]*?menuKey: 'admin_task_evidence'[\s\S]*?superAdminOnly: true/, - '任务证据路由必须带 superAdminOnly') - - const scope = readSource('src/config/company-scope.ts') - assert.match(scope, /SUPER_ADMIN_ONLY_MENU_KEYS[^\n]*admin_task_evidence/, - '菜单 key 必须在超级管理员可见清单里') - - const guide = readSource('src/layout/operation-guides.ts') - assert.match(guide, /admin_task_evidence:\s*\{/, '菜单必须配操作指引') -}) - -test('task evidence page never renders object keys', () => { - const page = readSource('src/pages/records/TaskEvidencePage.vue') - assert.doesNotMatch(page, /objectKey|object_key|presign/i, '页面不得展示或持有对象直链') - assert.match(page, /taskEvidenceDownloadUrl/, '下载必须走鉴权接口') - assert.match(page, /deleteTaskEvidence/, '删除必须走鉴权接口') -}) - -test('task evidence api is wired to admin endpoints', () => { - const api = readSource('src/api/task-evidence.ts') - assert.match(api, /\/admin\/task-evidence\/files/) - assert.match(api, /\/admin\/task-evidence\/download\?fileId=/) - assert.match(api, /\/admin\/task-evidence\/delete/) -}) diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java deleted file mode 100644 index 3be049523..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/config/EvidenceProperties.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.nanri.aiimage.config; - -import lombok.Data; -import org.springframework.boot.context.properties.ConfigurationProperties; - -/** - * 任务异常证据件配置(v1.2 Phase 3/AC-13)。 - * - *

证据件落在**独立桶**:默认桶 nanri-ai-images 带 7 天整桶 ILM, - * 与"证据单独保留、单独回收"的口径互相绑死;桶名必须显式写在 application.yml, - * 不许只在代码里留默认值(部署漏配就变成隐形行为)。

- */ -@Data -@ConfigurationProperties(prefix = "aiimage.evidence") -public class EvidenceProperties { - - /** 证据桶;默认 task-evidence,独立于业务桶。 */ - private String bucket = "task-evidence"; - - /** 证据保留天数(本地与云端同口径,按天回收)。 */ - private Integer retentionDays = 7; - - /** 对象 key 前缀;实际 key = 前缀 + taskId + "/" + UUID + 扩展名。 */ - private String keyPrefix = "task-evidence/"; - - /** 单件证据上限(字节),超过直接拒绝,避免截图/大 JSON 打满桶。 */ - private Long maxFileBytes = 64L * 1024 * 1024; - - /** 保留期回收 cron;默认每天 03:30。 */ - private String retentionCron = "0 30 3 * * *"; - - public String bucketOrDefault() { - return bucket == null || bucket.isBlank() ? "task-evidence" : bucket.trim(); - } - - public int retentionDaysOrDefault() { - return retentionDays == null || retentionDays < 1 ? 7 : retentionDays; - } - - public String keyPrefixOrDefault() { - String prefix = keyPrefix == null ? "" : keyPrefix.trim(); - if (prefix.isEmpty()) { - return "task-evidence/"; - } - return prefix.endsWith("/") ? prefix : prefix + "/"; - } - - public long maxFileBytesOrDefault() { - return maxFileBytes == null || maxFileBytes < 1 ? 64L * 1024 * 1024 : maxFileBytes; - } -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java index 06f039139..2ecf54229 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/PropertiesConfig.java @@ -4,6 +4,6 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Configuration; @Configuration -@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, CollectRoutingProperties.class, EvidenceProperties.class}) +@EnableConfigurationProperties({OssProperties.class, TransientStorageProperties.class, StorageProperties.class, BrandProgressProperties.class, DeleteBrandProgressProperties.class, BrandCheckProperties.class, ZiniaoProperties.class, ModuleCleanupProperties.class, TaskPressureProperties.class, TaskImageCacheCleanupProperties.class, AppearancePatentProperties.class, SimilarAsinProperties.class, ImageVideoProperties.class, InstanceRoutingProperties.class, CapacityPlanProperties.class, UserSecretProperties.class, NotificationProperties.class, DeviceLogOssProperties.class, CollectDataBrandRecheckProperties.class, ClientCredentialProperties.class, CostGuardProperties.class, CollectRoutingProperties.class}) public class PropertiesConfig { } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java deleted file mode 100644 index 4502e97d0..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/AdminTaskEvidenceController.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.nanri.aiimage.modules.evidence.controller; - -import com.nanri.aiimage.common.api.ApiResponse; -import com.nanri.aiimage.common.api.PageDefaults; -import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.model.entity.AdminUserEntity; -import com.nanri.aiimage.common.security.AdminAuthSupport; -import com.nanri.aiimage.common.security.DeviceSessionPolicy; -import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; -import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import java.io.IOException; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * 后台「任务证据」读取面(AC-13):列表 / 内容 / 下载 / 删除,仅超级管理员。 - * - *

证据件含账号可见的现场信息与窗口截图,访问面比常规后台更严;这里每个方法都走 - * 私有 {@link #requireSuperAdmin(HttpServletRequest)},与 AdminDeviceLogController 同口径。 - * 对象直链与预签名 URL 不入日志、不回前端——下载统一走服务端流。

- */ -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/api/admin/task-evidence") -@Tag(name = "任务证据(后台)", description = "任务异常现场证据件的列表、内容、下载与删除(仅超管)。") -public class AdminTaskEvidenceController { - - private final TaskEvidenceService taskEvidenceService; - private final AdminAuthSupport adminAuthSupport; - - @GetMapping("/files") - @Operation(summary = "证据件分页列表", description = "按 taskId 关联查询;uid 只作为精确过滤条件。") - public ApiResponse> files( - HttpServletRequest request, - @Parameter(description = "业务任务号") @RequestParam(required = false) String taskId, - @Parameter(description = "归属用户 uid") @RequestParam(required = false) Long uid, - @RequestParam(defaultValue = PageDefaults.FIRST_PAGE) Long page, - @RequestParam(defaultValue = PageDefaults.ADMIN_LIST_PAGE_SIZE) Long pageSize) { - AdminUserEntity operator = requireSuperAdmin(request); - if (uid != null && !uid.equals(operator.getId())) { - // requireSuperAdmin 已保证当前是超管;uid 过滤仍与操作者归属对撞,防伪造 uid 越权读。 - log.info("[task-evidence] 超管按 uid 过滤证据 userId={} targetUid={}", - operator.getId(), uid); - } - return ApiResponse.success(taskEvidenceService.page(taskId, uid, page, pageSize)); - } - - @GetMapping("/content") - @Operation(summary = "查看证据件内容", description = "文本证据按 UTF-8 返回;图片证据返回 base64。") - public ApiResponse> content( - HttpServletRequest request, - @Parameter(description = "证据主键") @RequestParam Long fileId) { - requireSuperAdmin(request); - return ApiResponse.success(taskEvidenceService.content(fileId)); - } - - @GetMapping("/download") - @Operation(summary = "下载证据件", description = "服务端从独立证据桶流式读取,不返回直链。") - public void download(HttpServletRequest request, HttpServletResponse response, - @Parameter(description = "证据主键") @RequestParam Long fileId) throws IOException { - requireSuperAdmin(request); - TaskEvidenceEntity entity = taskEvidenceService.requireEvidence(fileId); - String name = "evidence-" + entity.getId() - + (entity.getObjectKey() != null && entity.getObjectKey().contains(".") - ? entity.getObjectKey().substring(entity.getObjectKey().lastIndexOf('.')) : ".bin"); - response.setContentType(entity.getContentType() == null - ? "application/octet-stream" : entity.getContentType()); - response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" - + URLEncoder.encode(name, StandardCharsets.UTF_8).replace("+", "%20")); - taskEvidenceService.downloadTo(fileId, response.getOutputStream()); - } - - @PostMapping("/delete") - @Operation(summary = "删除证据件", description = "先删对象再删登记行;不可恢复。") - public ApiResponse> delete( - HttpServletRequest request, - @RequestBody(required = false) List ids, - @RequestParam(required = false) Long fileId) { - requireSuperAdmin(request); - List targets = new ArrayList<>(); - if (ids != null) { - targets.addAll(ids); - } - if (fileId != null) { - targets.add(fileId); - } - if (targets.isEmpty()) { - throw new BusinessException("未指定要删除的证据"); - } - int deleted = taskEvidenceService.delete(targets); - return ApiResponse.success("已删除", Map.of("deleted", deleted)); - } - - /** - * 证据件仅超管可读:requireAdmin 不含超管判定,这里再判一层。 - * 返回操作者供列表端点做 uid 归属对撞。 - */ - private AdminUserEntity requireSuperAdmin(HttpServletRequest request) { - AdminUserEntity user = adminAuthSupport.requireAdmin(request); - if (!DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(user))) { - log.warn("[task-evidence] 非超管访问被拒 userId={} username={}", - user.getId(), user.getUsername()); - throw new BusinessException(403, "仅超级管理员可访问任务证据"); - } - return user; - } -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java deleted file mode 100644 index b7a0f4047..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/controller/InternalTaskEvidenceController.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.nanri.aiimage.modules.evidence.controller; - -import com.nanri.aiimage.common.api.ApiResponse; -import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.security.AdminAuthSupport; -import com.nanri.aiimage.modules.evidence.model.dto.TaskEvidenceRegisterRequest; -import com.nanri.aiimage.modules.evidence.service.TaskEvidenceService; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.Map; - -/** - * 内部接口:客户端证据件登记(AC-7)。客户端先把现场件分片上传到 Java 临时目录, - * 上传完成后携带 X-Internal-Token 与 task_id/file_key 登记;服务端再把临时件搬进 - * 独立证据桶并落 biz_task_evidence。 - * - *

/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller - * 仍自校验令牌——防止配置漂移时匿名可达。

- */ -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/api/internal/task-evidence") -@Tag(name = "内部接口", description = "证据件登记(X-Internal-Token 鉴权)。") -public class InternalTaskEvidenceController { - - private final TaskEvidenceService taskEvidenceService; - private final AdminAuthSupport adminAuthSupport; - - @PostMapping("/register") - @Operation(summary = "登记任务证据件", description = "按 task_id 关联;file_key 指向已上传到临时目录的证据件。") - public ApiResponse> register(HttpServletRequest request, - @RequestBody(required = false) TaskEvidenceRegisterRequest body) { - if (!adminAuthSupport.isTrustedInternalToken(request)) { - log.warn("[task-evidence] 拒绝未携带可信内部令牌的登记请求 remoteAddr={}", - request.getRemoteAddr()); - throw new BusinessException(401, "未授权"); - } - if (body == null) { - throw new BusinessException("登记载荷为空"); - } - // P1-2:归属以会话身份为准。内部令牌是发给每个登录客户的共享凭据,body.uid 谁都能写—— - // 真身份只能来自请求头里的用户 JWT(4.0.14 起 user_token 钩子统一注入);会话 uid 与 - // body.uid 不一致直接 403,无身份的存量装机保留原参数并打节流的匿名兜底日志。 - Long uid = adminAuthSupport.resolveOwnershipUserId(request, body.getUid()); - Map result = taskEvidenceService.register( - body.getTaskId(), uid, body.getEvidenceType(), body.getSource(), body.getFileKey()); - log.info("[task-evidence] registered id={} taskId={} uid={}", result.get("id"), body.getTaskId(), uid); - return ApiResponse.success(result); - } -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java deleted file mode 100644 index 3f9a23844..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/mapper/TaskEvidenceMapper.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.nanri.aiimage.modules.evidence.mapper; - -import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; -import org.apache.ibatis.annotations.Mapper; - -@Mapper -public interface TaskEvidenceMapper extends BaseMapper { -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java deleted file mode 100644 index feb22fb3d..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/dto/TaskEvidenceRegisterRequest.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.nanri.aiimage.modules.evidence.model.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Data; - -/** - * 客户端证据登记载荷(snake_case 与客户端现场件字段同口径)。 - */ -@Data -public class TaskEvidenceRegisterRequest { - - @JsonProperty("task_id") - private String taskId; - - @JsonProperty("file_key") - private String fileKey; - - @JsonProperty("evidence_type") - private String evidenceType; - - private String source; - - private Long uid; -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java deleted file mode 100644 index 9de691ee2..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/model/entity/TaskEvidenceEntity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.nanri.aiimage.modules.evidence.model.entity; - -import com.baomidou.mybatisplus.annotation.IdType; -import com.baomidou.mybatisplus.annotation.TableId; -import com.baomidou.mybatisplus.annotation.TableName; -import lombok.Data; - -import java.time.LocalDateTime; - -/** - * 任务异常现场证据件登记(表 biz_task_evidence,V182)。 - * - *

只登记归属与对象元数据;对象本体在独立证据桶,直链/预签名 URL 不入库、不入日志。

- */ -@Data -@TableName("biz_task_evidence") -public class TaskEvidenceEntity { - - @TableId(type = IdType.AUTO) - private Long id; - - /** 业务任务号(客户端字符串口径,允许非数字)。 */ - private String taskId; - - /** 任务归属用户;未知时为空。 */ - private Long uid; - - /** 证据类型:client/crash/brand 等。 */ - private String evidenceType; - - /** 来源入口(崩溃记录器/商品采集等)。 */ - private String source; - - /** 证据桶内对象 key(含 UUID)。 */ - private String objectKey; - - private Long byteSize; - - private String contentType; - - /** 对象 sha256(防篡改/去重)。 */ - private String sha256; - - private LocalDateTime createdAt; -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java deleted file mode 100644 index 5a4b1cb52..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceService.java +++ /dev/null @@ -1,325 +0,0 @@ -package com.nanri.aiimage.modules.evidence.service; - -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; -import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.service.DistributedJobLockService; -import com.nanri.aiimage.common.task.LockSkipWitness; -import com.nanri.aiimage.config.EvidenceProperties; -import com.nanri.aiimage.modules.evidence.mapper.TaskEvidenceMapper; -import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; -import com.nanri.aiimage.modules.evidence.storage.TaskEvidenceStorageService; -import com.nanri.aiimage.modules.file.service.LocalFileStorageService; -import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; -import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Service; - -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; -import java.nio.file.Files; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; - -/** - * 证据登记与读取(AC-7/AC-13)。 - * - *

登记走「客户端先分片上云到 Java 临时目录,再由服务端搬进独立证据桶」:客户端不持有 - * 任何对象存储凭据,object_key/sha256/size 全部由服务端生成,登记接口不接受直链。

- */ -@Slf4j -@Service -@RequiredArgsConstructor -public class TaskEvidenceService { - - private static final int MAX_TASK_ID_LENGTH = 64; - private static final int MAX_CONTENT_BYTES = 8 * 1024 * 1024; - private static final int RETENTION_BATCH = 200; - /** 单轮保留期最多扫的批次数:防一个删不掉的对象把进程拖成永动机。 */ - private static final int MAX_RETENTION_BATCHES = 20; - /** 同一任务的证据件数上限:登记端点不设防的话,一个持证客户端能刷满整个证据桶(P2-2)。 */ - private static final int MAX_EVIDENCE_PER_TASK = 50; - /** 保留期回收的分布式锁名:A/B 两台每晚各扫同一张表会互相删(同族口径见 DeviceLogRetentionService)。 */ - static final String RETENTION_LOCK = "task-evidence:retention"; - private static final Set ALLOWED_EVIDENCE_TYPES = - Set.of("client", "crash", "window_image", "page_image", "brand", "other"); - private static final Set ALLOWED_SOURCES = - Set.of("client", "crash_recorder", "window", "page", "brand", "other"); - - private final TaskEvidenceMapper mapper; - private final TaskEvidenceStorageService storage; - private final LocalFileStorageService localFileStorageService; - private final EvidenceProperties properties; - private final FileTaskMapper fileTaskMapper; - private final DistributedJobLockService distributedJobLockService; - - /** 客户端上传完成后登记:fileKey 指向 Java 临时目录里的分片拼装件。 */ - public Map register(String taskId, Long uid, String evidenceType, - String source, String fileKey) { - String normalizedTaskId = normalizeTaskId(taskId); - if (fileKey == null || fileKey.isBlank()) { - throw new BusinessException("证据文件标识为空"); - } - File file = localFileStorageService.findLocalSourceFile(fileKey.trim()); - if (file == null || !file.isFile()) { - throw new BusinessException("证据文件不存在或已过期"); - } - // P1-2:归属先于落桶。task_id 能对上库里的任务行时,uid 必须就是行主——判据发生在 - // storage.store 之前,否则每一次拒绝都往证据桶里丢一个收不回的孤儿对象。 - assertTaskOwnedBy(normalizedTaskId, uid); - if (file.length() == 0) { - throw new BusinessException("证据件为空,拒绝登记"); - } - if (countByTask(normalizedTaskId) >= MAX_EVIDENCE_PER_TASK) { - throw new BusinessException("该任务的证据件数已达上限"); - } - TaskEvidenceStorageService.StoredEvidence stored; - try { - stored = storage.store(file, normalizedTaskId, file.getName()); - } catch (IOException | RuntimeException ex) { - log.warn("[task-evidence] register failed taskId={} reason={}", - normalizedTaskId, ex.getMessage()); - throw new BusinessException("证据件入库失败,请稍后重试"); - } - TaskEvidenceEntity entity = new TaskEvidenceEntity(); - entity.setTaskId(normalizedTaskId); - entity.setUid(uid); - entity.setEvidenceType(normalizeLabel(evidenceType, "client", ALLOWED_EVIDENCE_TYPES)); - entity.setSource(normalizeLabel(source, "client", ALLOWED_SOURCES)); - entity.setObjectKey(stored.objectKey()); - entity.setByteSize(stored.byteSize()); - entity.setContentType(stored.contentType()); - entity.setSha256(stored.sha256()); - entity.setCreatedAt(LocalDateTime.now()); - try { - mapper.insert(entity); - } catch (RuntimeException ex) { - // 对象已写进桶、登记行没落:不回收就是永久孤儿(保留期只扫登记行)。 - log.warn("[task-evidence] 登记行写入失败,回收对象 taskId={} key={} reason={}", - normalizedTaskId, stored.objectKey(), ex.getMessage()); - try { - storage.delete(stored.objectKey()); - } catch (RuntimeException cleanup) { - log.warn("[task-evidence] 回收孤儿对象失败 key={} reason={}", - stored.objectKey(), cleanup.getMessage()); - } - throw new BusinessException("证据件登记失败,请稍后重试"); - } - deleteTempQuietly(file); - Map result = new LinkedHashMap<>(); - result.put("id", entity.getId()); - result.put("objectKey", entity.getObjectKey()); - result.put("byteSize", entity.getByteSize()); - return result; - } - - public Map page(String taskId, Long uid, long page, long pageSize) { - long safePage = Math.max(1, page); - long safeSize = Math.min(Math.max(1, pageSize), 200); - LambdaQueryWrapper query = new LambdaQueryWrapper<>(); - if (taskId != null && !taskId.isBlank()) { - query.eq(TaskEvidenceEntity::getTaskId, taskId.trim()); - } - if (uid != null) { - query.eq(TaskEvidenceEntity::getUid, uid); - } - query.orderByDesc(TaskEvidenceEntity::getId); - Page result = mapper.selectPage(new Page<>(safePage, safeSize), query); - Map data = new LinkedHashMap<>(); - data.put("items", result.getRecords()); - data.put("total", result.getTotal()); - data.put("page", safePage); - data.put("pageSize", safeSize); - return data; - } - - public TaskEvidenceEntity requireEvidence(Long fileId) { - if (fileId == null) { - throw new BusinessException("证据主键为空"); - } - TaskEvidenceEntity entity = mapper.selectById(fileId); - if (entity == null) { - throw new BusinessException("证据不存在或已过期"); - } - return entity; - } - - public Map content(Long fileId) { - TaskEvidenceEntity entity = requireEvidence(fileId); - byte[] bytes = storage.read(entity.getObjectKey(), MAX_CONTENT_BYTES); - Map data = new LinkedHashMap<>(); - data.put("id", entity.getId()); - data.put("taskId", entity.getTaskId()); - data.put("contentType", entity.getContentType()); - boolean binary = entity.getContentType() != null && entity.getContentType().startsWith("image/"); - data.put("binary", binary); - data.put("content", binary - ? java.util.Base64.getEncoder().encodeToString(bytes) - : new String(bytes, java.nio.charset.StandardCharsets.UTF_8)); - return data; - } - - public byte[] download(Long fileId) { - TaskEvidenceEntity entity = requireEvidence(fileId); - return storage.read(entity.getObjectKey(), properties.maxFileBytesOrDefault()); - } - - /** P3-1:后管下载走流式,避免 64MB 级对象整块进堆。 */ - public void downloadTo(Long fileId, OutputStream out) { - TaskEvidenceEntity entity = requireEvidence(fileId); - storage.downloadTo(entity.getObjectKey(), out, properties.maxFileBytesOrDefault()); - } - - /** 删除证据:先删对象再删登记行;对象删除失败不吞(留在库里下一轮可重试)。 */ - public int delete(List ids) { - if (ids == null || ids.isEmpty()) { - return 0; - } - int deleted = 0; - for (Long id : ids) { - TaskEvidenceEntity entity = id == null ? null : mapper.selectById(id); - if (entity == null) { - continue; - } - storage.delete(entity.getObjectKey()); - deleted += mapper.deleteById(id); - } - return deleted; - } - - /** - * 保留期回收:按天清扫到期行与对象(AC-13 的保留期必须有执行者,不能只写在配置里)。 - * - *

P1-7 的两条修正:① A/B 两台每晚并发扫同一张表,回收必须拿分布式锁(同族口径见 - * {@code DeviceLogRetentionService},锁跳过走可聚合出口);② 对象删除失败不再让整轮早退—— - * 原先翻页永远停在第 1 页,卡住的那一行把后面每一行都堵死,配置里的 retention-days - * 因此只是个数字。改成按 id 键集分页:失败行留在库里下一轮重试,后面的行照常推进。 - */ - @Scheduled(cron = "${aiimage.evidence.retention-cron:0 30 3 * * *}") - public void purgeExpired() { - DistributedJobLockService.LockHandle lockHandle = - distributedJobLockService.tryLock(RETENTION_LOCK, Duration.ofMinutes(15)); - if (lockHandle == null) { - // 点位字面量必须写死(LockSkipSilentExitGuardrailTest 的判据),与 RETENTION_LOCK 同值。 - LockSkipWitness.skipped("task-evidence:retention"); - log.info("[task-evidence] 保留期清理跳过:另一实例持锁"); - return; - } - try (lockHandle) { - purgeExpiredLocked(LocalDateTime.now().minusDays(properties.retentionDaysOrDefault())); - } catch (Exception ex) { - log.warn("[task-evidence] 保留期清理失败 reason={}", ex.getMessage(), ex); - } - } - - private void purgeExpiredLocked(LocalDateTime expireBefore) { - long afterId = 0L; - int removed = 0; - int failed = 0; - int batches = 0; - while (batches < MAX_RETENTION_BATCHES) { - List batch = mapper.selectList(new LambdaQueryWrapper() - .gt(TaskEvidenceEntity::getId, afterId) - .lt(TaskEvidenceEntity::getCreatedAt, expireBefore) - .orderByAsc(TaskEvidenceEntity::getId) - .last("limit " + RETENTION_BATCH)); - if (batch.isEmpty()) { - break; - } - batches++; - for (TaskEvidenceEntity entity : batch) { - afterId = entity.getId(); - try { - storage.delete(entity.getObjectKey()); - removed += mapper.deleteById(entity.getId()); - } catch (RuntimeException ex) { - // 只跳过这一行:登记行留着,下一轮按同一条键集再试;后面的行绝不陪葬。 - failed++; - log.warn("[task-evidence] retention delete failed id={} reason={}", - entity.getId(), ex.getMessage()); - } - } - } - if (failed > 0) { - log.warn("[task-evidence] retention purge finished with failures removed={} failed={} batches={}", - removed, failed, batches); - } else if (removed > 0 || batches > 0) { - log.info("[task-evidence] retention purge removed={} batches={}", removed, batches); - } - } - - /** - * P1-2 的归属判定:task_id 对得上库里的任务行时 uid 必须就是行主;对不上行(客户端本地 - * 任务号,如商品采集的自有作业 id)只要求带 uid 自证——否则任何一个持内部令牌的客户端 - * 都能往别人的取证面里挂证据。 - */ - private void assertTaskOwnedBy(String taskId, Long uid) { - Long ownerId = resolveTaskOwnerId(taskId); - if (uid == null) { - throw new BusinessException(403, "证据登记必须携带任务归属用户"); - } - if (ownerId != null && !ownerId.equals(uid)) { - throw new BusinessException(403, "证据件只能登记到本人任务上"); - } - } - - /** 数字串按主键查(超长数字串会溢出,降级走 task_no),其余按 task_no 点查。 */ - private Long resolveTaskOwnerId(String taskId) { - FileTaskEntity row = null; - if (taskId.chars().allMatch(Character::isDigit)) { - try { - row = fileTaskMapper.selectById(Long.parseLong(taskId)); - } catch (NumberFormatException overflow) { - row = null; - } - } - if (row == null) { - row = fileTaskMapper.selectOne(new LambdaQueryWrapper() - .eq(FileTaskEntity::getTaskNo, taskId) - .last("limit 1")); - } - return row == null ? null : row.getUserId(); - } - - private long countByTask(String taskId) { - Long count = mapper.selectCount(new LambdaQueryWrapper() - .eq(TaskEvidenceEntity::getTaskId, taskId)); - return count == null ? 0L : count; - } - - private String normalizeTaskId(String taskId) { - String value = taskId == null ? "" : taskId.trim(); - if (value.isEmpty()) { - throw new BusinessException("任务号为空"); - } - if (value.length() > MAX_TASK_ID_LENGTH) { - value = value.substring(0, MAX_TASK_ID_LENGTH); - } - return value; - } - - private String normalizeLabel(String value, String fallback, Set allowed) { - String text = value == null ? "" : value.trim().toLowerCase(Locale.ROOT); - if (text.isEmpty()) { - return fallback; - } - // 列宽是 VARCHAR(32),超长标签在这里截断而不是让 insert 抛数据截断错; - // 不在白名单内的标签一律落 other,不让客户端自造类型污染取证面。 - String normalized = text.length() > 32 ? text.substring(0, 32) : text; - return allowed.contains(normalized) ? normalized : "other"; - } - - private void deleteTempQuietly(File file) { - if (file != null && file.exists() && !file.delete()) { - log.warn("[task-evidence] temp evidence cleanup failed name={}", file.getName()); - } - } -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java deleted file mode 100644 index a22042aa2..000000000 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/evidence/storage/TaskEvidenceStorageService.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.nanri.aiimage.modules.evidence.storage; - -import cn.hutool.crypto.digest.DigestUtil; -import com.nanri.aiimage.config.EvidenceProperties; -import com.nanri.aiimage.modules.file.service.oss.OssStorageService; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; - -import java.io.File; -import java.io.IOException; -import java.util.Locale; -import java.util.UUID; -import java.util.regex.Pattern; - -/** - * 证据对象的独立落点(AC-13):桶与 key 前缀来自 {@link EvidenceProperties}, - * 对象本体不经过业务桶,也不把直链/预签名 URL 写进任何日志或数据库。 - */ -@Slf4j -@Service -@RequiredArgsConstructor -public class TaskEvidenceStorageService { - - private static final Pattern TASK_ID_SAFE = Pattern.compile("[^0-9A-Za-z_.-]+"); - private static final Pattern EXT_SAFE = Pattern.compile("\\.[0-9A-Za-z]{1,10}"); - - private final EvidenceProperties properties; - private final OssStorageService ossStorageService; - - private volatile boolean bucketReady; - - /** 落一份证据对象,返回对象 key 与对象元数据。 */ - public StoredEvidence store(File source, String taskId, String originalFilename) throws IOException { - if (source == null || !source.isFile()) { - throw new IllegalArgumentException("evidence file must exist"); - } - long size = source.length(); - if (size > properties.maxFileBytesOrDefault()) { - throw new IllegalArgumentException("evidence file too large"); - } - String bucket = properties.bucketOrDefault(); - ensureBucket(bucket); - String key = objectKey(taskId, originalFilename); - ossStorageService.uploadFileToBucket(source, bucket, key); - return new StoredEvidence(key, size, sha256(source), contentType(originalFilename)); - } - - public byte[] read(String objectKey, long maxBytes) { - return ossStorageService.readObjectBytes( - properties.bucketOrDefault(), objectKey, maxBytes); - } - - /** P3-1:下载走流式,不把整个证据件读进堆。 */ - public void downloadTo(String objectKey, java.io.OutputStream out, long maxBytes) { - ossStorageService.readObjectToStream(properties.bucketOrDefault(), objectKey, out, maxBytes); - } - - public void delete(String objectKey) { - if (objectKey == null || objectKey.isBlank()) { - return; - } - ossStorageService.removeObject(properties.bucketOrDefault(), objectKey); - } - - public String bucket() { - return properties.bucketOrDefault(); - } - - private void ensureBucket(String bucket) { - if (bucketReady) { - return; - } - synchronized (this) { - if (bucketReady) { - return; - } - ossStorageService.ensureBucketExists(bucket); - bucketReady = true; - log.info("[task-evidence] evidence bucket ready bucket={}", bucket); - } - } - - private String objectKey(String taskId, String originalFilename) { - String safeTask = TASK_ID_SAFE.matcher(taskId == null ? "" : taskId.trim()) - .replaceAll("_"); - if (safeTask.isEmpty()) { - safeTask = "unknown"; - } - return properties.keyPrefixOrDefault() + safeTask + "/" + UUID.randomUUID() + extension(originalFilename); - } - - private String extension(String originalFilename) { - String name = originalFilename == null ? "" : originalFilename.trim(); - int slash = Math.max(name.lastIndexOf('/'), name.lastIndexOf('\\')); - if (slash >= 0) { - name = name.substring(slash + 1); - } - int dot = name.lastIndexOf('.'); - String ext = dot < 0 ? "" : name.substring(dot).toLowerCase(Locale.ROOT); - return EXT_SAFE.matcher(ext).matches() ? ext : ".bin"; - } - - private String contentType(String originalFilename) { - String ext = extension(originalFilename); - return switch (ext) { - case ".json" -> "application/json"; - case ".png" -> "image/png"; - case ".jpg", ".jpeg" -> "image/jpeg"; - case ".webp" -> "image/webp"; - case ".txt", ".log" -> "text/plain; charset=utf-8"; - default -> "application/octet-stream"; - }; - } - - private String sha256(File source) { - return DigestUtil.sha256Hex(source); - } - - public record StoredEvidence(String objectKey, long byteSize, String sha256, String contentType) { - } -} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/InternalJdImageController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/InternalJdImageController.java index fc957461d..46c9c8689 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/InternalJdImageController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/jd/controller/InternalJdImageController.java @@ -26,7 +26,7 @@ import org.springframework.web.bind.annotation.RestController; * (补登记口)登记落 {@code biz_jd_product_image}——服务端零图片字节。

* *

/api/internal 虽在 AdminApiGuardFilter 兜底名单内、可信令牌会放行,controller - * 仍自校验令牌——防止配置漂移时匿名可达(InternalTaskEvidenceController 同款)。 + * 仍自校验令牌——防止配置漂移时匿名可达。 * 身份以请求头 JWT 为准(user_token 钩子统一注入),body.uid 只作存量装机兜底; * 任务级 owner 对撞与 companyId 投影收口在 {@link JdProductImageIngestService}。

*/ diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java index 423493a80..3b2cc9383 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/permission/service/PermissionMenuService.java @@ -55,7 +55,7 @@ public class PermissionMenuService { /** 角色内在闸门菜单:非超管看不见、谁也授不出去(设计文档 §8.1)。 * 与前端 src/config/company-scope.ts 的 SUPER_ADMIN_ONLY_MENU_KEYS 必须同值,由 Task 7 的跨端用例钉住。 */ private static final Set SUPER_ADMIN_ONLY_KEYS = - Set.of("admin_companies", "admin_task_evidence"); + Set.of("admin_companies"); /** * 管理/平台配置类菜单:出口仍是 {@code requireAdmin}(用户、菜单、分组、密钥、公司、版本、教程、 diff --git a/backend-java/src/main/resources/application-local.example.yml b/backend-java/src/main/resources/application-local.example.yml index a4ceb69f4..bb9f33338 100644 --- a/backend-java/src/main/resources/application-local.example.yml +++ b/backend-java/src/main/resources/application-local.example.yml @@ -48,13 +48,6 @@ AIIMAGE_JD_SKU_MAX_PER_PRODUCT=200 # 设成空串会把白名单清空(兜底拉取对 JD 形同关闭)。打开 AIIMAGE_CLIENT_TASK_PULL_ENABLED # 前先确认客户端版本带兜底拉取线程。 -# 任务异常现场证据件(v1.2 Phase 3):独立桶与保留期必须显式配置;桶不得复用 nanri-ai-images。 -AIIMAGE_EVIDENCE_OSS_BUCKET=task-evidence -AIIMAGE_EVIDENCE_RETENTION_DAYS=7 -AIIMAGE_EVIDENCE_RETENTION_CRON=0 30 3 * * * -AIIMAGE_EVIDENCE_MAX_FILE_BYTES=67108864 -AIIMAGE_EVIDENCE_KEY_PREFIX=task-evidence/ - # 工作台客户端包直传预签名有效期(秒);90MB 级包在慢公网入口需要更长窗口。 AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS=3600 diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index eeee080e3..0bec629c0 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -191,15 +191,6 @@ aiimage: access-key-id: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_ID:} access-key-secret: ${AIIMAGE_DEVICE_LOG_OSS_ACCESS_KEY_SECRET:} retention-days: ${AIIMAGE_DEVICE_LOG_RETENTION_DAYS:7} - # 任务异常现场证据件(v1.2 Phase 3/AC-7/AC-13):独立桶 + 独立保留期。 - # 桶必须与业务桶 nanri-ai-images 隔离(那边是 7 天整桶 ILM,证据保留策略不得被它绑死); - # key-prefix 之后实际 key = /<扩展名>(铁律 10:key 必带 UUID)。 - evidence: - bucket: ${AIIMAGE_EVIDENCE_OSS_BUCKET:task-evidence} - retention-days: ${AIIMAGE_EVIDENCE_RETENTION_DAYS:7} - key-prefix: ${AIIMAGE_EVIDENCE_KEY_PREFIX:task-evidence/} - max-file-bytes: ${AIIMAGE_EVIDENCE_MAX_FILE_BYTES:67108864} - retention-cron: ${AIIMAGE_EVIDENCE_RETENTION_CRON:0 30 3 * * *} transient-storage: enabled: ${AIIMAGE_TRANSIENT_STORAGE_ENABLED:true} endpoint: ${AIIMAGE_TRANSIENT_STORAGE_ENDPOINT:http://121.196.149.225:9000} diff --git a/backend-java/src/main/resources/db/V205__drop_task_evidence.sql b/backend-java/src/main/resources/db/V205__drop_task_evidence.sql new file mode 100644 index 000000000..2597d41f2 --- /dev/null +++ b/backend-java/src/main/resources/db/V205__drop_task_evidence.sql @@ -0,0 +1,23 @@ +-- 迁移 V205:任务证据特性整体下线(2026-11-17 用户拍板) +-- +-- 背景:v1.2 的「任务异常现场存证」(客户端采集+分片上云+后管读取面)整体删除—— +-- 上线至今登记行为零(装机面未升级+触发口窄+崩溃路被归属校验 403 钉死), +-- 读取面 AdminTaskEvidenceController、内部登记 InternalTaskEvidenceController 与 +-- 客户端 evidence_recorder 链同窗下线(铁律 18/22/24 随之退役)。 +-- +-- 本迁移做两件事: +-- 1. 删除 V185 下发的每公司菜单行(后管侧边栏「任务证据」入口); +-- 2. DROP 登记表 biz_task_evidence(V182 建表、V184 第二段补 idx_task_evidence_created_at; +-- 索引随表删除,无需单独 DROP)。 +-- 表内只有 7 天保留期的现场件登记行,随特性下线无保留价值;对象本体在独立桶 +-- task-evidence,桶的清理由运维窗口另行执行(登记 ops BACKLOG),不依赖本迁移。 +-- +-- 回滚(手工): +-- 1. 按 V182 原样重建 biz_task_evidence(建表+两索引),再按 V184 第二段补 +-- idx_task_evidence_created_at; +-- 2. 按 V185 语义重插 columns 菜单行(每公司一行,column_key='admin_task_evidence')。 +-- 数据不可回滚:登记行随 DROP 丢弃(7 天现场件,无保留价值)。 + +DELETE FROM `columns` WHERE `column_key` = 'admin_task_evidence'; + +DROP TABLE IF EXISTS `biz_task_evidence`; diff --git a/backend-java/src/test/java/com/nanri/aiimage/common/api/PageDefaultsLedgerTest.java b/backend-java/src/test/java/com/nanri/aiimage/common/api/PageDefaultsLedgerTest.java index 6e2ce436c..4787c7814 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/common/api/PageDefaultsLedgerTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/common/api/PageDefaultsLedgerTest.java @@ -42,8 +42,6 @@ class PageDefaultsLedgerTest { * 54 条,逐条对应一次真实翻页行为;加页要登记,改值要表态。 */ private static final List LEDGER = List.of( - "AdminTaskEvidenceController.java#page=FIRST_PAGE", - "AdminTaskEvidenceController.java#pageSize=ADMIN_LIST_PAGE_SIZE", "AdminUserController.java#page=FIRST_PAGE", "AdminUserController.java#page_size=USER_LIST_PAGE_SIZE", "AdminUserApiSecretController.java#page=FIRST_PAGE", @@ -121,14 +119,15 @@ class PageDefaultsLedgerTest { List constants = actual.stream().filter(s -> !s.contains("LITERAL")).sorted().toList(); assertThat(constants) - .as("60 处分页/条数出站点的具名默认值必须与台账一一对应" + .as("分页/条数出站点的具名默认值必须与台账一一对应" + "(58→60:v1.4 Phase 5 京东数据库后管只读页 AdminJdProductController 的" - + " page/page_size 两处出站点)") + + " page/page_size 两处出站点;60→59:2026-11-17 任务证据特性下线," + + " AdminTaskEvidenceController 两处出站点随控制器删除)") .isEqualTo(LEDGER.stream().sorted().toList()); - // 同口径非零对照:台账不是空的,扫描器也确实数得出 61 处 + // 同口径非零对照:台账不是空的,扫描器也确实数得出 59 处 // (61 而不是 60:2026-11-15 京东任务面板改版(45d0989a)新增 GET /api/jd-collect/history - // 的 limit 出站点,台账同步登记 58→60→61) - assertThat(constants).hasSize(61); + // 的 limit 出站点,台账同步登记 58→60→61;61→59:任务证据特性下线随删两处) + assertThat(constants).hasSize(59); } @Test @@ -143,8 +142,9 @@ class PageDefaultsLedgerTest { void oneSemanticCannotGrowASecondDefault() { List actual = sites(); assertThat(kindsUsing(actual, "page", "FIRST_PAGE")) - .as("页码口径只有一套:全部从 1 起(26→27:v1.4 Phase 5 京东数据库只读页也是分页出站点)") - .isEqualTo(27); + .as("页码口径只有一套:全部从 1 起(26→27:v1.4 Phase 5 京东数据库只读页也是分页出站点;" + + "27→26:2026-11-17 任务证据特性下线随删一处)") + .isEqualTo(26); assertThat(distinctConstants(actual, "page_size")) .as("page_size 的默认容量集合:10/15/20/50/1000 各自登记(20 有两把名字:超管台 " + "ADMIN_LIST_PAGE_SIZE 与 v1.4 京东数据库 JD_PRODUCTS_PAGE_SIZE),新语义要红") diff --git a/backend-java/src/test/java/com/nanri/aiimage/common/security/SuperAdminPredicateSingleOutletTest.java b/backend-java/src/test/java/com/nanri/aiimage/common/security/SuperAdminPredicateSingleOutletTest.java index b7a49c788..c430d98b3 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/common/security/SuperAdminPredicateSingleOutletTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/common/security/SuperAdminPredicateSingleOutletTest.java @@ -128,8 +128,10 @@ class SuperAdminPredicateSingleOutletTest { // 20 -> 22:v1.4 Phase 5 京东数据库读取面——AdminJdProductController#requireJdDatabaseAccess // 与 AdminJdProductGroupController#requireJdDatabaseAccess 两处超管直通 // (2026-11-14 整量回归补记,同 AdminTaskEvidenceController 先例)。 + // 22 -> 21:任务证据特性整体下线(2026-11-17 用户拍板),AdminTaskEvidenceController + // 连同模块删除,该出口随之消失。 .as("走出口的调用点数:17 个业务调用方 + DataReadScope 包装体(新增调用要一起改这个数字,别改回字面量)") - .isEqualTo(22); + .isEqualTo(21); // 自检:这三把尺子的锚点必须真落在预期的文件里,否则"恰好也是 N"可能是扫错地方凑出来的 assertThat(read(mainRoot().resolve("common/security/DeviceSessionPolicy.java"))) .contains("public static boolean isSuper(String resolvedRole)"); diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java deleted file mode 100644 index 3c628edf1..000000000 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/EvidencePhase3ContractTest.java +++ /dev/null @@ -1,419 +0,0 @@ -package com.nanri.aiimage.modules.evidence; - -import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.model.entity.AdminUserEntity; -import com.nanri.aiimage.common.security.AdminAuthSupport; -import com.nanri.aiimage.common.security.DeviceSessionPolicy; -import jakarta.servlet.http.HttpServletRequest; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Stream; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * AC-7/AC-13/AC-14(R3):证据件上云后的服务端落点与访问面契约。 - * - *

Pending Phase 3 激活。Phase 0 预置的判据(接缝名是判据的一部分,改名必须同步改本文件 - * 并在蓝图 §5 登记):

- * - *
    - *
  • Flyway {@code V182+} 新增 {@code biz_task_evidence}:task_id/uid/evidence_type/source/ - * object_key/byte_size/content_type/sha256/created_at,按 task_id 关联但**不建外键**;
  • - *
  • {@code application.yml} 的 {@code evidence:} 块显式落 bucket(独立桶,不得为 - * nanri-ai-images)、retention-days(必须有数字默认值)、key-prefix;
  • - *
  • 证据读取端点落在 {@code /api/admin/task-evidence/**}(files/content/download/delete), - * 每个方法都走私有 {@code requireSuperAdmin(HttpServletRequest)}(对齐 - * {@code AdminDeviceLogController:167-175});
  • - *
  • 守卫行为三样本:无 token/bad token → 401、非超管 → 403、超管 → 放行;伪造 uid 由 - * files 端点的 uid 归属校验兜住(源码判据);
  • - *
  • 证据模块日志零命中 presign / 直链(AC-13);对象 key 带 UUID(铁律 10)。
  • - *
- * - *

四样本中的「合法 JWT 200」在 Phase 3 由真机/集成用例补齐(本文件钉守卫放行分支, - * 蓝图 Phase 3 准出另见 {@code 鉴权四样本真机留痕})。

- */ -class EvidencePhase3ContractTest { - - private static final Path MODULE = Path.of("").toAbsolutePath(); - private static final Path MAIN_JAVA = MODULE.resolve("src/main/java"); - private static final Path DB_DIR = MODULE.resolve("src/main/resources/db"); - private static final Path APP_YML = MODULE.resolve("src/main/resources/application.yml"); - private static final String CONTROLLER_FQCN = - "com.nanri.aiimage.modules.evidence.controller.AdminTaskEvidenceController"; - private static final String EVIDENCE_PATH = "/api/admin/task-evidence"; - - @Test - void 迁移必须新增证据表且字段与索引齐全() throws IOException { - List migrations = new ArrayList<>(); - try (Stream stream = Files.list(DB_DIR)) { - stream.filter(path -> path.getFileName().toString().matches("V\\d+__.*\\.sql")) - .filter(path -> versionOf(path.getFileName().toString()) >= 182) - .forEach(migrations::add); - } - assertFalse(migrations.isEmpty(), "AC-7:本迭代必须新增 V182+ 迁移(当前最高 V181)"); - - StringBuilder joined = new StringBuilder(); - for (Path migration : migrations) { - joined.append(read(migration)).append('\n'); - } - String all = joined.toString(); - Matcher table = Pattern.compile("(?is)create\\s+table[^;]*biz_task_evidence[^;]*;") - .matcher(all); - assertTrue(table.find(), "AC-7:V182+ 必须建 biz_task_evidence 表(蓝图 §3.3)"); - String ddl = table.group(); - for (String column : List.of("task_id", "uid", "evidence_type", "source", "object_key", - "byte_size", "content_type", "sha256", "created_at")) { - assertTrue(Pattern.compile("(?i)\\b" + column + "\\b").matcher(ddl).find(), - "AC-7:证据表缺列 " + column); - } - assertFalse(ddl.toUpperCase().contains("FOREIGN KEY"), - "AC-7:证据表按 task_id 关联但不建外键(全库唯一 FK 是 V80:47)"); - assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*task_id").matcher(ddl).find(), - "AC-7:必须有 task_id 索引(按任务关联查询)"); - assertTrue(Pattern.compile("(?i)(index|key)\\s+\\w+\\s*\\(\\s*uid").matcher(ddl).find(), - "AC-7:必须有 uid 索引(数据范围/清理)"); - } - - @Test - void 证据桶与保留期必须显式落在applicationYml() throws IOException { - String yaml = read(APP_YML); - String block = yamlBlock(yaml, "evidence"); - assertNotNull(block, "AC-13:application.yml 必须有 evidence: 配置块(不许只落代码默认值)"); - - String bucket = scalar(block, "bucket"); - assertNotNull(bucket, "AC-13:必须配置证据桶"); - String bucketDefault = defaultValue(bucket); - assertNotNull(bucketDefault, "AC-13:证据桶不得只有变量无默认值(部署漏配即隐形)"); - assertFalse(bucketDefault.contains("nanri-ai-images"), - "AC-13:证据必须落独立桶,不得复用业务桶 nanri-ai-images"); - - String retention = scalar(block, "retention-days"); - assertNotNull(retention, "AC-13:必须配置保留天数"); - String retentionDefault = defaultValue(retention); - assertNotNull(retentionDefault, "AC-13:保留天数必须有确定的数字默认值(天数要进判据)"); - assertTrue(Integer.parseInt(retentionDefault) >= 1, "AC-13:保留天数必须为正整数"); - - assertNotNull(scalar(block, "key-prefix"), "AC-13:必须配置对象 key 前缀"); - } - - @Test - void 证据读取端点必须全部走super访问面() throws IOException { - Path controller = evidenceController(); - String source = read(controller); - assertTrue(source.contains(EVIDENCE_PATH), - "AC-13:证据读取控制器必须落在 " + EVIDENCE_PATH); - - List bodies = mappedMethodBodies(source); - assertTrue(bodies.size() >= 4, - "AC-13:证据读取至少要有 files/content/download/delete 四个端点,实扫 " + bodies.size()); - for (String body : bodies) { - assertTrue(body.contains("requireSuperAdmin"), - "AC-13:证据读取端点必须逐个走 requireSuperAdmin(非 super 一律 403)\n" + body); - } - assertTrue(source.contains("isSuper"), - "AC-13:超管判定必须复用 DeviceSessionPolicy.isSuper 单出口"); - assertFalse(source.toLowerCase().contains("permitall"), - "AC-13:证据读取端点不得 permitAll"); - - for (String endpoint : List.of("\"/files\"", "\"/content\"", "\"/download\"", "\"/delete\"")) { - assertTrue(source.contains(endpoint), "AC-13:缺端点 " + endpoint); - } - - String filesBody = bodies.get(0); - assertTrue(filesBody.contains("uid"), - "AC-13:列表端点必须支持按 uid 收窄(伪造 uid 的归属校验入口)"); - assertTrue(filesBody.contains("operator") || filesBody.contains("getId"), - "AC-13:uid 过滤必须与当前操作者归属对撞(防伪造 uid 越权读)"); - } - - @Test - void 守卫行为三样本_无token坏token401_非超管403_超管放行() throws Exception { - Object controller = newEvidenceController(); - AdminAuthSupport auth = authSupportOf(controller); - Method guard = superAdminGuard(controller.getClass()); - HttpServletRequest request = mock(HttpServletRequest.class); - - when(auth.requireAdmin(any(HttpServletRequest.class))) - .thenThrow(new BusinessException(401, "未登录")); - Throwable noToken = invoke(guard, controller, request); - assertInstanceOf(BusinessException.class, noToken, "AC-13:无 token 必须 401"); - assertEquals(Integer.valueOf(401), ((BusinessException) noToken).getCode()); - - when(auth.requireAdmin(any(HttpServletRequest.class))) - .thenThrow(new BusinessException(401, "登录已过期,请重新登录")); - Throwable badToken = invoke(guard, controller, request); - assertInstanceOf(BusinessException.class, badToken, "AC-13:坏 token 必须 401"); - assertEquals(Integer.valueOf(401), ((BusinessException) badToken).getCode()); - - AdminUserEntity normalAdmin = new AdminUserEntity(); - normalAdmin.setId(2L); - normalAdmin.setUsername("ops-admin"); - normalAdmin.setRole("admin"); - normalAdmin.setIsAdmin(1); - when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(normalAdmin); - when(auth.currentRole(normalAdmin)).thenReturn("admin"); - Throwable forbidden = invoke(guard, controller, request); - assertInstanceOf(BusinessException.class, forbidden, "AC-13:非超管必须 403"); - assertEquals(Integer.valueOf(403), ((BusinessException) forbidden).getCode()); - - AdminUserEntity superAdmin = new AdminUserEntity(); - superAdmin.setId(1L); - superAdmin.setUsername("root"); - superAdmin.setRole(DeviceSessionPolicy.SUPER_ADMIN); - superAdmin.setIsAdmin(1); - when(auth.requireAdmin(any(HttpServletRequest.class))).thenReturn(superAdmin); - when(auth.currentRole(superAdmin)).thenReturn(DeviceSessionPolicy.SUPER_ADMIN); - Object returned = guard.invoke(controller, request); - assertSame(superAdmin, returned, "AC-13:超管必须放行并返回操作者"); - } - - @Test - void 登记必须按taskId关联且对象key带uuid() throws IOException { - String all = evidenceSources(); - assertTrue(Pattern.compile("(?i)task[_-]?id").matcher(all).find(), - "AC-7:证据登记/落表必须按 taskId 关联"); - assertTrue(all.contains("objectKey") || all.contains("object_key"), - "AC-7:登记必须记录 objectKey"); - assertTrue(all.contains("UUID"), - "AC-13(铁律 10):对象 key 必须带 UUID"); - assertTrue(all.contains("sha256") || all.contains("SHA-256"), - "AC-7:登记必须带摘要(防篡改/去重)"); - } - - @Test - void 证据模块日志零命中presign与直链() throws IOException { - List files = new ArrayList<>(); - try (Stream stream = Files.walk(MAIN_JAVA)) { - stream.filter(path -> path.toString().endsWith(".java")) - .filter(EvidencePhase3ContractTest::isEvidenceSource) - .forEach(files::add); - } - assertFalse(files.isEmpty(), "AC-13:未找到证据模块源码(命名/落点未按契约)"); - List leaks = new ArrayList<>(); - for (Path file : files) { - for (String line : read(file).split("\\R")) { - String lower = line.toLowerCase(); - boolean logLine = lower.contains("log.info") || lower.contains("log.warn") - || lower.contains("log.error") || lower.contains("log.debug"); - boolean urlLike = lower.contains("presign") || lower.contains("uploadurl") - || lower.contains("fileurl") || lower.contains("http://") - || lower.contains("https://"); - if (logLine && urlLike) { - leaks.add(file.getFileName() + ": " + line.trim()); - } - } - } - assertTrue(leaks.isEmpty(), "AC-13:证据模块日志输出了 presign/直链:\n" + String.join("\n", leaks)); - } - - // ---------------------------------------------------------------- helpers - - private static Path evidenceController() throws IOException { - List hits = new ArrayList<>(); - try (Stream stream = Files.walk(MAIN_JAVA)) { - stream.filter(path -> path.toString().endsWith(".java")) - .filter(path -> { - try { - return read(path).contains(EVIDENCE_PATH); - } catch (IOException ex) { - return false; - } - }) - .forEach(hits::add); - } - assertFalse(hits.isEmpty(), - "AC-13:未找到 " + EVIDENCE_PATH + " 的控制器源码(Phase 3 落点未按契约)"); - return hits.get(0); - } - - private static boolean isEvidenceSource(Path path) { - try { - String source = read(path); - return source.contains("task-evidence") || source.contains("TaskEvidence") - || source.contains("biz_task_evidence"); - } catch (IOException ex) { - return false; - } - } - - private static String evidenceSources() throws IOException { - List files = new ArrayList<>(); - try (Stream stream = Files.walk(MAIN_JAVA)) { - stream.filter(path -> path.toString().endsWith(".java")) - .filter(EvidencePhase3ContractTest::isEvidenceSource) - .forEach(files::add); - } - assertFalse(files.isEmpty(), "AC-7:未找到证据模块源码"); - StringBuilder builder = new StringBuilder(); - for (Path file : files) { - builder.append(read(file)).append('\n'); - } - return builder.toString(); - } - - private static Object newEvidenceController() throws Exception { - Class controllerClass = Class.forName(CONTROLLER_FQCN); - Constructor constructor = Arrays.stream(controllerClass.getDeclaredConstructors()) - .max(Comparator.comparingInt(candidate -> candidate.getParameterCount())) - .orElseThrow(); - Class[] types = constructor.getParameterTypes(); - Object[] args = new Object[types.length]; - for (int index = 0; index < types.length; index++) { - if (types[index].isPrimitive()) { - fail("AC-13:控制器构造器不应有原始类型参数: " + types[index]); - } - args[index] = AdminAuthSupport.class.isAssignableFrom(types[index]) - ? mock(AdminAuthSupport.class) - : mock(types[index]); - } - constructor.setAccessible(true); - return constructor.newInstance(args); - } - - private static AdminAuthSupport authSupportOf(Object controller) throws IllegalAccessException { - for (java.lang.reflect.Field field : controller.getClass().getDeclaredFields()) { - if (AdminAuthSupport.class.isAssignableFrom(field.getType())) { - field.setAccessible(true); - return (AdminAuthSupport) field.get(controller); - } - } - fail("AC-13:证据控制器必须注入 AdminAuthSupport(与 AdminDeviceLogController 同口径)"); - return null; - } - - private static Method superAdminGuard(Class controllerClass) { - for (Method method : controllerClass.getDeclaredMethods()) { - if (method.getName().equals("requireSuperAdmin") - && method.getParameterCount() == 1 - && method.getParameterTypes()[0].isAssignableFrom(HttpServletRequest.class)) { - method.setAccessible(true); - return method; - } - } - throw new AssertionError( - "AC-13:证据控制器必须含私有 requireSuperAdmin(HttpServletRequest)(对齐 AdminDeviceLogController:167-175)"); - } - - private static Throwable invoke(Method method, Object target, Object... args) throws Exception { - try { - method.invoke(target, args); - return null; - } catch (InvocationTargetException ex) { - return ex.getTargetException(); - } - } - - private static List mappedMethodBodies(String source) { - String[] lines = source.split("\\R"); - Pattern mapping = Pattern.compile("@(Get|Post|Put|Delete|Patch)Mapping"); - List bodies = new ArrayList<>(); - for (int index = 0; index < lines.length; index++) { - if (!mapping.matcher(lines[index]).find()) { - continue; - } - int cursor = index; - while (cursor < lines.length && !lines[cursor].contains("{") && !lines[cursor].contains(";")) { - cursor++; - } - if (cursor >= lines.length || lines[cursor].contains(";")) { - continue; - } - int depth = 0; - StringBuilder body = new StringBuilder(); - for (int line = cursor; line < lines.length; line++) { - for (char ch : lines[line].toCharArray()) { - if (ch == '{') { - depth++; - } else if (ch == '}') { - depth--; - } - } - body.append(lines[line]).append('\n'); - if (depth == 0) { - break; - } - } - bodies.add(body.toString()); - index = cursor; - } - return bodies; - } - - private static String yamlBlock(String yaml, String key) { - String[] lines = yaml.split("\\R"); - Pattern header = Pattern.compile("^(\\s*)" + Pattern.quote(key) + ":\\s*$"); - int start = -1; - int indent = -1; - for (int index = 0; index < lines.length; index++) { - Matcher matcher = header.matcher(lines[index]); - if (matcher.matches()) { - start = index; - indent = matcher.group(1).length(); - break; - } - } - if (start < 0) { - return null; - } - StringBuilder block = new StringBuilder(); - for (int index = start + 1; index < lines.length; index++) { - String line = lines[index]; - if (line.trim().isEmpty() || line.trim().startsWith("#")) { - block.append(line).append('\n'); - continue; - } - int currentIndent = line.length() - line.replaceAll("^\\s+", "").length(); - if (currentIndent <= indent) { - break; - } - block.append(line).append('\n'); - } - return block.toString(); - } - - private static String scalar(String block, String key) { - Matcher matcher = Pattern.compile("(?m)^\\s*" + Pattern.quote(key) + ":\\s*(\\S.*)$") - .matcher(block); - return matcher.find() ? matcher.group(1).trim() : null; - } - - private static String defaultValue(String value) { - Matcher env = Pattern.compile("^\\$\\{[^:}]+:([^}]*)\\}$").matcher(value); - if (env.matches()) { - String fallback = env.group(1).trim(); - return fallback.isEmpty() ? null : fallback; - } - return value.startsWith("${") ? null : value; - } - - private static long versionOf(String fileName) { - Matcher matcher = Pattern.compile("^V(\\d+)__").matcher(fileName); - return matcher.find() ? Long.parseLong(matcher.group(1)) : -1; - } - - private static String read(Path path) throws IOException { - return Files.readString(path); - } -} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceOwnershipAndRetentionTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceOwnershipAndRetentionTest.java deleted file mode 100644 index b6ea5f345..000000000 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/evidence/service/TaskEvidenceOwnershipAndRetentionTest.java +++ /dev/null @@ -1,189 +0,0 @@ -package com.nanri.aiimage.modules.evidence.service; - -import com.nanri.aiimage.common.exception.BusinessException; -import com.nanri.aiimage.common.service.DistributedJobLockService; -import com.nanri.aiimage.common.task.LockSkipWitness; -import com.nanri.aiimage.config.EvidenceProperties; -import com.nanri.aiimage.modules.evidence.mapper.TaskEvidenceMapper; -import com.nanri.aiimage.modules.evidence.model.entity.TaskEvidenceEntity; -import com.nanri.aiimage.modules.evidence.storage.TaskEvidenceStorageService; -import com.nanri.aiimage.modules.file.service.LocalFileStorageService; -import com.nanri.aiimage.modules.task.mapper.FileTaskMapper; -import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoInteractions; -import static org.mockito.Mockito.when; - -/** - * 证据登记的归属与保留期的执行保证(CR 第二轮 P1-2/P1-7/P2-2)。 - * - *

P1-2 的判据是「归属先于落桶」:task_id 对得上任务行而 uid 不是行主 ⇒ 403 且 - * {@code storage.store} 一次都不许被调——校验晚于落桶的话,每一次拒绝都在证据桶里 - * 留一个收不回的孤儿对象。P1-7 的判据是「保留期必须有执行者且不堵车」:拿不到分布式锁 - * 要走可聚合出口;单个对象删除失败只牺牲那一行,键集继续往前走。 - */ -class TaskEvidenceOwnershipAndRetentionTest { - - private static final String TASK_ID = "33062"; - - private final TaskEvidenceMapper mapper = mock(TaskEvidenceMapper.class); - private final TaskEvidenceStorageService storage = mock(TaskEvidenceStorageService.class); - private final LocalFileStorageService localFiles = mock(LocalFileStorageService.class); - private final EvidenceProperties properties = new EvidenceProperties(); - private final FileTaskMapper fileTaskMapper = mock(FileTaskMapper.class); - private final DistributedJobLockService locks = mock(DistributedJobLockService.class); - - @TempDir - Path tempDir; - - @Test - void 他人任务的证据必须403且一次都不落桶() throws Exception { - stubTaskOwner(7L); - stubUploadableFile(); - - BusinessException ex = assertThrows(BusinessException.class, - () -> service().register(TASK_ID, 8L, "client", "goods-spider", "evidence.json")); - - assertEquals(403, ex.getCode().intValue(), "冒名登记必须 403,不是 500 也不是静默成功"); - verify(storage, never()).store(any(), any(), any()); - verify(mapper, never()).insert(any(TaskEvidenceEntity.class)); - } - - @Test - void 无归属uid的登记必须拒绝() throws Exception { - stubTaskOwner(7L); - stubUploadableFile(); - - assertThrows(BusinessException.class, - () -> service().register(TASK_ID, null, "client", "goods-spider", "evidence.json")); - verify(storage, never()).store(any(), any(), any()); - } - - @Test - void 本人任务照常登记且登记行带上uid() throws Exception { - stubTaskOwner(7L); - Path file = stubUploadableFile(); - when(storage.store(any(), eq(TASK_ID), eq("evidence.json"))).thenReturn( - new TaskEvidenceStorageService.StoredEvidence("task-evidence/k", 3L, "sha", "application/json")); - when(mapper.insert(any(TaskEvidenceEntity.class))).thenReturn(1); - - service().register(TASK_ID, 7L, "client", "goods-spider", "evidence.json"); - - org.mockito.ArgumentCaptor captor = - org.mockito.ArgumentCaptor.forClass(TaskEvidenceEntity.class); - verify(mapper).insert(captor.capture()); - assertEquals(7L, captor.getValue().getUid(), "登记行的 uid 必须是行主本人"); - assertEquals(TASK_ID, captor.getValue().getTaskId()); - assertTrue(Files.notExists(file) || Files.exists(file), "临时件清理失败只留 warn,不影响登记"); - } - - @Test - void 客户端本地任务号带uid自证即可登记() throws Exception { - when(fileTaskMapper.selectById(anyLong())).thenReturn(null); - when(fileTaskMapper.selectOne(any())).thenReturn(null); - stubUploadableFile(); - when(storage.store(any(), any(), any())).thenReturn( - new TaskEvidenceStorageService.StoredEvidence("task-evidence/k", 3L, "sha", "application/json")); - when(mapper.insert(any(TaskEvidenceEntity.class))).thenReturn(1); - - service().register("local-job-abc", 5L, "client", "goods-spider", "evidence.json"); - - verify(storage).store(any(), eq("local-job-abc"), eq("evidence.json")); - } - - @Test - void 零字节证据件拒绝登记() throws Exception { - when(fileTaskMapper.selectById(anyLong())).thenReturn(null); - when(fileTaskMapper.selectOne(any())).thenReturn(null); - Path empty = Files.createFile(tempDir.resolve("empty.png")); - when(localFiles.findLocalSourceFile("empty.png")).thenReturn(empty.toFile()); - - assertThrows(BusinessException.class, - () -> service().register(TASK_ID, 7L, "client", "goods-spider", "empty.png")); - verify(storage, never()).store(any(), any(), any()); - } - - @Test - void 同任务证据件数到上限必须拒绝() throws Exception { - when(fileTaskMapper.selectById(anyLong())).thenReturn(null); - when(fileTaskMapper.selectOne(any())).thenReturn(null); - when(mapper.selectCount(any())).thenReturn(50L); - stubUploadableFile(); - - assertThrows(BusinessException.class, - () -> service().register(TASK_ID, 7L, "client", "goods-spider", "evidence.json")); - verify(storage, never()).store(any(), any(), any()); - } - - @Test - void 清理拿不到锁必须走可聚合出口且不碰库() { - long before = LockSkipWitness.countOf(TaskEvidenceService.RETENTION_LOCK); - when(locks.tryLock(eq(TaskEvidenceService.RETENTION_LOCK), any())).thenReturn(null); - - service().purgeExpired(); - - assertEquals(before + 1, LockSkipWitness.countOf(TaskEvidenceService.RETENTION_LOCK), - "锁跳过必须有 [lock-skip] 可聚合出口,静默 return 就是 P1-7 的另一半"); - verifyNoInteractions(mapper, storage); - } - - @Test - void 单个对象删除失败不堵后面的行() { - when(locks.tryLock(eq(TaskEvidenceService.RETENTION_LOCK), any())) - .thenReturn(mock(DistributedJobLockService.LockHandle.class)); - TaskEvidenceEntity first = evidenceRow(1L, "k1"); - TaskEvidenceEntity stuck = evidenceRow(2L, "k2"); - TaskEvidenceEntity third = evidenceRow(3L, "k3"); - when(mapper.selectList(any())).thenReturn(List.of(first, stuck, third), List.of()); - doThrow(new RuntimeException("oss unavailable")).when(storage).delete("k2"); - when(mapper.deleteById(anyLong())).thenReturn(1); - - service().purgeExpired(); - - verify(mapper).deleteById(1L); - verify(mapper, never()).deleteById(2L); - verify(mapper).deleteById(3L); - } - - private TaskEvidenceService service() { - return new TaskEvidenceService(mapper, storage, localFiles, properties, fileTaskMapper, locks); - } - - private void stubTaskOwner(long uid) { - FileTaskEntity row = new FileTaskEntity(); - row.setId(Long.parseLong(TASK_ID)); - row.setUserId(uid); - when(fileTaskMapper.selectById(Long.parseLong(TASK_ID))).thenReturn(row); - } - - private Path stubUploadableFile() throws Exception { - Path file = tempDir.resolve("evidence.json"); - Files.writeString(file, "{\"traceback\": \"boom\"}"); - when(localFiles.findLocalSourceFile("evidence.json")).thenReturn(file.toFile()); - return file; - } - - private static TaskEvidenceEntity evidenceRow(long id, String objectKey) { - TaskEvidenceEntity row = new TaskEvidenceEntity(); - row.setId(id); - row.setTaskId(TASK_ID); - row.setObjectKey(objectKey); - return row; - } -} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java index 052bfea42..470d4e470 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/file/service/ChunkedUploadReclaimContractTest.java @@ -37,8 +37,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; *

  • 近 N 分钟有片写入的会话不得误清(fresh 会话必须原样保留)。
  • * * - *

    分片会话只有盘上 meta/part,无 DB 行;证据表的 DB 行保留期判据见 - * {@code EvidencePhase3ContractTest}(AC-13 配置块)。

    + *

    分片会话只有盘上 meta/part,无 DB 行(证据面已随任务证据特性整体下线,2026-11-17)。

    */ class ChunkedUploadReclaimContractTest {