feat(datasource): enforce tenant isolation and knowledge base ownership

- Added checks to ensure that the tenant ID matches between data sources and knowledge bases during creation and updates.
- Implemented ownership verification for data sources and knowledge bases in the DataSourceHandler, enhancing security and access control.
- Refactored data source retrieval methods to centralize ownership validation, improving code maintainability and clarity.
- Updated error handling to provide more informative responses when access is denied or when knowledge bases are not found.
This commit is contained in:
wizardchen
2026-03-30 19:51:28 +08:00
committed by lyingbug
parent 6a5a46cfc3
commit 442c340e7f
2 changed files with 113 additions and 89 deletions
@@ -66,6 +66,9 @@ func (s *DataSourceService) CreateDataSource(ctx context.Context, ds *types.Data
if err != nil || kb == nil { if err != nil || kb == nil {
return nil, datasource.ErrKnowledgeBaseNotFound return nil, datasource.ErrKnowledgeBaseNotFound
} }
if kb.TenantID != ds.TenantID {
return nil, datasource.ErrKnowledgeBaseNotFound
}
// Validate connector type // Validate connector type
_, err = s.connectorRegistry.Get(ds.Type) _, err = s.connectorRegistry.Get(ds.Type)
@@ -135,6 +138,20 @@ func (s *DataSourceService) UpdateDataSource(ctx context.Context, ds *types.Data
return nil, err return nil, err
} }
if ds.KnowledgeBaseID == "" {
ds.KnowledgeBaseID = existing.KnowledgeBaseID
}
if ds.KnowledgeBaseID != existing.KnowledgeBaseID {
return nil, fmt.Errorf("changing knowledge base is not allowed")
}
if ds.TenantID == 0 {
ds.TenantID = existing.TenantID
}
if ds.TenantID != existing.TenantID {
return nil, datasource.ErrDataSourceInvalid
}
// Validate new configuration if changed // Validate new configuration if changed
if ds.Type != existing.Type || string(ds.Config) != string(existing.Config) { if ds.Type != existing.Type || string(ds.Config) != string(existing.Config) {
if err := s.validateDataSourceConfig(ctx, ds); err != nil { if err := s.validateDataSourceConfig(ctx, ds); err != nil {
+94 -87
View File
@@ -1,6 +1,7 @@
package handler package handler
import ( import (
"context"
"net/http" "net/http"
"strconv" "strconv"
@@ -12,13 +13,18 @@ import (
// DataSourceHandler handles HTTP requests for data source management // DataSourceHandler handles HTTP requests for data source management
type DataSourceHandler struct { type DataSourceHandler struct {
service interfaces.DataSourceService service interfaces.DataSourceService
kbService interfaces.KnowledgeBaseService
} }
// NewDataSourceHandler creates a new data source handler // NewDataSourceHandler creates a new data source handler
func NewDataSourceHandler(service interfaces.DataSourceService) *DataSourceHandler { func NewDataSourceHandler(
service interfaces.DataSourceService,
kbService interfaces.KnowledgeBaseService,
) *DataSourceHandler {
return &DataSourceHandler{ return &DataSourceHandler{
service: service, service: service,
kbService: kbService,
} }
} }
@@ -29,6 +35,45 @@ func (h *DataSourceHandler) getTenantID(c *gin.Context) uint64 {
return tenantID return tenantID
} }
// Data source settings contain connector credentials, so only the owning tenant can access them.
func (h *DataSourceHandler) getOwnedKnowledgeBase(
ctx context.Context,
tenantID uint64,
kbID string,
) (*types.KnowledgeBase, int, string) {
if kbID == "" {
return nil, http.StatusBadRequest, "kb_id is required"
}
kb, err := h.kbService.GetKnowledgeBaseByID(ctx, kbID)
if err != nil || kb == nil {
return nil, http.StatusNotFound, "knowledge base not found"
}
if kb.TenantID != tenantID {
return nil, http.StatusForbidden, "access denied"
}
return kb, http.StatusOK, ""
}
func (h *DataSourceHandler) getOwnedDataSource(
ctx context.Context,
tenantID uint64,
id string,
) (*types.DataSource, int, string) {
ds, err := h.service.GetDataSource(ctx, id)
if err != nil {
return nil, http.StatusNotFound, "data source not found"
}
if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, ds.KnowledgeBaseID); status != http.StatusOK {
return nil, status, msg
}
return ds, http.StatusOK, ""
}
// CreateDataSource godoc // CreateDataSource godoc
// @Summary Create a new data source // @Summary Create a new data source
// @Description Create a new data source configuration for a knowledge base // @Description Create a new data source configuration for a knowledge base
@@ -55,6 +100,11 @@ func (h *DataSourceHandler) CreateDataSource(c *gin.Context) {
return return
} }
if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, req.KnowledgeBaseID); status != http.StatusOK {
c.JSON(status, gin.H{"error": msg})
return
}
// Enforce tenant isolation // Enforce tenant isolation
req.TenantID = tenantID req.TenantID = tenantID
@@ -86,15 +136,9 @@ func (h *DataSourceHandler) GetDataSource(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
ds, err := h.service.GetDataSource(ctx, id) ds, status, msg := h.getOwnedDataSource(ctx, tenantID, id)
if err != nil { if status != http.StatusOK {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"}) c.JSON(status, gin.H{"error": msg})
return
}
// Verify ownership (data source belongs to this tenant)
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -121,8 +165,8 @@ func (h *DataSourceHandler) ListDataSources(c *gin.Context) {
} }
kbID := c.Query("kb_id") kbID := c.Query("kb_id")
if kbID == "" { if _, status, msg := h.getOwnedKnowledgeBase(ctx, tenantID, kbID); status != http.StatusOK {
c.JSON(http.StatusBadRequest, gin.H{"error": "kb_id is required"}) c.JSON(status, gin.H{"error": msg})
return return
} }
@@ -165,20 +209,15 @@ func (h *DataSourceHandler) UpdateDataSource(c *gin.Context) {
return return
} }
// Fetch existing data source to verify ownership existing, status, msg := h.getOwnedDataSource(ctx, tenantID, id)
existing, err := h.service.GetDataSource(ctx, id) if status != http.StatusOK {
if err != nil { c.JSON(status, gin.H{"error": msg})
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if existing.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
req.ID = id req.ID = id
req.TenantID = tenantID req.TenantID = existing.TenantID
req.KnowledgeBaseID = existing.KnowledgeBaseID
ds, err := h.service.UpdateDataSource(ctx, &req) ds, err := h.service.UpdateDataSource(ctx, &req)
if err != nil { if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
@@ -206,15 +245,8 @@ func (h *DataSourceHandler) DeleteDataSource(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership before deleting if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -244,15 +276,8 @@ func (h *DataSourceHandler) ValidateConnection(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -280,6 +305,11 @@ func (h *DataSourceHandler) ValidateConnection(c *gin.Context) {
// @Router /api/v1/datasource/validate-credentials [post] // @Router /api/v1/datasource/validate-credentials [post]
func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) { func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) {
ctx := c.Request.Context() ctx := c.Request.Context()
tenantID := h.getTenantID(c)
if tenantID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
var req struct { var req struct {
Type string `json:"type" binding:"required"` Type string `json:"type" binding:"required"`
@@ -297,6 +327,7 @@ func (h *DataSourceHandler) ValidateCredentials(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "connected"}) c.JSON(http.StatusOK, gin.H{"status": "connected"})
} }
// @Summary List available resources in data source // @Summary List available resources in data source
// @Description List resources available for sync in the external system // @Description List resources available for sync in the external system
// @Tags DataSource // @Tags DataSource
@@ -315,15 +346,8 @@ func (h *DataSourceHandler) ListAvailableResources(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -357,15 +381,8 @@ func (h *DataSourceHandler) ManualSync(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -396,15 +413,8 @@ func (h *DataSourceHandler) PauseDataSource(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -434,15 +444,8 @@ func (h *DataSourceHandler) ResumeDataSource(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -475,15 +478,8 @@ func (h *DataSourceHandler) GetSyncLogs(c *gin.Context) {
id := c.Param("id") id := c.Param("id")
// Verify ownership if _, status, msg := h.getOwnedDataSource(ctx, tenantID, id); status != http.StatusOK {
ds, err := h.service.GetDataSource(ctx, id) c.JSON(status, gin.H{"error": msg})
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "data source not found"})
return
}
if ds.TenantID != tenantID {
c.JSON(http.StatusForbidden, gin.H{"error": "access denied"})
return return
} }
@@ -525,6 +521,12 @@ func (h *DataSourceHandler) GetSyncLogs(c *gin.Context) {
// @Router /api/v1/datasource/logs/{log_id} [get] // @Router /api/v1/datasource/logs/{log_id} [get]
func (h *DataSourceHandler) GetSyncLog(c *gin.Context) { func (h *DataSourceHandler) GetSyncLog(c *gin.Context) {
ctx := c.Request.Context() ctx := c.Request.Context()
tenantID := h.getTenantID(c)
if tenantID == 0 {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return
}
logID := c.Param("log_id") logID := c.Param("log_id")
log, err := h.service.GetSyncLog(ctx, logID) log, err := h.service.GetSyncLog(ctx, logID)
@@ -533,6 +535,11 @@ func (h *DataSourceHandler) GetSyncLog(c *gin.Context) {
return return
} }
if _, status, msg := h.getOwnedDataSource(ctx, tenantID, log.DataSourceID); status != http.StatusOK {
c.JSON(status, gin.H{"error": msg})
return
}
c.JSON(http.StatusOK, log) c.JSON(http.StatusOK, log)
} }