Files
ssw-ontology/scripts/apply_instance_classification.py
T
adminandClaude Code aef8b83e24 docs(release): v1.8.0 发布说明 + 本体导入设计约束落地记录 + 对标分析
- 新增 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]>
2026-11-06 13:53:25 +08:00

140 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
P1-④ 实例级细粒度分级:敏感实例加 object_markings 覆盖层(自愈/幂等)。
两层分级模型(server/classification.go + handlers_objects.go):
1) objects.classification 列 = 类型默认级(recompute 物化),由 classificationCond 强制。
2) object_markings 表 = 实例级「额外覆盖层」,由 visibilityCond(CanRead) 强制。
两者都要过:实例 marking 要求主体 clearance 包含其全部 marking。
-> 给实例打 ["L4"] 即在列值之上再叠加一层「仅 L4+ 可见」限制。
规则:
- Standard (列默认 L0/公开) 正向命中敏感词 -> ["L3"] (员工可见, 公开/connector 不可见)
- PolicyDocument (列默认 L3) 正向命中敏感词 -> ["L4"] (仅 manager/ingest 可见)
- UsageControlPolicy(列默认 L3) 全部 -> ["L4"] (访问控制策略, 默认受限)
敏感词排除「非」前缀否定语境(如「非涉密」不计为正向命中)。
自愈:
- 正向命中 -> 确保 object_markings = [目标级](已是则跳过)。
- 未命中 -> 若当前有 object_markings(上一轮误标),PUT [] 移除覆盖层,回落到列默认级;
当前本就无标记(由列默认级治理)则不动。
"""
import sys, json, argparse
sys.path.insert(0, "scripts")
import raise_employee_clearance as r
SENSITIVE = ["涉密", "机密", "秘密", "保密", "内部", "受限", "敏感", "军事",
"国防", "绝密", "商密", "密级", "secret", "internal", "confidential",
"restricted", "classified", "sensitive"]
CONFIG = {
"Standard": {"scan": True, "level": "L3", "reason": "内部/涉密标准(实际扫描0命中, 保持L0公开)"},
"PolicyDocument": {"scan": True, "level": "L4", "reason": "仅国家秘密等真涉密文件升L4; 含'非涉密'否定语境的不升"},
# UsageControlPolicy 不在此处处理:用户确认降到 L3(经理以上也可治理),
# 由 scripts/revert_ucp_level.py 维护(实例级移除L4覆盖层, 回落类型默认L3)。
}
def list_instances(typ):
out, offset = [], 0
while True:
code, data = r.api("GET", f"/objects?type={typ}&limit=1000&offset={offset}")
if code != 200 or not data:
break
arr = data.get("items", data) if isinstance(data, dict) else data
if not arr:
break
out.extend(arr)
if len(arr) < 1000:
break
offset += 1000
return out
def text_of(o):
parts = [str(o.get(k, "") or "") for k in
("name", "title", "title_cn", "title_en", "standard_code",
"doc_no", "publisher", "issuer", "scope", "classification")]
pj = o.get("properties_json") or o.get("properties")
if isinstance(pj, dict):
parts += [str(v) for v in pj.values()]
elif isinstance(pj, str):
parts.append(pj)
return " ".join(parts).lower()
def positive_hit(txt):
hits = []
for kw in SENSITIVE:
k = kw.lower()
start = 0
found = False
while True:
i = txt.find(k, start)
if i < 0:
break
if i > 0 and txt[i - 1] == "非": # 否定语境,不算正向命中
start = i + len(k)
continue
found = True
start = i + len(k)
if found:
hits.append(kw)
return hits
def get_current_markings(typ, pk):
code, data = r.api("GET", f"/access/objects/{typ}/{pk}/markings")
if code == 200 and isinstance(data, dict):
return data.get("markings", []) or []
return []
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--dry-run", action="store_true", help="只盘点不写入")
args = ap.parse_args()
plan, applied, reverted = [], 0, 0
for typ, cfg in CONFIG.items():
insts = list_instances(typ)
for o in insts:
pk = o.get("pk")
name = o.get("name") or o.get("title_cn") or o.get("title") or pk
txt = text_of(o)
cur = get_current_markings(typ, pk)
hits = positive_hit(txt) if cfg["scan"] else ["(全部)"]
if hits:
desired = [cfg["level"]]
note = "升" + str(hits)
else:
# 未命中:仅当本就被误升到目标级(覆盖层)时才回落移除;
# 其余(如 recompute 物化的列默认级标记)保持原样不动。
desired = [] if cur == [cfg["level"]] else cur
note = "回落(移除误标)" if desired == [] else "保持"
if cur == desired:
continue
plan.append((typ, pk, name, desired, cur, note))
if args.dry_run:
continue
code, _ = r.api("PUT", f"/access/objects/{typ}/{pk}/markings",
{"markings": desired})
if code == 200:
if desired:
applied += 1
else:
reverted += 1
else:
print(f" ✗ 写入失败 {typ}/{pk}: HTTP {code}", file=sys.stderr)
print(f"[{typ}] 实例总数={len(insts)} 目标级={cfg['level']} ({cfg['reason']})")
print(f"\n=== 待处理 {len(plan)} 条(升={sum(1 for p in plan if p[3])} 移除误标={sum(1 for p in plan if not p[3])})===")
for typ, pk, name, desired, cur, note in plan[:60]:
print(f" {typ}/{pk} {cur} -> {desired or '[]'} {note} {name!r}")
if len(plan) > 60:
print(f" ... 其余 {len(plan)-60} 条省略")
if args.dry_run:
print("\n[DRY-RUN] 未写入。去掉 --dry-run 执行。")
else:
print(f"\n已写入:加覆盖层 {applied} 条,移除误标 {reverted} 条(幂等)。")
if __name__ == "__main__":
main()