CVE-2023-41891 (Go, published 2023-10-30T19:15Z)
Description: value: FlyteAdmin is the control plane for Flyte responsible for managing entities and administering workflow executions. Prior to version 1.1.124, list endpoints on FlyteAdmin have a SQL vulnerability where a malicious user can send a REST request with custom SQL statements as list filters. The attacker needs to have access to the FlyteAdmin installation, typically either behind a VPN or authentication. Version 1.1.124 contains a patch for this issue.
Severity: nan (CVSS v3 base score: 8.8)
Weakness: CWE-89 \u2014 Improper Neutralization of Special Elements used in an SQL Command ('SQL Injection')
The product constructs all or part of an SQL command using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended SQL command when it is sent to a downstream component. Without sufficient removal or quoting of SQL syntax in user-controllable inputs, the generated SQL query can cause those inputs to be interpreted as SQL instead of ordinary user data.
Repository: https://github.com/flyteorg/flyteadmin
Files changed:
pkg/clusterresource/impl/admin_service_data_provider.go
pkg/clusterresource/impl/shared.go
pkg/common/sorting.go
pkg/common/sorting_test.go
pkg/manager/impl/description_entity_manager.go
pkg/manager/impl/execution_manager.go
pkg/manager/impl/execution_manager_test.go
pkg/manager/impl/launch_plan_manager.go
pkg/manager/impl/named_entity_manager.go
pkg/manager/impl/node_execution_manager.go
pkg/manager/impl/node_execution_manager_test.go
pkg/manager/impl/project_manager.go
pkg/manager/impl/signal_manager.go
pkg/manager/impl/task_execution_manager.go
pkg/manager/impl/task_manager.go
pkg/manager/impl/util/filters.go
pkg/manager/impl/util/filters_test.go
pkg/manager/impl/workflow_manager.go
pkg/repositories/gormimpl/common.go
pkg/repositories/gormimpl/description_entity_repo.go
pkg/repositories/gormimpl/execution_repo.go
pkg/repositories/gormimpl/execution_repo_test.go
pkg/repositories/gormimpl/launch_plan_repo.go
pkg/repositories/gormimpl/launch_plan_repo_test.go
pkg/repositories/gormimpl/named_entity_repo.go
pkg/repositories/gormimpl/named_entity_repo_test.go
pkg/repositories/gormimpl/node_execution_event_repo.go
pkg/repositories/gormimpl/node_execution_repo.go
pkg/repositories/gormimpl/node_execution_repo_test.go
pkg/repositories/gormimpl/project_repo.go
pkg/repositories/gormimpl/project_repo_test.go
pkg/repositories/gormimpl/resource_repo_test.go
pkg/repositories/gormimpl/signal_repo.go
pkg/repositories/gormimpl/task_execution_repo.go
pkg/repositories/gormimpl/task_execution_repo_test.go
pkg/repositories/gormimpl/task_repo.go
pkg/repositories/gormimpl/task_repo_test.go
pkg/repositories/gormimpl/workflow_repo.go
pkg/repositories/gormimpl/workflow_repo_test.go
pkg/repositories/models/base_model.go
pkg/repositories/models/description_entity.go
pkg/repositories/models/execution.go
pkg/repositories/models/launch_plan.go
pkg/repositories/models/named_entity.go
pkg/repositories/models/node_execution.go
pkg/repositories/models/node_execution_event.go
pkg/repositories/models/project.go
pkg/repositories/models/signal.go
pkg/repositories/models/task.go
pkg/repositories/models/task_execution.go
pkg/repositories/models/workflow.go
Commit message: Merge pull request from GHSA-r847-6w6h-r8g4

* Fix list filters

* Fix sort keys

* split into 2 maps

* move columns to models

* move columns to models

Vulnerable code:
	"github.com/flyteorg/flyteadmin/pkg/clusterresource/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/common"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"

var descCreatedAtSortParam = admin.Sort{
	Direction: admin.Sort_DESCENDING,
	Key:       "created_at",
}

