From df1593b5286c9e938959e781405022343ceb8f25 Mon Sep 17 00:00:00 2001 From: Eleftheria Stein-Kousathana Date: Wed, 15 Jan 2025 12:00:24 +0100 Subject: [PATCH] Require project ID when listing data source rules --- database/mock/store.go | 8 +++--- database/query/datasources.sql | 2 +- internal/datasources/service/service.go | 5 +++- internal/datasources/service/service_test.go | 30 ++++++++++++++++---- internal/db/datasources.sql.go | 11 +++++-- internal/db/querier.go | 2 +- 6 files changed, 42 insertions(+), 16 deletions(-) diff --git a/database/mock/store.go b/database/mock/store.go index 2549133859..87343649c6 100644 --- a/database/mock/store.go +++ b/database/mock/store.go @@ -2242,18 +2242,18 @@ func (mr *MockStoreMockRecorder) ListRuleTypesByProject(ctx, projectID any) *gom } // ListRuleTypesReferencesByDataSource mocks base method. -func (m *MockStore) ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourcesID uuid.UUID) ([]db.RuleTypeDataSource, error) { +func (m *MockStore) ListRuleTypesReferencesByDataSource(ctx context.Context, arg db.ListRuleTypesReferencesByDataSourceParams) ([]db.RuleTypeDataSource, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListRuleTypesReferencesByDataSource", ctx, dataSourcesID) + ret := m.ctrl.Call(m, "ListRuleTypesReferencesByDataSource", ctx, arg) ret0, _ := ret[0].([]db.RuleTypeDataSource) ret1, _ := ret[1].(error) return ret0, ret1 } // ListRuleTypesReferencesByDataSource indicates an expected call of ListRuleTypesReferencesByDataSource. -func (mr *MockStoreMockRecorder) ListRuleTypesReferencesByDataSource(ctx, dataSourcesID any) *gomock.Call { +func (mr *MockStoreMockRecorder) ListRuleTypesReferencesByDataSource(ctx, arg any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRuleTypesReferencesByDataSource", reflect.TypeOf((*MockStore)(nil).ListRuleTypesReferencesByDataSource), ctx, dataSourcesID) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListRuleTypesReferencesByDataSource", reflect.TypeOf((*MockStore)(nil).ListRuleTypesReferencesByDataSource), ctx, arg) } // ListTokensToMigrate mocks base method. diff --git a/database/query/datasources.sql b/database/query/datasources.sql index c1480faaa2..39e1bdef9c 100644 --- a/database/query/datasources.sql +++ b/database/query/datasources.sql @@ -84,7 +84,7 @@ WHERE data_source_id = $1 AND project_id = $2; -- -- name: ListRuleTypesReferencesByDataSource :many SELECT * FROM rule_type_data_sources -WHERE data_sources_id = $1; +WHERE data_sources_id = $1 and project_id = $2; -- AddRuleTypeDataSourceReference adds a link between one rule type -- and one data source it uses. diff --git a/internal/datasources/service/service.go b/internal/datasources/service/service.go index c2116b19cf..ab2d74ed58 100644 --- a/internal/datasources/service/service.go +++ b/internal/datasources/service/service.go @@ -366,7 +366,10 @@ func (d *dataSourceService) Delete( tx := stx.Q() // List rule types referencing the data source - ret, err := tx.ListRuleTypesReferencesByDataSource(ctx, id) + ret, err := tx.ListRuleTypesReferencesByDataSource(ctx, db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: id, + ProjectID: project, + }) if err != nil { return fmt.Errorf("failed to list rule types referencing data source %s: %w", id, err) } diff --git a/internal/datasources/service/service_test.go b/internal/datasources/service/service_test.go index e7d1fdf88e..129873ca58 100644 --- a/internal/datasources/service/service_test.go +++ b/internal/datasources/service/service_test.go @@ -921,7 +921,10 @@ func TestDelete(t *testing.T) { setup: func(args args, mockDB *mockdb.MockStore) { // Mock ListRuleTypesReferencesByDataSource to return empty list mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return([]db.RuleTypeDataSource{}, nil) mockDB.EXPECT(). @@ -951,7 +954,10 @@ func TestDelete(t *testing.T) { setup: func(args args, mockDB *mockdb.MockStore) { // Mock ListRuleTypesReferencesByDataSource to return empty list mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return([]db.RuleTypeDataSource{}, nil) mockDB.EXPECT(). @@ -981,7 +987,10 @@ func TestDelete(t *testing.T) { setup: func(args args, mockDB *mockdb.MockStore) { // Mock ListRuleTypesReferencesByDataSource to return non-empty list mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return([]db.RuleTypeDataSource{ {RuleTypeID: uuid.New()}, }, nil) @@ -997,7 +1006,10 @@ func TestDelete(t *testing.T) { }, setup: func(args args, mockDB *mockdb.MockStore) { mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return([]db.RuleTypeDataSource{}, nil) mockDB.EXPECT(). GetDataSource(gomock.Any(), gomock.Any()). @@ -1018,7 +1030,10 @@ func TestDelete(t *testing.T) { setup: func(args args, mockDB *mockdb.MockStore) { // Mock ListRuleTypesReferencesByDataSource to return an error mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return(nil, fmt.Errorf("database error")) }, wantErr: true, @@ -1033,7 +1048,10 @@ func TestDelete(t *testing.T) { setup: func(args args, mockDB *mockdb.MockStore) { // Mock ListRuleTypesReferencesByDataSource to return empty list mockDB.EXPECT(). - ListRuleTypesReferencesByDataSource(gomock.Any(), args.id). + ListRuleTypesReferencesByDataSource(gomock.Any(), db.ListRuleTypesReferencesByDataSourceParams{ + DataSourcesID: args.id, + ProjectID: args.project, + }). Return([]db.RuleTypeDataSource{}, nil) mockDB.EXPECT(). GetDataSource(gomock.Any(), gomock.Any()). diff --git a/internal/db/datasources.sql.go b/internal/db/datasources.sql.go index 2b0656510f..6bf15da643 100644 --- a/internal/db/datasources.sql.go +++ b/internal/db/datasources.sql.go @@ -369,13 +369,18 @@ func (q *Queries) ListDataSources(ctx context.Context, projects []uuid.UUID) ([] const listRuleTypesReferencesByDataSource = `-- name: ListRuleTypesReferencesByDataSource :many SELECT rule_type_id, data_sources_id, project_id FROM rule_type_data_sources -WHERE data_sources_id = $1 +WHERE data_sources_id = $1 and project_id = $2 ` +type ListRuleTypesReferencesByDataSourceParams struct { + DataSourcesID uuid.UUID `json:"data_sources_id"` + ProjectID uuid.UUID `json:"project_id"` +} + // ListRuleTypesReferencesByDataSource retrieves all rule types // referencing a given data source in a given project. -func (q *Queries) ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourcesID uuid.UUID) ([]RuleTypeDataSource, error) { - rows, err := q.db.QueryContext(ctx, listRuleTypesReferencesByDataSource, dataSourcesID) +func (q *Queries) ListRuleTypesReferencesByDataSource(ctx context.Context, arg ListRuleTypesReferencesByDataSourceParams) ([]RuleTypeDataSource, error) { + rows, err := q.db.QueryContext(ctx, listRuleTypesReferencesByDataSource, arg.DataSourcesID, arg.ProjectID) if err != nil { return nil, err } diff --git a/internal/db/querier.go b/internal/db/querier.go index e48d845829..aeb2fe8b58 100644 --- a/internal/db/querier.go +++ b/internal/db/querier.go @@ -236,7 +236,7 @@ type Querier interface { // ListRuleTypesReferencesByDataSource retrieves all rule types // referencing a given data source in a given project. // - ListRuleTypesReferencesByDataSource(ctx context.Context, dataSourcesID uuid.UUID) ([]RuleTypeDataSource, error) + ListRuleTypesReferencesByDataSource(ctx context.Context, arg ListRuleTypesReferencesByDataSourceParams) ([]RuleTypeDataSource, error) // When doing a key/algorithm rotation, identify the secrets which need to be // rotated. The criteria for rotation are: // 1) The encrypted_access_token is NULL (this should be removed when we make