refactor(api-keys): simplify authorization to a single-role gate
Redesign tenant API key authorization around one dimension (role) plus a central default-deny gate, replacing the previous role + KB-scoped flag model. - Add APIKeyGate as the single authority for API-key authorization: routes declare one policy (MinRole) via apiKeyGroup/apiKeyRoute helpers; undeclared routes are denied by default. JWT principals short-circuit the gate and the JWT-centric RBAC guards short-circuit for API-key principals. - Roles: viewer/contributor/admin are knowledge-base data-plane only (KB scope is a pure downstream data filter); owner is a full-access tenant tier that unlocks tenant-level infrastructure routes and is required for every method. - Drop the KBScoped policy flag and the KB-restricted-vs-tenant branching. - Security hardening: never disclose HMAC secret (return presence only), abort writes on API-key encryption failure, deny KB-restricted keys on /files, and backfill legacy key hashes at startup instead of O(N) plaintext scans. - Frontend: two-level create dialog (access type -> permission level), rename role labels to reflect actual scope, sync en/zh/ko/ru locales. - Fix startup self-check false-positive on trailing-slash routes and normalize gin paths before comparison; add regression tests.
This commit is contained in:
@@ -85,6 +85,10 @@ type TenantAPIKeyRepository interface {
|
||||
RevokeAllAPIKeys(ctx context.Context, tenantID uint64) error
|
||||
UpdateAPIKeyHash(ctx context.Context, id uint64, hash string) error
|
||||
UpdateAPIKeyLastUsed(ctx context.Context, id uint64, at time.Time) error
|
||||
// ListKeysWithPlaceholderHash returns keys whose key_hash is still the
|
||||
// migration placeholder (never authenticated since the 000065 upgrade),
|
||||
// so the real SHA-256 hash can be computed and backfilled.
|
||||
ListKeysWithPlaceholderHash(ctx context.Context) ([]*types.TenantAPIKey, error)
|
||||
}
|
||||
|
||||
type TenantAPIKeyService interface {
|
||||
@@ -94,4 +98,9 @@ type TenantAPIKeyService interface {
|
||||
ListAPIKeys(ctx context.Context, tenantID uint64) ([]*types.TenantAPIKey, error)
|
||||
RevokeAPIKey(ctx context.Context, tenantID uint64, id uint64) error
|
||||
RevokeAllAPIKeys(ctx context.Context, tenantID uint64) error
|
||||
// BackfillMissingKeyHashes computes and persists the SHA-256 key_hash
|
||||
// for legacy keys still carrying the migration placeholder, so their
|
||||
// first authentication does not fall back to an O(n) plaintext scan.
|
||||
// Returns the number of keys backfilled.
|
||||
BackfillMissingKeyHashes(ctx context.Context) (int, error)
|
||||
}
|
||||
|
||||
@@ -35,9 +35,14 @@ func (TenantAPIKey) TableName() string {
|
||||
|
||||
func (k *TenantAPIKey) BeforeSave(tx *gorm.DB) error {
|
||||
if key := utils.GetAESKey(); key != nil && k.APIKey != "" {
|
||||
if encrypted, err := utils.EncryptAESGCM(k.APIKey, key); err == nil {
|
||||
tx.Statement.SetColumn("api_key", encrypted)
|
||||
encrypted, err := utils.EncryptAESGCM(k.APIKey, key)
|
||||
if err != nil {
|
||||
// Never fall through to storing the plaintext key: abort the
|
||||
// write so the caller sees the failure instead of silently
|
||||
// persisting an unencrypted secret.
|
||||
return fmt.Errorf("encrypt tenant_api_keys.api_key (id=%d): %w", k.ID, err)
|
||||
}
|
||||
tx.Statement.SetColumn("api_key", encrypted)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -84,6 +89,8 @@ func (s TenantAPIKeyScope) Normalize() TenantAPIKeyScope {
|
||||
// NormalizeTenantAPIKeyRole maps stored/API role strings to tenant RBAC roles.
|
||||
func NormalizeTenantAPIKeyRole(role TenantRole) TenantRole {
|
||||
switch strings.ToLower(strings.TrimSpace(string(role))) {
|
||||
case string(TenantRoleOwner):
|
||||
return TenantRoleOwner
|
||||
case string(TenantRoleAdmin):
|
||||
return TenantRoleAdmin
|
||||
case string(TenantRoleContributor):
|
||||
|
||||
@@ -18,8 +18,9 @@ func TestNormalizeTenantAPIKeyRole(t *testing.T) {
|
||||
{name: "viewer", in: TenantRoleViewer, want: TenantRoleViewer},
|
||||
{name: "contributor", in: TenantRoleContributor, want: TenantRoleContributor},
|
||||
{name: "admin", in: TenantRoleAdmin, want: TenantRoleAdmin},
|
||||
{name: "owner", in: TenantRoleOwner, want: TenantRoleOwner},
|
||||
{name: "empty defaults viewer", in: "", want: TenantRoleViewer},
|
||||
{name: "invalid", in: "owner", want: ""},
|
||||
{name: "invalid", in: "superuser", want: ""},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user