Skip to content

Commit

Permalink
Move EngineContext into a subpackage
Browse files Browse the repository at this point in the history
This avoids a circular dependency when importing the `flags` package
into `engine`
  • Loading branch information
dmjb committed Jun 24, 2024
1 parent ef45f63 commit ab5b0c0
Show file tree
Hide file tree
Showing 21 changed files with 165 additions and 164 deletions.
8 changes: 4 additions & 4 deletions internal/controlplane/handlers_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"database/sql"
"errors"
"fmt"
context2 "github.com/stacklok/minder/internal/engine/ectx"
"slices"
"strings"

Expand All @@ -28,7 +29,6 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/util"
"github.com/stacklok/minder/internal/util/ptr"
Expand All @@ -38,7 +38,7 @@ import (
// ListArtifacts lists all artifacts for a given project and provider
// nolint:gocyclo
func (s *Server) ListArtifacts(ctx context.Context, in *pb.ListArtifactsRequest) (*pb.ListArtifactsResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
providerName := entityCtx.Provider.Name

Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Server) GetArtifactByName(ctx context.Context, in *pb.GetArtifactByName
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid artifact name user repoOwner/repoName/artifactName")
}

entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
projectID := entityCtx.Project.ID
providerName := entityCtx.Provider.Name
providerFilter := getNameFilterParam(providerName)
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *Server) GetArtifactByName(ctx context.Context, in *pb.GetArtifactByName
// GetArtifactById gets an artifact by id
// nolint:gocyclo
func (s *Server) GetArtifactById(ctx context.Context, in *pb.GetArtifactByIdRequest) (*pb.GetArtifactByIdResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
projectID := entityCtx.Project.ID

// tag and latest versions cannot be set at same time
Expand Down
20 changes: 10 additions & 10 deletions internal/controlplane/handlers_authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"database/sql"
"errors"
context2 "github.com/stacklok/minder/internal/engine/ectx"
"time"

"github.com/google/uuid"
Expand All @@ -32,7 +33,6 @@ import (
"github.com/stacklok/minder/internal/auth"
"github.com/stacklok/minder/internal/authz"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/flags"
"github.com/stacklok/minder/internal/invite"
"github.com/stacklok/minder/internal/util"
Expand Down Expand Up @@ -102,7 +102,7 @@ func ProjectAuthorizationInterceptor(ctx context.Context, req interface{}, info
return nil, status.Errorf(codes.Internal, "error getting name for requested relation %v", relation)
}

entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
server := info.Server.(*Server)

if err := server.authzClient.Check(ctx, relationName, entityCtx.Project.ID); err != nil {
Expand Down Expand Up @@ -135,16 +135,16 @@ func populateEntityContext(
}
}

entityCtx := &engine.EntityContext{
Project: engine.Project{
entityCtx := &context2.EntityContext{
Project: context2.Project{
ID: projectID,
},
Provider: engine.Provider{
Provider: context2.Provider{
Name: getProviderFromContext(req),
},
}

return engine.WithEntityContext(ctx, entityCtx), nil
return context2.WithEntityContext(ctx, entityCtx), nil
}

func getProjectIDFromContext(req any) (uuid.UUID, error) {
Expand Down Expand Up @@ -233,7 +233,7 @@ func (s *Server) ListRoleAssignments(
) (*minder.ListRoleAssignmentsResponse, error) {
invitations := make([]*minder.Invitation, 0)
// Determine the target project.
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
targetProject := entityCtx.Project.ID

as, err := s.authzClient.AssignmentsToProject(ctx, targetProject)
Expand Down Expand Up @@ -300,7 +300,7 @@ func (s *Server) AssignRole(ctx context.Context, req *minder.AssignRoleRequest)
email := req.GetRoleAssignment().GetEmail()

// Determine the target project.
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
targetProject := entityCtx.Project.ID

// Ensure user is not updating their own role
Expand Down Expand Up @@ -496,7 +496,7 @@ func (s *Server) RemoveRole(ctx context.Context, req *minder.RemoveRoleRequest)
sub := req.GetRoleAssignment().GetSubject()
email := req.GetRoleAssignment().GetEmail()
// Determine the target project.
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
targetProject := entityCtx.Project.ID

// Ensure user is not updating their own role
Expand Down Expand Up @@ -613,7 +613,7 @@ func (s *Server) UpdateRole(ctx context.Context, req *minder.UpdateRoleRequest)
sub := req.GetSubject()

// Determine the target project.
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
targetProject := entityCtx.Project.ID

if sub == "" {
Expand Down
46 changes: 23 additions & 23 deletions internal/controlplane/handlers_authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"crypto/rand"
"crypto/rsa"
"fmt"
context2 "github.com/stacklok/minder/internal/engine/ectx"
"testing"
"time"

Expand All @@ -43,7 +44,6 @@ import (
"github.com/stacklok/minder/internal/authz"
"github.com/stacklok/minder/internal/authz/mock"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/flags"
"github.com/stacklok/minder/internal/util"
minder "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
Expand All @@ -60,7 +60,7 @@ func (m request) GetContext() *minder.Context {

// Reply type containing the detected entityContext.
type replyType struct {
Context engine.EntityContext
Context context2.EntityContext
}

func TestEntityContextProjectInterceptor(t *testing.T) {
Expand All @@ -83,7 +83,7 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
buildStubs func(t *testing.T, store *mockdb.MockStore)
rpcErr error
defaultProject bool
expectedContext engine.EntityContext // Only if non-error
expectedContext context2.EntityContext // Only if non-error
}{
{
name: "not implementing proto context throws error",
Expand All @@ -102,7 +102,7 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
name: "non project owner bypasses interceptor",
req: &request{},
resource: minder.TargetResource_TARGET_RESOURCE_USER,
expectedContext: engine.EntityContext{},
expectedContext: context2.EntityContext{},
},
{
name: "invalid request with nil context",
Expand Down Expand Up @@ -137,9 +137,9 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
ID: 1,
}, nil)
},
expectedContext: engine.EntityContext{
expectedContext: context2.EntityContext{
// Uses the default project id
Project: engine.Project{ID: defaultProjectID},
Project: context2.Project{ID: defaultProjectID},
},
}, {
name: "no provider",
Expand All @@ -149,8 +149,8 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
},
},
resource: minder.TargetResource_TARGET_RESOURCE_PROJECT,
expectedContext: engine.EntityContext{
Project: engine.Project{ID: projectID},
expectedContext: context2.EntityContext{
Project: context2.Project{ID: projectID},
},
}, {
name: "sets entity context",
Expand All @@ -161,9 +161,9 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
},
},
resource: minder.TargetResource_TARGET_RESOURCE_PROJECT,
expectedContext: engine.EntityContext{
Project: engine.Project{ID: projectID},
Provider: engine.Provider{Name: provider},
expectedContext: context2.EntityContext{
Project: context2.Project{ID: projectID},
Provider: context2.Provider{Name: provider},
},
},
}
Expand All @@ -177,7 +177,7 @@ func TestEntityContextProjectInterceptor(t *testing.T) {
}

unaryHandler := func(ctx context.Context, _ interface{}) (any, error) {
return replyType{engine.EntityFromContext(ctx)}, nil
return replyType{context2.EntityFromContext(ctx)}, nil
}

ctrl := gomock.NewController(t)
Expand Down Expand Up @@ -226,25 +226,25 @@ func TestProjectAuthorizationInterceptor(t *testing.T) {

testCases := []struct {
name string
entityCtx *engine.EntityContext
entityCtx *context2.EntityContext
resource minder.TargetResource
rpcErr error
}{
{
name: "anonymous bypasses interceptor",
entityCtx: &engine.EntityContext{},
entityCtx: &context2.EntityContext{},
resource: minder.TargetResource_TARGET_RESOURCE_NONE,
},
{
name: "non project owner bypasses interceptor",
resource: minder.TargetResource_TARGET_RESOURCE_USER,
entityCtx: &engine.EntityContext{},
entityCtx: &context2.EntityContext{},
},
{
name: "not authorized on project error",
resource: minder.TargetResource_TARGET_RESOURCE_PROJECT,
entityCtx: &engine.EntityContext{
Project: engine.Project{
entityCtx: &context2.EntityContext{
Project: context2.Project{
ID: projectID,
},
},
Expand All @@ -255,8 +255,8 @@ func TestProjectAuthorizationInterceptor(t *testing.T) {
{
name: "authorized on project",
resource: minder.TargetResource_TARGET_RESOURCE_PROJECT,
entityCtx: &engine.EntityContext{
Project: engine.Project{
entityCtx: &context2.EntityContext{
Project: context2.Project{
ID: defaultProjectID,
},
},
Expand All @@ -272,15 +272,15 @@ func TestProjectAuthorizationInterceptor(t *testing.T) {
}

unaryHandler := func(ctx context.Context, _ interface{}) (any, error) {
return replyType{engine.EntityFromContext(ctx)}, nil
return replyType{context2.EntityFromContext(ctx)}, nil
}
server := Server{
authzClient: &mock.SimpleClient{
Allowed: []uuid.UUID{defaultProjectID},
},
}
ctx := withRpcOptions(context.Background(), rpcOptions)
ctx = engine.WithEntityContext(ctx, tc.entityCtx)
ctx = context2.WithEntityContext(ctx, tc.entityCtx)
ctx = auth.WithAuthTokenContext(ctx, userJWT)
_, err := ProjectAuthorizationInterceptor(ctx, request{}, &grpc.UnaryServerInfo{
Server: &server,
Expand Down Expand Up @@ -505,8 +505,8 @@ func TestRoleManagement(t *testing.T) {

ctx := context.Background()
ctx = auth.WithAuthTokenContext(ctx, user)
ctx = engine.WithEntityContext(ctx, &engine.EntityContext{
Project: engine.Project{
ctx = context2.WithEntityContext(ctx, &context2.EntityContext{
Project: context2.Project{
ID: project,
},
})
Expand Down
4 changes: 2 additions & 2 deletions internal/controlplane/handlers_entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ package controlplane
import (
"context"
"errors"
context2 "github.com/stacklok/minder/internal/engine/ectx"

"github.com/ThreeDotsLabs/watermill/message"
"github.com/google/uuid"
"github.com/rs/zerolog"
"google.golang.org/grpc/codes"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
"github.com/stacklok/minder/internal/events"
"github.com/stacklok/minder/internal/logger"
"github.com/stacklok/minder/internal/providers"
Expand All @@ -44,7 +44,7 @@ func (s *Server) ReconcileEntityRegistration(
) (*pb.ReconcileEntityRegistrationResponse, error) {
l := zerolog.Ctx(ctx).With().Logger()

entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
projectID := entityCtx.Project.ID

logger.BusinessRecord(ctx).Project = projectID
Expand Down
6 changes: 3 additions & 3 deletions internal/controlplane/handlers_entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ package controlplane

import (
"context"
context2 "github.com/stacklok/minder/internal/engine/ectx"
"testing"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
mockevents "github.com/stacklok/minder/internal/events/mock"
mockgh "github.com/stacklok/minder/internal/providers/github/mock"
mockmanager "github.com/stacklok/minder/internal/providers/manager/mock"
Expand Down Expand Up @@ -97,8 +97,8 @@ func TestServer_ReconcileEntityRegistration(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

ctx := engine.WithEntityContext(context.Background(), &engine.EntityContext{
Project: engine.Project{ID: projectID},
ctx := context2.WithEntityContext(context.Background(), &context2.EntityContext{
Project: context2.Project{ID: projectID},
})

manager := mockmanager.NewMockProviderManager(ctrl)
Expand Down
4 changes: 2 additions & 2 deletions internal/controlplane/handlers_evalstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controlplane
import (
"context"
"fmt"
context2 "github.com/stacklok/minder/internal/engine/ectx"

"github.com/google/uuid"
"github.com/rs/zerolog"
Expand All @@ -26,7 +27,6 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/engine"
minderv1 "github.com/stacklok/minder/pkg/api/protobuf/go/minder/v1"
)

Expand All @@ -36,7 +36,7 @@ func (s *Server) ListEvaluationResults(
ctx context.Context,
in *minderv1.ListEvaluationResultsRequest,
) (*minderv1.ListEvaluationResultsResponse, error) {
entityCtx := engine.EntityFromContext(ctx)
entityCtx := context2.EntityFromContext(ctx)
projectID := entityCtx.Project.ID

if _, err := uuid.Parse(in.GetProfile()); err != nil && in.GetProfile() != "" {
Expand Down
Loading

0 comments on commit ab5b0c0

Please sign in to comment.