Skip to content

Commit

Permalink
Add pkg/querier package (#4849)
Browse files Browse the repository at this point in the history
* Add pkg/querier package

Signed-off-by: Radoslav Dimitrov <[email protected]>

* Add a Querier interface per transaction

Signed-off-by: Radoslav Dimitrov <[email protected]>

* Include the rule and profile services in the querier

Signed-off-by: Radoslav Dimitrov <[email protected]>

* Use the correct set bundle version query for the subscriptions table

Signed-off-by: Radoslav Dimitrov <[email protected]>

* Cleanup querier after transaction commit or cancel

Signed-off-by: Radoslav Dimitrov <[email protected]>

* Refactor if statements and remove duplicated log message

Signed-off-by: Radoslav Dimitrov <[email protected]>

---------

Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov authored Nov 1, 2024
1 parent 3477898 commit dc6fce9
Show file tree
Hide file tree
Showing 12 changed files with 643 additions and 2 deletions.
44 changes: 44 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion database/query/projects.sql
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,13 @@ WHERE id = $1 RETURNING *;
-- name: UpdateProjectMeta :one
UPDATE projects
SET metadata = $2
WHERE id = $1 RETURNING *;
WHERE id = $1 RETURNING *;

-- name: ListAllRootProjects :many
SELECT * FROM projects
WHERE parent_id IS NULL AND is_organization = FALSE;

-- name: GetRootProjectByID :one
SELECT * FROM projects
WHERE id = $1
AND parent_id IS NULL AND is_organization = FALSE LIMIT 1;
3 changes: 3 additions & 0 deletions database/query/rule_instances.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ SELECT rule_type_id FROM rule_instances
WHERE name = $1
AND entity_type = $2
AND profile_id = $3;

-- name: DeleteRuleInstanceOfProfileInProject :exec
DELETE FROM rule_instances WHERE project_id = $1 AND profile_id = $2 AND rule_type_id = $3;
1 change: 0 additions & 1 deletion database/query/subscriptions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ WHERE bu.namespace = $1 AND bu.name = $2 AND su.project_id = $3;

-- name: SetSubscriptionBundleVersion :exec
UPDATE subscriptions SET current_version = $2 WHERE project_id = $1;

57 changes: 57 additions & 0 deletions internal/db/projects.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/db/rule_instances.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions pkg/querier/bundle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: Copyright 2024 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"

"github.com/mindersec/minder/internal/db"
)

// BundleSubscription represents a bundle subscription
type BundleSubscription struct {
ID uuid.UUID
ProjectID uuid.UUID
BundleID uuid.UUID
CurrentVersion string
}

// BundleHandlers interface provides functions to interact with bundles and subscriptions
type BundleHandlers interface {
GetSubscriptionByProjectBundle(
ctx context.Context,
projectID uuid.UUID,
bundleNamespace, bundleName string,
) (*BundleSubscription, error)
SetCurrentVersion(ctx context.Context, projectID uuid.UUID, currentVersion string) error
}

// SetCurrentVersion sets the current version of the bundle for a project
func (q *querierType) SetCurrentVersion(ctx context.Context, projectID uuid.UUID, currentVersion string) error {
if q.querier == nil {
return ErrQuerierMissing
}
return q.querier.SetSubscriptionBundleVersion(ctx, db.SetSubscriptionBundleVersionParams{
ProjectID: projectID,
CurrentVersion: currentVersion,
})
}

// GetSubscriptionByProjectBundle gets a subscription by project bundle
func (q *querierType) GetSubscriptionByProjectBundle(
ctx context.Context,
projectID uuid.UUID,
bundleNamespace, bundleName string,
) (*BundleSubscription, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
ret, err := q.querier.GetSubscriptionByProjectBundle(ctx, db.GetSubscriptionByProjectBundleParams{
Namespace: bundleNamespace,
Name: bundleName,
ProjectID: projectID,
})
if err != nil {
return nil, err
}
return &BundleSubscription{
ID: ret.ID,
ProjectID: ret.ProjectID,
BundleID: ret.BundleID,
CurrentVersion: ret.CurrentVersion,
}, nil
}
120 changes: 120 additions & 0 deletions pkg/querier/profile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// SPDX-FileCopyrightText: Copyright 2024 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"

