Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15d0893522 | ||
|
|
6841712289 |
No files matched your search
+20
-50
@@ -18,64 +18,34 @@ AI 客户端在使用本平台过程中发现 bug 或有改进建议,**请提
|
||||
|
||||
## 版本历史
|
||||
|
||||
## [1.3.6] - 2026-09-07 MCP 读路径按真实调用者解析主体(issue #8)
|
||||
|
||||
**影响**:MCP 读工具(get_objects_by_type / get_object / query_related / search_ontology / get_object_types / get_facets)此前一律以 connector 主体执行,manager 走 MCP 只能看 connector clearance (L0-L2) 下的数据;现已按调用者真实 api_key 解析主体。
|
||||
|
||||
**改动**:
|
||||
- `callConn` 改为透传调用方 `X-API-Key` / session cookie,让 `principalFromRequest` 自然解析主体;不再注入 connector key + 手动 X-Principal(之前是「模拟身份」)。
|
||||
- `connectorAuth` 放宽:接受任何主体的 api_key(不再只接 connector key)。连接器入口 = 任何已认证主体;可见性仍由读路径 classification filter 按各自 clearance 控制。
|
||||
|
||||
**顺带**:`PUT /access/principals/:name` 只传 description 时响应 `has_api_key` 误报 false 已修复(读取 DB 真实状态回显)。
|
||||
|
||||
## [1.3.3] - 2026-09-07 访问控制:principal clearance 可配置(issue #7)
|
||||
|
||||
按 用户提交 issue #7:「principal_clearances 不可配置,导致 L3 实例对所有非 manager 主体不可见」
|
||||
——实测是 UX 缺口而非模型 bug:管理界面的主体编辑入口(PUT /principals/:name)完全不接受
|
||||
clearances 字段,导致管理员找不到"按主体配 clearance" 的入口(其实 PUT /principals/:name/clearances
|
||||
专用端点一直存在,只是用户没找到)。
|
||||
**影响**:之前管理员无法通过 `PUT /principals/:name` 配置 clearance(字段被静默忽略),主体 clearance 列表接口也不回显;现已补齐。
|
||||
|
||||
### 修复
|
||||
- `PUT /api/v1/access/principals/:name` 的 `principalUpdateInput` 新增 `clearances` 字段(`[]string`)。
|
||||
nil=不动;非空=全量替换(DELETE + INSERT),与 PUT /principals/:name/clearances 同义。
|
||||
- `GET /api/v1/access/principals/:name` 响应新增 `clearances` 数组(保证非 nil,空集序列化为 `[]`)。
|
||||
- `GET /api/v1/access/principals` 列表也回显 `clearances`,避免管理员看不到当前状态。
|
||||
|
||||
### 新增 MCP 工具
|
||||
- `list_principals`:列出全部主体及其 clearance(之前 36 个工具没有读入口)。
|
||||
- `get_principal`:按名取单个主体(含 description / has_api_key / clearances)。
|
||||
- `set_principal_clearance`:全量替换主体 clearance(alias 接受 `markings` / `clearances`)。
|
||||
|
||||
### 不动的部分
|
||||
- `seedAccessControlDefaults` 中 manager 默认 `[L0,L1,L2,L3,L4]` 已正确(CHANGELOG v1.2.0
|
||||
「manager 不受影响」承诺依然成立)。
|
||||
- `classificationCond` / `classAllowed` 中 `if authz.Principal == "manager" { return ... }` 已正确
|
||||
豁免;用户报告的「admin 看不到 L3」实际是误读——真正不可见的是非 manager 主体(默认
|
||||
connector L0-L2、public L0)。
|
||||
|
||||
### 验证
|
||||
- 新增 3 个集成测试(issue7_clearance_config_test.go):
|
||||
updatePrincipal clearances / MCP 端到端 / 旧专用端点回归,全绿。
|
||||
**改动**:
|
||||
- `PUT /access/principals/:name` 新增 `clearances []string`(nil=不动;非空=全量替换)。
|
||||
- `GET /principals/:name` 与列表回显 `clearances`(保证非 nil)。
|
||||
- MCP 新增 `list_principals` / `get_principal` / `set_principal_clearance` 三个工具。
|
||||
|
||||
## [1.3.2] - 2026-09-07 本体:补 update_object_type / add_object_type_property(issue #6)
|
||||
|
||||
按 用户提交 issue #6:「原生分级引擎无法接管存量 data_level:已存在类型无法注册 classification_property」——
|
||||
根本原因是「向已存在类型追加属性」这条能力同时缺 REST 和 MCP 两端的入口。修复后即可接
|
||||
管 587 个已打 data_level 的实例。
|
||||
**影响**:本体管理工具集缺「向已存在类型追加属性」入口,导致 587 个 `data_level` 实例无法被分级引擎接管;现已补齐。
|
||||
|
||||
### 修复
|
||||
- `PUT /api/v1/ontology/object-types/:api_name` 此前静默忽略 `properties` 字段(用户实测加
|
||||
`zz_probe` 属性再次 GET 未注册上)。现 `objectTypeInput` 新增 `properties` 数组字段,非空时
|
||||
由 `upsertObjectTypeProperties` 一并 upsert(同名属性更新元数据,新名属性新增)。语义与
|
||||
`createObjectType` 内联 properties 一致。
|
||||
- 同时为 `createObjectType` 也启用内联 `properties`(共用同一 upsert 辅助函数)。
|
||||
**改动**:
|
||||
- `PUT /object-types/:api_name` 静默忽略 `properties` 字段已修复,与 `createObjectType` 共用 `upsertObjectTypeProperties`(同名更新元数据,新名新增)。
|
||||
- MCP 新增 `update_object_type` / `add_object_type_property` 工具。
|
||||
|
||||
### 新增
|
||||
- MCP 工具 `update_object_type`(包装 `PUT /object-types/:name`)——支持在更新 display/icon/group/
|
||||
分级三件套的同时批量 upsert properties。
|
||||
- MCP 工具 `add_object_type_property`(包装 `POST /object-types/:name/properties`)——细粒度
|
||||
「加一个属性」入口,适合「只补一个」场景(如分级属性 data_level)。
|
||||
- `upsertObjectTypeProperties` 公共辅助函数,统一 `create_object_type` 内联 / `update_object_type`
|
||||
内联 / `add_object_type_property` 三条路径的 upsert 语义。
|
||||
|
||||
### 不采纳
|
||||
- 方案 C(让 `set_object_type_classification` 在 classification_property 引用的属性不存在于
|
||||
schema 时自动注入):把「属性注册」藏在「分类配置」里语义混淆;属性注册应是显式动作 + 可独立
|
||||
审计。
|
||||
|
||||
### 验证
|
||||
- 新增 3 个集成测试(issue6_object_type_property_test.go):update_object_type merge 语义、
|
||||
add_object_type_property MCP 端到端、update_object_type MCP 端到端,全绿。
|
||||
**设计**:拒绝方案 C(让 `set_object_type_classification` 自动注入未注册属性)——属性注册应是显式动作,不应藏在分类配置里失去审计。
|
||||
|
||||
## [1.3.1] - 2026-09-07 访问控制:内置主体可设置自己的 API Key;密钥完全由 DB 管理
|
||||
|
||||
|
||||
@@ -128,6 +128,18 @@ func (a *app) managerKeySet() bool {
|
||||
return a.principalKeySet("manager")
|
||||
}
|
||||
|
||||
// anyPrincipalKeySet 报告全库任意主体是否已配置非空 api_key(用于 connectorAuth 在
|
||||
// 全库无密钥时放行、否则要求带 key 的判定)。修复 issue #8 v2:原 connectorKeySet 仅看
|
||||
// connector 一个主体,现在需要看任意主体。
|
||||
func (a *app) anyPrincipalKeySet() bool {
|
||||
var n int
|
||||
if err := a.pool.QueryRow(context.Background(),
|
||||
"SELECT 1 FROM principals WHERE api_key IS NOT NULL AND api_key<>'' LIMIT 1").Scan(&n); err == nil {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// builtinKey 读取某内置主体当前存于 principals 表的 api_key(用于内部进程调用注入鉴权头)。
|
||||
func (a *app) builtinKey(name string) string {
|
||||
var k string
|
||||
|
||||
@@ -214,14 +214,24 @@ func (a *app) updatePrincipal(c *gin.Context) {
|
||||
a.InvalidateAuthzCache(name)
|
||||
// 回读,让前端能立即核对(同时验证上面 writes)
|
||||
resp := gin.H{"ok": true, "name": name}
|
||||
if finalKey != "" {
|
||||
resp["api_key"] = finalKey
|
||||
resp["api_key_hint"] = maskAPIKey(finalKey)
|
||||
resp["has_api_key"] = true
|
||||
} else if in.ClearAPIKey {
|
||||
resp["has_api_key"] = false
|
||||
keyTouched := in.ClearAPIKey || in.GenerateAPIKey || (in.APIKey != nil && *in.APIKey != "")
|
||||
if keyTouched {
|
||||
// 这次请求确实动了 api_key,按结果回显
|
||||
if finalKey != "" {
|
||||
resp["api_key"] = finalKey
|
||||
resp["api_key_hint"] = maskAPIKey(finalKey)
|
||||
resp["has_api_key"] = true
|
||||
} else if in.ClearAPIKey {
|
||||
resp["has_api_key"] = false
|
||||
} else {
|
||||
resp["has_api_key"] = finalKey != ""
|
||||
}
|
||||
} else {
|
||||
resp["has_api_key"] = finalKey != ""
|
||||
// 未提供 api_key 相关字段:回显 DB 真实状态,避免误导。
|
||||
// 修复 issue #8 附注:之前此处错误地把 "未传 api_key" 当作 "key 不存在"。
|
||||
var cur string
|
||||
_ = a.pool.QueryRow(c, `SELECT COALESCE(api_key,'') FROM principals WHERE name=$1`, name).Scan(&cur)
|
||||
resp["has_api_key"] = cur != ""
|
||||
}
|
||||
if in.Clearances != nil {
|
||||
resp["clearances"] = *in.Clearances
|
||||
|
||||
+27
-9
@@ -665,7 +665,7 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
|
||||
return string(b), false, false
|
||||
|
||||
case "get_object_types":
|
||||
return a.callConn("GET", "/api/v1/connector/object-types", nil), false, false
|
||||
return a.callConn(c, "GET", "/api/v1/connector/object-types", nil), false, false
|
||||
|
||||
case "get_objects_by_type":
|
||||
typ, _ := args["type"].(string)
|
||||
@@ -677,12 +677,12 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
|
||||
if v, ok := args["offset"].(float64); ok && v > 0 {
|
||||
qs += "&offset=" + strconv.Itoa(int(v))
|
||||
}
|
||||
return a.callConn("GET", "/api/v1/connector/objects/"+url.PathEscape(typ)+qs, nil), false, false
|
||||
return a.callConn(c, "GET", "/api/v1/connector/objects/"+url.PathEscape(typ)+qs, nil), false, false
|
||||
|
||||
case "get_object":
|
||||
typ, _ := args["type"].(string)
|
||||
pk, _ := args["pk"].(string)
|
||||
return a.callConn("GET", "/api/v1/connector/object/"+url.PathEscape(typ)+"/"+url.PathEscape(pk), nil), false, false
|
||||
return a.callConn(c, "GET", "/api/v1/connector/object/"+url.PathEscape(typ)+"/"+url.PathEscape(pk), nil), false, false
|
||||
|
||||
case "search_ontology":
|
||||
q, _ := args["q"].(string)
|
||||
@@ -699,7 +699,7 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
|
||||
if field != "" {
|
||||
u += "&field=" + url.QueryEscape(field)
|
||||
}
|
||||
return a.callConn("GET", u, nil), false, false
|
||||
return a.callConn(c, "GET", u, nil), false, false
|
||||
|
||||
case "km_list_tree":
|
||||
t, _ := a.mcpCall("GET", "/api/v1/km/tree", nil, principal)
|
||||
@@ -757,7 +757,7 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
|
||||
if typ != "" {
|
||||
u += "?type=" + url.QueryEscape(typ)
|
||||
}
|
||||
return a.callConn("GET", u, nil), false, false
|
||||
return a.callConn(c, "GET", u, nil), false, false
|
||||
|
||||
case "query_related":
|
||||
typ, _ := args["type"].(string)
|
||||
@@ -768,7 +768,7 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
|
||||
}
|
||||
filters, _ := args["filters"].(map[string]interface{})
|
||||
body, _ := json.Marshal(queryInput{Type: typ, Filters: filters, Expand: expand, Limit: limit})
|
||||
return a.callConn("POST", "/api/v1/connector/query", body), false, false
|
||||
return a.callConn(c, "POST", "/api/v1/connector/query", body), false, false
|
||||
|
||||
case "whoami":
|
||||
// principal / canWrite 已在函数顶部解析
|
||||
@@ -1116,17 +1116,35 @@ func (a *app) mcpCall(method, path string, body []byte, principal string) (strin
|
||||
}
|
||||
|
||||
// callConn 进程内把请求路由到连接器 handler,复用现有只读逻辑。
|
||||
func (a *app) callConn(method, path string, body []byte) string {
|
||||
// 修复 issue #8(v2,按用户「每个主体都有 mcp 访问 key,应该是各自主体身份」反馈):
|
||||
//
|
||||
// 透传调用方真实身份(X-API-Key 或 session cookie)给内层请求,
|
||||
// 让 principalFromRequest 自然解析到对应主体(manager / connector / analyst / ...),
|
||||
// 读路径的 visibility / classification filter 即按真实身份工作。
|
||||
//
|
||||
// 之前 v1 方案(注入 connector key + 手动 X-Principal)本质是「模拟身份」,
|
||||
// 绕开了主体解析链——仍以 connector 权限过 connectorAuth,再用 X-Principal 覆盖身份,
|
||||
// 同一份逻辑在两套机制间切来切去。透传更简洁,且与「每个主体各有 key」的设计一致。
|
||||
//
|
||||
// connectorAuth 同步放宽:接受任何内置/非内置主体的 key(详见 main.go 注释)。
|
||||
// principal 参数保留:用于日志/tracing,不参与 auth。
|
||||
func (a *app) callConn(c *gin.Context, method, path string, body []byte) string {
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
reader = bytes.NewReader(body)
|
||||
}
|
||||
req := httptest.NewRequest(method, path, reader)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
// 内部调用连接器 handler:注入 connector 主体的 api_key 以通过 connectorAuth(密钥统一存于 principals 表)。
|
||||
if k := a.builtinKey("connector"); k != "" {
|
||||
// 透传调用方真实鉴权:让 principalFromRequest 自然解析到对应主体。
|
||||
if k := c.GetHeader("X-API-Key"); k != "" {
|
||||
req.Header.Set("X-API-Key", k)
|
||||
}
|
||||
if k := c.Query("key"); k != "" && c.GetHeader("X-API-Key") == "" {
|
||||
req.Header.Set("X-API-Key", k)
|
||||
}
|
||||
if tok, err := c.Cookie(SessionCookieName); err == nil && tok != "" {
|
||||
req.AddCookie(&http.Cookie{Name: SessionCookieName, Value: tok})
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
a.engine.ServeHTTP(w, req)
|
||||
return w.Body.String()
|
||||
|
||||
@@ -0,0 +1,328 @@
|
||||
//go:build integration
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func lenAny(v interface{}) int {
|
||||
if v == nil {
|
||||
return 0
|
||||
}
|
||||
if sl, ok := v.([]interface{}); ok {
|
||||
return len(sl)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
// extractCount 从 MCP 响应里取 count 字段(MCP 工具结果以 JSON 字符串放在 content[0].text)。
|
||||
// 返回 -1 表示解析失败。
|
||||
func extractCount(t *testing.T, body map[string]interface{}) int {
|
||||
t.Helper()
|
||||
result, ok := body["result"].(map[string]interface{})
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
content, ok := result["content"].([]interface{})
|
||||
if !ok || len(content) == 0 {
|
||||
return -1
|
||||
}
|
||||
first, ok := content[0].(map[string]interface{})
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
text, ok := first["text"].(string)
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
// 在 text 里找 "count":N
|
||||
needle := `"count":`
|
||||
i := strings.Index(text, needle)
|
||||
if i < 0 {
|
||||
return -1
|
||||
}
|
||||
rest := text[i+len(needle):]
|
||||
// 跳过空白
|
||||
for len(rest) > 0 && (rest[0] == ' ' || rest[0] == '\t') {
|
||||
rest = rest[1:]
|
||||
}
|
||||
// 读数字
|
||||
end := 0
|
||||
for end < len(rest) && rest[end] >= '0' && rest[end] <= '9' {
|
||||
end++
|
||||
}
|
||||
if end == 0 {
|
||||
return -1
|
||||
}
|
||||
n, err := strconvAtoi(rest[:end])
|
||||
if err != nil {
|
||||
return -1
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// strconvAtoi 局部 int 解析(避免 import strconv)
|
||||
func strconvAtoi(s string) (int, error) {
|
||||
n := 0
|
||||
if s == "" {
|
||||
return 0, errEmpty
|
||||
}
|
||||
for _, c := range s {
|
||||
if c < '0' || c > '9' {
|
||||
return 0, errNotDigit
|
||||
}
|
||||
n = n*10 + int(c-'0')
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
var (
|
||||
errEmpty = simpleErr("empty")
|
||||
errNotDigit = simpleErr("not digit")
|
||||
)
|
||||
|
||||
type simpleErr string
|
||||
|
||||
func (e simpleErr) Error() string { return string(e) }
|
||||
|
||||
// seedConnectorForTest 在测试 DB 里塞内置 connector 主体(api_key 已知)。
|
||||
// setupTestEnv 不调用 seedAccessControlDefaults(避免污染共享测试库),所以每个
|
||||
// 走 callConn 路径的测试自己负责准备。
|
||||
func seedConnectorForTest(t *testing.T) string {
|
||||
t.Helper()
|
||||
pool, err := initDB(context.Background(), testDSN())
|
||||
if err != nil {
|
||||
t.Skipf("无法连接测试 PG: %v", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
const connKey = "test-connector-key-do-not-use-in-prod"
|
||||
const mgrKey = "test-manager-key-do-not-use-in-prod"
|
||||
const anaKey = "test-analyst-key-do-not-use-in-prod"
|
||||
_, _ = pool.Exec(context.Background(),
|
||||
`INSERT INTO principals(name,description,is_builtin,api_key) VALUES('connector','test connector',true,$1)
|
||||
ON CONFLICT(name) DO UPDATE SET api_key=EXCLUDED.api_key`,
|
||||
connKey)
|
||||
_, _ = pool.Exec(context.Background(),
|
||||
`INSERT INTO principals(name,description,is_builtin,api_key) VALUES('manager','test manager',true,$1)
|
||||
ON CONFLICT(name) DO UPDATE SET api_key=EXCLUDED.api_key`,
|
||||
mgrKey)
|
||||
_, _ = pool.Exec(context.Background(),
|
||||
`INSERT INTO principals(name,description,is_builtin,api_key) VALUES('analyst','test analyst',true,$1)
|
||||
ON CONFLICT(name) DO UPDATE SET api_key=EXCLUDED.api_key`,
|
||||
anaKey)
|
||||
// 注入 clearance:connector L0+L1(与生产 seed 一致);manager L0..L4(豁免前的真实
|
||||
// clearance 仍写入 DB,classificationCond 对 manager 走 return TRUE 所以不影响;
|
||||
// analyst 仅 L0(最受限)
|
||||
// 先 DELETE 再 INSERT,避免上次测试残留的 clearance 留下误配置
|
||||
_, _ = pool.Exec(context.Background(),
|
||||
`DELETE FROM principal_clearances WHERE principal IN ('connector','manager','analyst')`)
|
||||
_, _ = pool.Exec(context.Background(),
|
||||
`INSERT INTO principal_clearances(principal,marking)
|
||||
VALUES('connector','L0'),('connector','L1'),
|
||||
('manager','L0'),('manager','L1'),('manager','L2'),('manager','L3'),('manager','L4'),
|
||||
('analyst','L0')`)
|
||||
invalidateClassCache()
|
||||
authzCache.Range(func(k, _ any) bool { authzCache.Delete(k); return true })
|
||||
return connKey
|
||||
}
|
||||
|
||||
// TestIntegration_MCPRead_RespectsPrincipal 修复 issue #8:
|
||||
// 之前 callConn 硬编码 connector 的 api_key,导致所有 MCP 读工具
|
||||
// (get_objects_by_type / get_object / query_related / search_ontology /
|
||||
// get_object_types / get_facets)一律以 connector 主体执行——manager 走
|
||||
// MCP 也只能看到 connector clearance(L0-L2)下的数据,违反 v1.2.0 「manager 不受影响」。
|
||||
//
|
||||
// 新行为:callConn 接受 principal 参数,内层请求同时注入
|
||||
// X-API-Key=<connector>(让 connectorAuth 放行)+ X-Principal=<实际 caller>
|
||||
// 让 principalFromRequest 走 X-Principal 分支,读 filter 按真实身份工作。
|
||||
func TestIntegration_MCPRead_RespectsPrincipal(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
seedConnectorForTest(t)
|
||||
|
||||
hMgr := map[string]string{"X-Principal": "manager"}
|
||||
|
||||
// 建一个含 L0/L1/L3 三档的测试类型 + 3 个实例
|
||||
key := "test_mcp_read_principal"
|
||||
t.Cleanup(func() {
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+key, nil, hMgr)
|
||||
})
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{
|
||||
"api_name": key, "display_name": "测试 MCP 读 principal",
|
||||
"classification_property": "lvl",
|
||||
"classification_levels": []string{"L0", "L1", "L3"},
|
||||
"classification_default": "L0",
|
||||
}, hMgr); code != 201 {
|
||||
t.Fatalf("create type failed: %d", code)
|
||||
}
|
||||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties",
|
||||
map[string]any{"api_name": "lvl", "display_name": "级别", "data_type": "string",
|
||||
"enum_values": []string{"L0", "L1", "L3"}}, hMgr)
|
||||
|
||||
for _, c := range []struct{ pk, lvl string }{
|
||||
{"a_l0", "L0"}, {"a_l1", "L1"}, {"a_l3", "L3"},
|
||||
} {
|
||||
code, body := env.httpDo(t, "POST", "/api/v1/objects",
|
||||
map[string]any{"type": key, "pk": c.pk, "properties": map[string]any{"lvl": c.lvl}},
|
||||
hMgr)
|
||||
if code >= 300 {
|
||||
t.Fatalf("create %s 失败 code=%d body=%v", c.pk, code, body)
|
||||
}
|
||||
// 显式写入 object_markings,让 classification 物化到 L3
|
||||
env.httpDo(t, "PUT", "/api/v1/access/objects/"+key+"/"+c.pk+"/markings",
|
||||
map[string]any{"markings": []string{c.lvl}}, hMgr)
|
||||
}
|
||||
|
||||
// 3) 通过 MCP /api/v1/mcp 调 get_objects_by_type:connector 身份(X-API-Key: connector_key)应见 2
|
||||
_, bodyC := env.httpDo(t, "POST", "/api/v1/mcp",
|
||||
map[string]any{
|
||||
"jsonrpc": "2.0", "id": "1", "method": "tools/call",
|
||||
"params": map[string]any{
|
||||
"name": "get_objects_by_type",
|
||||
"arguments": map[string]any{"type": key, "limit": 50},
|
||||
},
|
||||
}, map[string]string{"X-API-Key": "test-connector-key-do-not-use-in-prod"})
|
||||
connectorCount := extractCount(t, bodyC)
|
||||
if connectorCount != 2 {
|
||||
t.Errorf("connector 走 MCP get_objects_by_type 应见 2 条(L0+L1), got %d body=%v", connectorCount, bodyC)
|
||||
}
|
||||
|
||||
// 4) manager 身份走 MCP:应见 3(豁免)
|
||||
_, bodyM := env.httpDo(t, "POST", "/api/v1/mcp",
|
||||
map[string]any{
|
||||
"jsonrpc": "2.0", "id": "2", "method": "tools/call",
|
||||
"params": map[string]any{
|
||||
"name": "get_objects_by_type",
|
||||
"arguments": map[string]any{"type": key, "limit": 50},
|
||||
},
|
||||
}, map[string]string{"X-API-Key": "test-manager-key-do-not-use-in-prod"})
|
||||
managerCount := extractCount(t, bodyM)
|
||||
if managerCount != 3 {
|
||||
t.Errorf("manager 走 MCP get_objects_by_type 应见 3 条(含 L3), got %d body=%v", managerCount, bodyM)
|
||||
}
|
||||
|
||||
// 5) analyst 身份(L0 only)走 MCP:应见 1
|
||||
_, bodyA := env.httpDo(t, "POST", "/api/v1/mcp",
|
||||
map[string]any{
|
||||
"jsonrpc": "2.0", "id": "3", "method": "tools/call",
|
||||
"params": map[string]any{
|
||||
"name": "get_objects_by_type",
|
||||
"arguments": map[string]any{"type": key, "limit": 50},
|
||||
},
|
||||
}, map[string]string{"X-API-Key": "test-analyst-key-do-not-use-in-prod"})
|
||||
analystCount := extractCount(t, bodyA)
|
||||
if analystCount != 1 {
|
||||
t.Errorf("analyst 走 MCP get_objects_by_type 应见 1 条(L0 only), got %d body=%v", analystCount, bodyA)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_MCPRead_QueryRelatedPrincipal 通过 MCP /api/v1/mcp 端点
|
||||
// 验证 callConn 修复:manager 走 query_related 应能见 L3,connector 只能见 L0。
|
||||
func TestIntegration_MCPRead_QueryRelatedPrincipal(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
seedConnectorForTest(t)
|
||||
|
||||
hMgr := map[string]string{"X-Principal": "manager"}
|
||||
|
||||
key := "test_mcp_query_related"
|
||||
t.Cleanup(func() {
|
||||
env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+key, nil, hMgr)
|
||||
})
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{
|
||||
"api_name": key, "display_name": "MCP query 测试",
|
||||
"classification_property": "lvl",
|
||||
"classification_levels": []string{"L0", "L3"},
|
||||
"classification_default": "L0",
|
||||
}, hMgr); code != 201 {
|
||||
t.Fatalf("create type failed: %d", code)
|
||||
}
|
||||
env.httpDo(t, "POST", "/api/v1/ontology/object-types/"+key+"/properties",
|
||||
map[string]any{"api_name": "lvl", "display_name": "级别", "data_type": "string",
|
||||
"enum_values": []string{"L0", "L3"}}, hMgr)
|
||||
|
||||
for _, c := range []struct{ pk, lvl string }{
|
||||
{"a_l0", "L0"}, {"a_l3", "L3"},
|
||||
} {
|
||||
env.httpDo(t, "POST", "/api/v1/objects",
|
||||
map[string]any{"type": key, "pk": c.pk, "properties": map[string]any{"lvl": c.lvl}, "via": "test"},
|
||||
hMgr)
|
||||
env.httpDo(t, "PUT", "/api/v1/access/objects/"+key+"/"+c.pk+"/markings",
|
||||
map[string]any{"markings": []string{c.lvl}}, hMgr)
|
||||
}
|
||||
|
||||
// manager 走 query_related
|
||||
code, body := env.httpDo(t, "POST", "/api/v1/mcp",
|
||||
map[string]any{
|
||||
"jsonrpc": "2.0", "id": "1", "method": "tools/call",
|
||||
"params": map[string]any{
|
||||
"name": "query_related",
|
||||
"arguments": map[string]any{"type": key, "limit": 50},
|
||||
},
|
||||
}, map[string]string{"X-API-Key": "test-manager-key-do-not-use-in-prod"})
|
||||
if code != 200 {
|
||||
t.Fatalf("query_related 应 200, got %d body=%v", code, body)
|
||||
}
|
||||
result := body["result"].(map[string]any)
|
||||
content := result["content"].([]any)
|
||||
text := content[0].(map[string]any)["text"].(string)
|
||||
if !strings.Contains(text, `"count":2`) {
|
||||
t.Errorf("manager query_related 应见 count=2, got %s", text)
|
||||
}
|
||||
|
||||
// connector 走 query_related:应见 1(L0 only)
|
||||
code, body = env.httpDo(t, "POST", "/api/v1/mcp",
|
||||
map[string]any{
|
||||
"jsonrpc": "2.0", "id": "2", "method": "tools/call",
|
||||
"params": map[string]any{
|
||||
"name": "query_related",
|
||||
"arguments": map[string]any{"type": key, "limit": 50},
|
||||
},
|
||||
}, map[string]string{"X-API-Key": "test-connector-key-do-not-use-in-prod"})
|
||||
if code != 200 {
|
||||
t.Fatalf("query_related 应 200, got %d", code)
|
||||
}
|
||||
result2 := body["result"].(map[string]any)
|
||||
content2 := result2["content"].([]any)
|
||||
text2 := content2[0].(map[string]any)["text"].(string)
|
||||
if !strings.Contains(text2, `"count":1`) {
|
||||
t.Errorf("connector query_related 应见 count=1, got %s", text2)
|
||||
}
|
||||
}
|
||||
|
||||
// TestIntegration_UpdatePrincipal_NotTouchKeyHasAPIEchoTrue 修复 issue #8 附注:
|
||||
// PUT /access/principals/:name 只传 description 不传 api_key 时,响应 has_api_key
|
||||
// 应反映 DB 真实状态,而非误报 false。
|
||||
func TestIntegration_UpdatePrincipal_NotTouchKeyHasAPIEchoTrue(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
h := map[string]string{"X-Principal": "manager"}
|
||||
|
||||
key := "test_not_touch_key"
|
||||
t.Cleanup(func() {
|
||||
env.httpDo(t, "DELETE", "/api/v1/access/principals/"+key, nil, h)
|
||||
})
|
||||
// 建主体并显式设个 key
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/access/principals",
|
||||
map[string]any{"name": key, "description": "初始", "generate_api_key": true}, h); code != 201 {
|
||||
t.Fatalf("create failed: %d", code)
|
||||
}
|
||||
|
||||
// PUT 只改 description,不动 key
|
||||
code, body := env.httpDo(t, "PUT", "/api/v1/access/principals/"+key,
|
||||
map[string]any{"description": "只改 desc"}, h)
|
||||
if code != 200 {
|
||||
t.Fatalf("PUT 应 200, got %d", code)
|
||||
}
|
||||
if hasKey, _ := body["has_api_key"].(bool); !hasKey {
|
||||
t.Errorf("PUT 不动 key 时 has_api_key 应保持 true, got body=%v", body)
|
||||
}
|
||||
|
||||
// 再 GET 确认 key 还在
|
||||
_, get := env.httpDo(t, "GET", "/api/v1/access/principals/"+key, nil, h)
|
||||
if hasKey, _ := get["has_api_key"].(bool); !hasKey {
|
||||
t.Errorf("GET 复查 has_api_key 应 true, got %v", get)
|
||||
}
|
||||
}
|
||||
+12
-4
@@ -388,8 +388,15 @@ func (a *app) loginRequired() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// connectorAuth 连接器入口鉴权:connector 主体已配置 api_key 时须在 X-API-Key(或 key 查询参数)
|
||||
// 携带该 key;未配置 key 的部署则放行(兼容无密钥只读场景)。
|
||||
// connectorAuth 连接器入口鉴权:key 是「任意主体的有效 api_key」即可放行(修复 issue #8 v2)。
|
||||
//
|
||||
// 历史:早期只接受 name='connector' 的 key(连接器入口 = 单一 connector 身份)。
|
||||
// v2:放宽到任一主体的 key——既然 manager / connector / analyst 等各有 api_key,
|
||||
// 连接器入口就应当接受调用方的真实身份,让 principalFromRequest 按真实主体解析,
|
||||
// 读路径的 visibility / classification filter 即按真实身份工作(manager 豁免、
|
||||
// connector/analyst 按各自 clearance 子集)。
|
||||
//
|
||||
// 未提供 key 时:若全库任意主体已配置 key 则要求带(401),全库无 key 则放行(无密钥只读场景)。
|
||||
func (a *app) connectorAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
key := c.GetHeader("X-API-Key")
|
||||
@@ -397,7 +404,8 @@ func (a *app) connectorAuth() gin.HandlerFunc {
|
||||
key = c.Query("key")
|
||||
}
|
||||
if key == "" {
|
||||
if a.principalKeySet("connector") {
|
||||
// 无 key:若任意主体已有 key,则视为必须带 key;否则放行(无密钥只读兼容)
|
||||
if a.anyPrincipalKeySet() {
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "连接器接口需要 X-API-Key"})
|
||||
return
|
||||
}
|
||||
@@ -406,7 +414,7 @@ func (a *app) connectorAuth() gin.HandlerFunc {
|
||||
}
|
||||
var name string
|
||||
if err := a.pool.QueryRow(c,
|
||||
"SELECT name FROM principals WHERE name='connector' AND api_key=$1", key).Scan(&name); err == nil && name != "" {
|
||||
"SELECT name FROM principals WHERE api_key=$1", key).Scan(&name); err == nil && name != "" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user