feat(api-keys): add platform-scoped API keys for control-plane automation

Introduce platform API keys with capability-based access to tenant management,
system settings, runtime queues, and audit logs, plus admin UI for key lifecycle
and system audit log viewing.
This commit is contained in:
wizardchen
2026-07-21 15:05:57 +08:00
committed by lyingbug
parent c695125dce
commit 23db592c4a
42 changed files with 2967 additions and 1083 deletions
+24 -3
View File
@@ -34,9 +34,17 @@ func NewTenantAPIKeyService(repo interfaces.TenantAPIKeyRepository) interfaces.T
func (s *tenantAPIKeyService) CreateAPIKey(
ctx context.Context, req interfaces.TenantAPIKeyCreateRequest,
) (*interfaces.TenantAPIKeyCreateResult, error) {
if req.TenantID == 0 {
scopeType := types.NormalizeAPIKeyScopeType(req.ScopeType)
if scopeType == types.APIKeyScopeTenant && req.TenantID == 0 {
return nil, errors.New("tenant_id is required")
}
if scopeType == types.APIKeyScopePlatform && req.FullAccess {
return nil, errors.New("platform API keys require explicit capabilities")
}
capabilities := types.NormalizeAPIKeyCapabilities(types.StringArray(req.Capabilities))
if scopeType == types.APIKeyScopePlatform && len(capabilities) == 0 {
return nil, errors.New("platform API keys require at least one capability")
}
name := strings.TrimSpace(req.Name)
if name == "" {
return nil, errors.New("name is required")
@@ -45,14 +53,19 @@ func (s *tenantAPIKeyService) CreateAPIKey(
if err != nil {
return nil, err
}
var tenantID *uint64
if scopeType == types.APIKeyScopeTenant {
tenantID = &req.TenantID
}
key := &types.TenantAPIKey{
TenantID: req.TenantID,
TenantID: tenantID,
ScopeType: scopeType,
Name: name,
KeyHash: hashTenantAPIKey(token),
APIKey: token,
FullAccess: req.FullAccess,
KnowledgeBaseIDs: normalizeAPIKeyIDs(req.KnowledgeBaseIDs),
Capabilities: types.NormalizeAPIKeyCapabilities(types.StringArray(req.Capabilities)),
Capabilities: capabilities,
ExpiresAt: req.ExpiresAt,
}
if key.FullAccess {
@@ -108,10 +121,18 @@ func (s *tenantAPIKeyService) ListAPIKeys(ctx context.Context, tenantID uint64)
return s.repo.ListAPIKeys(ctx, tenantID)
}
func (s *tenantAPIKeyService) ListPlatformAPIKeys(ctx context.Context) ([]*types.TenantAPIKey, error) {
return s.repo.ListPlatformAPIKeys(ctx)
}
func (s *tenantAPIKeyService) RevokeAPIKey(ctx context.Context, tenantID uint64, id uint64) error {
return s.repo.RevokeAPIKey(ctx, tenantID, id)
}
func (s *tenantAPIKeyService) RevokePlatformAPIKey(ctx context.Context, id uint64) error {
return s.repo.RevokePlatformAPIKey(ctx, id)
}
func (s *tenantAPIKeyService) BackfillMissingKeyHashes(ctx context.Context) (int, error) {
has, err := s.repo.HasKeysWithPlaceholderHash(ctx)
if err != nil {