From e5bb6eceffd3248537b74c8441a17623068b52c3 Mon Sep 17 00:00:00 2001 From: PiMaDaum Date: Wed, 1 Feb 2023 21:11:06 +0100 Subject: [PATCH] Outsource the resources filter function to seperate file --- resource/filter.go | 105 +++++++++++++++++++++++++++++++++++++++++++++ resource/list.go | 93 --------------------------------------- 2 files changed, 105 insertions(+), 93 deletions(-) create mode 100644 resource/filter.go diff --git a/resource/filter.go b/resource/filter.go new file mode 100644 index 0000000..f694f59 --- /dev/null +++ b/resource/filter.go @@ -0,0 +1,105 @@ +package resource + +import ( + "context" + "fmt" + "time" + + "github.com/google/cel-go/cel" + "github.com/google/cel-go/common/types" + "github.com/google/cel-go/common/types/ref" + "github.com/passbolt/go-passbolt/api" + "github.com/passbolt/go-passbolt/helper" +) + +// Environments for CEl +var celEnvOptions = []cel.EnvOption{ + cel.Variable("ID", cel.StringType), + cel.Variable("FolderParentID", cel.StringType), + cel.Variable("Name", cel.StringType), + cel.Variable("Username", cel.StringType), + cel.Variable("URI", cel.StringType), + cel.Variable("Password", cel.StringType), + cel.Variable("Description", cel.StringType), + cel.Variable("CreatedTimestamp", cel.TimestampType), + cel.Variable("ModifiedTimestamp", cel.TimestampType), + cel.Function("parseTimestamp", + cel.Overload("parse_timestamp_string", + []*cel.Type{cel.StringType}, + cel.TimestampType, + cel.UnaryBinding(func(timeStampInput ref.Val) ref.Val { + timeStampString := fmt.Sprintf("%s", timeStampInput.Value()) + timeStamp, err := time.Parse(time.R, timeStampString) + if err != nil { + fmt.Printf("Error while parsing timestamp: %v\n", err) + } + return types.Timestamp{Time: timeStamp} + }, + ), + ), + ), +} + +// Filters the slice resources by invoke CEL program for each resource +func filterResources(resources *[]api.Resource, celCmd string, ctx context.Context, client *api.Client) ([]api.Resource, error) { + if celCmd == "" { + return *resources, nil + } + + env, err := cel.NewEnv(celEnvOptions...) + if err != nil { + return nil, err + } + + ast, issue := env.Compile(celCmd) + if issue.Err() != nil { + return nil, issue.Err() + } + + program, err := env.Program(ast) + if err != nil { + return nil, err + } + + filteredResources := []api.Resource{} + for _, resource := range *resources { + val, _, err := program.ContextEval(ctx, map[string]any{ + "Id": resource.ID, + "FolderParentID": resource.FolderParentID, + "Name": resource.Name, + "Username": resource.Username, + "URI": resource.URI, + "Password": func() ref.Val { + _, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID) + if err != nil { + fmt.Printf("Get Resource %v", err) + return types.String("") + } + return types.String(pass) + }, + "Description": func() ref.Val { + _, _, _, _, _, descr, err := helper.GetResource(ctx, client, resource.ID) + if err != nil { + fmt.Printf("Get Resource %v", err) + return types.String("") + } + return types.String(descr) + }, + "CreatedTimestamp": resource.Created.Time, + "ModifiedTimestamp": resource.Modified.Time, + }) + + if err != nil { + return nil, err + } + + if val.Value() == true { + filteredResources = append(filteredResources, resource) + } + } + + if len(filteredResources) == 0 { + return nil, fmt.Errorf("No such Resources found with filter %v!", celCmd) + } + return filteredResources, nil +} diff --git a/resource/list.go b/resource/list.go index 999dd85..26c3387 100644 --- a/resource/list.go +++ b/resource/list.go @@ -8,9 +8,6 @@ import ( "time" "github.com/alessio/shellescape" - "github.com/google/cel-go/cel" - "github.com/google/cel-go/common/types" - "github.com/google/cel-go/common/types/ref" "github.com/passbolt/go-passbolt-cli/util" "github.com/passbolt/go-passbolt/api" "github.com/passbolt/go-passbolt/helper" @@ -28,33 +25,6 @@ var ResourceListCmd = &cobra.Command{ RunE: ResourceList, } -var celEnvOptions = []cel.EnvOption{ - cel.Variable("ID", cel.StringType), - cel.Variable("FolderParentID", cel.StringType), - cel.Variable("Name", cel.StringType), - cel.Variable("Username", cel.StringType), - cel.Variable("URI", cel.StringType), - cel.Variable("Password", cel.StringType), - cel.Variable("Description", cel.StringType), - cel.Variable("CreatedTimestamp", cel.TimestampType), - cel.Variable("ModifiedTimestamp", cel.TimestampType), - cel.Function("parseTimestamp", - cel.Overload("parse_timestamp_string", - []*cel.Type{cel.StringType}, - cel.TimestampType, - cel.UnaryBinding(func(timeStampInput ref.Val) ref.Val { - timeStampString := fmt.Sprintf("%s", timeStampInput.Value()) - timeStamp, err := time.Parse(`2006-01-02+15:04:05`, timeStampString) - if err != nil { - fmt.Printf("Error while parsing timestamp: %v\n", err) - } - return types.Timestamp{Time: timeStamp} - }, - ), - ), - ), -} - func init() { ResourceListCmd.Flags().Bool("favorite", false, "Resources that are marked as favorite") ResourceListCmd.Flags().Bool("own", false, "Resources that are owned by me") @@ -196,66 +166,3 @@ func ResourceList(cmd *cobra.Command, args []string) error { } return nil } - -func filterResources(resources *[]api.Resource, celCmd string, ctx context.Context, client *api.Client) ([]api.Resource, error) { - if celCmd == "" { - return *resources, nil - } - - env, err := cel.NewEnv(celEnvOptions...) - if err != nil { - return nil, err - } - - ast, issue := env.Compile(celCmd) - if issue.Err() != nil { - return nil, issue.Err() - } - - program, err := env.Program(ast) - if err != nil { - return nil, err - } - - filteredResources := []api.Resource{} - for _, resource := range *resources { - val, _, err := program.ContextEval(ctx, map[string]any{ - "Id": resource.ID, - "FolderParentID": resource.FolderParentID, - "Name": resource.Name, - "Username": resource.Username, - "URI": resource.URI, - "Password": func() ref.Val { - _, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID) - if err != nil { - fmt.Printf("Get Resource %v", err) - return types.String("") - } - return types.String(pass) - }, - "Description": func() ref.Val { - _, _, _, _, _, descr, err := helper.GetResource(ctx, client, resource.ID) - if err != nil { - fmt.Printf("Get Resource %v", err) - return types.String("") - } - return types.String(descr) - }, - "CreatedTimestamp": resource.Created.Time, - "ModifiedTimestamp": resource.Modified.Time, - }) - - if err != nil { - return nil, err - } - - if val.Value() == true { - filteredResources = append(filteredResources, resource) - } - } - - if len(filteredResources) == 0 { - return nil, fmt.Errorf("No such Resources found with filter %v!", celCmd) - } - return filteredResources, nil -}