feat: add scoped tenant api keys
This commit is contained in:
@@ -9,28 +9,26 @@ import (
|
||||
)
|
||||
|
||||
// TenantResponse is the viewer-safe tenant profile shape. Secret-bearing
|
||||
// columns are omitted or redacted unless the caller has Admin+ (Owner for
|
||||
// api_key).
|
||||
// columns are omitted or redacted unless the caller has Admin+.
|
||||
type TenantResponse struct {
|
||||
ID uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
APIKey string `json:"api_key,omitempty"`
|
||||
Status string `json:"status"`
|
||||
RetrieverEngines types.RetrieverEngines `json:"retriever_engines"`
|
||||
Business string `json:"business"`
|
||||
StorageQuota int64 `json:"storage_quota"`
|
||||
StorageUsed int64 `json:"storage_used"`
|
||||
ContextConfig *types.ContextConfig `json:"context_config,omitempty"`
|
||||
WebSearchConfig *types.WebSearchConfig `json:"web_search_config,omitempty"`
|
||||
ParserEngineConfig *types.ParserEngineConfig `json:"parser_engine_config,omitempty"`
|
||||
Credentials *types.CredentialsConfig `json:"credentials,omitempty"`
|
||||
StorageEngineConfig *types.StorageEngineConfig `json:"storage_engine_config,omitempty"`
|
||||
ChatHistoryConfig *types.ChatHistoryConfig `json:"chat_history_config,omitempty"`
|
||||
RetrievalConfig *types.RetrievalConfig `json:"retrieval_config,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at"`
|
||||
ID uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Status string `json:"status"`
|
||||
RetrieverEngines types.RetrieverEngines `json:"retriever_engines"`
|
||||
Business string `json:"business"`
|
||||
StorageQuota int64 `json:"storage_quota"`
|
||||
StorageUsed int64 `json:"storage_used"`
|
||||
ContextConfig *types.ContextConfig `json:"context_config,omitempty"`
|
||||
WebSearchConfig *types.WebSearchConfig `json:"web_search_config,omitempty"`
|
||||
ParserEngineConfig *types.ParserEngineConfig `json:"parser_engine_config,omitempty"`
|
||||
Credentials *types.CredentialsConfig `json:"credentials,omitempty"`
|
||||
StorageEngineConfig *types.StorageEngineConfig `json:"storage_engine_config,omitempty"`
|
||||
ChatHistoryConfig *types.ChatHistoryConfig `json:"chat_history_config,omitempty"`
|
||||
RetrievalConfig *types.RetrievalConfig `json:"retrieval_config,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `json:"deleted_at"`
|
||||
}
|
||||
|
||||
// NewTenantResponse converts a stored tenant into its HTTP response shape.
|
||||
@@ -45,8 +43,6 @@ func NewTenantResponseWithRole(tenant *types.Tenant, role types.TenantRole) *Ten
|
||||
return nil
|
||||
}
|
||||
includeSecrets := role.HasPermission(types.TenantRoleAdmin)
|
||||
includeAPIKey := RoleCanViewTenantAPIKey(role)
|
||||
|
||||
resp := &TenantResponse{
|
||||
ID: tenant.ID,
|
||||
Name: tenant.Name,
|
||||
@@ -63,9 +59,6 @@ func NewTenantResponseWithRole(tenant *types.Tenant, role types.TenantRole) *Ten
|
||||
UpdatedAt: tenant.UpdatedAt,
|
||||
DeletedAt: tenant.DeletedAt,
|
||||
}
|
||||
if includeAPIKey {
|
||||
resp.APIKey = tenant.APIKey
|
||||
}
|
||||
if includeSecrets {
|
||||
resp.WebSearchConfig = types.WebSearchConfigForResponse(tenant.WebSearchConfig, true)
|
||||
resp.ParserEngineConfig = types.ParserEngineConfigForResponse(tenant.ParserEngineConfig, true)
|
||||
|
||||
@@ -602,6 +602,7 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
|
||||
}
|
||||
kbs = filtered
|
||||
}
|
||||
kbs = filterKnowledgeBasesForAPIKeyScope(ctx, kbs)
|
||||
|
||||
// `all` mode: authoritative server-side capability filter so a client
|
||||
// that bypassed the frontend (old tab, curl, rogue plugin) can't @ a
|
||||
@@ -669,6 +670,7 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
|
||||
}
|
||||
kbs = filtered
|
||||
}
|
||||
kbs = filterKnowledgeBasesForAPIKeyScope(ctx, kbs)
|
||||
|
||||
// Get share counts for all knowledge bases
|
||||
if len(kbs) > 0 && h.kbShareService != nil {
|
||||
@@ -701,6 +703,20 @@ func (h *KnowledgeBaseHandler) ListKnowledgeBases(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func filterKnowledgeBasesForAPIKeyScope(ctx context.Context, kbs []*types.KnowledgeBase) []*types.KnowledgeBase {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok || len(scope.KnowledgeBaseIDs) == 0 {
|
||||
return kbs
|
||||
}
|
||||
filtered := make([]*types.KnowledgeBase, 0, len(kbs))
|
||||
for _, kb := range kbs {
|
||||
if kb != nil && scope.AllowsKnowledgeBase(kb.ID) {
|
||||
filtered = append(filtered, kb)
|
||||
}
|
||||
}
|
||||
return filtered
|
||||
}
|
||||
|
||||
// enrichKBCreatorNames 把 KB 列表里的 CreatorID 批量解析成展示名(username
|
||||
// 优先,退化到 email)。任意一步失败都吞掉错误:creator_name 缺失只会影响
|
||||
// 卡片右下角的徽章展示,不该影响列表本身可用。
|
||||
|
||||
@@ -129,6 +129,9 @@ func (h *Handler) parseQARequest(c *gin.Context, logPrefix string) (*qaRequestCo
|
||||
|
||||
// Merge @mentioned items into knowledge_base_ids and knowledge_ids
|
||||
kbIDs, knowledgeIDs := mergeKnowledgeTargets(request.KnowledgeBaseIDs, request.KnowledgeIds, request.MentionedItems)
|
||||
if err := authorizeTenantAPIKeyKnowledgeTargets(ctx, kbIDs, knowledgeIDs); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// The built-in wiki fixer is invoked from a KB page, not from a tenant's
|
||||
// regular agent picker. When the KB is shared, run it in the source tenant
|
||||
@@ -530,6 +533,10 @@ func (h *Handler) SearchKnowledge(c *gin.Context) {
|
||||
c.Error(errors.NewBadRequestError("At least one knowledge_base_id, knowledge_base_ids, knowledge_ids, or scoped tag must be provided"))
|
||||
return
|
||||
}
|
||||
if err := authorizeTenantAPIKeyKnowledgeTargets(ctx, knowledgeBaseIDs, request.KnowledgeIDs); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(
|
||||
ctx,
|
||||
@@ -555,6 +562,20 @@ func (h *Handler) SearchKnowledge(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func authorizeTenantAPIKeyKnowledgeTargets(ctx context.Context, kbIDs []string, knowledgeIDs []string) error {
|
||||
scope, ok := types.TenantAPIKeyScopeFromContext(ctx)
|
||||
if !ok || !scope.IsKnowledgeBaseRestricted() {
|
||||
return nil
|
||||
}
|
||||
if len(knowledgeIDs) > 0 {
|
||||
return errors.NewForbiddenError("API key scope does not allow knowledge_ids without a verified knowledge base")
|
||||
}
|
||||
if !scope.AllowsKnowledgeBases(kbIDs) {
|
||||
return errors.NewForbiddenError("API key scope does not allow one or more knowledge bases")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// KnowledgeQA godoc
|
||||
// @Summary 知识问答
|
||||
// @Description 基于知识库的问答(使用LLM总结),支持SSE流式响应
|
||||
|
||||
+174
-4
@@ -25,6 +25,7 @@ import (
|
||||
// through the REST API endpoints
|
||||
type TenantHandler struct {
|
||||
service interfaces.TenantService
|
||||
apiKeyService interfaces.TenantAPIKeyService
|
||||
userService interfaces.UserService
|
||||
memberService interfaces.TenantMemberService
|
||||
kbService interfaces.KnowledgeBaseService
|
||||
@@ -55,6 +56,7 @@ type TenantHandler struct {
|
||||
// stays focused on business logic.
|
||||
func NewTenantHandler(
|
||||
service interfaces.TenantService,
|
||||
apiKeyService interfaces.TenantAPIKeyService,
|
||||
userService interfaces.UserService,
|
||||
memberService interfaces.TenantMemberService,
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
@@ -63,6 +65,7 @@ func NewTenantHandler(
|
||||
) *TenantHandler {
|
||||
return &TenantHandler{
|
||||
service: service,
|
||||
apiKeyService: apiKeyService,
|
||||
userService: userService,
|
||||
memberService: memberService,
|
||||
kbService: kbService,
|
||||
@@ -130,6 +133,29 @@ type apiPrincipalTestTokenResponse struct {
|
||||
ExternalUserID string `json:"external_user_id"`
|
||||
}
|
||||
|
||||
type tenantAPIKeyCreateRequest struct {
|
||||
Name string `json:"name"`
|
||||
Scopes []string `json:"scopes"`
|
||||
KnowledgeBaseIDs []string `json:"knowledge_base_ids"`
|
||||
ExpiresAt *int64 `json:"expires_at_unix"`
|
||||
}
|
||||
|
||||
type tenantAPIKeyResponse struct {
|
||||
ID uint64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
APIKey string `json:"api_key"`
|
||||
Scopes types.StringArray `json:"scopes"`
|
||||
KnowledgeBaseIDs types.StringArray `json:"knowledge_base_ids"`
|
||||
LastUsedAt *time.Time `json:"last_used_at,omitempty"`
|
||||
ExpiresAt *time.Time `json:"expires_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type tenantAPIKeyCreateResponse struct {
|
||||
tenantAPIKeyResponse
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
const (
|
||||
defaultAPIPrincipalDirectHeader = "X-External-User-ID"
|
||||
defaultAPIPrincipalTokenHeader = "X-External-User-Token"
|
||||
@@ -210,8 +236,9 @@ func (h *TenantHandler) CreateTenant(c *gin.Context) {
|
||||
} else {
|
||||
// Self-service path: a regular user can only set name and
|
||||
// description. Everything else is server-generated by
|
||||
// TenantService.CreateTenant (api_key, status="active",
|
||||
// storage_quota default, retriever engines from RETRIEVE_DRIVER).
|
||||
// TenantService.CreateTenant (status="active", storage_quota
|
||||
// default, retriever engines from RETRIEVE_DRIVER). API keys are
|
||||
// created explicitly through the integration API-key list.
|
||||
var req createTenantRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(ctx, "Failed to parse request parameters", err)
|
||||
@@ -531,7 +558,19 @@ func (h *TenantHandler) ResetAPIKey(c *gin.Context) {
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Resetting API key for tenant, ID: %d", id)
|
||||
apiKey, err := h.service.UpdateAPIKey(ctx, id)
|
||||
if h.apiKeyService == nil {
|
||||
c.Error(errors.NewInternalServerError("API key service is not configured"))
|
||||
return
|
||||
}
|
||||
result, err := h.apiKeyService.CreateAPIKey(ctx, interfaces.TenantAPIKeyCreateRequest{
|
||||
TenantID: id,
|
||||
Name: "Tenant API key",
|
||||
Scopes: []string{
|
||||
types.TenantAPIKeyScopeRead,
|
||||
types.TenantAPIKeyScopeWrite,
|
||||
types.TenantAPIKeyScopeAdmin,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
if appErr, ok := errors.IsAppError(err); ok {
|
||||
logger.Error(ctx, "Failed to reset API key: application error", appErr)
|
||||
@@ -547,11 +586,142 @@ func (h *TenantHandler) ResetAPIKey(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"success": true,
|
||||
"data": gin.H{
|
||||
"api_key": apiKey,
|
||||
"api_key": result.APIKey.APIKey,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TenantHandler) ListAPIKeys(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("Invalid tenant ID"))
|
||||
return
|
||||
}
|
||||
keys, err := h.apiKeyService.ListAPIKeys(ctx, id)
|
||||
if err != nil {
|
||||
c.Error(errors.NewInternalServerError("Failed to list API keys").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
resp := make([]tenantAPIKeyResponse, 0, len(keys))
|
||||
for _, key := range keys {
|
||||
resp = append(resp, tenantAPIKeyForResponse(key))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": resp})
|
||||
}
|
||||
|
||||
func (h *TenantHandler) CreateAPIKey(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("Invalid tenant ID"))
|
||||
return
|
||||
}
|
||||
var req tenantAPIKeyCreateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := validateTenantAPIKeyRequest(ctx, h.kbService, id, req); err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
var expiresAt *time.Time
|
||||
if req.ExpiresAt != nil {
|
||||
t := time.Unix(*req.ExpiresAt, 0)
|
||||
if !t.After(time.Now()) {
|
||||
c.Error(errors.NewValidationError("expires_at_unix must be in the future"))
|
||||
return
|
||||
}
|
||||
expiresAt = &t
|
||||
}
|
||||
result, err := h.apiKeyService.CreateAPIKey(ctx, interfaces.TenantAPIKeyCreateRequest{
|
||||
TenantID: id,
|
||||
Name: req.Name,
|
||||
Scopes: req.Scopes,
|
||||
KnowledgeBaseIDs: req.KnowledgeBaseIDs,
|
||||
ExpiresAt: expiresAt,
|
||||
})
|
||||
if err != nil {
|
||||
c.Error(errors.NewInternalServerError("Failed to create API key").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"success": true,
|
||||
"data": tenantAPIKeyCreateResponse{
|
||||
tenantAPIKeyResponse: tenantAPIKeyForResponse(result.APIKey),
|
||||
Token: result.Token,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *TenantHandler) DeleteAPIKey(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
tenantID, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
c.Error(errors.NewBadRequestError("Invalid tenant ID"))
|
||||
return
|
||||
}
|
||||
keyID, err := strconv.ParseUint(c.Param("key_id"), 10, 64)
|
||||
if err != nil || keyID == 0 {
|
||||
c.Error(errors.NewBadRequestError("Invalid API key ID"))
|
||||
return
|
||||
}
|
||||
if err := h.apiKeyService.RevokeAPIKey(ctx, tenantID, keyID); err != nil {
|
||||
c.Error(errors.NewNotFoundError("API key not found"))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
func tenantAPIKeyForResponse(key *types.TenantAPIKey) tenantAPIKeyResponse {
|
||||
if key == nil {
|
||||
return tenantAPIKeyResponse{}
|
||||
}
|
||||
return tenantAPIKeyResponse{
|
||||
ID: key.ID,
|
||||
Name: key.Name,
|
||||
APIKey: key.APIKey,
|
||||
Scopes: key.Scopes,
|
||||
KnowledgeBaseIDs: key.KnowledgeBaseIDs,
|
||||
LastUsedAt: key.LastUsedAt,
|
||||
ExpiresAt: key.ExpiresAt,
|
||||
CreatedAt: key.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func validateTenantAPIKeyRequest(
|
||||
ctx context.Context,
|
||||
kbService interfaces.KnowledgeBaseService,
|
||||
tenantID uint64,
|
||||
req tenantAPIKeyCreateRequest,
|
||||
) *errors.AppError {
|
||||
if strings.TrimSpace(req.Name) == "" {
|
||||
return errors.NewValidationError("name is required")
|
||||
}
|
||||
for _, scope := range req.Scopes {
|
||||
switch strings.ToLower(strings.TrimSpace(scope)) {
|
||||
case types.TenantAPIKeyScopeRead, types.TenantAPIKeyScopeWrite, types.TenantAPIKeyScopeAdmin:
|
||||
default:
|
||||
return errors.NewValidationError("scopes must contain only read, write, or admin")
|
||||
}
|
||||
}
|
||||
for _, kbID := range req.KnowledgeBaseIDs {
|
||||
kbID = strings.TrimSpace(kbID)
|
||||
if kbID == "" {
|
||||
continue
|
||||
}
|
||||
kb, err := kbService.GetKnowledgeBaseByID(ctx, kbID)
|
||||
if err != nil || kb == nil {
|
||||
return errors.NewValidationError("knowledge_base_ids contains an unknown knowledge base")
|
||||
}
|
||||
if kb.TenantID != tenantID {
|
||||
return errors.NewForbiddenError("knowledge_base_ids contains a knowledge base outside this tenant")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func apiPrincipalConfigForResponse(cfg *types.APIPrincipalConfig) apiPrincipalConfigResponse {
|
||||
if cfg == nil {
|
||||
cfg = &types.APIPrincipalConfig{}
|
||||
|
||||
@@ -44,7 +44,7 @@ func (s *stubTenantService) SearchTenants(context.Context, string, uint64, int,
|
||||
return nil, 0, nil
|
||||
}
|
||||
func (s *stubTenantService) UpdateAPIKey(context.Context, uint64) (string, error) { return "", nil }
|
||||
func (s *stubTenantService) ExtractTenantIDFromAPIKey(string) (uint64, error) { return 0, nil }
|
||||
func (s *stubTenantService) ExtractTenantIDFromAPIKey(string) (uint64, error) { return 0, nil }
|
||||
func (s *stubTenantService) BulkSetStorageQuota(context.Context, int64) (int64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -127,8 +127,8 @@ func TestGetTenantKVAdminReturnsRedactedSecrets(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
var payload struct {
|
||||
Success bool `json:"success"`
|
||||
Data types.ParserEngineConfig `json:"data"`
|
||||
Success bool `json:"success"`
|
||||
Data types.ParserEngineConfig `json:"data"`
|
||||
}
|
||||
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload))
|
||||
assert.Equal(t, types.RedactedSecretPlaceholder, payload.Data.MinerUAPIKey)
|
||||
@@ -137,9 +137,8 @@ func TestGetTenantKVAdminReturnsRedactedSecrets(t *testing.T) {
|
||||
|
||||
func secretTenantFixture() *types.Tenant {
|
||||
return &types.Tenant{
|
||||
ID: 42,
|
||||
Name: "tenant",
|
||||
APIKey: "tenant-api-key-123",
|
||||
ID: 42,
|
||||
Name: "tenant",
|
||||
WebSearchConfig: &types.WebSearchConfig{
|
||||
APIKey: "legacy-search-secret-999",
|
||||
},
|
||||
@@ -169,7 +168,7 @@ func TestPutTenantParserConfigAdminPreservesRedactedSecrets(t *testing.T) {
|
||||
tenant := secretTenantFixture()
|
||||
engine := newTenantHandlerTestEngine(t, types.TenantRoleAdmin, tenant)
|
||||
|
||||
body := `{"mineru_api_key":"***","mineru_endpoint":"http://new-endpoint"}`
|
||||
body := `{"mineru_api_key":"***","mineru_endpoint":"https://example.com/mineru"}`
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodPut, "/tenants/kv/parser-engine-config", strings.NewReader(body))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
@@ -177,5 +176,5 @@ func TestPutTenantParserConfigAdminPreservesRedactedSecrets(t *testing.T) {
|
||||
require.Equal(t, http.StatusOK, rec.Code)
|
||||
require.NotNil(t, tenant.ParserEngineConfig)
|
||||
assert.Equal(t, "parser-secret-123", tenant.ParserEngineConfig.MinerUAPIKey)
|
||||
assert.Equal(t, "http://new-endpoint", tenant.ParserEngineConfig.MinerUEndpoint)
|
||||
assert.Equal(t, "https://example.com/mineru", tenant.ParserEngineConfig.MinerUEndpoint)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user