var descCreatedAtSortDBParam, _ = common.NewSortParameter(descCreatedAtSortParam)
func NewSortParameter(sort admin.Sort) (SortParameter, error) {
		gormOrderExpression = fmt.Sprintf(gormDescending, sort.Key)
		gormOrderExpression = fmt.Sprintf(gormAscending, sort.Key)
	sortParameter, err := NewSortParameter(admin.Sort{
	})
	assert.Nil(t, err)
	sortParameter, err := NewSortParameter(admin.Sort{
	})
	assert.Nil(t, err)

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flytestdlib/contextutils"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	"github.com/flyteorg/flytestdlib/promutils/labeled"

	"github.com/flyteorg/flyteadmin/plugins"


	"github.com/flyteorg/flyteadmin/auth"

	"github.com/flyteorg/flyteadmin/pkg/manager/impl/resources"

	dataInterfaces "github.com/flyteorg/flyteadmin/pkg/data/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/common"

	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/storage"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"google.golang.org/grpc/codes"

	"github.com/benbjohnson/clock"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	"github.com/golang/protobuf/proto"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}

	"github.com/flyteorg/flyteadmin/plugins"

	"google.golang.org/grpc/status"

	"google.golang.org/protobuf/types/known/timestamppb"
	"github.com/flyteorg/flyteadmin/pkg/common"
	commonTestUtils "github.com/flyteorg/flyteadmin/pkg/common/testutils"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/executions"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	managerInterfaces "github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
	managerMocks "github.com/flyteorg/flyteadmin/pkg/manager/mocks"
	"github.com/flyteorg/flyteadmin/pkg/runtime"


	eventWriterMocks "github.com/flyteorg/flyteadmin/pkg/async/events/mocks"

	commonMocks "github.com/flyteorg/flyteadmin/pkg/common/mocks"

	"github.com/flyteorg/flytestdlib/storage"

	"time"

	"fmt"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes/wrappers"
	"github.com/stretchr/testify/assert"
		assert.Equal(t, "domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "domain",
		assert.Equal(t, "domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "domain",


	"github.com/flyteorg/flytestdlib/logger"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/golang/protobuf/proto"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}

	"google.golang.org/grpc/codes"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	cloudeventInterfaces "github.com/flyteorg/flyteadmin/pkg/async/cloudevent/interfaces"

	"github.com/flyteorg/flytestdlib/promutils/labeled"

	eventWriter "github.com/flyteorg/flyteadmin/pkg/async/events/interfaces"

	notificationInterfaces "github.com/flyteorg/flyteadmin/pkg/async/notifications/interfaces"
	"github.com/golang/protobuf/proto"

	"github.com/flyteorg/flytestdlib/storage"


	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	"github.com/flyteorg/flytestdlib/logger"

	"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"

	"fmt"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if sortBy != nil {
		sortParameter, err = common.NewSortParameter(*sortBy)
		if err != nil {
			return nil, err
		}
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/testutils"
	"github.com/flyteorg/flyteadmin/pkg/common"
	commonMocks "github.com/flyteorg/flyteadmin/pkg/common/mocks"
	dataMocks "github.com/flyteorg/flyteadmin/pkg/data/mocks"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
	repositoryMocks "github.com/flyteorg/flyteadmin/pkg/repositories/mocks"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
			assert.Equal(t, "domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "domain",
	assert.Nil(t, err)
			assert.Equal(t, "domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "domain",
			Key:       "domain",
	assert.Nil(t, err)
			assert.Equal(t, "domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "domain",
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"google.golang.org/grpc/codes"
var alphabeticalSortParam, _ = common.NewSortParameter(admin.Sort{
})
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	} else {

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"

	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"github.com/flyteorg/flytestdlib/promutils/labeled"

	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	cloudeventInterfaces "github.com/flyteorg/flyteadmin/pkg/async/cloudevent/interfaces"

	"github.com/flyteorg/flytestdlib/promutils/labeled"

	notificationInterfaces "github.com/flyteorg/flyteadmin/pkg/async/notifications/interfaces"
	"github.com/golang/protobuf/proto"

	"github.com/flyteorg/flytestdlib/storage"


	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}

	"github.com/prometheus/client_golang/prometheus"



	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}

	"github.com/flyteorg/flyteadmin/pkg/errors"

	"fmt"
	"github.com/stretchr/testify/assert"
func TestParseFilters(t *testing.T) {
	filterExpression := "eq(foo, 123)+ne(version, TheWorst)+value_in(bar, 4;5;6)"
	assert.Len(t, taskFilters, 3)
	assert.Equal(t, "foo = ?", actualFilterExpression.Query)
	assert.Equal(t, "123", actualFilterExpression.Args)
	assert.Equal(t, "version <> ?", actualFilterExpression.Query)
	assert.Equal(t, "TheWorst", actualFilterExpression.Args)
	assert.Equal(t, "bar in (?)", actualFilterExpression.Query)
	filterExpression = "invalid_function(foo,bar)"
	_, err = ParseFilters(filterExpression, common.Task)
	assert.Error(t, err)
func TestAddRequestFilters(t *testing.T) {
		"ne(version, TheWorst)+eq(workflow.name, workflow)", common.Execution, make([]common.InlineFilter, 0))
	assert.Nil(t, err)
	assert.Len(t, filters, 2)
	assert.Nil(t, err)
	assert.Equal(t, "version <> ?", expression.Query)
	assert.Nil(t, err)
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	compiler "github.com/flyteorg/flytepropeller/pkg/compiler/common"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/ptypes"
	"github.com/prometheus/client_golang/prometheus"
	"google.golang.org/grpc/codes"
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}
	var sortParameter common.SortParameter
	if request.SortBy != nil {
		sortParameter, err = common.NewSortParameter(*request.SortBy)
		if err != nil {
			return nil, err
		}

	"google.golang.org/grpc/codes"
	"gorm.io/gorm"
	"github.com/flyteorg/flyteadmin/pkg/common"
	"gorm.io/gorm"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/promutils"

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"

	mocket "github.com/Selvatico/go-mocket"
	"github.com/stretchr/testify/assert"
	mockQuery := GlobalMock.NewMock().WithQuery(`name asc`)
	sortParameter, _ := common.NewSortParameter(admin.Sort{
		Key:       "name",
	})
	_, err := executionRepo.List(context.Background(), interfaces.ListResourceInput{
	mockQuery := GlobalMock.NewMock().WithQuery(`name asc`)
	sortParameter, _ := common.NewSortParameter(admin.Sort{
		Key:       "name",
	})
	"github.com/flyteorg/flytestdlib/logger"
	"gorm.io/gorm"
	mocket "github.com/Selvatico/go-mocket"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/stretchr/testify/assert"
	sortParameter, _ := common.NewSortParameter(admin.Sort{
	})
	"github.com/flyteorg/flytestdlib/promutils"
	"gorm.io/gorm"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"
	sortParameter, _ := common.NewSortParameter(admin.Sort{
	})
	"github.com/flyteorg/flytestdlib/promutils"
	"gorm.io/gorm"

	"gorm.io/gorm"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"google.golang.org/grpc/codes"
	"gorm.io/gorm"

	mockScope "github.com/flyteorg/flytestdlib/promutils"

	mocket "github.com/Selvatico/go-mocket"
	"github.com/stretchr/testify/assert"
	mockQuery.WithQuery(`project desc`)
	sortParameter, _ := common.NewSortParameter(admin.Sort{
		Key:       "project",
	})
	_, err := nodeExecutionRepo.List(context.Background(), interfaces.ListResourceInput{
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"google.golang.org/grpc/codes"


	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"
var alphabeticalSortParam, _ = common.NewSortParameter(admin.Sort{
})
	assert.Nil(t, err)
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"

	mocket "github.com/Selvatico/go-mocket"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"

	"github.com/flyteorg/flytestdlib/promutils"

	"google.golang.org/grpc/codes"

	"gorm.io/gorm"

	"gorm.io/gorm"
	mocket "github.com/Selvatico/go-mocket"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/stretchr/testify/assert"

	"gorm.io/gorm"
	"github.com/stretchr/testify/assert"
	sortParameter, _ := common.NewSortParameter(admin.Sort{
	})
	flyteAdminDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"
	sortParameter, _ := common.NewSortParameter(admin.Sort{
	})
import "time"

Fixed code:

	"github.com/flyteorg/flyteadmin/pkg/clusterresource/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/common"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
var descCreatedAtSortParam = admin.Sort{
	Direction: admin.Sort_DESCENDING,
	Key:       "created_at",
}

var descCreatedAtSortDBParam, _ = common.NewSortParameter(&descCreatedAtSortParam, models.ProjectColumns)

	"k8s.io/apimachinery/pkg/util/sets"
func NewSortParameter(sort *admin.Sort, allowed sets.String) (SortParameter, error) {
	if sort == nil {
		return nil, nil
	}

	key := sort.Key
	if !allowed.Has(key) {
		return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid sort key '%s'", key)
	}

		gormOrderExpression = fmt.Sprintf(gormDescending, key)
		gormOrderExpression = fmt.Sprintf(gormAscending, key)
	"k8s.io/apimachinery/pkg/util/sets"

func TestSortParameter_Nil(t *testing.T) {
	sortParameter, err := NewSortParameter(nil, nil)

	assert.NoError(t, err)
	assert.Nil(t, sortParameter)
}

func TestSortParameter_InvalidSortKey(t *testing.T) {
	_, err := NewSortParameter(&admin.Sort{
		Direction: admin.Sort_ASCENDING,
		Key:       "wrong",
	}, sets.NewString("name"))

	assert.EqualError(t, err, "invalid sort key 'wrong'")
}

func TestSortParameter_InvalidSortDirection(t *testing.T) {
	_, err := NewSortParameter(&admin.Sort{
		Direction: 2,
		Key:       "name",
	}, sets.NewString("name"))

	assert.EqualError(t, err, `invalid sort order specified: key:"name" direction:2 `)
}

	sortParameter, err := NewSortParameter(&admin.Sort{
	}, sets.NewString("name"))

	assert.NoError(t, err)
	sortParameter, err := NewSortParameter(&admin.Sort{
	}, sets.NewString("project"))

	assert.NoError(t, err)
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flytestdlib/contextutils"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"google.golang.org/grpc/codes"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.DescriptionEntityColumns)
	if err != nil {
		return nil, err

	"github.com/benbjohnson/clock"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils/labeled"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/proto"
	"google.golang.org/grpc/codes"
	"github.com/flyteorg/flyteadmin/auth"
	"github.com/flyteorg/flyteadmin/pkg/common"
	dataInterfaces "github.com/flyteorg/flyteadmin/pkg/data/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/resources"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	"github.com/flyteorg/flyteadmin/plugins"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.ExecutionColumns)
	if err != nil {
		return nil, err
	"fmt"
	"time"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes/wrappers"
	"github.com/stretchr/testify/assert"
	"google.golang.org/grpc/status"
	"google.golang.org/protobuf/types/known/timestamppb"
	eventWriterMocks "github.com/flyteorg/flyteadmin/pkg/async/events/mocks"
	"github.com/flyteorg/flyteadmin/pkg/common"
	commonMocks "github.com/flyteorg/flyteadmin/pkg/common/mocks"
	commonTestUtils "github.com/flyteorg/flyteadmin/pkg/common/testutils"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/executions"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	managerInterfaces "github.com/flyteorg/flyteadmin/pkg/manager/interfaces"
	managerMocks "github.com/flyteorg/flyteadmin/pkg/manager/mocks"
	"github.com/flyteorg/flyteadmin/pkg/runtime"
	"github.com/flyteorg/flyteadmin/plugins"
		assert.Equal(t, "execution_domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "execution_domain",
		assert.Equal(t, "execution_domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "execution_domain",
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/golang/protobuf/proto"
	"google.golang.org/grpc/codes"
	sortParameter, err := common.NewSortParameter(request.SortBy, models.LaunchPlanColumns)
	if err != nil {
		return nil, err

	sortParameter, err := common.NewSortParameter(request.SortBy, models.LaunchPlanColumns)
	if err != nil {
		return nil, err


	sortParameter, err := common.NewSortParameter(request.SortBy, models.LaunchPlanColumns)
	if err != nil {
		return nil, err

	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"google.golang.org/grpc/codes"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.NamedEntityColumns)
	if err != nil {
		return nil, err

	"fmt"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils/labeled"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/proto"
	"google.golang.org/grpc/codes"
	cloudeventInterfaces "github.com/flyteorg/flyteadmin/pkg/async/cloudevent/interfaces"
	eventWriter "github.com/flyteorg/flyteadmin/pkg/async/events/interfaces"
	notificationInterfaces "github.com/flyteorg/flyteadmin/pkg/async/notifications/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
	"github.com/flyteorg/flyteadmin/pkg/manager/impl/validation"

	sortParameter, err := common.NewSortParameter(sortBy, models.NodeExecutionColumns)
	if err != nil {
		return nil, err

	"github.com/flyteorg/flyteadmin/pkg/manager/impl/testutils"


	"github.com/flyteorg/flyteadmin/pkg/common"
	commonMocks "github.com/flyteorg/flyteadmin/pkg/common/mocks"
	dataMocks "github.com/flyteorg/flyteadmin/pkg/data/mocks"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
	repositoryMocks "github.com/flyteorg/flyteadmin/pkg/repositories/mocks"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
			assert.Equal(t, "execution_domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "execution_domain",
	assert.NoError(t, err)
			assert.Equal(t, "execution_domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "execution_domain",

			Key:       "execution_domain",

	assert.NoError(t, err)
			assert.Equal(t, "execution_domain asc", input.SortParameter.GetGormOrderExpr())
			Key:       "execution_domain",
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"google.golang.org/grpc/codes"

	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
var alphabeticalSortParam, _ = common.NewSortParameter(&admin.Sort{
}, models.ProjectColumns)
	sortParameter, err := common.NewSortParameter(request.SortBy, models.ProjectColumns)
	if err != nil {
		return nil, err
	}
	if sortParameter == nil {
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"github.com/flyteorg/flytestdlib/promutils/labeled"
	"google.golang.org/grpc/codes"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.SignalColumns)
	if err != nil {
		return nil, err
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils/labeled"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/proto"
	"google.golang.org/grpc/codes"
	cloudeventInterfaces "github.com/flyteorg/flyteadmin/pkg/async/cloudevent/interfaces"
	notificationInterfaces "github.com/flyteorg/flyteadmin/pkg/async/notifications/interfaces"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.TaskExecutionColumns)
	if err != nil {
		return nil, err
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/prometheus/client_golang/prometheus"
	"google.golang.org/grpc/codes"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.TaskColumns)
	if err != nil {
		return nil, err


	sortParameter, err := common.NewSortParameter(request.SortBy, models.TaskColumns)
	if err != nil {
		return nil, err

	"fmt"
	"k8s.io/apimachinery/pkg/util/sets"
	"github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
var allowedJoinEntities = map[common.Entity]sets.String{
	common.Execution:           sets.NewString(common.Execution, common.LaunchPlan, common.Workflow, common.Task, common.AdminTag),
	common.LaunchPlan:          sets.NewString(common.LaunchPlan, common.Workflow),
	common.NodeExecution:       sets.NewString(common.NodeExecution, common.Execution),
	common.NodeExecutionEvent:  sets.NewString(common.NodeExecutionEvent),
	common.Task:                sets.NewString(common.Task),
	common.TaskExecution:       sets.NewString(common.TaskExecution, common.Task, common.Execution, common.NodeExecution),
	common.Workflow:            sets.NewString(common.Workflow),
	common.NamedEntity:         sets.NewString(common.NamedEntity),
	common.NamedEntityMetadata: sets.NewString(common.NamedEntityMetadata),
	common.Project:             sets.NewString(common.Project),
	common.Signal:              sets.NewString(common.Signal),
	common.AdminTag:            sets.NewString(common.AdminTag),
}

var entityColumns = map[common.Entity]sets.String{
	common.Execution:           models.ExecutionColumns,
	common.LaunchPlan:          models.LaunchPlanColumns,
	common.NodeExecution:       models.NodeExecutionColumns,
	common.NodeExecutionEvent:  models.NodeExecutionEventColumns,
	common.Task:                models.TaskColumns,
	common.TaskExecution:       models.TaskExecutionColumns,
	common.Workflow:            models.WorkflowColumns,
	common.NamedEntity:         models.NamedEntityColumns,
	common.NamedEntityMetadata: models.NamedEntityMetadataColumns,
	common.Project:             models.ProjectColumns,
	common.Signal:              models.SignalColumns,
	common.AdminTag:            models.AdminTagColumns,
}

		joinEntities, ok := allowedJoinEntities[primaryEntity]
		if !ok {
			return nil, fmt.Errorf("unsupported entity '%s'", primaryEntity)
		}

		if !joinEntities.Has(referencedEntity) {
			return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "'%s' entity is not allowed in filters", referencedEntity)
		}

		if !entityColumns[referencedEntity].Has(field) {
			return nil, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "'%s.%s' is invalid filter", referencedEntity, field)
		}

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
func Test_ParseFilters_Success(t *testing.T) {
	filterExpression := "eq(project, flytesnacks)+ne(domain, development)+value_in(type, 4;5;6)"


	require.Len(t, taskFilters, 3)
	assert.Equal(t, "project = ?", actualFilterExpression.Query)
	assert.Equal(t, "flytesnacks", actualFilterExpression.Args)
	assert.Equal(t, "domain <> ?", actualFilterExpression.Query)
	assert.Equal(t, "development", actualFilterExpression.Args)
	assert.Equal(t, "type in (?)", actualFilterExpression.Query)
}

func Test_ParseFilters_InvalidFunction(t *testing.T) {
	filterExpression := "invalid_function(type,bar)"

	_, err := ParseFilters(filterExpression, common.Task)
func Test_ParseFilters_UnsupportedEntity(t *testing.T) {
	filterExpression := "eq(foo, 123)"

	_, err := ParseFilters(filterExpression, "wrong")

	assert.EqualError(t, err, "unsupported entity 'wrong'")
}

func Test_ParseFilters_InvalidJoinEntity(t *testing.T) {
	filterExpression := "eq(project.name, 123)"

	_, err := ParseFilters(filterExpression, common.Workflow)

	assert.EqualError(t, err, "'p' entity is not allowed in filters")
}

func Test_ParseFilters_InvalidFilter(t *testing.T) {
	filterExpression := "eq(foo, 123)"

	_, err := ParseFilters(filterExpression, common.Task)

	assert.EqualError(t, err, "'t.foo' is invalid filter")
}

func Test_AddRequestFilters(t *testing.T) {
		"ne(cluster, TheWorst)+eq(workflow.name, workflow)", common.Execution, make([]common.InlineFilter, 0))

	assert.NoError(t, err)
	require.Len(t, filters, 2)

	assert.NoError(t, err)
	assert.Equal(t, "cluster <> ?", expression.Query)
	assert.NoError(t, err)
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	compiler "github.com/flyteorg/flytepropeller/pkg/compiler/common"
	"github.com/flyteorg/flytestdlib/logger"
	"github.com/flyteorg/flytestdlib/promutils"
	"github.com/flyteorg/flytestdlib/storage"
	"github.com/golang/protobuf/ptypes"
	"github.com/prometheus/client_golang/prometheus"
	"google.golang.org/grpc/codes"

	sortParameter, err := common.NewSortParameter(request.SortBy, models.WorkflowColumns)
	if err != nil {
		return nil, err


	sortParameter, err := common.NewSortParameter(request.SortBy, models.WorkflowColumns)
	if err != nil {
		return nil, err

	"sync"

	"google.golang.org/grpc/codes"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"k8s.io/apimachinery/pkg/util/sets"

func modelColumns(v any) sets.String {
	s, err := schema.Parse(v, &sync.Map{}, schema.NamingStrategy{})
	if err != nil {
		panic(err)
	}
	return sets.NewString(s.DBNames...)
}
	"gorm.io/gorm"
	"github.com/flyteorg/flyteadmin/pkg/common"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/flyteorg/flytestdlib/promutils"

	mocket "github.com/Selvatico/go-mocket"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	mockQuery := GlobalMock.NewMock().WithQuery(`execution_name asc`)
	sortParameter, err := common.NewSortParameter(&admin.Sort{
		Key:       "execution_name",
	}, models.ExecutionColumns)
	require.NoError(t, err)

	_, err = executionRepo.List(context.Background(), interfaces.ListResourceInput{

	mockQuery := GlobalMock.NewMock().WithQuery(`execution_name asc`)
	sortParameter, err := common.NewSortParameter(&admin.Sort{
		Key:       "execution_name",
	}, models.ExecutionColumns)
	require.NoError(t, err)


	"github.com/flyteorg/flytestdlib/logger"
	"gorm.io/gorm"

	mocket "github.com/Selvatico/go-mocket"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	"github.com/stretchr/testify/assert"
	sortParameter, _ := common.NewSortParameter(&admin.Sort{
	}, models.LaunchPlanColumns)
	"github.com/flyteorg/flytestdlib/promutils"
	"gorm.io/gorm"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"

	sortParameter, _ := common.NewSortParameter(&admin.Sort{
	}, models.NamedEntityColumns)
	"github.com/flyteorg/flytestdlib/promutils"
	"gorm.io/gorm"

	"gorm.io/gorm"
	mocket "github.com/Selvatico/go-mocket"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"google.golang.org/grpc/codes"
	"gorm.io/gorm"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	mockQuery.WithQuery(`execution_project desc`)
	sortParameter, err := common.NewSortParameter(&admin.Sort{
		Key:       "execution_project",
	}, models.NodeExecutionColumns)
	require.NoError(t, err)

	_, err = nodeExecutionRepo.List(context.Background(), interfaces.ListResourceInput{

	"google.golang.org/grpc/codes"
	flyteAdminErrors "github.com/flyteorg/flyteadmin/pkg/errors"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"

var alphabeticalSortParam, _ = common.NewSortParameter(&admin.Sort{
}, models.ProjectColumns)

	assert.NoError(t, err)
	mocket "github.com/Selvatico/go-mocket"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
	"github.com/flyteorg/flytestdlib/promutils"
	"google.golang.org/grpc/codes"
	"gorm.io/gorm"

	"gorm.io/gorm"
	mocket "github.com/Selvatico/go-mocket"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
	"github.com/stretchr/testify/assert"
	"gorm.io/gorm"
	"github.com/stretchr/testify/assert"

	sortParameter, _ := common.NewSortParameter(&admin.Sort{
	}, models.TaskColumns)

	flyteAdminDbErrors "github.com/flyteorg/flyteadmin/pkg/repositories/errors"
	"github.com/flyteorg/flyteadmin/pkg/repositories/interfaces"
	"github.com/flyteorg/flyteadmin/pkg/repositories/models"
	"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
	mockScope "github.com/flyteorg/flytestdlib/promutils"
	"github.com/stretchr/testify/assert"

	sortParameter, _ := common.NewSortParameter(&admin.Sort{
	}, models.WorkflowColumns)
import (
	"sync"
	"time"

	"gorm.io/gorm/schema"
	"k8s.io/apimachinery/pkg/util/sets"
)

func modelColumns(v any) sets.String {
	s, err := schema.Parse(v, &sync.Map{}, schema.NamingStrategy{})
	if err != nil {
		panic(err)
	}
	return sets.NewString(s.DBNames...)
}

var DescriptionEntityColumns = modelColumns(DescriptionEntity{})

var (
	ExecutionColumns = modelColumns(Execution{})
	AdminTagColumns  = modelColumns(AdminTag{})
)

var LaunchPlanColumns = modelColumns(LaunchPlan{})

var (
	NamedEntityColumns         = modelColumns(NamedEntity{})
	NamedEntityMetadataColumns = modelColumns(NamedEntityMetadata{})
)

var NodeExecutionColumns = modelColumns(NodeExecution{})

var NodeExecutionEventColumns = modelColumns(NodeExecutionEvent{})

var ProjectColumns = modelColumns(Project{})

var SignalColumns = modelColumns(Signal{})

var TaskColumns = modelColumns(Task{})

var TaskExecutionColumns = modelColumns(TaskExecution{})

var WorkflowColumns = modelColumns(Workflow{})