feat(apikey): 支持原地轮换 API Key,保留 Key ID 和计费历史 - #6439
Open
xpflying wants to merge 1 commit into
Open
Conversation
Contributor
|
All contributors have signed the CLA. ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
Add POST /api/v1/keys/:id/rotate and POST /api/v1/admin/api-keys/:id/rotate to replace an API key credential without creating a new record. The key ID, owner, name, group, quota, rate limits, IP rules, expiry, usage and billing history stay unchanged; only the secret is regenerated. - Atomic compare-and-swap update so concurrent rotations cannot both succeed - Invalidate old and new credentials in process/Redis auth caches and notify other instances via existing pub/sub; the DB trigger enqueues both hashes into the durable invalidation outbox - Rotated keys keep the platform's existing display behavior: list/detail responses return the full credential and copy/use/import stay available; API responses gain last_rotated_at recording the rotation time - Frontend adds confirm dialog and new-credential display with copy for both user and admin views - Audit logs record operator and key ID (admin also records target user), never plaintext secrets Closes Wei-Shaw#6409
xpflying
force-pushed
the
feat/api-key-rotate-in-place
branch
from
August 31, 2026 15:39
bf19c73 to
a95f068
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue
Closes #6409
背景
API Key 泄露后目前只能禁用旧 Key 再新建,新 Key 会拿到新的
api_key_id,历史用量和计费记录被拆分。本 PR 在不创建新记录的前提下原地替换鉴权凭据,保留原 Key ID、用量、计费历史和全部配置。实现内容
后端
POST /api/v1/keys/:id/rotate,只允许轮换自己的 Key;管理员端新增POST /api/v1/admin/api-keys/:id/rotate,沿用现有管理员权限。GenerateKey()(crypto/rand32 字节随机数 + 配置前缀),数据库只更新key、last_rotated_at、updated_at,其余字段全部保留。last_rotated_at字段记录轮换时间。前端
数据
232_api_keys_last_rotated_at.sql(幂等,ADD COLUMN IF NOT EXISTS)。测试
go test ./...、go vet、vue-tsc、ESLint、Vitest、Vite 生产构建均通过。部署说明
232_api_keys_last_rotated_at.sql(随现有迁移流程自动执行)。last_rotated_at字段。