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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user