Skip to content

Commit

Permalink
Move ruletypes from internal to pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Radoslav Dimitrov <[email protected]>
  • Loading branch information
rdimitrov committed Oct 24, 2024
1 parent f27c512 commit 27f4d48
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 116 deletions.
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_evalstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_ruletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/handlers_ruletype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/controlplane/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/rtengine/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/marketplaces/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/marketplaces/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions internal/marketplaces/subscriptions/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/profiles/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 0 additions & 71 deletions internal/ruletypes/util.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
File renamed without changes.
31 changes: 0 additions & 31 deletions internal/ruletypes/service.go → pkg/ruletypes/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 103 additions & 0 deletions pkg/ruletypes/util.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 27f4d48

Please sign in to comment.