perf(用户密钥): 探测与告警按密钥值收敛,共用同一把 key 的账号不再各付一次提取费

实测 314 条配置只有 33 种密钥(一把平均挂 9.5 个账号):定时巡检按「模块+密文」分组后一轮只
真实探测一次;后台「立即检测该用户」的 90s 新鲜期缓存键去掉 uid、改为按密钥值指纹共享,
管理员连点 N 个共用 key 的账号只付 1 次费。管理员侧告警按密钥组合并成一条(受众仍按数据权限
过滤,正文不列 uid——同 key 账号常分属不同主管),单账号组保持原文案与原去重键。
This commit is contained in:
2026-10-02 09:21:25 +08:00
parent 5ad0f5f685
commit a4391de6fa
5 changed files with 418 additions and 52 deletions
@@ -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<String, String> redisValues = new HashMap<>();
/** 巡检测试用:记录 updateById 写入的 userId → checkStatus 快照。 */
private final Map<Long, String> inspectionWrites = new LinkedHashMap<>();
private ValueOperations<String, String> 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<String> title = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> content = ArgumentCaptor.forClass(String.class);
@SuppressWarnings("unchecked")
ArgumentCaptor<Collection<Long>> 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<String> title = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> content = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> 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<Long, UserApiSecretEntity> storedByUser) {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
UserApiSecretEntity.class);
inspectionWrites.clear();
when(mapper.selectOne(any())).thenAnswer(inv -> {
LambdaQueryWrapper<UserApiSecretEntity> 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);