Refine scoped API key capabilities

This commit is contained in:
wizardchen
2026-07-07 11:09:10 +08:00
committed by lyingbug
parent b5cc7d2a28
commit 2c6f082062
38 changed files with 1718 additions and 655 deletions
@@ -6,7 +6,7 @@ CREATE TABLE IF NOT EXISTS tenant_api_keys (
name VARCHAR(128) NOT NULL,
key_hash VARCHAR(64) NOT NULL UNIQUE,
api_key TEXT NOT NULL DEFAULT '',
role VARCHAR(32) NOT NULL DEFAULT 'viewer',
full_access BOOLEAN NOT NULL DEFAULT FALSE,
knowledge_base_ids JSONB NOT NULL DEFAULT '[]'::jsonb,
last_used_at TIMESTAMP,
expires_at TIMESTAMP,
@@ -25,7 +25,7 @@ INSERT INTO tenant_api_keys (
name,
key_hash,
api_key,
role,
full_access,
knowledge_base_ids,
created_at,
updated_at
@@ -35,7 +35,7 @@ SELECT
'Tenant API key',
'migrated-tenant-' || id::text,
api_key,
'owner',
TRUE,
'[]'::jsonb,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
@@ -0,0 +1 @@
ALTER TABLE tenant_api_keys DROP COLUMN IF EXISTS capabilities;
@@ -0,0 +1,9 @@
DO $$ BEGIN RAISE NOTICE '[Migration 000066] Adding tenant_api_keys.capabilities...'; END $$;
-- Additive per-key grants for non-full-access keys. Capabilities let a scoped
-- key call a bounded route family, while knowledge_base_ids still constrains
-- the KBs the key can touch where a route targets knowledge-base data.
ALTER TABLE tenant_api_keys
ADD COLUMN IF NOT EXISTS capabilities JSONB NOT NULL DEFAULT '[]'::jsonb;
DO $$ BEGIN RAISE NOTICE '[Migration 000066] tenant_api_keys.capabilities ready'; END $$;