feat: add scoped tenant api keys

This commit is contained in:
wizardchen
2026-07-05 10:25:37 +08:00
committed by lyingbug
parent 1708d6895a
commit 505bc7ddec
49 changed files with 2899 additions and 1578 deletions
-1
View File
@@ -10,7 +10,6 @@ CREATE TABLE tenants (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
api_key VARCHAR(256) NOT NULL,
retriever_engines JSON NOT NULL,
status VARCHAR(50) DEFAULT 'active',
business VARCHAR(255) NOT NULL,
-2
View File
@@ -10,7 +10,6 @@ CREATE TABLE IF NOT EXISTS tenants (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
api_key VARCHAR(256) NOT NULL,
retriever_engines JSONB NOT NULL DEFAULT '[]',
status VARCHAR(50) DEFAULT 'active',
business VARCHAR(255) NOT NULL,
@@ -28,7 +27,6 @@ COMMENT ON COLUMN tenants.agent_config IS 'Tenant-level agent configuration in J
ALTER SEQUENCE tenants_id_seq RESTART WITH 10000;
-- Add indexes
CREATE INDEX IF NOT EXISTS idx_tenants_api_key ON tenants(api_key);
CREATE INDEX IF NOT EXISTS idx_tenants_status ON tenants(status);
-- Create model table
+1
View File
@@ -1,4 +1,5 @@
DROP TABLE IF EXISTS tenant_invitations;
DROP TABLE IF EXISTS tenant_api_keys;
DROP TABLE IF EXISTS user_kb_pins;
DROP TABLE IF EXISTS user_resource_favorites;
DROP TABLE IF EXISTS tenant_disabled_shared_agents;
+19 -2
View File
@@ -4,7 +4,6 @@ CREATE TABLE IF NOT EXISTS tenants (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255) NOT NULL,
description TEXT,
api_key VARCHAR(256) NOT NULL,
retriever_engines TEXT NOT NULL DEFAULT '[]',
status VARCHAR(50) DEFAULT 'active',
business VARCHAR(255) NOT NULL,
@@ -24,7 +23,6 @@ CREATE TABLE IF NOT EXISTS tenants (
deleted_at DATETIME
);
CREATE INDEX IF NOT EXISTS idx_tenants_api_key ON tenants(api_key);
CREATE INDEX IF NOT EXISTS idx_tenants_status ON tenants(status);
CREATE TABLE IF NOT EXISTS models (
@@ -751,3 +749,22 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_vector_stores_name_tenant
CREATE INDEX IF NOT EXISTS idx_vector_stores_tenant_id ON vector_stores(tenant_id);
CREATE INDEX IF NOT EXISTS idx_vector_stores_engine_type ON vector_stores(engine_type);
CREATE INDEX IF NOT EXISTS idx_vector_stores_deleted_at ON vector_stores(deleted_at);
CREATE TABLE IF NOT EXISTS tenant_api_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
tenant_id INTEGER NOT NULL,
name TEXT NOT NULL,
key_hash TEXT NOT NULL UNIQUE,
api_key TEXT NOT NULL DEFAULT '',
scopes TEXT NOT NULL DEFAULT '[]',
knowledge_base_ids TEXT NOT NULL DEFAULT '[]',
last_used_at DATETIME,
expires_at DATETIME,
revoked_at DATETIME,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS idx_tenant_api_keys_tenant ON tenant_api_keys(tenant_id);
CREATE INDEX IF NOT EXISTS idx_tenant_api_keys_revoked_at ON tenant_api_keys(revoked_at);
@@ -0,0 +1,5 @@
ALTER TABLE tenants ADD COLUMN IF NOT EXISTS api_key VARCHAR(256) NOT NULL DEFAULT '';
CREATE INDEX IF NOT EXISTS idx_tenants_api_key ON tenants(api_key);
DROP INDEX IF EXISTS idx_tenant_api_keys_revoked_at;
DROP INDEX IF EXISTS idx_tenant_api_keys_tenant;
DROP TABLE IF EXISTS tenant_api_keys;
@@ -0,0 +1,49 @@
DO $$ BEGIN RAISE NOTICE '[Migration 000065] Creating tenant_api_keys...'; END $$;
CREATE TABLE IF NOT EXISTS tenant_api_keys (
id BIGSERIAL PRIMARY KEY,
tenant_id INTEGER NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
name VARCHAR(128) NOT NULL,
key_hash VARCHAR(64) NOT NULL UNIQUE,
api_key TEXT NOT NULL DEFAULT '',
scopes JSONB NOT NULL DEFAULT '[]'::jsonb,
knowledge_base_ids JSONB NOT NULL DEFAULT '[]'::jsonb,
last_used_at TIMESTAMP,
expires_at TIMESTAMP,
revoked_at TIMESTAMP,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_tenant_api_keys_tenant
ON tenant_api_keys(tenant_id);
CREATE INDEX IF NOT EXISTS idx_tenant_api_keys_revoked_at
ON tenant_api_keys(revoked_at);
INSERT INTO tenant_api_keys (
tenant_id,
name,
key_hash,
api_key,
scopes,
knowledge_base_ids,
created_at,
updated_at
)
SELECT
id,
'Tenant API key',
'migrated-tenant-' || id::text,
api_key,
'["read","write","admin"]'::jsonb,
'[]'::jsonb,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
FROM tenants
WHERE COALESCE(api_key, '') <> ''
ON CONFLICT (key_hash) DO NOTHING;
DROP INDEX IF EXISTS idx_tenants_api_key;
ALTER TABLE tenants DROP COLUMN IF EXISTS api_key;
DO $$ BEGIN RAISE NOTICE '[Migration 000065] tenant_api_keys ready'; END $$;