feat(sense): implement Control API v1 [T-011]
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Sense 控制面、准入投影与本地审计契约 v1
|
||||
|
||||
> 冻结日期:2026-08-07。契约版本:`1.0.0`。Sense 是设备期望态的提供方;Bell 是 Tenant、Site、Area、RBAC、配额与全局审计的所有者。T-009/T-010 已实现两个只读投影和本地审计事务基础;HTTP handler、Bell 管理服务和 Outbox relay 仍未实现。
|
||||
> 冻结日期:2026-08-07。Control API 契约版本:`1.0.0`。Sense 是设备期望态的提供方;Bell 是 Tenant、Site、Area、RBAC、配额与全局审计的所有者。T-011 已实现 Control API handler 与 PostgreSQL 一致性边界;Bell 管理服务、JWT/OIDC 和 Outbox relay 仍未实现。
|
||||
|
||||
## 契约文件
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
| [`site-quota-v1.sql`](site-quota-v1.sql) | Bell | Sense | 单 PostgreSQL 实例内的站点视频配额只读投影 |
|
||||
| [`area-policy-v1.sql`](area-policy-v1.sql) | Bell | Sense | Area 归属与 `capture_policy` 只读投影 |
|
||||
| [`sense-device-audit-v1.schema.json`](sense-device-audit-v1.schema.json) | Sense | 本地 Outbox;未来 Bell relay | 脱敏设备操作审计事实,不包含传输协议 |
|
||||
| [`sense-device-audit-v2.schema.json`](sense-device-audit-v2.schema.json) | Sense | 本地 Outbox;未来 Bell relay | v1 后继,增加脱敏配置修改受理事实;v1 文件保持不变 |
|
||||
|
||||
OpenAPI 的 `/api/v1` 路径是公共控制面边界;`/healthz`、`/readyz` 仍是非业务运维探针。v1 不提供设备删除:停用设备使用期望态接口,保留设备、操作和审计历史。Site、Area、配额、RBAC 和审计聚合不由 Sense 提供 CRUD。
|
||||
|
||||
@@ -54,7 +55,7 @@ T-010 冻结 `bell.area_policy_v1` 的列顺序为 `tenant_id/site_id/area_id/ca
|
||||
|
||||
## 本地设备操作审计
|
||||
|
||||
`sense-device-audit-v1.schema.json` 冻结本地审计事实的逻辑 envelope。当前事件只有 `device.created` 和 `device.desired_state.accepted`;主体类型为 `user | service | system`,投影版本与 generation 随事实保存。`data` 只包含 Area、模态、能力和状态变化等脱敏字段,禁止 endpoint、credential、profile token、path、密码、完整流 URI 或 MediaMTX 配置。
|
||||
`sense-device-audit-v1.schema.json` 继续冻结创建与期望态两类事实且不原地扩展严格枚举。T-011 新增 v2 后继,兼容 v1 两类事件并增加 `device.configuration.accepted`;该 payload 只保存是否变化、字段名和 Area 逻辑 ID,不保存字段值。主体类型为 `user | service | system`,投影版本与 generation 随事实保存;endpoint、credential、profile token、path、密码、完整流 URI 或 MediaMTX 配置始终禁止进入审计。
|
||||
|
||||
PostgreSQL repository 必须在设备创建/期望态事务内写 `sense.device_operation_outbox`;Outbox 失败回滚业务写入。相同期望态不增加 generation,但仍产生独立审计事实。Schema 不是 Bell relay 协议:传输端点、签名、批量确认、重放窗口和留存由后续任务冻结。
|
||||
|
||||
@@ -71,7 +72,9 @@ PostgreSQL repository 必须在设备创建/期望态事务内写 `sense.device_
|
||||
|
||||
```powershell
|
||||
python -m json.tool docs/contracts/sense-control-v1.openapi.json | Out-Null
|
||||
python -m json.tool docs/contracts/sense-device-audit-v2.schema.json | Out-Null
|
||||
python -m unittest discover -s tests -p "test_sense_control_contract.py"
|
||||
python -m unittest discover -s tests -p "test_sense_control_implementation.py"
|
||||
```
|
||||
|
||||
测试校验本仓库依赖的 OpenAPI 结构与安全不变量,并不替代后续实现任务对完整 OpenAPI 标准验证器、HTTP handler 和 PostgreSQL migration 的验证。
|
||||
测试同时校验 OpenAPI 结构、生成 server glue、HTTP handler 与 PostgreSQL migration/事务;它不替代 Bell 消费方联合验收或客户现场容量验证。
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "https://yovision.local/contracts/sense-device-audit-v2.schema.json",
|
||||
"title": "Sense Device Audit Event v2",
|
||||
"description": "v1 的向后兼容后继:增加脱敏设备配置受理事实,仍不定义 Bell relay。",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"event_id", "event_type", "tenant_id", "site_id", "device_id",
|
||||
"actor", "reason", "trace_id", "aggregate_generation",
|
||||
"projection_versions", "data", "occurred_at"
|
||||
],
|
||||
"properties": {
|
||||
"event_id": {"type": "string", "pattern": "^audit_[0-9a-f]{32}$"},
|
||||
"event_type": {
|
||||
"enum": [
|
||||
"device.created",
|
||||
"device.desired_state.accepted",
|
||||
"device.configuration.accepted"
|
||||
]
|
||||
},
|
||||
"tenant_id": {"$ref": "#/$defs/logicalId"},
|
||||
"site_id": {"$ref": "#/$defs/logicalId"},
|
||||
"device_id": {"$ref": "#/$defs/logicalId"},
|
||||
"actor": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["type", "id"],
|
||||
"properties": {
|
||||
"type": {"enum": ["user", "service", "system"]},
|
||||
"id": {"type": "string", "minLength": 1, "maxLength": 200}
|
||||
}
|
||||
},
|
||||
"reason": {"type": ["string", "null"], "maxLength": 500},
|
||||
"trace_id": {"type": ["string", "null"], "maxLength": 128},
|
||||
"aggregate_generation": {"type": "integer", "minimum": 1},
|
||||
"projection_versions": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["quota_source_version", "area_policy_source_version"],
|
||||
"properties": {
|
||||
"quota_source_version": {"type": ["integer", "null"], "minimum": 1},
|
||||
"area_policy_source_version": {"type": ["integer", "null"], "minimum": 1}
|
||||
}
|
||||
},
|
||||
"data": {
|
||||
"oneOf": [
|
||||
{"$ref": "#/$defs/deviceCreatedData"},
|
||||
{"$ref": "#/$defs/desiredStateData"},
|
||||
{"$ref": "#/$defs/configurationData"}
|
||||
]
|
||||
},
|
||||
"occurred_at": {"type": "string", "format": "date-time"}
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"if": {"properties": {"event_type": {"const": "device.created"}}},
|
||||
"then": {"properties": {"data": {"$ref": "#/$defs/deviceCreatedData"}}}
|
||||
},
|
||||
{
|
||||
"if": {"properties": {"event_type": {"const": "device.desired_state.accepted"}}},
|
||||
"then": {"properties": {"data": {"$ref": "#/$defs/desiredStateData"}}}
|
||||
},
|
||||
{
|
||||
"if": {"properties": {"event_type": {"const": "device.configuration.accepted"}}},
|
||||
"then": {"properties": {"data": {"$ref": "#/$defs/configurationData"}}}
|
||||
}
|
||||
],
|
||||
"$defs": {
|
||||
"logicalId": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 128,
|
||||
"pattern": "^[A-Za-z0-9][A-Za-z0-9._:-]*$"
|
||||
},
|
||||
"desiredState": {"enum": ["disabled", "enabled"]},
|
||||
"deviceCreatedData": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["kind", "area_id", "modality", "capabilities", "desired_state"],
|
||||
"properties": {
|
||||
"kind": {"const": "device_created"},
|
||||
"area_id": {"$ref": "#/$defs/logicalId"},
|
||||
"modality": {"enum": ["video", "radar", "contact", "button", "wearable", "other"]},
|
||||
"capabilities": {
|
||||
"type": "array",
|
||||
"maxItems": 16,
|
||||
"uniqueItems": true,
|
||||
"items": {"enum": ["video_capture", "audio_capture", "spatial_rule", "telemetry"]}
|
||||
},
|
||||
"desired_state": {"$ref": "#/$defs/desiredState"}
|
||||
}
|
||||
},
|
||||
"desiredStateData": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["kind", "previous_desired_state", "desired_state", "changed"],
|
||||
"properties": {
|
||||
"kind": {"const": "desired_state_accepted"},
|
||||
"previous_desired_state": {"$ref": "#/$defs/desiredState"},
|
||||
"desired_state": {"$ref": "#/$defs/desiredState"},
|
||||
"changed": {"type": "boolean"}
|
||||
}
|
||||
},
|
||||
"configurationData": {
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"required": ["kind", "changed", "changed_fields", "area_id"],
|
||||
"properties": {
|
||||
"kind": {"const": "configuration_accepted"},
|
||||
"changed": {"type": "boolean"},
|
||||
"changed_fields": {
|
||||
"type": "array",
|
||||
"maxItems": 5,
|
||||
"uniqueItems": true,
|
||||
"items": {
|
||||
"enum": ["name", "area_id", "endpoint_ref", "credential_ref", "profile_token"]
|
||||
}
|
||||
},
|
||||
"area_id": {"$ref": "#/$defs/logicalId"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user