6 Commits
Author SHA1 Message Date
admin 5e2ddf7241 docs(changelog): v1.4.0 发布说明(使用分析 + schema 层审计) 2026-09-11 18:32:43 +08:00
admin 8ed658382f feat(analytics): 使用分析面板 + schema 层审计 + access_log target/保留策略
- access_log 中间件按路由模板映射 target(修复 :name/:id 参数名导致
  /object-types/:name 等路由 target 长期为空);401/4xx 兜底记 deny;
  action 简化为 read/write/delete/manage;新增 90 天保留策略
  (ACCESS_LOG_RETENTION_DAYS 覆盖,0 关闭)
- schema 审计:对象类型/属性/链接类型/链接属性/动作类型/分级配置的
  增删改写入 ontology_audit_log(target_pk 落库,update/delete 带 before 快照);
  存量库补 target_pk 列(幂等 ALTER)
- 新增 /api/v1/analytics/{overview,types,users,sharing}(仅 manager):
  总体读写比/活跃主体/来源分布/按天趋势、分项热度、用户活跃、共享授权使用量
- 前端新增「使用分析」tab(AnalyticsView)
2026-09-11 18:23:47 +08:00
admin 15d0893522 docs(changelog): v1.3.6 发布说明([Unreleased] 定版 issue #8 MCP 读路径真实身份) 2026-09-07 18:41:14 +08:00
admin 6841712289 fix(mcp): 读路径透传调用方真实身份(issue #8 v2)
按用户反馈「每个主体都有 mcp 访问 key,就应该是各自主体身份」——
v1 方案「注入 connector key + 手动 X-Principal」本质是「模拟身份」,
绕开了主体解析链。v2 改为透传调用方真实鉴权。

### 改动
- callConn 接受源 *gin.Context,透传 X-API-Key / session cookie 给内层请求,
  让 principalFromRequest 自然解析到 manager / connector / analyst / 等。
- connectorAuth 放宽到「任一主体 api_key 即放行」:
  - 移除 WHERE name='connector' 的硬约束
  - 助手 anyPrincipalKeySet() 判断全库是否已有密钥(有则要求带 key,无则放行兼容无密钥只读)
- 6 个调用点去掉 principal 参数,改传 c。

### 影响
- 之前:MCP 读工具一律以 connector 执行,manager 走 MCP 只能看 L0-L2。
- 现在:各主体按自己 clearance 视图读,manager 走 MCP 见全部分级(含 L3)。
- callConn 路径与 connectorAuth 路径身份解析统一(都走 principalFromRequest 自然解析)。

### 验证
- 3 个新集成测试全绿(issue8_mcp_read_principal_test.go):
  按 connector / manager / analyst 三种 api_key 调 MCP get_objects_by_type,
  分别见 2 / 3 / 1 条(符合各自 clearance)。
- 既有 issue #6/#7 的 6 个测试 + SetObjectTypeClassification 全绿。

### CHANGELOG 风格
按用户反馈「只有改了什么 / 有什么影响,不要流水账」,
重写 [Unreleased] / [1.3.3] / [1.3.2] 三段:每段只留「影响 + 改动 [+ 设计]」,
去掉过程叙述、commit hash、调试日志、issue 评论引用等。
2026-09-07 18:36:21 +08:00
admin 2a58d5adab docs(changelog): v1.3.3 发布说明([Unreleased] 定版 issue #7 clearance 配置) 2026-09-07 17:43:46 +08:00
admin 8388e14772 feat(access): 补 principal clearance 配置入口(采纳 issue #7)
用户报告「principal_clearances 不可配置,导致 L3 实例对所有非 manager 主体不可见」
—— 实测是 UX 缺口:

### 修复
- PUT /access/principals/:name 的 principalUpdateInput 新增 clearances 字段(nil=不动;
  非空数组(含 [])= 全量替换),与 PUT /principals/:name/clearances 同义。
- GET /access/principals/:name 响应新增 clearances 数组(保证非 nil,空集序列化为 [])。
- GET /access/principals 列表也回显 clearances,避免管理员看不到当前状态。

### 新增 MCP 工具(之前 36 个没有这一片)
- list_principals:列出全部主体及其 clearance。
- 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 对 manager 显式放行已正确;用户报告的「admin
  看不到 L3」实际是误读——真正不可见的是非 manager 主体(默认 connector L0-L2、public L0)。

### 验证
- 新增 3 个集成测试(issue7_clearance_config_test.go):
  updatePrincipal clearances / MCP 端到端 / 旧专用端点回归,全绿。
- 既有测试除 TestIntegration_ClassificationFilter 外全绿(后者预存在失败)。
2026-09-07 17:38:54 +08:00
17 changed files with 1810 additions and 68 deletions

No files matched your search

+35 -25
View File
@@ -18,35 +18,45 @@ AI 客户端在使用本平台过程中发现 bug 或有改进建议,**请提
## 版本历史
## [1.4.0] - 2026-09-11 使用分析 + schema 层审计
**影响**:本体定义(类型/属性/链接类型等)的增删改此前只有 access_log 的 method+path 间接痕迹,无 before/after;使用/浏览情况无法按本体分项统计(:name 参数路由的 target 长期为空)。
**改动**:
- **schema 审计**:对象类型/属性/链接类型/链接属性/动作类型/分级配置的增删改写入 `ontology_audit_log`(target_pk 落库,update/delete 带 before 快照);存量库幂等补 `target_pk` 列。
- **access_log 中间件**:按路由模板映射 target(修复 `:name`/`:id` 参数名导致 target 为空);4xx 响应兜底记 `deny`;action 归一为 read/write/delete/manage。
- **保留策略**:access_log 默认保留 90 天(`ACCESS_LOG_RETENTION_DAYS` 覆盖,0 关闭),启动时 + 每 24h 清理。
- **使用分析 API**(仅 manager):`GET /api/v1/analytics/overview|types|users|sharing` —— 总体读写比/活跃主体/来源分布/按天趋势、分项热度、用户活跃、共享授权使用量,全部现算不建新表。
- **前端**:新增「使用分析」tab(指标卡 + 按天趋势柱图 + 分项热度/用户活跃/共享明细表,7/30/90 天切换)。
## [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)
**影响**:之前管理员无法通过 `PUT /principals/:name` 配置 clearance(字段被静默忽略),主体 clearance 列表接口也不回显;现已补齐。
**改动**:
- `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 管理
+12
View File
@@ -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
+108 -16
View File
@@ -17,6 +17,7 @@ import (
"encoding/json"
"log"
"strconv"
"strings"
"sync"
"time"
@@ -110,20 +111,24 @@ func (a *app) accessLogMiddleware() gin.HandlerFunc {
}
// 解析 principal
p := a.principalFromRequest(c)
// 提取目标 (type/pk)
typ := c.Param("type")
pk := c.Param("pk")
if typ == "" {
typ = c.Query("type")
}
if pk == "" {
pk = c.Query("pk")
// 提取目标 (type/pk)。三层来源:
// 1) handler 通过 c.Set("audit_target_pk", ...) 精确指定(create 类目标名在 body 里)
// 2) 按路由模板映射(targetFromRoute,修复 :name/:id 参数名导致 target 为空)
// 3) query 参数兜底(老行为)
typ, pk := targetFromRoute(c)
if v := c.GetString("audit_target_pk"); v != "" {
pk = v
}
// 决定(默认 allow;若 handler 已通过 c.Set("authz_decision", ...) 标记,则用它)
decision := c.GetString("authz_decision")
if decision == "" {
decision = "allow"
}
// 修复(2026-09-11):鉴权/业务层直接 401/403/404 的请求此前被记成 allow,
// 导致"拒绝"统计失真 —— 按最终响应状态码兜底标记。
if decision == "allow" && c.Writer.Status() >= 400 {
decision = "deny"
}
// 命中 grant(可选:handler 通过 c.Set("authz_grants", [...]) 写入)
var matchedGrants []int64
if v, ok := c.Get("authz_grants"); ok {
@@ -153,18 +158,77 @@ func (a *app) accessLogMiddleware() gin.HandlerFunc {
}
}
// targetFromRoute 按路由模板(c.FullPath())把请求映射到 (target_type, target_pk)。
// 此前只认 :type/:pk 两个参数名,而 /object-types/:name、/properties/:id 等路由的
// 参数名是 name/id,导致本体类型层操作的 target 长期为空,分项统计无从聚合。
// 约定:实例类路由(/objects/:type/:pk、/connector/object[s]/…)的 target_type
// 就是对象类型 api_name;元原语路由用固定类型名(object_type/link_type/...)。
func targetFromRoute(c *gin.Context) (string, string) {
// 实例路由::type(+:pk)参数天然就是目标
if t := c.Param("type"); t != "" {
return t, c.Param("pk")
}
// query 兜底(老行为保留)
if t := c.Query("type"); t != "" {
return t, c.Query("pk")
}
fp := c.FullPath()
if fp == "" {
return "", ""
}
name := c.Param("name")
id := c.Param("id")
switch {
case strings.Contains(fp, "/object-types/:name/properties"):
return "object_property", name // 具体属性名在 body,schemaAudit 会记全
case strings.Contains(fp, "/object-types/:name"):
return "object_type", name
case strings.Contains(fp, "/object-types"):
return "object_type", "" // create:名字在 body
case strings.Contains(fp, "/properties/:id"):
return "object_property", id
case strings.Contains(fp, "/link-types/:name/properties"):
return "link_property", name
case strings.Contains(fp, "/link-types/:name"):
return "link_type", name
case strings.Contains(fp, "/link-types"):
return "link_type", ""
case strings.Contains(fp, "/link-properties/:id"):
return "link_property", id
case strings.Contains(fp, "/action-types"):
return "action_type", ""
case strings.Contains(fp, "/function-types/:name"):
return "function_type", name
case strings.Contains(fp, "/function-types"):
return "function_type", ""
case strings.Contains(fp, "/interface-types/:name"):
return "interface_type", name
case strings.Contains(fp, "/interface-types"):
return "interface_type", ""
case strings.Contains(fp, "/materializations/:name"):
return "materialization", name
case strings.Contains(fp, "/backing-sources/:name"):
return "backing_source", name
case strings.Contains(fp, "/principals/:name"):
return "principal", name
case strings.Contains(fp, "/grants/:id"):
return "grant", id
case strings.Contains(fp, "/field-policies/:id"):
return "field_policy", id
}
return "", ""
}
func classifyAction(method, path string) string {
switch {
case contains(path, "/objects/") && method == "GET":
return "read"
case contains(path, "/objects") && (method == "POST" || method == "PUT"):
return "write"
case contains(path, "/objects/") && method == "DELETE":
return "delete"
case contains(path, "/connector/") && method == "GET":
return "read"
case contains(path, "/access/"):
return "manage"
case method == "GET":
return "read"
case method == "DELETE":
return "delete"
case method == "POST" || method == "PUT":
return "write"
default:
return method + " " + path
}
@@ -262,5 +326,33 @@ func (a *app) accessLogStats(c *gin.Context) {
c.JSON(200, gin.H{"stats": out})
}
// ---------- 保留策略 ----------
// startAccessLogRetention 定期清理过期 access_log,防止审计表无限膨胀。
// 默认保留 90 天(ACCESS_LOG_RETENTION_DAYS 覆盖,0 = 关闭清理)。
func startAccessLogRetention(p dbExec, days int) {
if days <= 0 {
log.Printf("[access_log] 保留策略已关闭(ACCESS_LOG_RETENTION_DAYS=0)")
return
}
go func() {
clean := func() {
tag, err := p.Exec(context.Background(),
`DELETE FROM access_log WHERE created_at < now() - make_interval(days => $1)`, days)
if err != nil {
log.Printf("[access_log] 过期清理失败: %v", err)
} else if tag.RowsAffected() > 0 {
log.Printf("[access_log] 清理过期审计日志 %d 行(保留 %d 天)", tag.RowsAffected(), days)
}
}
clean() // 启动时清一次
t := time.NewTicker(24 * time.Hour)
defer t.Stop()
for range t.C {
clean()
}
}()
}
// strconv_Atoi 占位(已用标准库,保留符号避免引用残留)
var _ = strconv.Atoi
+8
View File
@@ -614,6 +614,14 @@ func (a *app) setObjectTypeClassification(c *gin.Context) {
c.JSON(400, gin.H{"error": "body 必须是合法 JSON: " + err.Error()})
return
}
// before 快照(审计用)
before := a.objectTypeSnapshot(c, name)
// 分级配置也是 schema 变更,统一走 ontology_audit_log(仅在成功响应时记录)
defer func() {
if c.Writer.Status() < 400 {
a.schemaAudit(c, "object_type_classification", "object_type", name, map[string]any{"before": before, "after": in})
}
}()
// 关掉分级(property=空)允许 levels=[] 与 default=""
if in.Property == "" {
// property=空直接落库:levels 与 default 都以「空」处理(不会去校验词汇)
+2
View File
@@ -17,6 +17,7 @@ type Config struct {
AllowCORS bool
SeedOnStart bool // 首次启动若本体为空则灌入极简占位种子
ArchiveDir string // 知识文件根目录(archive),文件管理功能使用
AccessLogRetentionDays int // access_log 保留天数(0=不清理),默认 90
}
func loadConfig() *Config {
@@ -30,6 +31,7 @@ func loadConfig() *Config {
AllowCORS: getEnvBool("ALLOW_CORS", true),
SeedOnStart: getEnvBool("SEED_ON_START", true),
ArchiveDir: getEnv("ARCHIVE_DIR", "archive"),
AccessLogRetentionDays: getEnvInt("ACCESS_LOG_RETENTION_DAYS", 90),
}
return c
}
+2
View File
@@ -327,6 +327,8 @@ CREATE TABLE IF NOT EXISTS ontology_audit_log (
);
CREATE INDEX IF NOT EXISTS idx_audit_log_created ON ontology_audit_log(created_at);
CREATE INDEX IF NOT EXISTS idx_audit_log_actor ON ontology_audit_log(actor);
-- 兼容存量库(2026-09-11):schema 审计按 target_pk 精确落库
ALTER TABLE ontology_audit_log ADD COLUMN IF NOT EXISTS target_pk TEXT DEFAULT '';
-- 兼容存量库:补 P1-4 Interface 共享 properties + 多态 (2026-08-24)
ALTER TABLE interface_types ADD COLUMN IF NOT EXISTS shared_properties JSONB DEFAULT '[]'::jsonb;
+86 -14
View File
@@ -32,6 +32,8 @@ func (a *app) listPrincipals(c *gin.Context) {
HasAPIKey bool `json:"has_api_key"`
APIKeyHint string `json:"api_key_hint"`
APIKeyCreatedAt string `json:"api_key_created_at,omitempty"`
// 修复 issue #7:clearances 一并回显,否则管理员看不到当前主体的可见级别集。
Clearances []string `json:"clearances"`
}
var out []pRow
for rows.Next() {
@@ -40,7 +42,7 @@ func (a *app) listPrincipals(c *gin.Context) {
var builtin bool
if err := rows.Scan(&p.Name, &p.Description, &builtin, &key); err == nil {
p.IsBuiltin = builtin
row := pRow{Principal: p, HasAPIKey: key != ""}
row := pRow{Principal: p, HasAPIKey: key != "", Clearances: []string{}}
if key != "" {
row.APIKeyHint = maskAPIKey(key)
row.APIKeyCreatedAt = "已绑定"
@@ -48,10 +50,30 @@ func (a *app) listPrincipals(c *gin.Context) {
out = append(out, row)
}
}
// 单独查询所有 clearances(按 principal 分组),避免 N+1
if len(out) > 0 {
clRows, err := a.pool.Query(c, `SELECT principal, marking FROM principal_clearances ORDER BY principal, marking`)
if err == nil {
idx := map[string]int{}
for i, p := range out {
idx[p.Name] = i
}
for clRows.Next() {
var pn, mk string
if clRows.Scan(&pn, &mk) == nil {
if i, ok := idx[pn]; ok {
out[i].Clearances = append(out[i].Clearances, mk)
}
}
}
clRows.Close()
}
}
c.JSON(200, gin.H{"principals": out, "count": len(out)})
}
// getPrincipal 返回单个主体,manager 可拿到完整 api_key(仅展示一次以外不会在 list 中回)
// getPrincipal 返回单个主体,manager 可拿到完整 api_key(仅展示一次以外不会在 list 中回)。
// 修复 issue #7:响应同时回显 clearances 数组,否则前端拿不到主体可见级别集。
func (a *app) getPrincipal(c *gin.Context) {
name := c.Param("name")
var description string
@@ -75,6 +97,20 @@ func (a *app) getPrincipal(c *gin.Context) {
resp["api_key"] = key
resp["api_key_hint"] = maskAPIKey(key)
}
// 拉 clearances(保证非 nil,序列化成 [] 而不是 null)
cls := []string{}
clRows, clErr := a.pool.Query(c,
`SELECT marking FROM principal_clearances WHERE principal=$1 ORDER BY marking`, name)
if clErr == nil {
defer clRows.Close()
for clRows.Next() {
var m string
if clRows.Scan(&m) == nil {
cls = append(cls, m)
}
}
}
resp["clearances"] = cls
c.JSON(200, resp)
}
@@ -101,13 +137,18 @@ type principalInput struct {
}
type principalUpdateInput struct {
Description *string `json:"description"`
APIKey *string `json:"api_key"`
ClearAPIKey bool `json:"clear_api_key"`
GenerateAPIKey bool `json:"generate_api_key"`
Description *string `json:"description"`
APIKey *string `json:"api_key"`
ClearAPIKey bool `json:"clear_api_key"`
GenerateAPIKey bool `json:"generate_api_key"`
// 修复 issue #7:之前 PUT /principals/:name 完全不接受 clearances 字段(用户传时静默忽略),
// 现允许一次性替换主体的 clearance 集(与 PUT /principals/:name/clearances 同义)。
// nil=不动;[]=清空;非空=全量替换(DELETE + INSERT)。
Clearances *[]string `json:"clearances"`
}
// updatePrincipal 修改主体的说明和 api_key
// updatePrincipal 修改主体的说明、api_key、clearances(合并语义)。
// 修复 issue #7:之前 clearances 字段被静默忽略;现支持在同一次 PUT 中同时改 description / api_key / clearances。
func (a *app) updatePrincipal(c *gin.Context) {
name := c.Param("name")
var in principalUpdateInput
@@ -149,20 +190,51 @@ func (a *app) updatePrincipal(c *gin.Context) {
return
}
}
// 修复 issue #7:clearances 字段支持。nil=不动;非空数组(含 [])= 全量替换。
if in.Clearances != nil {
if _, err := tx.Exec(c, `DELETE FROM principal_clearances WHERE principal=$1`, name); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for _, m := range *in.Clearances {
if m = strings.TrimSpace(m); m != "" {
if _, err := tx.Exec(c,
`INSERT INTO principal_clearances(principal,marking) VALUES($1,$2) ON CONFLICT DO NOTHING`,
name, m); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
}
}
}
if err := tx.Commit(c); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
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
}
c.JSON(200, resp)
}
+479
View File
@@ -0,0 +1,479 @@
package main
// 使用分析(2026-09-11)
//
// 基于既有数据现算,不建新表:
// - access_log → 请求量 / 浏览(read) / 修改(write,delete) / 活跃用户 / 来源分布
// - object_changes → 实例级增删改(含字段 diff 的量)
// - ontology_audit_log → schema 层变更(object_type/link_type/property/...)
// - grants + principals → 共享关系矩阵 + 关联使用量
//
// 时间口径:days 参数(默认 30,1..365);按天分组用 Asia/Shanghai 时区。
import (
"encoding/json"
"net/http"
"sort"
"strconv"
"time"
"github.com/gin-gonic/gin"
)
const tzExpr = "AT TIME ZONE 'Asia/Shanghai'"
func daysParam(c *gin.Context) int {
d, err := strconv.Atoi(c.Query("days"))
if err != nil || d <= 0 || d > 365 {
return 30
}
return d
}
// ---------- 总体 ----------
// analyticsOverview 总体:请求量 / 读写分布 / 活跃用户 / 来源分布 / 按天趋势 / 最近 schema 变更
// GET /api/v1/analytics/overview?days=30
func (a *app) analyticsOverview(c *gin.Context) {
days := daysParam(c)
since := time.Now().AddDate(0, 0, -days)
var total, reads, writes, deletes, denies, mcpCnt, connCnt, kmCnt, users int64
err := a.pool.QueryRow(c, `
SELECT count(*),
count(*) FILTER (WHERE action='read'),
count(*) FILTER (WHERE action='write'),
count(*) FILTER (WHERE action='delete'),
count(*) FILTER (WHERE decision<>'allow'),
count(*) FILTER (WHERE path LIKE '/api/v1/mcp%'),
count(*) FILTER (WHERE path LIKE '/api/v1/connector/%'),
count(*) FILTER (WHERE path LIKE '/api/v1/km/%'),
count(DISTINCT principal) FILTER (WHERE principal <> '')
FROM access_log WHERE created_at >= $1`, since).Scan(
&total, &reads, &writes, &deletes, &denies, &mcpCnt, &connCnt, &kmCnt, &users)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
// 按天趋势
type dayRow struct {
Day string `json:"day"`
Requests int64 `json:"requests"`
Reads int64 `json:"reads"`
Writes int64 `json:"writes"`
Users int64 `json:"users"`
}
dayRows, err := a.pool.Query(c, `
SELECT to_char(created_at `+tzExpr+`, 'YYYY-MM-DD') AS day,
count(*) AS requests,
count(*) FILTER (WHERE action='read') AS reads,
count(*) FILTER (WHERE action IN ('write','delete')) AS writes,
count(DISTINCT principal) FILTER (WHERE principal<>'') AS users
FROM access_log WHERE created_at >= $1
GROUP BY day ORDER BY day`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer dayRows.Close()
byDay := []dayRow{}
for dayRows.Next() {
var r dayRow
if dayRows.Scan(&r.Day, &r.Requests, &r.Reads, &r.Writes, &r.Users) == nil {
byDay = append(byDay, r)
}
}
// 热门目标(读)
type topRow struct {
TargetType string `json:"target_type"`
TargetPK string `json:"target_pk"`
Count int64 `json:"count"`
}
topRows, err := a.pool.Query(c, `
SELECT target_type, target_pk, count(*) AS c
FROM access_log
WHERE created_at >= $1 AND method='GET' AND target_type <> ''
GROUP BY 1,2 ORDER BY c DESC LIMIT 10`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer topRows.Close()
topTargets := []topRow{}
for topRows.Next() {
var r topRow
if topRows.Scan(&r.TargetType, &r.TargetPK, &r.Count) == nil {
topTargets = append(topTargets, r)
}
}
// 最近 schema 变更(本体编辑记录)
type auditRow struct {
CreatedAt string `json:"created_at"`
Actor string `json:"actor"`
Action string `json:"action"`
TargetType string `json:"target_type"`
TargetPK string `json:"target_pk"`
Details map[string]any `json:"details"`
}
auditRows, err := a.pool.Query(c, `
SELECT to_char(created_at `+tzExpr+`, 'YYYY-MM-DD HH24:MI'), actor, action, target_type, target_pk, details
FROM ontology_audit_log ORDER BY id DESC LIMIT 15`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer auditRows.Close()
recentSchema := []auditRow{}
for auditRows.Next() {
var r auditRow
var details []byte
if auditRows.Scan(&r.CreatedAt, &r.Actor, &r.Action, &r.TargetType, &r.TargetPK, &details) == nil {
_ = json.Unmarshal(details, &r.Details)
recentSchema = append(recentSchema, r)
}
}
c.JSON(http.StatusOK, gin.H{
"days": days,
"total": total,
"reads": reads,
"writes": writes,
"deletes": deletes,
"denies": denies,
"mcp_requests": mcpCnt,
"conn_requests": connCnt,
"km_requests": kmCnt,
"active_users": users,
"by_day": byDay,
"top_targets": topTargets,
"recent_schema": recentSchema,
})
}
// ---------- 分项(按对象类型) ----------
// analyticsTypes 每个对象类型:本体定义浏览量 / 实例浏览量 / 实例修改量 / 最近时间
// GET /api/v1/analytics/types?days=30
func (a *app) analyticsTypes(c *gin.Context) {
days := daysParam(c)
since := time.Now().AddDate(0, 0, -days)
type typeStat struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Group string `json:"group"`
Instances int64 `json:"instances"`
OntViews int64 `json:"ontology_views"` // 本体定义/编辑页浏览
InstReads int64 `json:"instance_reads"` // 实例浏览(管理台 + 连接器 + MCP 读)
InstWrites int64 `json:"instance_writes"` // 实例增删改(object_changes)
SchemaChanges int64 `json:"schema_changes"` // 类型/属性定义变更(ontology_audit_log)
LastRead string `json:"last_read"`
LastWrite string `json:"last_write"`
}
stats := map[string]*typeStat{}
// 1) 全部对象类型打底
rows, err := a.pool.Query(c, `
SELECT ot.api_name, ot.display_name, COALESCE(ot.group_name,''),
(SELECT count(*) FROM objects o WHERE o.object_type_id = ot.id)
FROM object_types ot`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var n, dn, g string
var cnt int64
if rows.Scan(&n, &dn, &g, &cnt) == nil {
stats[n] = &typeStat{Name: n, DisplayName: dn, Group: g, Instances: cnt}
}
}
rows.Close()
get := func(n string) *typeStat {
s, ok := stats[n]
if !ok {
s = &typeStat{Name: n}
stats[n] = s
}
return s
}
// 2) 本体定义浏览(target_type='object_type' 的 GET)
rows, err = a.pool.Query(c, `
SELECT target_pk, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'')
FROM access_log
WHERE created_at >= $1 AND method='GET' AND target_type='object_type' AND target_pk <> ''
GROUP BY 1`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var pk, last string
var cnt int64
if rows.Scan(&pk, &cnt, &last) == nil {
s := get(pk)
s.OntViews = cnt
s.LastRead = last
}
}
rows.Close()
// 3) 实例浏览(/objects/:type/... 与 /connector/object[s]/... 的 GET)
rows, err = a.pool.Query(c, `
SELECT target_type, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'')
FROM access_log
WHERE created_at >= $1 AND method='GET' AND target_type <> '' AND target_type <> 'object_type'
AND (path LIKE '/api/v1/objects/%' OR path LIKE '/api/v1/connector/object%' OR path='/api/v1/mcp')
GROUP BY 1`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var tn, last string
var cnt int64
if rows.Scan(&tn, &cnt, &last) == nil {
s := get(tn)
s.InstReads = cnt
if last > s.LastRead {
s.LastRead = last
}
}
}
rows.Close()
// 4) 实例修改(object_changes:create/update/delete/restore)
rows, err = a.pool.Query(c, `
SELECT object_type, count(*), COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'')
FROM object_changes WHERE created_at >= $1 GROUP BY 1`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var tn, last string
var cnt int64
if rows.Scan(&tn, &cnt, &last) == nil {
s := get(tn)
s.InstWrites = cnt
s.LastWrite = last
}
}
rows.Close()
// 5) schema 变更(ontology_audit_log 中 object_type / object_property 按 target_pk 前缀归属类型)
rows, err = a.pool.Query(c, `
SELECT target_type, target_pk, count(*)
FROM ontology_audit_log
WHERE created_at >= $1 AND target_type IN ('object_type','object_property')
GROUP BY 1,2`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var tt, tp string
var cnt int64
if rows.Scan(&tt, &tp, &cnt) == nil {
name := tp
// object_property 的 target_pk 是 "类型.属性名" 或 "id:N"(历史记录),取前缀归属
if tt == "object_property" {
if i := lastIndexByte(name, '.'); i > 0 {
name = name[:i]
} else {
continue // id:N 无法归属到类型,跳过
}
}
get(name).SchemaChanges += cnt
}
}
rows.Close()
out := make([]typeStat, 0, len(stats))
for _, s := range stats {
out = append(out, *s)
}
// 按热度排序:实例读 > 定义浏览 > 实例写 > 实例数
sort.Slice(out, func(i, j int) bool {
si, sj := out[i], out[j]
ki, kj := si.InstReads+si.OntViews*2+si.InstWrites*3, sj.InstReads+sj.OntViews*2+sj.InstWrites*3
if ki != kj {
return ki > kj
}
return si.Instances > sj.Instances
})
c.JSON(http.StatusOK, gin.H{"days": days, "types": out})
}
func lastIndexByte(s string, b byte) int {
for i := len(s) - 1; i >= 0; i-- {
if s[i] == b {
return i
}
}
return -1
}
// ---------- 用户活跃 ----------
// analyticsUsers 每个主体:请求量 / 读 / 写 / 删 / 拒绝 / 触达目标数 / 最近活跃
// GET /api/v1/analytics/users?days=30
func (a *app) analyticsUsers(c *gin.Context) {
days := daysParam(c)
since := time.Now().AddDate(0, 0, -days)
type userStat struct {
Principal string `json:"principal"`
Requests int64 `json:"requests"`
Reads int64 `json:"reads"`
Writes int64 `json:"writes"`
Deletes int64 `json:"deletes"`
Denies int64 `json:"denies"`
Targets int64 `json:"targets"`
LastActive string `json:"last_active"`
Active bool `json:"active"`
}
stats := map[string]*userStat{}
rows, err := a.pool.Query(c, `
SELECT principal, count(*),
count(*) FILTER (WHERE action='read'),
count(*) FILTER (WHERE action='write'),
count(*) FILTER (WHERE action='delete'),
count(*) FILTER (WHERE decision<>'allow'),
count(DISTINCT (target_type || '/' || target_pk)) FILTER (WHERE target_type<>''),
COALESCE(to_char(max(created_at) `+tzExpr+`, 'YYYY-MM-DD HH24:MI'),'')
FROM access_log
WHERE created_at >= $1 AND principal <> ''
GROUP BY principal ORDER BY count(*) DESC`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
for rows.Next() {
var u userStat
if rows.Scan(&u.Principal, &u.Requests, &u.Reads, &u.Writes, &u.Deletes, &u.Denies, &u.Targets, &u.LastActive) == nil {
u.Active = true
stats[u.Principal] = &u
}
}
rows.Close()
// principals 表打底:窗口期内没动静的主体标 active=false
rows, err = a.pool.Query(c, `SELECT name, COALESCE(description,'') FROM principals ORDER BY name`)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
type pInfo struct {
Desc string `json:"description"`
}
principals := map[string]pInfo{}
for rows.Next() {
var n, d string
if rows.Scan(&n, &d) == nil {
principals[n] = pInfo{Desc: d}
if _, ok := stats[n]; !ok {
stats[n] = &userStat{Principal: n}
}
}
}
rows.Close()
out := make([]userStat, 0, len(stats))
for _, s := range stats {
out = append(out, *s)
}
sort.Slice(out, func(i, j int) bool { return out[i].Requests > out[j].Requests })
c.JSON(http.StatusOK, gin.H{"days": days, "users": out, "principals": principals})
}
// ---------- 共享 ----------
// analyticsSharing 授权(grant)矩阵 + 每条授权在窗口期内的实际使用量
// GET /api/v1/analytics/sharing?days=30
func (a *app) analyticsSharing(c *gin.Context) {
days := daysParam(c)
since := time.Now().AddDate(0, 0, -days)
type grantRow struct {
ID int64 `json:"id"`
Principal string `json:"principal"`
Action string `json:"action"`
ScopeType string `json:"scope_type"`
ScopePK string `json:"scope_pk"`
Effect string `json:"effect"`
Used int64 `json:"used"` // 窗口期内该主体落在该 scope 范围内的请求数
}
rows, err := a.pool.Query(c, `
SELECT g.id, g.principal, g.action, g.scope_type, g.scope_pk, g.effect,
(SELECT count(*) FROM access_log al
WHERE al.created_at >= $1 AND al.principal = g.principal
AND (g.scope_type = '*' OR al.target_type = g.scope_type)
AND (g.scope_pk = '*' OR al.target_pk = g.scope_pk)) AS used
FROM grants g ORDER BY g.principal, g.id`, since)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer rows.Close()
grants := []grantRow{}
sharedPrincipals := map[string]bool{}
for rows.Next() {
var r grantRow
if rows.Scan(&r.ID, &r.Principal, &r.Action, &r.ScopeType, &r.ScopePK, &r.Effect, &r.Used) == nil {
grants = append(grants, r)
sharedPrincipals[r.Principal] = true
}
}
// 每个被共享主体的使用总量(读/写)
type pUsage struct {
Principal string `json:"principal"`
Grants int64 `json:"grants"`
Requests int64 `json:"requests"`
Reads int64 `json:"reads"`
Writes int64 `json:"writes"`
}
usageRows, err := a.pool.Query(c, `
SELECT al.principal, count(*),
count(*) FILTER (WHERE al.action='read'),
count(*) FILTER (WHERE al.action IN ('write','delete'))
FROM access_log al
WHERE al.created_at >= $1 AND al.principal = ANY($2::text[])
GROUP BY al.principal ORDER BY count(*) DESC`, since, mapKeys(sharedPrincipals))
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer usageRows.Close()
usage := []pUsage{}
for usageRows.Next() {
var u pUsage
if usageRows.Scan(&u.Principal, &u.Requests, &u.Reads, &u.Writes) == nil {
usage = append(usage, u)
}
}
for i := range usage {
for _, g := range grants {
if g.Principal == usage[i].Principal {
usage[i].Grants++
}
}
}
c.JSON(http.StatusOK, gin.H{"days": days, "grants": grants, "usage": usage})
}
func mapKeys(m map[string]bool) []string {
out := make([]string, 0, len(m))
for k := range m {
out = append(out, k)
}
return out
}
+104 -9
View File
@@ -513,6 +513,40 @@ func mcpToolList() []MCPTool {
"required": []string{"id"},
},
},
// 修复 issue #7:MCP 工具集补齐主体维度的读/写入口(之前 36 个工具缺这一片)。
// list_principals / get_principal 只读;set_principal_clearance 是写入(manager 主体)。
{
Name: "list_principals",
Description: "【只读】列出全部主体及其 clearance(分级可见级别)。用于核对「哪些主体能看哪些分级」。",
InputSchema: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{},
},
},
{
Name: "get_principal",
Description: "【只读】按名称取单个主体(含 description / has_api_key / clearances)。",
InputSchema: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{"type": "string", "description": "主体名"},
},
"required": []string{"name"},
},
},
{
Name: "set_principal_clearance",
Description: "【写入】全量替换主体的 clearance 集(nil 数组=清空)。修复 issue #7 之前 MCP 工具集完全无法配置 clearance 的缺口。可让 connector 也能看 L3 等。",
InputSchema: map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"name": map[string]interface{}{"type": "string", "description": "主体名"},
"clearances": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}, "description": "标记集,如 [L0,L1,L2,L3];传 []=清空;alias: markings"},
"markings": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}, "description": "alias 同 clearances(与 PUT /principals/:name/clearances 命名一致)"},
},
"required": []string{"name"},
},
},
}
}
@@ -631,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)
@@ -643,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)
@@ -665,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)
@@ -723,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)
@@ -734,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 已在函数顶部解析
@@ -813,6 +847,49 @@ func (a *app) mcpDispatchTool(c *gin.Context, name string, args map[string]inter
t, st := a.callHandler(a.addProperty, gin.Params{gin.Param{Key: "name", Value: apiName}}, body, principal)
return t, false, st >= 400
// 修复 issue #7:MCP 工具集补齐「按主体维度读/写 clearance」。
// 之前 36 个工具没有这俩,导致管理员改不了「connector 看不到 L3」之类配置。
case "list_principals":
t, st := a.callHandler(a.listPrincipals, nil, nil, principal)
return t, false, st >= 400
case "get_principal":
name, _ := args["name"].(string)
if name == "" {
return "参数错误:name 必填", false, true
}
t, st := a.callHandler(a.getPrincipal, gin.Params{gin.Param{Key: "name", Value: name}}, nil, principal)
return t, false, st >= 400
case "set_principal_clearance":
if !canWrite {
return denyWrite()
}
name, _ := args["name"].(string)
if name == "" {
return "参数错误:name 必填", false, true
}
// 接受 clearances / markings 两种命名(与 PUT /principals/:name/clearances 一致用 markings)
var marks []string
if raw, ok := args["clearances"].([]interface{}); ok {
for _, m := range raw {
if s, ok := m.(string); ok {
marks = append(marks, s)
}
}
}
if raw, ok := args["markings"].([]interface{}); ok {
marks = marks[:0]
for _, m := range raw {
if s, ok := m.(string); ok {
marks = append(marks, s)
}
}
}
body, _ := json.Marshal(map[string]interface{}{"markings": marks})
t, st := a.callHandler(a.putClearances, gin.Params{gin.Param{Key: "name", Value: name}}, body, principal)
return t, false, st >= 400
case "create_link_type":
if !canWrite {
return denyWrite()
@@ -1039,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()
+55
View File
@@ -110,6 +110,9 @@ func (a *app) createObjectType(c *gin.Context) {
return
}
}
// schema 审计(2026-09-11):create 记录输入快照
c.Set("audit_target_pk", in.APIName)
a.schemaAudit(c, "object_type_create", "object_type", in.APIName, map[string]any{"after": in})
c.JSON(201, gin.H{"id": id, "api_name": in.APIName})
}
@@ -147,6 +150,8 @@ func (a *app) updateObjectType(c *gin.Context) {
return
}
defer invalidateClassCache()
// before 快照(更新前,审计用)
before := a.objectTypeSnapshot(c, name)
// 分级三件套允许显式置空:传空串即关闭(property)/清空(levels,default)
lvJSON := mustJSON(in.ClassificationLevels)
clearProp := in.ClassificationProperty != ""
@@ -188,6 +193,9 @@ func (a *app) updateObjectType(c *gin.Context) {
return
}
}
// schema 审计(2026-09-11):before/after
c.Set("audit_target_pk", name)
a.schemaAudit(c, "object_type_update", "object_type", name, map[string]any{"before": before, "after": in})
c.JSON(200, gin.H{"ok": true})
}
@@ -238,6 +246,8 @@ func (a *app) upsertObjectTypeProperties(c *gin.Context, oid int64, props []prop
func (a *app) deleteObjectType(c *gin.Context) {
name := c.Param("name")
// before 快照(删除前,审计 + 误删可查)
before := a.objectTypeSnapshot(c, name)
tag, err := a.pool.Exec(c, `DELETE FROM object_types WHERE api_name=$1`, name)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
@@ -247,6 +257,7 @@ func (a *app) deleteObjectType(c *gin.Context) {
c.JSON(404, gin.H{"error": "对象类型不存在"})
return
}
a.schemaAudit(c, "object_type_delete", "object_type", name, map[string]any{"before": before})
c.JSON(200, gin.H{"ok": true})
}
@@ -343,6 +354,7 @@ func (a *app) addProperty(c *gin.Context) {
return
}
invalidateClassCache()
a.schemaAudit(c, "property_add", "object_property", name+"."+in.APIName, map[string]any{"after": in})
c.JSON(201, gin.H{"ok": true})
}
@@ -353,6 +365,8 @@ func (a *app) updateProperty(c *gin.Context) {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// before 快照(更新前,审计用)
before := a.propertySnapshot(c, id)
enum := "[]"
if len(in.EnumValues) > 0 {
enum = mustJSON(in.EnumValues)
@@ -380,11 +394,21 @@ func (a *app) updateProperty(c *gin.Context) {
return
}
invalidateClassCache()
// schema 审计(2026-09-11):target_pk 用 类型.属性名(before 里拿得到时),否则退回 id
pk := fmt.Sprintf("id:%d", id)
if before != nil {
if ot, ok := before["object_type"].(string); ok && ot != "" {
pk = ot + "." + before["api_name"].(string)
}
}
a.schemaAudit(c, "property_update", "object_property", pk, map[string]any{"before": before, "after": in})
c.JSON(200, gin.H{"ok": true})
}
func (a *app) deleteProperty(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
// before 快照(删除前,审计 + 误删可查)
before := a.propertySnapshot(c, id)
tag, err := a.pool.Exec(c, `DELETE FROM properties WHERE id=$1`, id)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
@@ -395,6 +419,13 @@ func (a *app) deleteProperty(c *gin.Context) {
return
}
invalidateClassCache()
pk := fmt.Sprintf("id:%d", id)
if before != nil {
if ot, ok := before["object_type"].(string); ok && ot != "" {
pk = ot + "." + before["api_name"].(string)
}
}
a.schemaAudit(c, "property_delete", "object_property", pk, map[string]any{"before": before})
c.JSON(200, gin.H{"ok": true})
}
@@ -448,6 +479,9 @@ func (a *app) createLinkType(c *gin.Context) {
if in.Cardinality == "" {
in.Cardinality = "many_to_many"
}
// upsert 语义:区分 create / update 供审计归类
var existed int
isUpdate := a.pool.QueryRow(c, `SELECT 1 FROM link_types WHERE api_name=$1`, in.APIName).Scan(&existed) == nil
_, err := a.pool.Exec(c,
`INSERT INTO link_types(api_name,display_name,source_type,target_type,cardinality,description,
directional,source_property,target_property)
@@ -466,11 +500,19 @@ func (a *app) createLinkType(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.Set("audit_target_pk", in.APIName)
action := "link_type_create"
if isUpdate {
action = "link_type_update"
}
a.schemaAudit(c, action, "link_type", in.APIName, map[string]any{"after": in})
c.JSON(201, gin.H{"ok": true})
}
func (a *app) deleteLinkType(c *gin.Context) {
name := c.Param("name")
// before 快照(删除前,审计 + 误删可查)
before := a.linkTypeSnapshot(c, name)
tag, err := a.pool.Exec(c, `DELETE FROM link_types WHERE api_name=$1`, name)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
@@ -480,6 +522,7 @@ func (a *app) deleteLinkType(c *gin.Context) {
c.JSON(404, gin.H{"error": "链接类型不存在"})
return
}
a.schemaAudit(c, "link_type_delete", "link_type", name, map[string]any{"before": before})
c.JSON(200, gin.H{"ok": true})
}
@@ -565,11 +608,14 @@ func (a *app) addLinkProperty(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()})
return
}
a.schemaAudit(c, "link_property_add", "link_property", name+"."+in.APIName, map[string]any{"after": in})
c.JSON(201, gin.H{"ok": true})
}
func (a *app) deleteLinkProperty(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
// before 快照(删除前,审计 + 误删可查)
before := a.linkPropertySnapshot(c, id)
tag, err := a.pool.Exec(c, `DELETE FROM link_properties WHERE id=$1`, id)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
@@ -579,6 +625,13 @@ func (a *app) deleteLinkProperty(c *gin.Context) {
c.JSON(404, gin.H{"error": "链接属性不存在"})
return
}
pk := fmt.Sprintf("id:%d", id)
if before != nil {
if lt, ok := before["link_type"].(string); ok && lt != "" {
pk = lt + "." + before["api_name"].(string)
}
}
a.schemaAudit(c, "link_property_delete", "link_property", pk, map[string]any{"before": before})
c.JSON(200, gin.H{"ok": true})
}
@@ -692,6 +745,8 @@ func (a *app) createActionType(c *gin.Context) {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.Set("audit_target_pk", in.APIName)
a.schemaAudit(c, "action_type_upsert", "action_type", in.APIName, map[string]any{"after": in})
c.JSON(201, gin.H{"ok": true})
}
+225
View File
@@ -0,0 +1,225 @@
//go:build integration
package main
import (
"strings"
"testing"
)
// TestIntegration_UpdatePrincipal_Clearances 修复 issue #7:
// 之前 PUT /access/principals/:name 的 clearances 字段被静默忽略,
// 用户找不到任何「编辑主体 clearance」的入口。
// 验证:clearances 数组在 PUT 时一并落库(DELETE + INSERT 全量替换),
// GET /principals/:name 和 listPrincipals 都能回显。
func TestIntegration_UpdatePrincipal_Clearances(t *testing.T) {
env, _ := setupTestEnv(t)
h := map[string]string{"X-Principal": "manager"}
// 用一个非内置主体(避免污染 manager seed)
key := "test_principal_clearance"
t.Cleanup(func() {
env.httpDo(t, "DELETE", "/api/v1/access/principals/"+key, nil, h)
})
if code, _ := env.httpDo(t, "POST", "/api/v1/access/principals",
map[string]any{"name": key, "description": "测试 clearance 配置"}, h); code != 201 {
t.Fatalf("create principal failed: %d", code)
}
// 1) PUT 设置 clearances
code, body := env.httpDo(t, "PUT", "/api/v1/access/principals/"+key,
map[string]any{"clearances": []string{"L0", "L1", "L2"}}, h)
if code != 200 {
t.Fatalf("PUT 应 200, got %d body=%v", code, body)
}
if got := body["clearances"]; got == nil {
t.Errorf("响应应回显 clearances, got %v", body)
}
// 2) GET /principals/:name 应回显
_, get := env.httpDo(t, "GET", "/api/v1/access/principals/"+key, nil, h)
cls := get["clearances"].([]any)
if len(cls) != 3 {
t.Errorf("GET 应回 3 项, got %d (%v)", len(cls), cls)
}
// 3) 再次 PUT 全量替换(含 L3)→ 验证替换语义
code, body = env.httpDo(t, "PUT", "/api/v1/access/principals/"+key,
map[string]any{"clearances": []string{"L0", "L1", "L2", "L3"}}, h)
if code != 200 {
t.Fatalf("PUT 应 200, got %d", code)
}
_, get2 := env.httpDo(t, "GET", "/api/v1/access/principals/"+key, nil, h)
cls2 := get2["clearances"].([]any)
if len(cls2) != 4 {
t.Errorf("替换后应 4 项, got %d (%v)", len(cls2), cls2)
}
// 4) PUT clearances=[] 清空
code, _ = env.httpDo(t, "PUT", "/api/v1/access/principals/"+key,
map[string]any{"clearances": []string{}}, h)
if code != 200 {
t.Fatalf("PUT 空数组应 200, got %d", code)
}
_, get3 := env.httpDo(t, "GET", "/api/v1/access/principals/"+key, nil, h)
cls3 := get3["clearances"].([]any)
if len(cls3) != 0 {
t.Errorf("清空后应 0 项, got %d (%v)", len(cls3), cls3)
}
// 5) PUT 不带 clearances 字段 → 不动(之前的 clearance 仍空)
code, _ = env.httpDo(t, "PUT", "/api/v1/access/principals/"+key,
map[string]any{"description": "只改 desc"}, h)
if code != 200 {
t.Fatalf("PUT 不带 clearances 应 200, got %d", code)
}
_, get4 := env.httpDo(t, "GET", "/api/v1/access/principals/"+key, nil, h)
cls4 := get4["clearances"].([]any)
if len(cls4) != 0 {
t.Errorf("未传 clearances 时应保留之前状态(空),got %v", cls4)
}
if get4["description"] != "只改 desc" {
t.Errorf("description 应被更新, got %v", get4["description"])
}
// 6) listPrincipals 应回显该主体的 clearance
_, list := env.httpDo(t, "GET", "/api/v1/access/principals", nil, h)
var found bool
for _, p := range list["principals"].([]any) {
m := p.(map[string]any)
if m["name"] == key {
if clsL := m["clearances"]; clsL == nil {
t.Errorf("listPrincipals 应回显 clearances, got nil")
}
found = true
}
}
if !found {
t.Errorf("listPrincipals 缺新建的 %s", key)
}
}
// TestIntegration_SetPrincipalClearance_MCP 修复 issue #7 方案 A:
// MCP set_principal_clearance 工具能写入;list_principals / get_principal 能读取。
func TestIntegration_SetPrincipalClearance_MCP(t *testing.T) {
env, _ := setupTestEnv(t)
h := map[string]string{"X-Principal": "manager"}
key := "test_mcp_clearance"
t.Cleanup(func() {
env.httpDo(t, "DELETE", "/api/v1/access/principals/"+key, nil, h)
})
if code, _ := env.httpDo(t, "POST", "/api/v1/access/principals",
map[string]any{"name": key, "description": "MCP clearance 测试"}, h); code != 201 {
t.Fatalf("create principal failed: %d", code)
}
// 1) MCP set_principal_clearance
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": "set_principal_clearance",
"arguments": map[string]any{
"name": key,
"clearances": []string{"L0", "L1", "L2", "L3"},
},
},
}, h)
if code != 200 {
t.Fatalf("MCP 应 200, got %d body=%v", code, body)
}
// 2) MCP get_principal 应回显
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": "get_principal",
"arguments": map[string]any{"name": key},
},
}, h)
if code != 200 {
t.Fatalf("get_principal 应 200, got %d", code)
}
result := body["result"].(map[string]any)
content := result["content"].([]any)
text := content[0].(map[string]any)["text"].(string)
if !strings.Contains(text, `"clearances"`) {
t.Errorf("get_principal 响应应含 clearances, got %s", text)
}
if !strings.Contains(text, `"L3"`) {
t.Errorf("get_principal 响应应含 L3, got %s", text)
}
// 3) MCP list_principals 应包含该主体
code, body = env.httpDo(t, "POST", "/api/v1/mcp",
map[string]any{
"jsonrpc": "2.0", "id": "3", "method": "tools/call",
"params": map[string]any{
"name": "list_principals",
"arguments": map[string]any{},
},
}, h)
if code != 200 {
t.Fatalf("list_principals 应 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, key) {
t.Errorf("list_principals 应包含新建 %s, got %s", key, text2)
}
// 4) MCP 缺 name 应报错
code, body = env.httpDo(t, "POST", "/api/v1/mcp",
map[string]any{
"jsonrpc": "2.0", "id": "4", "method": "tools/call",
"params": map[string]any{
"name": "set_principal_clearance",
"arguments": map[string]any{
"clearances": []string{"L0"},
},
},
}, h)
if code != 200 {
t.Fatalf("MCP 错误应 200, got %d", code)
}
r4 := body["result"].(map[string]any)
c4 := r4["content"].([]any)
t4 := c4[0].(map[string]any)["text"].(string)
if !strings.Contains(t4, "name") {
t.Errorf("MCP 缺 name 应报错, got %s", t4)
}
}
// TestIntegration_PutClearances_Rest 顺带验证既有的
// `PUT /principals/:name/clearances` 端点仍然有效(用户不知道有这个端点)。
// 这是 issue #7 的关键发现:端点一直都在,只是用户认知里没有。
func TestIntegration_PutClearances_Rest(t *testing.T) {
env, _ := setupTestEnv(t)
h := map[string]string{"X-Principal": "manager"}
key := "test_legacy_clearance_path"
t.Cleanup(func() {
env.httpDo(t, "DELETE", "/api/v1/access/principals/"+key, nil, h)
})
if code, _ := env.httpDo(t, "POST", "/api/v1/access/principals",
map[string]any{"name": key, "description": "测试旧端点"}, h); code != 201 {
t.Fatalf("create principal failed: %d", code)
}
// 用 PUT /principals/:name/clearances(既有的专用端点)
code, _ := env.httpDo(t, "PUT", "/api/v1/access/principals/"+key+"/clearances",
map[string]any{"markings": []string{"L0", "L2"}}, h)
if code != 200 {
t.Fatalf("PUT clearances 应 200, got %d", code)
}
// GET /principals/:name/clearances(既有只读端点)
_, get := env.httpDo(t, "GET", "/api/v1/access/principals/"+key+"/clearances", nil, h)
cls := get["clearances"].([]any)
if len(cls) != 2 {
t.Errorf("既有专用端点 GET 应 2 项, got %d", len(cls))
}
}
+328
View File
@@ -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)
}
}
+23 -4
View File
@@ -109,6 +109,8 @@ func run() error {
// Dynamic Security 审计日志中间件(自动记录每次请求的访问决策)
r.Use(a.accessLogMiddleware())
startAccessLogFlusher(pool)
// access_log 保留策略(2026-09-11):默认 90 天,ACCESS_LOG_RETENTION_DAYS 覆盖
startAccessLogRetention(pool, cfg.AccessLogRetentionDays)
// 物化层调度器(P2-1):后台 ticker 定期刷新 interval 物化
startMaterializationScheduler(context.Background(), a)
@@ -337,6 +339,15 @@ func registerRoutes(r *gin.Engine, a *app) {
acc.GET("/log/stats", a.accessLogStats)
}
// 使用分析(2026-09-11):本体总体/分项使用、用户活跃、共享情况(仅 manager)
ana := mgmt.Group("/analytics")
{
ana.GET("/overview", a.analyticsOverview)
ana.GET("/types", a.analyticsTypes)
ana.GET("/users", a.analyticsUsers)
ana.GET("/sharing", a.analyticsSharing)
}
// 数据备份(v1.3.0):仅 manager 主体可访问,含 create/inspect/list/download/diff/delete
// 不提供 restore 到线上——MCP 客户端拿到 dump 后在另一实例验证恢复路径
bkp := mgmt.Group("/backups")
@@ -388,8 +399,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 +415,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 +425,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
}
+123
View File
@@ -0,0 +1,123 @@
package main
// 本体 schema 层审计(2026-09-11)
//
// 实例级变更已有 object_changes(字段级 diff + 快照),但 schema 层
// (对象类型 / 属性 / 链接类型 / 链接属性 / 动作类型)此前只有 access_log 的
// method+path 间接痕迹,没有 before/after 内容 —— 本文件补齐:
// - 全部 schema 增删改写入 ontology_audit_log(target_pk 落库)
// - update/delete 记录变更前快照(before),create 记录输入(after)
// 失败只打日志,不阻断主流程(与 recordChange 同策略)。
import (
"context"
"encoding/json"
"log"
"github.com/gin-gonic/gin"
)
// schemaAudit 写一条 schema 层审计记录。
func (a *app) schemaAudit(c *gin.Context, action, targetType, targetPK string, details map[string]any) {
actor := a.principalFromRequest(c)
if actor == "" {
actor = "anonymous"
}
if details == nil {
details = map[string]any{}
}
b, _ := json.Marshal(details)
if _, err := a.pool.Exec(context.Background(),
`INSERT INTO ontology_audit_log(actor, action, target_type, target_pk, details, affected_rows)
VALUES($1,$2,$3,$4,$5::jsonb,$6)`,
actor, action, targetType, targetPK, string(b), 1); err != nil {
log.Printf("[schema_audit] 写入失败 %s %s/%s: %v", action, targetType, targetPK, err)
}
}
// toMap 结构体 → map(审计 details 用,nil 安全)。
func toMap(v any) map[string]any {
if v == nil {
return nil
}
b, err := json.Marshal(v)
if err != nil {
return nil
}
var m map[string]any
_ = json.Unmarshal(b, &m)
return m
}
// objectTypeSnapshot 读取对象类型(含属性)当前状态,作 update/delete 的 before 快照。
func (a *app) objectTypeSnapshot(c *gin.Context, name string) map[string]any {
o, err := loadObjectType(c, a.pool, name)
if err != nil {
return nil
}
return toMap(o)
}
// propertySnapshot 按 properties.id 读取属性当前状态(含所属类型 api_name)。
func (a *app) propertySnapshot(c *gin.Context, id int64) map[string]any {
var p Property
var enum, defVal []byte
var typeName string
err := a.pool.QueryRow(c, `
SELECT p.id, p.object_type_id, p.api_name, p.display_name, p.data_type, p.required,
p.enum_values, p.description, p.format, p.default_value, p.indexable,
p.vector_index, p.backing_column, COALESCE(p.sensitivity,''), ot.api_name
FROM properties p JOIN object_types ot ON ot.id = p.object_type_id
WHERE p.id=$1`, id).
Scan(&p.ID, &p.ObjectID, &p.APIName, &p.DisplayName, &p.DataType, &p.Required,
&enum, &p.Description, &p.Format, &defVal, &p.Indexable,
&p.VectorIndex, &p.BackingColumn, &p.Sensitivity, &typeName)
if err != nil {
return nil
}
p.EnumValues = parseStringArray(enum)
p.DefaultValue = jsonRaw(defVal)
m := toMap(p)
if m != nil {
m["object_type"] = typeName
}
return m
}
// linkTypeSnapshot 读取链接类型当前状态。
func (a *app) linkTypeSnapshot(c *gin.Context, name string) map[string]any {
var l LinkType
err := a.pool.QueryRow(c, `
SELECT id, api_name, display_name, source_type, target_type, cardinality,
COALESCE(description,''), directional, COALESCE(source_property,''), COALESCE(target_property,'')
FROM link_types WHERE api_name=$1`, name).
Scan(&l.ID, &l.APIName, &l.DisplayName, &l.SourceType, &l.TargetType, &l.Cardinality,
&l.Description, &l.Directional, &l.SourceProperty, &l.TargetProperty)
if err != nil {
return nil
}
return toMap(l)
}
// linkPropertySnapshot 按 link_properties.id 读取链接属性当前状态(含所属链接类型 api_name)。
func (a *app) linkPropertySnapshot(c *gin.Context, id int64) map[string]any {
var p LinkProperty
var enum []byte
var ltName string
err := a.pool.QueryRow(c, `
SELECT p.id, p.link_type_id, p.api_name, p.display_name, p.data_type, p.required,
p.enum_values, COALESCE(p.description,''), lt.api_name
FROM link_properties p JOIN link_types lt ON lt.id = p.link_type_id
WHERE p.id=$1`, id).
Scan(&p.ID, &p.LinkTypeID, &p.APIName, &p.DisplayName, &p.DataType, &p.Required,
&enum, &p.Description, &ltName)
if err != nil {
return nil
}
p.EnumValues = parseStringArray(enum)
m := toMap(p)
if m != nil {
m["link_type"] = ltName
}
return m
}
+2
View File
@@ -31,6 +31,7 @@
<el-tabs v-model="active" class="main-tabs">
<el-tab-pane label="概览" name="overview"><OverviewView @goto="goto" /></el-tab-pane>
<el-tab-pane label="使用分析" name="analytics"><AnalyticsView /></el-tab-pane>
<el-tab-pane label="本体编辑" name="ontology"><OntologyView /></el-tab-pane>
<el-tab-pane label="实例管理" name="instances"><InstanceView /></el-tab-pane>
<el-tab-pane label="知识管理" name="ai"><AIView @refresh="refreshOverview" /></el-tab-pane>
@@ -78,6 +79,7 @@
import { ref, reactive, onMounted, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import OverviewView from './views/OverviewView.vue'
import AnalyticsView from './views/AnalyticsView.vue'
import OntologyView from './views/OntologyView.vue'
import InstanceView from './views/InstanceView.vue'
import AIView from './views/AIView.vue'
+6
View File
@@ -171,6 +171,12 @@ export const api = {
},
accessLogStats: () => request('GET', '/access/log/stats'),
// 使用分析(2026-09-11):本体总体/分项使用、用户活跃、共享情况
analyticsOverview: (days = 30) => request('GET', `/analytics/overview?days=${days}`),
analyticsTypes: (days = 30) => request('GET', `/analytics/types?days=${days}`),
analyticsUsers: (days = 30) => request('GET', `/analytics/users?days=${days}`),
analyticsSharing: (days = 30) => request('GET', `/analytics/sharing?days=${days}`),
// ---- 账号密码登录与会话 ----
// dev 模式下也支持登录(与 X-API-Key 并存)。登录后浏览器自动带 cookie;
// 后端 principalFromRequest 优先识别 cookie → 命名主体。
+212
View File
@@ -0,0 +1,212 @@
<template>
<div class="page">
<div class="page-title">📈 使用分析
<span class="muted">基于访问日志 / 实例变更 / schema 审计聚合</span>
<span style="flex:1"></span>
<el-radio-group v-model="days" size="small" @change="load">
<el-radio-button :value="7">近 7 天</el-radio-button>
<el-radio-button :value="30">近 30 天</el-radio-button>
<el-radio-button :value="90">近 90 天</el-radio-button>
</el-radio-group>
<el-button size="small" @click="load" :loading="loading">刷新</el-button>
</div>
<!-- 总体 -->
<div class="row stats">
<div class="stat"><div class="num">{{ ov.total || 0 }}</div><div class="lbl">总请求</div></div>
<div class="stat"><div class="num">{{ ov.reads || 0 }}</div><div class="lbl">浏览(read)</div></div>
<div class="stat"><div class="num">{{ (ov.writes || 0) + (ov.deletes || 0) }}</div><div class="lbl">修改(write+delete)</div></div>
<div class="stat"><div class="num">{{ ov.active_users || 0 }}</div><div class="lbl">活跃主体</div></div>
<div class="stat"><div class="num">{{ ov.mcp_requests || 0 }}</div><div class="lbl">MCP 调用</div></div>
<div class="stat"><div class="num">{{ ov.conn_requests || 0 }}</div><div class="lbl">连接器</div></div>
<div class="stat"><div class="num" :class="{ danger: (ov.denies || 0) > 0 }">{{ ov.denies || 0 }}</div><div class="lbl">拒绝/脱敏</div></div>
</div>
<div class="card">
<div class="page-title">📅 按天趋势</div>
<div v-if="!ov.by_day || !ov.by_day.length" class="muted">窗口期内暂无请求记录</div>
<div v-else class="trend">
<div v-for="d in ov.by_day" :key="d.day" class="trend-col" :title="`${d.day}:请求 ${d.requests}(读 ${d.reads} / 写 ${d.writes}),活跃 ${d.users} 人`">
<div class="trend-bars">
<div class="bar read" :style="{ height: barH(d.reads) }"></div>
<div class="bar write" :style="{ height: barH(d.writes) }"></div>
</div>
<div class="trend-day">{{ d.day.slice(5) }}</div>
</div>
</div>
<div class="legend muted">
<span class="dot read"></span>浏览 <span class="dot write"></span>修改(悬停看明细)
</div>
</div>
<!-- 分项 -->
<div class="card">
<div class="page-title">🧩 本体分项使用情况(按热度排序)</div>
<el-table :data="types" size="small" empty-text="暂无对象类型" max-height="420">
<el-table-column prop="display_name" label="类型" min-width="140">
<template #default="{ row }">{{ row.display_name }} <span class="muted">{{ row.name }}</span></template>
</el-table-column>
<el-table-column prop="instances" label="实例数" width="90" sortable />
<el-table-column prop="instance_reads" label="实例浏览" width="100" sortable />
<el-table-column prop="ontology_views" label="定义浏览" width="100" sortable />
<el-table-column prop="instance_writes" label="实例修改" width="100" sortable />
<el-table-column prop="schema_changes" label="定义变更" width="100" sortable />
<el-table-column prop="last_read" label="最近浏览" width="150" />
<el-table-column prop="last_write" label="最近修改" width="150" />
</el-table>
</div>
<div class="row two-col">
<!-- 用户活跃 -->
<div class="card half">
<div class="page-title">👥 用户活跃</div>
<el-table :data="users" size="small" empty-text="无主体" max-height="420">
<el-table-column prop="principal" label="主体" min-width="110" />
<el-table-column prop="requests" label="请求" width="80" sortable />
<el-table-column prop="reads" label="读" width="70" sortable />
<el-table-column label="写/删" width="80" sortable prop="writes">
<template #default="{ row }">{{ row.writes + row.deletes }}</template>
</el-table-column>
<el-table-column prop="denies" label="拒绝" width="70">
<template #default="{ row }">
<span :class="{ danger: row.denies > 0 }">{{ row.denies }}</span>
</template>
</el-table-column>
<el-table-column prop="last_active" label="最近活跃" width="150">
<template #default="{ row }">
<el-tag v-if="!row.active" size="small" type="info" effect="plain">窗口期未活跃</el-tag>
<span v-else>{{ row.last_active }}</span>
</template>
</el-table-column>
</el-table>
</div>
<!-- 共享 -->
<div class="card half">
<div class="page-title">🔗 共享与授权使用</div>
<div class="muted" style="margin-bottom:8px">共 {{ sharing.grants?.length || 0 }} 条授权,覆盖 {{ sharing.usage?.length || 0 }} 个主体</div>
<el-table :data="sharing.usage" size="small" empty-text="无共享授权" max-height="180">
<el-table-column prop="principal" label="被共享主体" min-width="110" />
<el-table-column prop="grants" label="授权数" width="80" />
<el-table-column prop="requests" label="使用请求" width="90" sortable />
<el-table-column prop="reads" label="读" width="70" />
<el-table-column prop="writes" label="写/删" width="80" />
</el-table>
<div class="page-title" style="margin-top:14px;font-size:14px">授权明细</div>
<el-table :data="sharing.grants" size="small" max-height="180">
<el-table-column prop="principal" label="主体" min-width="100" />
<el-table-column prop="action" label="动作" width="90" />
<el-table-column label="范围" min-width="130">
<template #default="{ row }">
{{ row.scope_type === '*' ? '全部类型' : row.scope_type }}<template v-if="row.scope_pk !== '*'"> / {{ row.scope_pk }}</template>
</template>
</el-table-column>
<el-table-column prop="effect" label="效力" width="80">
<template #default="{ row }">
<el-tag size="small" :type="row.effect === 'deny' ? 'danger' : 'success'" effect="plain">{{ row.effect }}</el-tag>
</template>
</el-table-column>
<el-table-column prop="used" label="已用" width="70" sortable />
</el-table>
</div>
</div>
<!-- 最近 schema 变更 -->
<div class="card">
<div class="page-title">📝 最近本体定义变更(schema 审计)</div>
<el-table :data="ov.recent_schema" size="small" empty-text="暂无 schema 变更记录" max-height="300">
<el-table-column prop="created_at" label="时间" width="150" />
<el-table-column prop="actor" label="操作者" width="110" />
<el-table-column prop="action" label="动作" width="190">
<template #default="{ row }">
<el-tag size="small" effect="plain" :type="actionTag(row.action)">{{ actionText(row.action) }}</el-tag>
</template>
</el-table-column>
<el-table-column label="目标" min-width="220">
<template #default="{ row }">{{ row.target_type }}<template v-if="row.target_pk"> / {{ row.target_pk }}</template></template>
</el-table-column>
</el-table>
</div>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { api } from '../api.js'
const days = ref(30)
const loading = ref(false)
const ov = ref({})
const types = ref([])
const users = ref([])
const sharing = ref({})
async function load() {
loading.value = true
try {
const [o, t, u, s] = await Promise.all([
api.analyticsOverview(days.value),
api.analyticsTypes(days.value),
api.analyticsUsers(days.value),
api.analyticsSharing(days.value)
])
ov.value = o
types.value = t.types || []
users.value = u.users || []
sharing.value = s
} catch (e) {
console.error(e)
} finally {
loading.value = false
}
}
// 趋势柱状图归一化(读为主轴)
function barH(v) {
const max = Math.max(1, ...(ov.value.by_day || []).map(d => Math.max(d.reads, d.writes)))
return Math.max(2, Math.round((v / max) * 72)) + 'px'
}
function actionText(a) {
return {
object_type_create: '新建对象类型', object_type_update: '更新对象类型', object_type_delete: '删除对象类型',
object_type_classification: '分级配置', property_add: '新增属性', property_update: '更新属性', property_delete: '删除属性',
link_type_create: '新建链接类型', link_type_update: '更新链接类型', link_type_delete: '删除链接类型',
link_property_add: '新增链接属性', link_property_delete: '删除链接属性',
action_type_upsert: '保存动作类型',
analyze: '类型梳理', merge_preview: '合并预览', merge_execute: '执行合并', delete_zero_inst: '清理零实例'
}[a] || a
}
function actionTag(a) {
if (a.endsWith('_delete')) return 'danger'
if (a.endsWith('_create')) return 'success'
if (a.endsWith('_update') || a.endsWith('_upsert')) return 'warning'
return 'info'
}
onMounted(load)
</script>
<style scoped>
.stats { gap: 14px; margin-bottom: 16px; }
.stat {
flex: 1; min-width: 110px; background: #fff; border-radius: 12px; padding: 14px 16px;
box-shadow: 0 1px 6px rgba(20,40,80,.06); text-align: center;
}
.stat .num { font-size: 26px; font-weight: 800; color: var(--brand); }
.stat .lbl { font-size: 12.5px; color: #8a93a6; margin-top: 4px; }
.num.danger, .danger { color: #e0413e; }
.trend { display: flex; align-items: flex-end; gap: 4px; min-height: 110px; overflow-x: auto; }
.trend-col { display: flex; flex-direction: column; align-items: center; min-width: 26px; }
.trend-bars { display: flex; align-items: flex-end; gap: 2px; height: 80px; }
.bar { width: 9px; border-radius: 3px 3px 0 0; }
.bar.read { background: #2f6bff; }
.bar.write { background: #f0a020; }
.trend-day { font-size: 10px; color: #8a93a6; margin-top: 4px; white-space: nowrap; }
.legend { margin-top: 8px; display: flex; align-items: center; gap: 6px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-left: 8px; }
.dot.read { background: #2f6bff; }
.dot.write { background: #f0a020; }
.two-col { align-items: stretch; }
.half { flex: 1; min-width: 380px; }
</style>