diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java index 18610dc3b..2e4551f76 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationDispatchService.java @@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -73,6 +74,37 @@ public class NotificationDispatchService { return pushed; } + /** + * 一条事件发给「能看到任一所列用户」的管理员:同一 dedupeKeyBase 下每个管理员只落一条。 + * 用于一把密钥被多个账号共用时的告警收敛——按主体逐个调用 {@code pushToAdmins} 会让同一个 + * 管理员收到 N 条同内容通知,这里改成每个管理员最多一条,同时保留数据权限边界。 + */ + public int pushToAdminsAnyOf(AdminAudience audience, String scene, String level, String title, String content, + String dedupeKeyBase, Collection subjectUserIds) { + int pushed = 0; + int filtered = 0; + for (AdminUserEntity admin : audience.admins()) { + boolean visible = false; + for (Long subjectUserId : subjectUserIds) { + if (audience.canReceive(admin.getId(), subjectUserId)) { + visible = true; + break; + } + } + if (!visible) { + filtered++; + continue; + } + if (notificationService.pushOrRefresh(admin.getId(), NotificationService.AUDIENCE_ADMIN, + scene, level, title, content, dedupeKeyBase + ":" + admin.getId())) { + pushed++; + } + } + log.info("[notification] 管理员分发(合并)完成 scene={} 涉及用户数={} 受众={} 权限过滤={} 落库={}", + scene, subjectUserIds.size(), audience.admins().size(), filtered, pushed); + return pushed; + } + /** * 「任务成功,但有一部分行没跑出来」通知:整任务没失败、铃铛里的 task_failed 只覆盖终态失败, * 这类部分缺失过去对用户完全不可见(品牌检测的查询失败行只躺在结果文件的一张子表里)。 diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/controller/AdminUserApiSecretController.java b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/controller/AdminUserApiSecretController.java index 68c7cb5bc..64300a433 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/controller/AdminUserApiSecretController.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/controller/AdminUserApiSecretController.java @@ -114,6 +114,7 @@ public class AdminUserApiSecretController { userSecretProperties.getCheckMaxRows(), userSecretProperties.getCheckBudgetMinutes()); return ApiResponse.success("巡检完成", Map.of( "checked", summary.checked(), + "probed", summary.probed(), "passed", summary.passed(), "failed", summary.failed(), "errors", summary.errors(), diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretCheckScheduler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretCheckScheduler.java index 4cfe3b5c4..524da1ccd 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretCheckScheduler.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretCheckScheduler.java @@ -11,9 +11,9 @@ import java.time.Duration; /** * 用户密钥连通性巡检:默认每隔一天 04:30(Asia/Shanghai)跑一轮 - * (代理模块每次巡检真实提取一次,按次计费;2026-09-14 成本审查后由 - * 每日降为隔日),双实例经 Redis 分布式锁互斥;单轮受条数与时间预算 - * 约束,超出顺延下一轮。 + * (代理模块每次探测都是真实提取、按次计费,故同一把密钥被多个账号配置时一轮只探测一次; + * 2026-09-14 成本审查后巡检频次由每日降为隔日),双实例经 Redis 分布式锁互斥; + * 单轮受条数与时间预算约束,超出顺延下一轮。 */ @Service @Slf4j @@ -42,8 +42,9 @@ public class UserApiSecretCheckScheduler { properties.getCheckMaxRows(), properties.getCheckBudgetMinutes()); UserApiSecretService.CheckSummary summary = userApiSecretService.checkAllForScheduler( properties.getCheckMaxRows(), properties.getCheckBudgetMinutes()); - log.info("[user-secret] 每日巡检完成 checked={} passed={} failed={} errors={} skipped={}", - summary.checked(), summary.passed(), summary.failed(), summary.errors(), summary.skipped()); + log.info("[user-secret] 每日巡检完成 checked={} probed={} passed={} failed={} errors={} skipped={}", + summary.checked(), summary.probed(), summary.passed(), summary.failed(), + summary.errors(), summary.skipped()); } catch (Exception ex) { log.warn("[user-secret] 每日巡检异常终止 err={}", ex.getMessage(), ex); } diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretService.java index 175145c34..ecb2a7ccd 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretService.java @@ -282,14 +282,14 @@ public class UserApiSecretService implements UserSecretCleanupPort { } else if (module == UserSecretModule.PROXY) { validateProxyValue(plainKey); } - UserApiSecretCheckService.CheckOutcome outcome = reuseFreshOutcome(userId, module, plainKey); + UserApiSecretCheckService.CheckOutcome outcome = reuseFreshOutcome(module, plainKey); if (outcome != null) { log.info("[user-secret] 新鲜期内重复检测相同配置,复用上次结果(代理按次计费,避免连点重复提取)userId={} module={}", userId, module.key()); } else { outcome = checkService.probe(module, plainKey); if (UserApiSecretCheckService.STATUS_PASSED.equals(outcome.status())) { - rememberFreshOutcome(userId, module, plainKey, outcome); + rememberFreshOutcome(module, plainKey, outcome); } } UserApiSecretCheckResultVo vo = toCheckResult(module, outcome); @@ -522,17 +522,24 @@ public class UserApiSecretService implements UserSecretCleanupPort { } /** - * 定时巡检:遍历全部密钥逐条探测并更新状态。 - * 单轮受 maxRows 与时间预算约束,超出部分顺延下一轮;单条异常不影响整轮。 + * 定时巡检:按「模块 + 密文」分组探测,同一把密钥被多个账号配置时一轮只真实探测一次, + * 结论回写组内全部配置行。代理探测每次都是真实提取(按次计费)、LLM 检测每次都在烧用户额度, + * 逐账号重复探测同一把密钥纯属浪费(2026-10-01 实测:314 条配置实为 33 种密钥)。 + * 单轮受 maxRows(按配置行计)与时间预算约束,超出部分顺延下一轮;单条异常不影响整轮。 */ public CheckSummary checkAllForScheduler(int maxRows, int budgetMinutes) { long deadline = System.currentTimeMillis() + Duration.ofMinutes(Math.max(1, budgetMinutes)).toMillis(); int checked = 0; + int probed = 0; int passed = 0; int failed = 0; int errors = 0; int skipped = 0; int notifySent = 0; + // 密文可作分组键的前提:ShopCredentialCryptoService 用 AES/ECB 无 IV,同明文必同密文 + Map outcomeBySecret = new HashMap<>(); + // 管理员告警按密钥分组收敛:一把 key 挂了,组内 N 个账号只推一条(用户侧仍逐账号通知) + Map adminAlertGroups = new LinkedHashMap<>(); // 管理员受众整轮复用:轮内管理员/分组几乎不变,避免每条异常都重新解析 NotificationDispatchService.AdminAudience adminAudience = userSecretProperties.isNotifyEnabled() ? notificationDispatchService.prepareAdminAudience() : null; @@ -557,20 +564,25 @@ public class UserApiSecretService implements UserSecretCleanupPort { continue; } try { - UserApiSecretCheckService.CheckOutcome outcome; - String plainKey = normalize(cryptoService.decrypt(row.getSecretValue())); - if (plainKey.isEmpty()) { - outcome = new UserApiSecretCheckService.CheckOutcome( - UserApiSecretCheckService.STATUS_FAILED, - UserApiSecretCheckService.CODE_INVALID_KEY, - "配置内容为空,请重新配置", null, false); - } else { - outcome = checkService.probe(module.get(), plainKey); + String groupKey = inspectionGroupKey(row); + UserApiSecretCheckService.CheckOutcome outcome = outcomeBySecret.get(groupKey); + if (outcome == null) { + String plainKey = normalize(cryptoService.decrypt(row.getSecretValue())); + outcome = plainKey.isEmpty() + ? new UserApiSecretCheckService.CheckOutcome( + UserApiSecretCheckService.STATUS_FAILED, + UserApiSecretCheckService.CODE_INVALID_KEY, + "配置内容为空,请重新配置", null, false) + : checkService.probe(module.get(), plainKey); + outcomeBySecret.put(groupKey, outcome); + probed++; + sleepQuietly(200L); } applyCheckOutcome(row.getUserId(), module.get().key(), outcome); checked++; if (adminAudience != null) { - notifySent += notifySecretAlert(adminAudience, row.getUserId(), module.get(), outcome); + notifySent += notifySecretUserAlert(row.getUserId(), module.get(), outcome); + collectAdminAlert(adminAlertGroups, groupKey, row.getUserId(), module.get(), outcome); } if (UserApiSecretCheckService.STATUS_PASSED.equals(outcome.status())) { passed++; @@ -584,7 +596,6 @@ public class UserApiSecretService implements UserSecretCleanupPort { log.warn("[user-secret] 巡检单条失败 id={} userId={} module={} err={}", row.getId(), row.getUserId(), row.getModuleKey(), ex.getMessage()); } - sleepQuietly(200L); } if (checked >= maxRows || System.currentTimeMillis() >= deadline) { Long remaining = userApiSecretMapper.selectCount(new LambdaQueryWrapper() @@ -593,12 +604,21 @@ public class UserApiSecretService implements UserSecretCleanupPort { break; } } - CheckSummary summary = new CheckSummary(checked, passed, failed, errors, skipped); - log.info("[user-secret] 巡检结束 checked={} passed={} failed={} errors={} skipped={} 通知={}", - summary.checked(), summary.passed(), summary.failed(), summary.errors(), summary.skipped(), notifySent); + int adminNotifySent = dispatchAdminAlerts(adminAudience, adminAlertGroups); + CheckSummary summary = new CheckSummary(checked, probed, passed, failed, errors, skipped); + log.info("[user-secret] 巡检结束 checked={} 真实探测={} 复用={} passed={} failed={} errors={} skipped={} " + + "用户通知={} 管理员告警组={} 管理员通知={}", + summary.checked(), summary.probed(), summary.checked() - summary.probed(), + summary.passed(), summary.failed(), summary.errors(), summary.skipped(), + notifySent, adminAlertGroups.size(), adminNotifySent); return summary; } + /** 巡检分组键:同模块 + 同密文视为同一把密钥。密文确定性加密,无需解密即可分组。 */ + private String inspectionGroupKey(UserApiSecretEntity row) { + return row.getModuleKey() + '|' + row.getSecretValue(); + } + /** * 巡检告警判定:欠费(insufficient_balance)→ error 级 secret_balance; * 密钥无效/被拒(invalid_key/forbidden)→ warning 级 secret_invalid; @@ -619,10 +639,12 @@ public class UserApiSecretService implements UserSecretCleanupPort { return null; } - /** 巡检发现欠费/失效:推用户通知 + 按数据权限推管理员通知;同一模块每天最多各一条。返回落库条数。 */ - private int notifySecretAlert(NotificationDispatchService.AdminAudience adminAudience, - Long userId, UserSecretModule module, - UserApiSecretCheckService.CheckOutcome outcome) { + /** + * 巡检发现欠费/失效:推该账号的用户通知(同一模块每天最多一条)。 + * 管理员侧不在此逐条推,由 {@link #collectAdminAlert} + {@link #dispatchAdminAlerts} 按密钥合并后一次发。 + */ + private int notifySecretUserAlert(Long userId, UserSecretModule module, + UserApiSecretCheckService.CheckOutcome outcome) { SecretAlertDecision decision = decideSecretAlert(outcome); if (decision == null) { return 0; @@ -639,19 +661,80 @@ public class UserApiSecretService implements UserSecretCleanupPort { int pushed = notificationDispatchService.pushToUser(userId, decision.scene(), decision.level(), userTitle, userContent, decision.scene() + ":" + userId + ":" + module.key() + ":" + day) ? 1 : 0; - - String username = notificationDispatchService.displayNameOf(userId); - String adminContent = "用户 " + username + "(uid=" + userId + ")的" + moduleLabel - + (balance ? "对应服务商余额不足" : "检测失败") - + (reason.isEmpty() ? "" : ":" + reason); - pushed += notificationDispatchService.pushToAdmins(adminAudience, decision.scene(), decision.level(), - "用户密钥异常:" + username, adminContent, - decision.scene() + "_admin:" + userId + ":" + module.key() + ":" + day, userId); - log.info("[user-secret] 巡检通知已发 userId={} module={} scene={} level={} 条数={}", + log.info("[user-secret] 巡检用户通知已发 userId={} module={} scene={} level={} 条数={}", userId, module.key(), decision.scene(), decision.level(), pushed); return pushed; } + /** 把本条异常按密钥归组:同「模块 + 密文」的 N 个账号只留一条待发管理员通知。 */ + private void collectAdminAlert(Map groups, String groupKey, Long userId, + UserSecretModule module, UserApiSecretCheckService.CheckOutcome outcome) { + SecretAlertDecision decision = decideSecretAlert(outcome); + if (decision == null || userId == null) { + return; + } + groups.computeIfAbsent(groupKey, key -> new SecretAlertGroup(module, decision, outcome)).userIds.add(userId); + } + + /** + * 管理员告警下发:一把密钥一条。多账号共用时正文只报「受影响账号数 + 原因」,不逐个点名—— + * 同一把 key 的账号往往分属不同主管,列出他人 uid 等于把归属关系泄露给对方, + * 具体账号可在后台「用户密钥」页按状态筛出;单账号组保持原来的点名文案。 + * 受众仍按数据权限过滤:只看得到组内任一号的管理员才收得到。 + */ + private int dispatchAdminAlerts(NotificationDispatchService.AdminAudience audience, + Map groups) { + if (audience == null || groups.isEmpty()) { + return 0; + } + String day = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE); + int pushed = 0; + for (Map.Entry entry : groups.entrySet()) { + SecretAlertGroup group = entry.getValue(); + boolean balance = NotificationService.SCENE_SECRET_BALANCE.equals(group.decision.scene()); + String reason = group.outcome.message() == null ? "" : group.outcome.message().trim(); + String what = group.module.label() + (balance ? "对应服务商余额不足" : "检测失败"); + String title; + String content; + String dedupeBase; + if (group.userIds.size() == 1) { + Long userId = group.userIds.iterator().next(); + String username = notificationDispatchService.displayNameOf(userId); + title = "用户密钥异常:" + username; + content = "用户 " + username + "(uid=" + userId + ")的" + what + + (reason.isEmpty() ? "" : ":" + reason); + dedupeBase = group.decision.scene() + "_admin:" + userId + ":" + group.module.key(); + } else { + int affected = group.userIds.size(); + title = "用户密钥异常:" + what + "(" + affected + " 个账号共用同一把)"; + content = group.module.label() + "有 " + affected + " 个账号共用同一把配置," + + (balance ? "对应服务商余额不足" : "检测失败") + + (reason.isEmpty() ? "" : ":" + reason) + + "。具体账号请到后台「用户密钥」页按状态筛选。"; + dedupeBase = group.decision.scene() + "_admin:group:" + sha256Hex(entry.getKey()) + + ":" + group.module.key(); + } + pushed += notificationDispatchService.pushToAdminsAnyOf(audience, group.decision.scene(), + group.decision.level(), title, content, dedupeBase + ":" + day, group.userIds); + } + return pushed; + } + + /** 一把密钥(同模块同密文)的管理员告警分组。 */ + private static final class SecretAlertGroup { + private final UserSecretModule module; + private final SecretAlertDecision decision; + private final UserApiSecretCheckService.CheckOutcome outcome; + private final Set userIds = new LinkedHashSet<>(); + + private SecretAlertGroup(UserSecretModule module, SecretAlertDecision decision, + UserApiSecretCheckService.CheckOutcome outcome) { + this.module = module; + this.decision = decision; + this.outcome = outcome; + } + } + /** 巡检告警判定结果(场景 + 级别)。 */ record SecretAlertDecision(String scene, String level) { } @@ -801,16 +884,28 @@ public class UserApiSecretService implements UserSecretCleanupPort { * 只在 Redis 暂存里做比对,不落库、不下发。 */ private String fingerprint(Long userId, UserSecretModule module, String plainValue) { + String hash = sha256Hex(userId + "|" + module.key() + "|" + plainValue); + if (hash.isEmpty()) { + log.warn("[user-secret] 生成检测指纹失败 userId={} module={}", userId, module.key()); + } + return hash; + } + + /** 新鲜期指纹:只按「模块 + 明文」,不含 uid —— 同一把密钥在任意账号上的检测结论是同一个。 */ + private String sharedFingerprint(UserSecretModule module, String plainValue) { + return sha256Hex(module.key() + "|" + plainValue); + } + + private String sha256Hex(String raw) { try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); - byte[] bytes = digest.digest((userId + "|" + module.key() + "|" + plainValue).getBytes(StandardCharsets.UTF_8)); + byte[] bytes = digest.digest(raw.getBytes(StandardCharsets.UTF_8)); StringBuilder hex = new StringBuilder(bytes.length * 2); for (byte value : bytes) { hex.append(Character.forDigit((value >> 4) & 0xF, 16)).append(Character.forDigit(value & 0xF, 16)); } return hex.toString(); } catch (Exception ex) { - log.warn("[user-secret] 生成检测指纹失败 userId={} module={} err={}", userId, module.key(), ex.getMessage()); return ""; } } @@ -835,19 +930,23 @@ public class UserApiSecretService implements UserSecretCleanupPort { } } - /** 「重复检测」新鲜期键:按登录用户 + 模块隔离。 */ - private String freshCheckKey(Long userId, UserSecretModule module) { - return FRESH_CHECK_KEY_PREFIX + ":" + userId + ":" + module.key(); + /** + * 「重复检测」新鲜期键:按「模块 + 配置值指纹」隔离,不按 uid。 + * 一把密钥平均被 9.5 个账号共用(2026-10-01 实测 314 条配置=33 种密钥),而代理探测每次都是真实提取计费, + * 按 uid 隔离时管理员连点 N 个共用同 key 的账号就要付 N 次;结论本身只取决于密钥,跨账号复用才不重复扣费。 + */ + private String freshCheckKey(UserSecretModule module, String sharedHash) { + return FRESH_CHECK_KEY_PREFIX + ":" + module.key() + ":" + sharedHash; } /** 读取新鲜期结果:同值且窗口内返回上次 outcome,否则 null(走真实探测)。 */ - private UserApiSecretCheckService.CheckOutcome reuseFreshOutcome(Long userId, UserSecretModule module, String plainValue) { - String hash = fingerprint(userId, module, plainValue); + private UserApiSecretCheckService.CheckOutcome reuseFreshOutcome(UserSecretModule module, String plainValue) { + String hash = sharedFingerprint(module, plainValue); if (hash.isEmpty()) { return null; } try { - String raw = stringRedisTemplate.opsForValue().get(freshCheckKey(userId, module)); + String raw = stringRedisTemplate.opsForValue().get(freshCheckKey(module, hash)); if (raw == null || raw.isBlank()) { return null; } @@ -858,16 +957,16 @@ public class UserApiSecretService implements UserSecretCleanupPort { return new UserApiSecretCheckService.CheckOutcome( record.status(), record.code(), record.message(), record.latencyMs(), false); } catch (Exception ex) { - log.warn("[user-secret] 读取检测新鲜期结果失败(按未命中处理)userId={} module={} err={}", - userId, module.key(), ex.getMessage()); + log.warn("[user-secret] 读取检测新鲜期结果失败(按未命中处理)module={} err={}", + module.key(), ex.getMessage()); return null; } } /** 暂存「通过」的检测结果供新鲜期内重复检测复用;失败不暂存(允许立即重试)。 */ - private void rememberFreshOutcome(Long userId, UserSecretModule module, String plainValue, + private void rememberFreshOutcome(UserSecretModule module, String plainValue, UserApiSecretCheckService.CheckOutcome outcome) { - String hash = fingerprint(userId, module, plainValue); + String hash = sharedFingerprint(module, plainValue); if (hash.isEmpty()) { return; } @@ -875,10 +974,10 @@ public class UserApiSecretService implements UserSecretCleanupPort { String payload = objectMapper.writeValueAsString(new PendingVerification( hash, outcome.status(), outcome.code(), truncate(outcome.message(), MESSAGE_MAX_LENGTH), outcome.latencyMs())); - stringRedisTemplate.opsForValue().set(freshCheckKey(userId, module), payload, FRESH_CHECK_TTL); + stringRedisTemplate.opsForValue().set(freshCheckKey(module, hash), payload, FRESH_CHECK_TTL); } catch (Exception ex) { - log.warn("[user-secret] 暂存检测新鲜期结果失败(不影响本次检测)userId={} module={} err={}", - userId, module.key(), ex.getMessage()); + log.warn("[user-secret] 暂存检测新鲜期结果失败(不影响本次检测)module={} err={}", + module.key(), ex.getMessage()); } } @@ -1136,6 +1235,10 @@ public class UserApiSecretService implements UserSecretCleanupPort { } /** 巡检统计。 */ - public record CheckSummary(int checked, int passed, int failed, int errors, int skipped) { + /** + * 巡检汇总:checked 为更新过状态的配置条数,probed 为真实对外探测次数 + * (同一把密钥被多个账号共用时 probed < checked)。 + */ + public record CheckSummary(int checked, int probed, int passed, int failed, int errors, int skipped) { } } diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretServiceTest.java index 734cb0c4b..4c460ddc5 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/usersecret/service/UserApiSecretServiceTest.java @@ -28,9 +28,12 @@ import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import java.time.Duration; +import java.util.Collection; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -60,6 +63,8 @@ class UserApiSecretServiceTest { private final ObjectMapper objectMapper = new ObjectMapper(); /** 内存版 Redis 值操作:让「检测暂存 → 保存读取」的 JSON 往返在单测里真实走一遍。 */ private final Map redisValues = new HashMap<>(); + /** 巡检测试用:记录 updateById 写入的 userId → checkStatus 快照。 */ + private final Map inspectionWrites = new LinkedHashMap<>(); private ValueOperations valueOps; private UserApiSecretService newService() { @@ -226,6 +231,47 @@ class UserApiSecretServiceTest { verify(checkService, times(2)).probe(any(), anyString()); } + /** + * 新鲜期按「配置值」共享而非按账号:一把密钥平均挂 9.5 个账号(2026-10-01 实测 314 条配置=33 种), + * 后台连点两个共用同 key 的账号若各探一次就是付两次提取费;结论只取决于密钥,跨账号复用才不重复扣费。 + */ + @Test + void freshCheckIsSharedAcrossAccountsUsingTheSameSecret() { + UserApiSecretService service = newService(); + when(checkService.probe(any(), anyString())).thenReturn(outcome("passed", "ok", "代理可用", 120)); + + var first = service.check(7L, "proxy", "https://api.jikip.com/ip-get?num=1&key=abc"); + var second = service.check(8L, "proxy", "https://api.jikip.com/ip-get?num=1&key=abc"); + + verify(checkService, times(1)).probe(any(), anyString()); + assertThat(second.getCheckStatus()).isEqualTo("passed"); + assertThat(second.getCheckStatus()).isEqualTo(first.getCheckStatus()); + } + + /** 共享的边界是「同一个值」:换了 key 必须真实探测,否则新鲜期退化成全局缓存一份结论。 */ + @Test + void freshCheckDoesNotLeakAcrossDifferentSecretValues() { + UserApiSecretService service = newService(); + when(checkService.probe(any(), anyString())).thenReturn(outcome("passed", "ok", "正常", 100)); + + service.check(7L, "proxy", "https://api.jikip.com/ip-get?num=1&key=aaa"); + service.check(8L, "proxy", "https://api.jikip.com/ip-get?num=1&key=bbb"); + + verify(checkService, times(2)).probe(any(), anyString()); + } + + /** 同一串值在不同模块下结论可能不同(货源/外观各自独立的检测端点),新鲜期按模块隔离。 */ + @Test + void freshCheckIsScopedPerModule() { + UserApiSecretService service = newService(); + when(checkService.probe(any(), anyString())).thenReturn(outcome("passed", "ok", "连通正常", 100)); + + service.check(7L, "similar-asin", "sk-shared"); + service.check(8L, "appearance-patent", "sk-shared"); + + verify(checkService, times(2)).probe(any(), anyString()); + } + private UserApiSecretEntity existingRow(String cipher) { UserApiSecretEntity existing = new UserApiSecretEntity(); existing.setId(5L); @@ -730,6 +776,189 @@ class UserApiSecretServiceTest { assertThat(bundle.getProxy().getMasked()).isEqualTo("https://api.jikip.com"); } + /** 巡检去重:同一把代理密钥被 3 个账号共用,一轮只真实提取一次,结论回写 3 行。 */ + @Test + void schedulerProbesSharedSecretOnceForAllAccounts() { + UserApiSecretService service = newService(); + String sharedCipher = "enc:https://api.jikip.com/ip-get?num=1&key=abc"; + stubInspectionStore(Map.of( + 11L, storedRow(11L, "proxy", sharedCipher), + 12L, storedRow(12L, "proxy", sharedCipher), + 13L, storedRow(13L, "proxy", sharedCipher))); + when(checkService.probe(any(), anyString())).thenReturn(outcome("passed", "ok", "代理可用", 120)); + when(mapper.selectList(any())).thenReturn(List.of( + inspectionRow(1L, 11L, "proxy", sharedCipher), + inspectionRow(2L, 12L, "proxy", sharedCipher), + inspectionRow(3L, 13L, "proxy", sharedCipher)), List.of()); + + var summary = service.checkAllForScheduler(500, 20); + + verify(checkService, times(1)).probe(any(), anyString()); + assertThat(summary.checked()).isEqualTo(3); + assertThat(summary.probed()).isEqualTo(1); + assertThat(inspectionWrites.keySet()).containsExactlyInAnyOrder(11L, 12L, 13L); + assertThat(inspectionWrites.values()).containsOnly("passed"); + } + + /** 失败结论同样整组复用:否则一把欠费的 key 会按账号数重复付费提取。 */ + @Test + void schedulerReusesFailedOutcomeAcrossSharedSecretGroup() { + UserApiSecretService service = newService(); + String sharedCipher = "enc:https://api.jikip.com/ip-get?num=1&key=abc"; + stubInspectionStore(Map.of( + 11L, storedRow(11L, "proxy", sharedCipher), + 12L, storedRow(12L, "proxy", sharedCipher))); + when(checkService.probe(any(), anyString())).thenReturn( + outcome("failed", "insufficient_balance", "代理服务商余额不足,请充值后重试", 80)); + when(mapper.selectList(any())).thenReturn(List.of( + inspectionRow(1L, 11L, "proxy", sharedCipher), + inspectionRow(2L, 12L, "proxy", sharedCipher)), List.of()); + + var summary = service.checkAllForScheduler(500, 20); + + verify(checkService, times(1)).probe(any(), anyString()); + assertThat(summary.probed()).isEqualTo(1); + assertThat(summary.failed()).isEqualTo(2); + assertThat(inspectionWrites.values()).containsOnly("failed"); + } + + /** 分组只在同模块内生效:同一串值在货源/外观两个模块下各探一次(两模块的检测主机可独立配置)。 */ + @Test + void schedulerProbesSameValueOncePerModule() { + UserApiSecretService service = newService(); + String sharedCipher = "enc:sk-shared"; + stubInspectionStore(Map.of( + 11L, storedRow(11L, "similar-asin", sharedCipher), + 12L, storedRow(12L, "appearance-patent", sharedCipher))); + when(checkService.probe(any(), anyString())).thenReturn(outcome("passed", "ok", "连通正常", 100)); + when(mapper.selectList(any())).thenReturn(List.of( + inspectionRow(1L, 11L, "similar-asin", sharedCipher), + inspectionRow(2L, 12L, "appearance-patent", sharedCipher)), List.of()); + + var summary = service.checkAllForScheduler(500, 20); + + verify(checkService, times(2)).probe(any(), anyString()); + assertThat(summary.checked()).isEqualTo(2); + assertThat(summary.probed()).isEqualTo(2); + } + + /** + * 一把欠费密钥被 3 个账号共用:用户通知逐个发(各自的余额问题各自得知道), + * 管理员侧收敛成一条 —— 过去是 3 条同内容告警,33 种密钥×平均 9.5 账号时后台铃铛会被刷屏。 + */ + @Test + void schedulerGroupsAdminAlertForAccountsSharingOneSecret() { + UserApiSecretService service = newService(); + String sharedCipher = "enc:https://api.jikip.com/ip-get?num=1&key=abc"; + stubInspectionStore(Map.of( + 11L, storedRow(11L, "proxy", sharedCipher), + 12L, storedRow(12L, "proxy", sharedCipher), + 13L, storedRow(13L, "proxy", sharedCipher))); + when(checkService.probe(any(), anyString())).thenReturn( + outcome("failed", "insufficient_balance", "代理服务商余额不足,请充值后重试", 80)); + when(mapper.selectList(any())).thenReturn(List.of( + inspectionRow(1L, 11L, "proxy", sharedCipher), + inspectionRow(2L, 12L, "proxy", sharedCipher), + inspectionRow(3L, 13L, "proxy", sharedCipher)), List.of()); + stubAdminNotificationPath(); + + var summary = service.checkAllForScheduler(500, 20); + + assertThat(summary.failed()).isEqualTo(3); + verify(notificationDispatchService, times(3)).pushToUser(any(), any(), any(), any(), any(), any()); + ArgumentCaptor title = ArgumentCaptor.forClass(String.class); + ArgumentCaptor content = ArgumentCaptor.forClass(String.class); + @SuppressWarnings("unchecked") + ArgumentCaptor> subjects = ArgumentCaptor.forClass(Collection.class); + verify(notificationDispatchService, times(1)).pushToAdminsAnyOf(any(), anyString(), anyString(), + title.capture(), content.capture(), anyString(), subjects.capture()); + assertThat(title.getValue()).contains("3 个账号共用同一把"); + assertThat(subjects.getValue()).containsExactlyInAnyOrder(11L, 12L, 13L); + // 合并告警不点名具体账号:同 key 的账号往往分属不同主管,列 uid 等于泄露归属 + assertThat(content.getValue()).doesNotContain("uid="); + verify(notificationDispatchService, never()).pushToAdmins(any(), anyString(), anyString(), + anyString(), anyString(), anyString(), any()); + } + + /** 只有一个账号在用的密钥保持原口径:仍然点名「用户密钥异常:{用户名}」,去重键与改动前一致。 */ + @Test + void schedulerKeepsNamedAdminAlertWhenSecretIsUsedByOneAccount() { + UserApiSecretService service = newService(); + String cipher = "enc:sk-solo"; + stubInspectionStore(Map.of(11L, storedRow(11L, "similar-asin", cipher))); + when(checkService.probe(any(), anyString())).thenReturn( + outcome("failed", "invalid_key", "密钥无效或已被拒绝", 90)); + when(mapper.selectList(any())).thenReturn(List.of( + inspectionRow(1L, 11L, "similar-asin", cipher)), List.of()); + stubAdminNotificationPath(); + + service.checkAllForScheduler(500, 20); + + ArgumentCaptor title = ArgumentCaptor.forClass(String.class); + ArgumentCaptor content = ArgumentCaptor.forClass(String.class); + ArgumentCaptor dedupeBase = ArgumentCaptor.forClass(String.class); + verify(notificationDispatchService, times(1)).pushToAdminsAnyOf(any(), anyString(), anyString(), + title.capture(), content.capture(), dedupeBase.capture(), any()); + assertThat(title.getAllValues()).containsExactly("用户密钥异常:shop-a"); + assertThat(content.getValue()).contains("用户 shop-a(uid=11)的货源查询密钥检测失败"); + assertThat(dedupeBase.getValue()) + .isEqualTo("secret_invalid_admin:11:similar-asin:" + today()); + } + + /** 管理员受众与推送出口打桩:只关心「发几条、发给谁、什么文案」,受众解析本身有专测。 */ + private void stubAdminNotificationPath() { + when(notificationDispatchService.prepareAdminAudience()).thenReturn( + new NotificationDispatchService.AdminAudience(List.of(), Set.of(), Map.of())); + when(notificationDispatchService.pushToUser(any(), any(), any(), any(), any(), any())).thenReturn(true); + when(notificationDispatchService.pushToAdminsAnyOf(any(), any(), any(), any(), any(), any(), any())) + .thenReturn(1); + when(notificationDispatchService.displayNameOf(any())).thenReturn("shop-a"); + } + + private String today() { + return java.time.LocalDate.now().format(java.time.format.DateTimeFormatter.BASIC_ISO_DATE); + } + + /** + * 巡检落库打桩:selectOne 按 wrapper 里的 userId 取库内行,updateById 记录写入状态。 + * 条件值要构建 SQL 段才写入参数表,而 mock 不会建 SQL,故 stub 内手动触发一次。 + */ + private void stubInspectionStore(Map storedByUser) { + TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), + UserApiSecretEntity.class); + inspectionWrites.clear(); + when(mapper.selectOne(any())).thenAnswer(inv -> { + LambdaQueryWrapper wrapper = inv.getArgument(0); + wrapper.getSqlSegment(); + Long userId = wrapper.getParamNameValuePairs().values().stream() + .filter(value -> value instanceof Long) + .map(value -> (Long) value) + .findFirst().orElse(null); + return storedByUser.get(userId); + }); + doAnswer(inv -> { + UserApiSecretEntity entity = inv.getArgument(0); + inspectionWrites.put(entity.getUserId(), entity.getCheckStatus()); + return 1; + }).when(mapper).updateById(any(UserApiSecretEntity.class)); + } + + private UserApiSecretEntity inspectionRow(long id, long userId, String moduleKey, String cipher) { + UserApiSecretEntity entity = storedRow(userId, moduleKey, cipher); + entity.setId(id); + entity.setCheckStatus("unknown"); + return entity; + } + + private UserApiSecretEntity storedRow(long userId, String moduleKey, String cipher) { + UserApiSecretEntity entity = new UserApiSecretEntity(); + entity.setId(userId * 100); + entity.setUserId(userId); + entity.setModuleKey(moduleKey); + entity.setSecretValue(cipher); + return entity; + } + private AdminUserSecretModuleVo moduleVo(boolean exists, String status) { AdminUserSecretModuleVo vo = new AdminUserSecretModuleVo(); vo.setExists(exists);