feat(mcp): 新增 integration.context.get 身份与能力自检工具 (REQ-20260911-0012)
- 实时取 AkAuthFilter 鉴权上下文(ThreadLocal AuthContext)+ ToolGuard 同源能力计算,不缓存权限 - tenant_id 映射启用作用域编码;principal_type 集中在 IntegrationContextService.resolvePrincipalType 判定(当前按 token 绑定用户推导 user_delegated,5629 给 agent_api_key 加 principal_type 列后仅改该方法) - data_scope_version = token_scope 绑定集合最大 modify_time 的 epoch 毫秒,无状态计算 - capability_levels 数据驱动:能力集合含 *.changes.list 工具即具备 SYNC,当前返回 QUERY - seed-dev.sql operation 作用域白名单加入该工具
This commit is contained in:
+165
@@ -0,0 +1,165 @@
|
||||
package me.iiv.agent.mcp;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import me.iiv.agent.entity.m0.User;
|
||||
import me.iiv.agent.entity.svc.AgentApiKey;
|
||||
import me.iiv.agent.entity.svc.AgentScope;
|
||||
import me.iiv.agent.entity.svc.TokenScope;
|
||||
import me.iiv.agent.mapper.svc.AgentScopeMapper;
|
||||
import me.iiv.agent.mapper.svc.TokenScopeMapper;
|
||||
import me.iiv.agent.security.AuthContext;
|
||||
import me.iiv.agent.security.ToolGuard;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@SuppressWarnings("unchecked")
|
||||
class IntegrationContextServiceTest {
|
||||
|
||||
@Mock
|
||||
TokenScopeMapper tokenScopeMapper;
|
||||
@Mock
|
||||
AgentScopeMapper agentScopeMapper;
|
||||
@Mock
|
||||
ToolGuard toolGuard;
|
||||
|
||||
@Test
|
||||
void buildContextReturnsFullStructure() {
|
||||
Date modifyTime = new Date(1_800_000_000_000L);
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(binding(3L, modifyTime, null)));
|
||||
when(agentScopeMapper.selectList(any(LambdaQueryWrapper.class)))
|
||||
.thenReturn(List.of(scope(3L, "operation")));
|
||||
when(toolGuard.allowedToolNames(7L)).thenReturn(Set.of("agent.count", "sys.ping"));
|
||||
IntegrationContextService service = service();
|
||||
|
||||
Map<String, Object> result = service.buildContext(context(token(7L, 42L), user(42L)));
|
||||
|
||||
assertEquals("operation", result.get("tenant_id"));
|
||||
assertEquals("user_delegated", result.get("principal_type"));
|
||||
assertEquals(42L, result.get("subject_id"));
|
||||
assertEquals(List.of("agent.count", "sys.ping"), result.get("scopes"));
|
||||
assertEquals(1_800_000_000_000L, result.get("data_scope_version"));
|
||||
assertEquals(List.of("QUERY"), result.get("capability_levels"));
|
||||
// server_time 必须是带时区的合法 ISO_OFFSET_DATE_TIME
|
||||
OffsetDateTime.parse((String) result.get("server_time"));
|
||||
assertEquals(7, result.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalTypeIsTenantServiceWhenNoUserBound() {
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
|
||||
when(toolGuard.allowedToolNames(7L)).thenReturn(Set.of());
|
||||
IntegrationContextService service = service();
|
||||
AuthContext ctx = context(token(7L, null), null);
|
||||
|
||||
assertEquals("tenant_service", service.resolvePrincipalType(ctx));
|
||||
assertNull(service.buildContext(ctx).get("subject_id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void principalTypeIsUserDelegatedWhenUserBound() {
|
||||
IntegrationContextService service = service();
|
||||
|
||||
assertEquals("user_delegated", service.resolvePrincipalType(context(token(7L, 42L), user(42L))));
|
||||
// 人未加载但 token 绑定了 user_id,仍按用户委托判定(5629 切列前以绑定关系为准)
|
||||
assertEquals("user_delegated", service.resolvePrincipalType(context(token(7L, 42L), null)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void capabilityLevelsAreDataDriven() {
|
||||
IntegrationContextService service = service();
|
||||
|
||||
assertEquals(List.of("QUERY"), service.capabilityLevels(Set.of("sys.ping")));
|
||||
assertEquals(List.of("QUERY", "SYNC"),
|
||||
service.capabilityLevels(Set.of("sys.ping", "agent.changes.list")));
|
||||
assertEquals(List.of("SYNC"), service.capabilityLevels(Set.of("agent.changes.list")));
|
||||
assertEquals(List.of(), service.capabilityLevels(Set.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void dataScopeVersionUsesMaxModifyTime() {
|
||||
IntegrationContextService service = service();
|
||||
|
||||
long version = service.dataScopeVersion(List.of(
|
||||
binding(3L, new Date(1000L), null),
|
||||
binding(4L, new Date(3000L), null),
|
||||
binding(5L, new Date(2000L), null)));
|
||||
assertEquals(3000L, version);
|
||||
// modify_time 为空时回退 create_time
|
||||
assertEquals(500L, service.dataScopeVersion(List.of(binding(3L, null, new Date(500L)))));
|
||||
assertEquals(0L, service.dataScopeVersion(List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tenantIdMapsSortedScopeCodes() {
|
||||
IntegrationContextService service = service();
|
||||
|
||||
assertEquals("hardware,operation",
|
||||
service.tenantId(List.of(scope(1L, "operation"), scope(2L, "hardware"))));
|
||||
assertEquals("", service.tenantId(List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void buildContextWithNoBindingsReturnsEmptyScopeAndZeroVersion() {
|
||||
when(tokenScopeMapper.selectList(any(LambdaQueryWrapper.class))).thenReturn(List.of());
|
||||
when(toolGuard.allowedToolNames(7L)).thenReturn(Set.of());
|
||||
IntegrationContextService service = service();
|
||||
|
||||
Map<String, Object> result = service.buildContext(context(token(7L, 42L), user(42L)));
|
||||
|
||||
assertEquals("", result.get("tenant_id"));
|
||||
assertEquals(List.of(), result.get("scopes"));
|
||||
assertEquals(0L, result.get("data_scope_version"));
|
||||
assertEquals(List.of(), result.get("capability_levels"));
|
||||
}
|
||||
|
||||
private IntegrationContextService service() {
|
||||
return new IntegrationContextService(tokenScopeMapper, agentScopeMapper, toolGuard);
|
||||
}
|
||||
|
||||
private static AuthContext context(AgentApiKey token, User user) {
|
||||
return new AuthContext(token, user);
|
||||
}
|
||||
|
||||
private static AgentApiKey token(Long id, Long userId) {
|
||||
AgentApiKey token = new AgentApiKey();
|
||||
token.setId(id);
|
||||
token.setUserId(userId);
|
||||
return token;
|
||||
}
|
||||
|
||||
private static User user(Long id) {
|
||||
User user = new User();
|
||||
user.setId(id);
|
||||
return user;
|
||||
}
|
||||
|
||||
private static TokenScope binding(Long scopeId, Date modifyTime, Date createTime) {
|
||||
TokenScope binding = new TokenScope();
|
||||
binding.setScopeId(scopeId);
|
||||
binding.setModifyTime(modifyTime);
|
||||
binding.setCreateTime(createTime);
|
||||
return binding;
|
||||
}
|
||||
|
||||
private static AgentScope scope(Long id, String code) {
|
||||
AgentScope scope = new AgentScope();
|
||||
scope.setId(id);
|
||||
scope.setCode(code);
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user