diff --git a/internal/controlplane/handlers_evalstatus.go b/internal/controlplane/handlers_evalstatus.go index b241411db2..27726ce81b 100644 --- a/internal/controlplane/handlers_evalstatus.go +++ b/internal/controlplane/handlers_evalstatus.go @@ -24,9 +24,9 @@ import ( propSvc "github.com/mindersec/minder/internal/entities/properties/service" "github.com/mindersec/minder/internal/history" ghprop "github.com/mindersec/minder/internal/providers/github/properties" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" + "github.com/mindersec/minder/pkg/ruletypes" ) const ( diff --git a/internal/controlplane/handlers_profile.go b/internal/controlplane/handlers_profile.go index f5a7c93db0..e4f41af75f 100644 --- a/internal/controlplane/handlers_profile.go +++ b/internal/controlplane/handlers_profile.go @@ -26,9 +26,9 @@ import ( "github.com/mindersec/minder/internal/logger" prof "github.com/mindersec/minder/internal/profiles" ghprop "github.com/mindersec/minder/internal/providers/github/properties" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" + "github.com/mindersec/minder/pkg/ruletypes" ) // CreateProfile creates a profile for a project diff --git a/internal/controlplane/handlers_ruletype.go b/internal/controlplane/handlers_ruletype.go index 004d714c63..15267853b8 100644 --- a/internal/controlplane/handlers_ruletype.go +++ b/internal/controlplane/handlers_ruletype.go @@ -24,9 +24,9 @@ import ( "github.com/mindersec/minder/internal/db" "github.com/mindersec/minder/internal/engine/engcontext" "github.com/mindersec/minder/internal/logger" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" + "github.com/mindersec/minder/pkg/ruletypes" ) var ( diff --git a/internal/controlplane/handlers_ruletype_test.go b/internal/controlplane/handlers_ruletype_test.go index acefebfbd9..f36facbab5 100644 --- a/internal/controlplane/handlers_ruletype_test.go +++ b/internal/controlplane/handlers_ruletype_test.go @@ -15,8 +15,8 @@ import ( mockdb "github.com/mindersec/minder/database/mock" df "github.com/mindersec/minder/database/mock/fixtures" db "github.com/mindersec/minder/internal/db" - sf "github.com/mindersec/minder/internal/ruletypes/mock/fixtures" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" + sf "github.com/mindersec/minder/pkg/ruletypes/mock/fixtures" ) func TestCreateRuleType(t *testing.T) { diff --git a/internal/controlplane/server.go b/internal/controlplane/server.go index 75cb8143bf..e74e78c607 100644 --- a/internal/controlplane/server.go +++ b/internal/controlplane/server.go @@ -61,10 +61,10 @@ import ( "github.com/mindersec/minder/internal/providers/session" reposvc "github.com/mindersec/minder/internal/repositories" "github.com/mindersec/minder/internal/roles" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util" pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" serverconfig "github.com/mindersec/minder/pkg/config/server" + "github.com/mindersec/minder/pkg/ruletypes" ) const metricsPath = "/metrics" diff --git a/internal/engine/rtengine/cache.go b/internal/engine/rtengine/cache.go index 3c67690e9d..6023c6aad2 100644 --- a/internal/engine/rtengine/cache.go +++ b/internal/engine/rtengine/cache.go @@ -14,9 +14,9 @@ import ( "github.com/mindersec/minder/internal/db" "github.com/mindersec/minder/internal/engine/ingestcache" eoptions "github.com/mindersec/minder/internal/engine/options" - "github.com/mindersec/minder/internal/ruletypes" rtengine2 "github.com/mindersec/minder/pkg/engine/v1/rtengine" provinfv1 "github.com/mindersec/minder/pkg/providers/v1" + "github.com/mindersec/minder/pkg/ruletypes" ) // Cache contains a set of RuleTypeEngine instances diff --git a/internal/marketplaces/factory.go b/internal/marketplaces/factory.go index 05dc51a16f..aa2e33b723 100644 --- a/internal/marketplaces/factory.go +++ b/internal/marketplaces/factory.go @@ -10,10 +10,10 @@ import ( sub "github.com/mindersec/minder/internal/marketplaces/subscriptions" "github.com/mindersec/minder/internal/profiles" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/pkg/config/server" "github.com/mindersec/minder/pkg/mindpak" src "github.com/mindersec/minder/pkg/mindpak/sources" + "github.com/mindersec/minder/pkg/ruletypes" ) // NewMarketplaceFromServiceConfig takes the Minder service config and diff --git a/internal/marketplaces/subscriptions/service.go b/internal/marketplaces/subscriptions/service.go index c920e19e72..87592826a2 100644 --- a/internal/marketplaces/subscriptions/service.go +++ b/internal/marketplaces/subscriptions/service.go @@ -16,10 +16,10 @@ import ( "github.com/mindersec/minder/internal/db" profsvc "github.com/mindersec/minder/internal/profiles" - "github.com/mindersec/minder/internal/ruletypes" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" "github.com/mindersec/minder/pkg/mindpak" "github.com/mindersec/minder/pkg/mindpak/reader" + "github.com/mindersec/minder/pkg/ruletypes" ) //go:generate go run go.uber.org/mock/mockgen -package mock_$GOPACKAGE -destination=./mock/$GOFILE -source=./$GOFILE diff --git a/internal/marketplaces/subscriptions/service_test.go b/internal/marketplaces/subscriptions/service_test.go index 01eb3e5b5e..ddf733730b 100644 --- a/internal/marketplaces/subscriptions/service_test.go +++ b/internal/marketplaces/subscriptions/service_test.go @@ -19,9 +19,9 @@ import ( "github.com/mindersec/minder/internal/marketplaces/subscriptions" "github.com/mindersec/minder/internal/profiles" psf "github.com/mindersec/minder/internal/profiles/mock/fixtures" - "github.com/mindersec/minder/internal/ruletypes" - rsf "github.com/mindersec/minder/internal/ruletypes/mock/fixtures" "github.com/mindersec/minder/pkg/mindpak/reader" + "github.com/mindersec/minder/pkg/ruletypes" + rsf "github.com/mindersec/minder/pkg/ruletypes/mock/fixtures" ) func TestSubscriptionService_Subscribe(t *testing.T) { diff --git a/internal/profiles/validator.go b/internal/profiles/validator.go index a6f268245b..5265b856e8 100644 --- a/internal/profiles/validator.go +++ b/internal/profiles/validator.go @@ -17,10 +17,10 @@ import ( "k8s.io/apimachinery/pkg/util/sets" "github.com/mindersec/minder/internal/db" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util" minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" "github.com/mindersec/minder/pkg/engine/selectors" + "github.com/mindersec/minder/pkg/ruletypes" ) // Validator encapsulates the logic for validating profiles diff --git a/internal/ruletypes/util.go b/internal/ruletypes/util.go deleted file mode 100644 index fab420930f..0000000000 --- a/internal/ruletypes/util.go +++ /dev/null @@ -1,71 +0,0 @@ -// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors -// SPDX-License-Identifier: Apache-2.0 - -package ruletypes - -import ( - "fmt" - - "google.golang.org/protobuf/encoding/protojson" - - "github.com/mindersec/minder/internal/db" - minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" -) - -// RuleDefFromDB converts a rule type definition from the database to a protobuf -// rule type definition -func RuleDefFromDB(r *db.RuleType) (*minderv1.RuleType_Definition, error) { - def := &minderv1.RuleType_Definition{} - - if err := protojson.Unmarshal(r.Definition, def); err != nil { - return nil, fmt.Errorf("cannot unmarshal rule type definition: %w", err) - } - return def, nil -} - -// RuleTypePBFromDB converts a rule type from the database to a protobuf -// rule type -func RuleTypePBFromDB(rt *db.RuleType) (*minderv1.RuleType, error) { - def, err := RuleDefFromDB(rt) - if err != nil { - return nil, fmt.Errorf("cannot get rule type definition: %w", err) - } - - id := rt.ID.String() - project := rt.ProjectID.String() - - var seval minderv1.Severity_Value - if err := seval.FromString(string(rt.SeverityValue)); err != nil { - seval = minderv1.Severity_VALUE_UNKNOWN - } - - displayName := rt.DisplayName - if displayName == "" { - displayName = rt.Name - } - - var releasePhase minderv1.RuleTypeReleasePhase - if err := releasePhase.FromString(string(rt.ReleasePhase)); err != nil { - releasePhase = minderv1.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED - } - - // TODO: (2024/03/28) this is for compatibility with old CLI versions that expect provider, remove this eventually - noProvider := "" - return &minderv1.RuleType{ - Id: &id, - Name: rt.Name, - DisplayName: displayName, - ShortFailureMessage: rt.ShortFailureMessage, - Context: &minderv1.Context{ - Provider: &noProvider, - Project: &project, - }, - Description: rt.Description, - Guidance: rt.Guidance, - Def: def, - Severity: &minderv1.Severity{ - Value: seval, - }, - ReleasePhase: releasePhase, - }, nil -} diff --git a/internal/service/service.go b/internal/service/service.go index 347ea200eb..2cd62d3379 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -49,9 +49,9 @@ import ( "github.com/mindersec/minder/internal/reminderprocessor" "github.com/mindersec/minder/internal/repositories" "github.com/mindersec/minder/internal/roles" - "github.com/mindersec/minder/internal/ruletypes" serverconfig "github.com/mindersec/minder/pkg/config/server" "github.com/mindersec/minder/pkg/engine/selectors" + "github.com/mindersec/minder/pkg/ruletypes" ) // AllInOneServerService is a helper function that starts the gRPC and HTTP servers, diff --git a/internal/ruletypes/mock/fixtures/service.go b/pkg/ruletypes/mock/fixtures/service.go similarity index 95% rename from internal/ruletypes/mock/fixtures/service.go rename to pkg/ruletypes/mock/fixtures/service.go index ac84042ea6..1d759acdca 100644 --- a/internal/ruletypes/mock/fixtures/service.go +++ b/pkg/ruletypes/mock/fixtures/service.go @@ -10,7 +10,7 @@ package fixtures import ( "errors" - mockrulesvc "github.com/mindersec/minder/internal/ruletypes/mock" + mockrulesvc "github.com/mindersec/minder/pkg/ruletypes/mock" "go.uber.org/mock/gomock" ) diff --git a/internal/ruletypes/mock/service.go b/pkg/ruletypes/mock/service.go similarity index 100% rename from internal/ruletypes/mock/service.go rename to pkg/ruletypes/mock/service.go diff --git a/internal/ruletypes/service.go b/pkg/ruletypes/service.go similarity index 90% rename from internal/ruletypes/service.go rename to pkg/ruletypes/service.go index 1103b90271..93666e2ad9 100644 --- a/internal/ruletypes/service.go +++ b/pkg/ruletypes/service.go @@ -300,34 +300,3 @@ func validateRuleUpdate(existingRecord *db.RuleType, newRuleType *pb.RuleType) e return nil } - -// GetDBReleaseStatusFromPBReleasePhase converts a protobuf release phase to a database release status -func GetDBReleaseStatusFromPBReleasePhase(in pb.RuleTypeReleasePhase) (*db.ReleaseStatus, error) { - sev, err := in.InitializedStringValue() - if err != nil { - return nil, errors.Join(ErrRuleTypeInvalid, err) - } - var rel db.ReleaseStatus - - if err := rel.Scan(sev); err != nil { - // errors from the `Scan` method appear to be caused entirely by bad - // input - return nil, errors.Join(ErrRuleTypeInvalid, err) - } - - return &rel, nil -} - -// GetPBReleasePhaseFromDBReleaseStatus converts a database release status to a protobuf release phase -func GetPBReleasePhaseFromDBReleaseStatus(s *db.ReleaseStatus) (pb.RuleTypeReleasePhase, error) { - if s == nil { - return pb.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED, nil - } - - var rel pb.RuleTypeReleasePhase - if err := rel.FromString(string(*s)); err != nil { - return pb.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED, err - } - - return rel, nil -} diff --git a/internal/ruletypes/service_test.go b/pkg/ruletypes/service_test.go similarity index 99% rename from internal/ruletypes/service_test.go rename to pkg/ruletypes/service_test.go index e5f8297077..9f190c5763 100644 --- a/internal/ruletypes/service_test.go +++ b/pkg/ruletypes/service_test.go @@ -17,9 +17,9 @@ import ( "github.com/mindersec/minder/internal/db" dbf "github.com/mindersec/minder/internal/db/fixtures" - "github.com/mindersec/minder/internal/ruletypes" "github.com/mindersec/minder/internal/util/ptr" pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" + "github.com/mindersec/minder/pkg/ruletypes" ) // both create and update are bundled together since the testing harness is diff --git a/pkg/ruletypes/util.go b/pkg/ruletypes/util.go new file mode 100644 index 0000000000..d24899d448 --- /dev/null +++ b/pkg/ruletypes/util.go @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: Copyright 2024 The Minder Authors +// SPDX-License-Identifier: Apache-2.0 + +package ruletypes + +import ( + "errors" + "fmt" + + "google.golang.org/protobuf/encoding/protojson" + + "github.com/mindersec/minder/internal/db" + pb "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1" +) + +// RuleDefFromDB converts a rule type definition from the database to a protobuf +// rule type definition +func RuleDefFromDB(r *db.RuleType) (*pb.RuleType_Definition, error) { + def := &pb.RuleType_Definition{} + + if err := protojson.Unmarshal(r.Definition, def); err != nil { + return nil, fmt.Errorf("cannot unmarshal rule type definition: %w", err) + } + return def, nil +} + +// RuleTypePBFromDB converts a rule type from the database to a protobuf +// rule type +func RuleTypePBFromDB(rt *db.RuleType) (*pb.RuleType, error) { + def, err := RuleDefFromDB(rt) + if err != nil { + return nil, fmt.Errorf("cannot get rule type definition: %w", err) + } + + id := rt.ID.String() + project := rt.ProjectID.String() + + var seval pb.Severity_Value + if err := seval.FromString(string(rt.SeverityValue)); err != nil { + seval = pb.Severity_VALUE_UNKNOWN + } + + displayName := rt.DisplayName + if displayName == "" { + displayName = rt.Name + } + + var releasePhase pb.RuleTypeReleasePhase + if err := releasePhase.FromString(string(rt.ReleasePhase)); err != nil { + releasePhase = pb.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED + } + + // TODO: (2024/03/28) this is for compatibility with old CLI versions that expect provider, remove this eventually + noProvider := "" + return &pb.RuleType{ + Id: &id, + Name: rt.Name, + DisplayName: displayName, + ShortFailureMessage: rt.ShortFailureMessage, + Context: &pb.Context{ + Provider: &noProvider, + Project: &project, + }, + Description: rt.Description, + Guidance: rt.Guidance, + Def: def, + Severity: &pb.Severity{ + Value: seval, + }, + ReleasePhase: releasePhase, + }, nil +} + +// GetDBReleaseStatusFromPBReleasePhase converts a protobuf release phase to a database release status +func GetDBReleaseStatusFromPBReleasePhase(in pb.RuleTypeReleasePhase) (*db.ReleaseStatus, error) { + sev, err := in.InitializedStringValue() + if err != nil { + return nil, errors.Join(ErrRuleTypeInvalid, err) + } + var rel db.ReleaseStatus + + if err := rel.Scan(sev); err != nil { + // errors from the `Scan` method appear to be caused entirely by bad + // input + return nil, errors.Join(ErrRuleTypeInvalid, err) + } + + return &rel, nil +} + +// GetPBReleasePhaseFromDBReleaseStatus converts a database release status to a protobuf release phase +func GetPBReleasePhaseFromDBReleaseStatus(s *db.ReleaseStatus) (pb.RuleTypeReleasePhase, error) { + if s == nil { + return pb.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED, nil + } + + var rel pb.RuleTypeReleasePhase + if err := rel.FromString(string(*s)); err != nil { + return pb.RuleTypeReleasePhase_RULE_TYPE_RELEASE_PHASE_UNSPECIFIED, err + } + + return rel, nil +}