- 新增 docs/ONTOLOGY_IMPORT.md:本体 schema 导入的硬约束(fail-fast 闸门、整事务、 幂等键、IRI 双空间映射、不做范围对账删除),并记录实现落点与逐条偏差 - 新增 docs/auto-ontology-benchmark-20261106.md:NVIDIA auto-ontology 对标分析与 Top-8 路线图,本版已落地项逐条标注 - docs/README.md 同步 API 清单与 MCP 工具表(54 个:只读 27 / 写入 27) - 项目 CLAUDE.md 补「本体 schema 导入」小节:闸门顺序、事务与串行理由、幂等表、 RDF Schema 命名空间年份坑 - README.md 镜像标签更新到 1.8.0,本地构建镜像补上 Linux 静态二进制交叉编译步骤, 健康检查端点改回 /api/v1/version - scripts/ 收拢本体 v2 迁移与分级校正脚本(一次性运维工具,不参与构建) Co-Authored-By: Claude Code <[email protected]>
148 lines
5.9 KiB
Python
148 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
按规划修正本体分级与主体 clearance(2026-10-13 续):
|
||
规划:L0 公众 / L1 客户 / L2 员工 / L3 经理以上 / L4 公司管理层
|
||
|
||
问题:上一轮误把 75 名员工 clearance 提到 L0-L3,且把本该员工可见的数据定在 L3,
|
||
导致"员工该看到的很多数据反而靠提权才看得到"。正确做法:
|
||
① 员工 clearance 回落 L0-L2;
|
||
② 把本该员工可见的数据类型默认级从 L3 降到 L2;
|
||
③ UsageControlPolicy 等经理级数据保持 L3(员工不可见,正确);
|
||
④ 仅「国家秘密」等真正涉密实例保留 L4 覆盖层。
|
||
|
||
幂等、可复跑;默认 dry-run 仅预览,--apply 才落库。
|
||
"""
|
||
import sys, json, argparse
|
||
sys.path.insert(0, 'scripts')
|
||
import raise_employee_clearance as r
|
||
|
||
BUILTIN = {'public', 'analyst', 'connector', 'TradingAssistant', 'manager', 'ingest'}
|
||
EMP_LEVELS = ['L0', 'L1', 'L2']
|
||
LEVELS = ['L0', 'L1', 'L2', 'L3', 'L4']
|
||
|
||
# 类型默认级重分类:
|
||
# L3 -> L2(员工可见的内部/运营/政策类,有实例的才是"员工访问不了"的根因)
|
||
TO_L2 = ['PolicyDocument', 'SecurityRequirement', 'SpaceManager',
|
||
'TrustedCryptographyCard', 'TrustCredential', 'TDSInterconnection',
|
||
'UsageAction', 'UsageConstraint']
|
||
# 保持 L3(经理以上,含 UsageControlPolicy 36 实例 + 两个 0 实例契约/计费类)
|
||
KEEP_L3 = ['DigitalContract', 'MeteringBill', 'UsageControlPolicy']
|
||
|
||
# 实例覆盖层修正:UsageControlPolicy 是否从 L4 回落 L3 交由用户确认(默认保留 L4,
|
||
# 它属「公司管理层」治理范畴,且不影响员工可见性;员工本就不该看访问控制策略)。
|
||
# PolicyDocument 的 cs_20260918_124(国家秘密)保持 L4 不动。
|
||
REVERT_UCP_L4 = False
|
||
|
||
|
||
def ensure_classification_property(name):
|
||
# 补建 classification 属性(幂等 ON CONFLICT),否则 setObjectTypeClassification 校验会拒
|
||
body = {
|
||
'api_name': 'classification',
|
||
'display_name': '数据分级',
|
||
'data_type': 'string',
|
||
'enum_values': LEVELS,
|
||
'description': '数据分级 L0公众/L1客户/L2员工/L3经理以上/L4公司管理层',
|
||
'indexable': False,
|
||
}
|
||
return r.api('POST', f'/ontology/object-types/{name}/properties', body)
|
||
|
||
|
||
def set_type_default(name, default):
|
||
# 先确保 classification 属性存在(校验依赖它)
|
||
ensure_classification_property(name)
|
||
body = {'classification_property': 'classification',
|
||
'classification_levels': LEVELS,
|
||
'classification_default': default}
|
||
return r.api('PUT', f'/ontology/object-types/{name}/classification', body)
|
||
|
||
|
||
def get_clearance(name):
|
||
code, data = r.api('GET', f'/access/principals/{name}/clearances')
|
||
if isinstance(data, dict):
|
||
return data.get('markings') or data.get('clearances')
|
||
return data
|
||
|
||
|
||
def set_clearance(name, levels):
|
||
# 关键:端点只认 markings 字段(handlers_access.go:318),传 clearances 会被清空
|
||
return r.api('PUT', f'/access/principals/{name}/clearances', {'markings': levels})
|
||
|
||
|
||
def get_markings(typ, pk):
|
||
code, data = r.api('GET', f'/access/objects/{typ}/{pk}/markings')
|
||
if isinstance(data, dict):
|
||
return data.get('markings') or []
|
||
return []
|
||
|
||
|
||
def set_markings(typ, pk, levels):
|
||
return r.api('PUT', f'/access/objects/{typ}/{pk}/markings', {'markings': levels})
|
||
|
||
|
||
def list_instances(typ, limit=2000):
|
||
code, data = r.api('GET', f'/objects?type={typ}&limit={limit}')
|
||
if isinstance(data, dict):
|
||
return data.get('items', data.get('objects', []))
|
||
return data or []
|
||
|
||
|
||
def main():
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument('--apply', action='store_true', help='落库(默认 dry-run)')
|
||
args = ap.parse_args()
|
||
A = args.apply
|
||
print(f'== 模式: {"APPLY" if A else "DRY-RUN"} ==')
|
||
|
||
# ---- Phase 1: 员工 clearance 回落 L0-L2 ----
|
||
print('\n[Phase 1] 员工 clearance -> L0-L2')
|
||
code, pdata = r.api('GET', '/access/principals')
|
||
ps = pdata if isinstance(pdata, list) else pdata.get('principals', pdata)
|
||
emp = [p['name'] for p in ps if p['name'] not in BUILTIN]
|
||
print(f' 员工主体数: {len(emp)}')
|
||
for n in emp:
|
||
cur = get_clearance(n)
|
||
if cur == EMP_LEVELS:
|
||
continue
|
||
print(f' {n}: {cur} -> {EMP_LEVELS}')
|
||
if A:
|
||
set_clearance(n, EMP_LEVELS)
|
||
|
||
# ---- Phase 2: 类型默认级重分类 ----
|
||
print('\n[Phase 2] 类型默认级 L3->L2(员工可见类)')
|
||
for n in TO_L2:
|
||
print(f' {n}: default L3 -> L2')
|
||
if A:
|
||
set_type_default(n, 'L2')
|
||
for n in KEEP_L3:
|
||
print(f' {n}: 保持 L3(经理以上)')
|
||
|
||
# ---- Phase 3: recompute(仅写 classification 列,不碰 object_markings)----
|
||
if A:
|
||
print('\n[Phase 3] recompute classification(apply=true)')
|
||
code, data = r.api('POST', '/ontology/classification/recompute',
|
||
{'apply': True, 'dry_run': False, 'limit': 20000})
|
||
print(' ', json.dumps(data, ensure_ascii=False)[:400])
|
||
else:
|
||
print('\n[Phase 3] recompute 将在 --apply 时执行(materialize 类型默认级)')
|
||
|
||
# ---- Phase 4: 实例覆盖层修正 ----
|
||
print('\n[Phase 4] 实例覆盖层')
|
||
if REVERT_UCP_L4:
|
||
ucp = list_instances('UsageControlPolicy', 200)
|
||
print(f' UsageControlPolicy 实例 {len(ucp)} 条:回落被误标的 L4 -> (移除覆盖, 落类型默认 L3)')
|
||
for o in ucp:
|
||
pk = o.get('pk')
|
||
cur = get_markings('UsageControlPolicy', pk)
|
||
if cur == ['L4']:
|
||
print(f' {pk}: L4 -> [] (回落 L3)')
|
||
if A:
|
||
set_markings('UsageControlPolicy', pk, [])
|
||
print(' PolicyDocument.cs_20260918_124(国家秘密)保持 L4 覆盖层(不动)')
|
||
|
||
print('\n== 完成 ==')
|
||
|
||
|
||
if __name__ == '__main__':
|
||
main()
|