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 {
return nil, datasource.ErrKnowledgeBaseNotFound
}
if kb.TenantID != ds.TenantID {
return nil, datasource.ErrKnowledgeBaseNotFound
}
// Validate connector type
_, err = s.connectorRegistry.Get(ds.Type)
@@ -135,6 +138,20 @@ func (s *DataSourceService) UpdateDataSource(ctx context.Context, ds *types.Data
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
if ds.Type != existing.Type || string(ds.Config) != string(existing.Config) {
if err := s.validateDataSourceConfig(ctx, ds); err != nil {