From 7644c7263c38869f0193f2988056879d5d3439c3 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein-Kousathana Date: Thu, 16 Jan 2025 07:29:22 +0100 Subject: [PATCH] Expose data source operations to Querier (#5310) --- pkg/querier/datasource.go | 94 +++++++++++++++++++++++++++++++++++++++ pkg/querier/querier.go | 26 ++++++----- pkg/querier/ruletype.go | 20 +++++++++ 3 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 pkg/querier/datasource.go diff --git a/pkg/querier/datasource.go b/pkg/querier/datasource.go new file mode 100644 index 0000000000..4a10ae8664 --- /dev/null +++ b/pkg/querier/datasource.go @@ -0,0 +1,94 @@ +// SPDX-FileCopyrightText: Copyright 2025 The Minder Authors +// SPDX-License-Identifier: Apache-2.0 + +// Package querier provides tools to interact with the Minder database +package querier + +import ( + "context" + + "github.com/google/uuid" + + dsservice "github.com/mindersec/minder/internal/datasources/service" + pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" +) + +// DataSourceHandlers interface provides functions to interact with data sources +type DataSourceHandlers interface { + CreateDataSource( + ctx context.Context, + projectID uuid.UUID, + subscriptionID uuid.UUID, + dataSource *pb.DataSource, + ) (*pb.DataSource, error) + GetDataSourceByName( + ctx context.Context, + projectID uuid.UUID, + name string, + ) (*pb.DataSource, error) + UpdateDataSource( + ctx context.Context, + projectID uuid.UUID, + subscriptionID uuid.UUID, + dataSource *pb.DataSource, + ) (*pb.DataSource, error) + DeleteDataSource( + ctx context.Context, + projectID uuid.UUID, + dataSourceID uuid.UUID, + ) error +} + +// CreateDataSource creates a data source +func (q *querierType) CreateDataSource( + ctx context.Context, + projectID uuid.UUID, + subscriptionID uuid.UUID, + dataSource *pb.DataSource, +) (*pb.DataSource, error) { + if q.querier == nil { + return nil, ErrQuerierMissing + } + if q.dataSourceSvc == nil { + return nil, ErrDataSourceSvcMissing + } + return q.dataSourceSvc.Create(ctx, projectID, subscriptionID, dataSource, dsservice.OptionsBuilder().WithTransaction(q.querier)) +} + +// UpdateDataSource updates a data source +func (q *querierType) UpdateDataSource( + ctx context.Context, + projectID uuid.UUID, + subscriptionID uuid.UUID, + dataSource *pb.DataSource, +) (*pb.DataSource, error) { + if q.querier == nil { + return nil, ErrQuerierMissing + } + if q.dataSourceSvc == nil { + return nil, ErrDataSourceSvcMissing + } + return q.dataSourceSvc.Update(ctx, projectID, subscriptionID, dataSource, dsservice.OptionsBuilder().WithTransaction(q.querier)) +} + +// GetDataSourceByName returns a data source by name and project IDs +func (q *querierType) GetDataSourceByName(ctx context.Context, projectID uuid.UUID, name string) (*pb.DataSource, error) { + if q.querier == nil { + return nil, ErrQuerierMissing + } + if q.dataSourceSvc == nil { + return nil, ErrDataSourceSvcMissing + } + return q.dataSourceSvc.GetByName(ctx, name, projectID, dsservice.ReadBuilder().WithTransaction(q.querier)) +} + +// DeleteDataSource deletes a data source +func (q *querierType) DeleteDataSource(ctx context.Context, projectID uuid.UUID, dataSourceID uuid.UUID) error { + if q.querier == nil { + return ErrQuerierMissing + } + if q.dataSourceSvc == nil { + return ErrDataSourceSvcMissing + } + return q.dataSourceSvc.Delete(ctx, projectID, dataSourceID, dsservice.OptionsBuilder().WithTransaction(q.querier)) +} diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go index 792ad6ede5..3971f9db31 100644 --- a/pkg/querier/querier.go +++ b/pkg/querier/querier.go @@ -13,6 +13,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + datasourceservice "github.com/mindersec/minder/internal/datasources/service" "github.com/mindersec/minder/internal/db" "github.com/mindersec/minder/pkg/config/server" "github.com/mindersec/minder/pkg/engine/selectors" @@ -28,6 +29,8 @@ var ( ErrProfileSvcMissing = fmt.Errorf("profile service is missing") // ErrRuleSvcMissing is returned when the rule service is not initialized ErrRuleSvcMissing = fmt.Errorf("rule service is missing") + // ErrDataSourceSvcMissing is returned when the data source service is not initialized + ErrDataSourceSvcMissing = fmt.Errorf("data source service is missing") ) // Store interface provides functions to execute db queries and transactions @@ -48,17 +51,19 @@ type Querier interface { RuleTypeHandlers ProfileHandlers BundleHandlers + DataSourceHandlers Commit() error Cancel() error } // querierType represents the database querier type querierType struct { - store db.Store - tx *sql.Tx - querier db.ExtendQuerier - ruleSvc ruletypes.RuleTypeService - profileSvc profiles.ProfileService + store db.Store + tx *sql.Tx + querier db.ExtendQuerier + ruleSvc ruletypes.RuleTypeService + profileSvc profiles.ProfileService + dataSourceSvc datasourceservice.DataSourcesService } // Closer is a function that closes the database connection @@ -95,11 +100,12 @@ func (q *querierType) BeginTx() (Querier, error) { return nil, status.Errorf(codes.Internal, "failed to begin transaction") } return &querierType{ - store: q.store, - querier: q.store.GetQuerierWithTransaction(tx), - ruleSvc: q.ruleSvc, - profileSvc: q.profileSvc, - tx: tx, + store: q.store, + querier: q.store.GetQuerierWithTransaction(tx), + ruleSvc: q.ruleSvc, + profileSvc: q.profileSvc, + dataSourceSvc: q.dataSourceSvc, + tx: tx, }, nil } diff --git a/pkg/querier/ruletype.go b/pkg/querier/ruletype.go index d247790018..0a54ad1c8d 100644 --- a/pkg/querier/ruletype.go +++ b/pkg/querier/ruletype.go @@ -18,6 +18,7 @@ import ( // RuleTypeHandlers interface provides functions to interact with ruletypes type RuleTypeHandlers interface { ListRuleTypesByProject(ctx context.Context, projectID uuid.UUID) ([]*pb.RuleType, error) + ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourceID uuid.UUID) ([]uuid.UUID, error) CreateRuleType(ctx context.Context, projectID uuid.UUID, subscriptionID uuid.UUID, ruleType *pb.RuleType) (*pb.RuleType, error) UpdateRuleType(ctx context.Context, projectID uuid.UUID, subscriptionID uuid.UUID, ruleType *pb.RuleType) (*pb.RuleType, error) DeleteRuleType(ctx context.Context, ruleTypeID uuid.UUID) error @@ -98,3 +99,22 @@ func (q *querierType) CreateRuleType( } return q.ruleSvc.CreateRuleType(ctx, projectID, subscriptionID, ruleType, q.querier) } + +// ListRuleTypesReferencesByDataSource returns a list of rule types using a data source +func (q *querierType) ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourceID uuid.UUID) ([]uuid.UUID, error) { + if q.querier == nil { + return nil, ErrQuerierMissing + } + ruleTypes, err := q.querier.ListRuleTypesReferencesByDataSource(ctx, dataSourceID) + if err != nil { + return nil, err + } + + // Convert ruleTypes to a slice of strings + ruleTypeIds := make([]uuid.UUID, len(ruleTypes)) + for i, r := range ruleTypes { + ruleTypeIds[i] = r.RuleTypeID + } + + return ruleTypeIds, nil +}