"github.com/mindersec/minder/internal/db"
pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
"github.com/mindersec/minder/pkg/profiles"
)

// ProfileHandlers interface provides functions to interact with profiles
type ProfileHandlers interface {
CreateProfile(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
profile *pb.Profile,
) (*pb.Profile, error)
UpdateProfile(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
profile *pb.Profile,
) (*pb.Profile, error)
DeleteProfile(ctx context.Context, projectID uuid.UUID, profileID uuid.UUID) error
ListProfilesInstantiatingRuleType(ctx context.Context, ruleTypeID uuid.UUID) ([]string, error)
GetProfileByProjectAndName(ctx context.Context, projectID uuid.UUID, name string) (map[string]*pb.Profile, error)
DeleteRuleInstanceOfProfileInProject(ctx context.Context, projectID, profileID, ruleTypeID uuid.UUID) error
}

// DeleteProfile deletes a profile
func (q *querierType) DeleteProfile(ctx context.Context, projectID uuid.UUID, profileID uuid.UUID) error {
if q.querier == nil {
return ErrQuerierMissing
}
return q.querier.DeleteProfile(ctx, db.DeleteProfileParams{
ProjectID: projectID,
ID: profileID,
})
}

// UpdateProfile updates a profile
func (q *querierType) UpdateProfile(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
profile *pb.Profile,
) (*pb.Profile, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
if q.profileSvc == nil {
return nil, ErrProfileSvcMissing
}
return q.profileSvc.UpdateProfile(ctx, projectID, subscriptionID, profile, q.querier)
}

// CreateProfile creates a profile
func (q *querierType) CreateProfile(
ctx context.Context,
projectID uuid.UUID,
subscriptionID uuid.UUID,
profile *pb.Profile,
) (*pb.Profile, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
if q.profileSvc == nil {
return nil, ErrProfileSvcMissing
}
return q.profileSvc.CreateProfile(ctx, projectID, subscriptionID, profile, q.querier)
}

// ListProfilesInstantiatingRuleType returns a list of profiles instantiating a rule type
func (q *querierType) ListProfilesInstantiatingRuleType(ctx context.Context, ruleTypeID uuid.UUID) ([]string, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
return q.querier.ListProfilesInstantiatingRuleType(ctx, ruleTypeID)
}

// GetProfileByProjectAndName returns a profile by project ID and name
func (q *querierType) GetProfileByProjectAndName(
ctx context.Context,
projectID uuid.UUID,
name string,
) (map[string]*pb.Profile, error) {
if q.querier == nil {
return nil, ErrQuerierMissing
}
ret, err := q.querier.GetProfileByProjectAndName(ctx, db.GetProfileByProjectAndNameParams{
Name: name,
ProjectID: projectID,
})
if err != nil {
return nil, err
}
return profiles.MergeDatabaseGetByNameIntoProfiles(ret), nil
}

// DeleteRuleInstanceOfProfileInProject deletes a rule instance for a profile in a project
func (q *querierType) DeleteRuleInstanceOfProfileInProject(
ctx context.Context,
projectID, profileID, ruleTypeID uuid.UUID,
) error {
if q.querier == nil {
return ErrQuerierMissing
}
return q.querier.DeleteRuleInstanceOfProfileInProject(ctx, db.DeleteRuleInstanceOfProfileInProjectParams{
ProjectID: projectID,
ProfileID: profileID,
RuleTypeID: ruleTypeID,
})
}
Loading

0 comments on commit dc6fce9

Please sign in to comment.