b5cc7d2a28
Enhance the tenant API key service by introducing asynchronous throttling for the last_used_at updates during API key authentication. This change reduces database write frequency under high request rates, improving performance. Additionally, update tests to validate the throttling behavior, ensuring that last_used_at is only updated once per key within the defined interval. This implementation aligns with the goal of optimizing API key management and maintaining system efficiency.
171 lines
4.6 KiB
Go
171 lines
4.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/hex"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
|
|
"github.com/Tencent/WeKnora/internal/logger"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
)
|
|
|
|
// apiKeyLastUsedMinInterval bounds how often we persist last_used_at per key.
|
|
// The UI only needs minute-level freshness; throttling avoids a DB write on
|
|
// every authenticated request under high QPS.
|
|
const apiKeyLastUsedMinInterval = time.Minute
|
|
|
|
type tenantAPIKeyService struct {
|
|
repo interfaces.TenantAPIKeyRepository
|
|
lastUsedTouch sync.Map // key ID (uint64) -> time.Time of last persisted touch
|
|
}
|
|
|
|
func NewTenantAPIKeyService(repo interfaces.TenantAPIKeyRepository) interfaces.TenantAPIKeyService {
|
|
return &tenantAPIKeyService{repo: repo}
|
|
}
|
|
|
|
func (s *tenantAPIKeyService) CreateAPIKey(
|
|
ctx context.Context, req interfaces.TenantAPIKeyCreateRequest,
|
|
) (*interfaces.TenantAPIKeyCreateResult, error) {
|
|
if req.TenantID == 0 {
|
|
return nil, errors.New("tenant_id is required")
|
|
}
|
|
name := strings.TrimSpace(req.Name)
|
|
if name == "" {
|
|
return nil, errors.New("name is required")
|
|
}
|
|
token, err := generateTenantAPIKeyToken()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
key := &types.TenantAPIKey{
|
|
TenantID: req.TenantID,
|
|
Name: name,
|
|
KeyHash: hashTenantAPIKey(token),
|
|
APIKey: token,
|
|
Role: types.NormalizeTenantAPIKeyRole(req.Role),
|
|
KnowledgeBaseIDs: normalizeAPIKeyIDs(req.KnowledgeBaseIDs),
|
|
ExpiresAt: req.ExpiresAt,
|
|
}
|
|
if key.Role == "" {
|
|
key.Role = types.TenantRoleViewer
|
|
}
|
|
if err := s.repo.CreateAPIKey(ctx, key); err != nil {
|
|
return nil, err
|
|
}
|
|
return &interfaces.TenantAPIKeyCreateResult{APIKey: key, Token: token}, nil
|
|
}
|
|
|
|
func (s *tenantAPIKeyService) AuthenticateAPIKey(ctx context.Context, token string) (*types.TenantAPIKey, error) {
|
|
token = strings.TrimSpace(token)
|
|
if token == "" {
|
|
return nil, apprepo.ErrTenantAPIKeyNotFound
|
|
}
|
|
key, err := s.repo.GetAPIKeyByHash(ctx, hashTenantAPIKey(token))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if key.RevokedAt != nil {
|
|
return nil, apprepo.ErrTenantAPIKeyNotFound
|
|
}
|
|
if key.ExpiresAt != nil && time.Now().After(*key.ExpiresAt) {
|
|
return nil, apprepo.ErrTenantAPIKeyNotFound
|
|
}
|
|
s.touchAPIKeyLastUsedAsync(key.ID)
|
|
return key, nil
|
|
}
|
|
|
|
// touchAPIKeyLastUsedAsync persists last_used_at at most once per key per
|
|
// apiKeyLastUsedMinInterval. The write runs in a detached goroutine so auth
|
|
// latency is not tied to an UPDATE on the hot path.
|
|
func (s *tenantAPIKeyService) touchAPIKeyLastUsedAsync(keyID uint64) {
|
|
now := time.Now()
|
|
if v, ok := s.lastUsedTouch.Load(keyID); ok {
|
|
if now.Sub(v.(time.Time)) < apiKeyLastUsedMinInterval {
|
|
return
|
|
}
|
|
}
|
|
s.lastUsedTouch.Store(keyID, now)
|
|
go func(id uint64, at time.Time) {
|
|
if err := s.repo.UpdateAPIKeyLastUsed(context.Background(), id, at); err != nil {
|
|
logger.Warnf(context.Background(),
|
|
"failed to update tenant api key last_used_at (id=%d): %v", id, err)
|
|
s.lastUsedTouch.Delete(id)
|
|
}
|
|
}(keyID, now)
|
|
}
|
|
|
|
func (s *tenantAPIKeyService) ListAPIKeys(ctx context.Context, tenantID uint64) ([]*types.TenantAPIKey, error) {
|
|
return s.repo.ListAPIKeys(ctx, tenantID)
|
|
}
|
|
|
|
func (s *tenantAPIKeyService) RevokeAPIKey(ctx context.Context, tenantID uint64, id uint64) error {
|
|
return s.repo.RevokeAPIKey(ctx, tenantID, id)
|
|
}
|
|
|
|
func (s *tenantAPIKeyService) BackfillMissingKeyHashes(ctx context.Context) (int, error) {
|
|
has, err := s.repo.HasKeysWithPlaceholderHash(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if !has {
|
|
return 0, nil
|
|
}
|
|
keys, err := s.repo.ListKeysWithPlaceholderHash(ctx)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
backfilled := 0
|
|
for _, key := range keys {
|
|
if key == nil || strings.TrimSpace(key.APIKey) == "" {
|
|
continue
|
|
}
|
|
hash := hashTenantAPIKey(key.APIKey)
|
|
if key.KeyHash == hash {
|
|
continue
|
|
}
|
|
if err := s.repo.UpdateAPIKeyHash(ctx, key.ID, hash); err != nil {
|
|
return backfilled, err
|
|
}
|
|
backfilled++
|
|
}
|
|
return backfilled, nil
|
|
}
|
|
|
|
func generateTenantAPIKeyToken() (string, error) {
|
|
var b [32]byte
|
|
if _, err := rand.Read(b[:]); err != nil {
|
|
return "", err
|
|
}
|
|
return "sk-" + base64.RawURLEncoding.EncodeToString(b[:]), nil
|
|
}
|
|
|
|
func hashTenantAPIKey(token string) string {
|
|
sum := sha256.Sum256([]byte(token))
|
|
return hex.EncodeToString(sum[:])
|
|
}
|
|
|
|
func normalizeAPIKeyIDs(in []string) types.StringArray {
|
|
out := types.StringArray{}
|
|
seen := map[string]struct{}{}
|
|
for _, id := range in {
|
|
id = strings.TrimSpace(id)
|
|
if id == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
out = append(out, id)
|
|
}
|
|
return out
|
|
}
|