Files
ai-gateway-go/migrations/000042_governance.sql
T
LLMGuardX Dev 87c2b04174 0.11.3: 旗舰版第四轮完善(统一审批中心/工具治理/平台环境变量/数字员工入口/个人渠道/报表多维/租户配额)
- 统一审批中心:模型/资源/渠道/工具四类申请聚合审批,通过自动开通
  (marketplace 安装/渠道授权),outbox 双向站内信;门户可发起/撤回。
- 工具治理:rate_limit_rpm(固定窗口原子 upsert,多实例共享)+ approval_required
  (首次调用自动发起审批,批准前一律拒绝)。
- 平台环境变量:平台级注入 skill/MCP 运行时,个人可覆盖;系统管理员可写。
- 数字员工会话入口:门户列表/对话/调用记录,复用用户运行时凭据。
- 个人渠道:webhook 入站令牌 SHA-256 摘要 + constant-time 校验,绑定已批准
  模型,用量归属用户 Key。
- 报表多维:工具调用/审批授权/安全事件三组统计端点与页面。
- 租户配额:部门 Key/月 Token 上限,运行时凭据开通强制校验,概览展示用量。
- 迁移 000042-000045;修复渠道空 API Key NOT NULL 违约与 inet 扫描;
  25 包测试通过,前后端构建通过,端到端验证完成。
2026-08-13 13:41:22 +08:00

61 lines
3.5 KiB
SQL

-- 000042_governance.sql — 治理增强:工具限流与审批、资源/渠道权限申请(全类型审批流)。
-- 工具治理:调用频率上限(0 = 不限)与审批标记(approval_required 工具首次调用需管理员审批)。
ALTER TABLE gateway.tool_definitions
ADD COLUMN IF NOT EXISTS rate_limit_rpm integer NOT NULL DEFAULT 0,
ADD COLUMN IF NOT EXISTS approval_required boolean NOT NULL DEFAULT false;
COMMENT ON COLUMN gateway.tool_definitions.rate_limit_rpm IS
'Per-tool rate limit in calls per minute, 0 = unlimited. Enforced with an atomic fixed-window upsert so multiple gateway replicas share the same budget.';
COMMENT ON COLUMN gateway.tool_definitions.approval_required IS
'When true the tool cannot run until an admin approves it; the first invocation creates a tool_approval_requests entry.';
-- 工具限流固定窗口计数(PostgreSQL 原子 upsert,多实例共享)。
CREATE TABLE IF NOT EXISTS gateway.tool_rate_usage (
tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
window_start timestamptz NOT NULL,
call_count bigint NOT NULL DEFAULT 0,
PRIMARY KEY (tool_id, window_start)
);
-- 工具审批申请:每工具至多一个待审项;审批通过后该工具可被调用。
CREATE TABLE IF NOT EXISTS gateway.tool_approval_requests (
id uuid PRIMARY KEY,
tool_id uuid NOT NULL REFERENCES gateway.tool_definitions(id) ON DELETE CASCADE,
requester_kind varchar(16) NOT NULL DEFAULT 'system',
requester_id text NOT NULL DEFAULT '',
reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected')),
decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
decided_at timestamptz
);
CREATE UNIQUE INDEX IF NOT EXISTS tool_approval_requests_one_pending_idx
ON gateway.tool_approval_requests (tool_id) WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS tool_approval_requests_status_time_idx
ON gateway.tool_approval_requests (status, created_at DESC);
-- 资源/渠道权限申请:门户用户申请 mcp/skill/数字员工/渠道的使用权限,管理员审批后
-- 自动开通(marketplace 安装或渠道部门授权)。
CREATE TABLE IF NOT EXISTS gateway.resource_access_requests (
id uuid PRIMARY KEY,
portal_user_id uuid NOT NULL REFERENCES gateway.portal_users(id) ON DELETE CASCADE,
resource_type varchar(24) NOT NULL CHECK (resource_type IN ('mcp_server', 'skill', 'digital_employee', 'channel')),
resource_code text NOT NULL CHECK (length(resource_code) BETWEEN 1 AND 128),
reason text NOT NULL DEFAULT '' CHECK (length(reason) <= 4000),
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'approved', 'rejected', 'cancelled')),
decision_note text NOT NULL DEFAULT '' CHECK (length(decision_note) <= 4000),
decided_by uuid REFERENCES gateway.admin_accounts(id) ON DELETE SET NULL,
decided_at timestamptz,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE UNIQUE INDEX IF NOT EXISTS resource_access_requests_one_pending_idx
ON gateway.resource_access_requests (portal_user_id, resource_type, resource_code)
WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS resource_access_requests_status_time_idx
ON gateway.resource_access_requests (status, created_at DESC);