From b0303103da5d51bf47658795deab3a64f11268ed Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Wed, 8 Sep 2021 11:07:38 +0200 Subject: [PATCH] add resource commands --- cmd/create.go | 2 + cmd/delete.go | 2 + cmd/get.go | 3 +- cmd/list.go | 2 + cmd/move.go | 3 ++ cmd/share.go | 2 + cmd/update.go | 3 ++ resource/create.go | 83 ++++++++++++++++++++++++++++++++ resource/delete.go | 43 +++++++++++++++++ resource/get.go | 56 ++++++++++++++++++++++ resource/list.go | 117 +++++++++++++++++++++++++++++++++++++++++++++ resource/move.go | 57 ++++++++++++++++++++++ resource/share.go | 69 ++++++++++++++++++++++++++ resource/update.go | 80 +++++++++++++++++++++++++++++++ 14 files changed, 521 insertions(+), 1 deletion(-) create mode 100644 resource/create.go create mode 100644 resource/delete.go create mode 100644 resource/get.go create mode 100644 resource/list.go create mode 100644 resource/move.go create mode 100644 resource/share.go create mode 100644 resource/update.go diff --git a/cmd/create.go b/cmd/create.go index bc02667..9cafa10 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -14,4 +15,5 @@ var createCmd = &cobra.Command{ func init() { rootCmd.AddCommand(createCmd) + createCmd.AddCommand(resource.ResourceCreateCmd) } diff --git a/cmd/delete.go b/cmd/delete.go index 6490f37..f93505f 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -14,6 +15,7 @@ var deleteCmd = &cobra.Command{ func init() { rootCmd.AddCommand(deleteCmd) + deleteCmd.AddCommand(resource.ResourceDeleteCmd) deleteCmd.PersistentFlags().String("id", "", "ID of the Entity to Delete") } diff --git a/cmd/get.go b/cmd/get.go index e02c1bd..550fc19 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -14,5 +15,5 @@ var getCmd = &cobra.Command{ func init() { rootCmd.AddCommand(getCmd) - + getCmd.AddCommand(resource.ResourceGetCmd) } diff --git a/cmd/list.go b/cmd/list.go index 15d4185..7ee83c3 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -15,4 +16,5 @@ var listCmd = &cobra.Command{ func init() { rootCmd.AddCommand(listCmd) + listCmd.AddCommand(resource.ResourceListCmd) } diff --git a/cmd/move.go b/cmd/move.go index f616366..9e60501 100644 --- a/cmd/move.go +++ b/cmd/move.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -12,4 +13,6 @@ var moveCmd = &cobra.Command{ } func init() { + rootCmd.AddCommand(moveCmd) + moveCmd.AddCommand(resource.ResourceMoveCmd) } diff --git a/cmd/share.go b/cmd/share.go index c0271fe..37e5a23 100644 --- a/cmd/share.go +++ b/cmd/share.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -13,4 +14,5 @@ var shareCmd = &cobra.Command{ func init() { rootCmd.AddCommand(shareCmd) + shareCmd.AddCommand(resource.ResourceShareCmd) } diff --git a/cmd/update.go b/cmd/update.go index be89241..af8d6af 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -14,4 +15,6 @@ var updateCmd = &cobra.Command{ func init() { rootCmd.AddCommand(updateCmd) + + updateCmd.AddCommand(resource.ResourceUpdateCmd) } diff --git a/resource/create.go b/resource/create.go new file mode 100644 index 0000000..db72e92 --- /dev/null +++ b/resource/create.go @@ -0,0 +1,83 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// ResourceCreateCmd Creates a Passbolt Resource +var ResourceCreateCmd = &cobra.Command{ + Use: "resource", + Short: "Creates a Passbolt Resource", + Long: `Creates a Passbolt Resource and Returns the Resources ID`, + RunE: ResourceCreate, +} + +func init() { + ResourceCreateCmd.Flags().StringP("name", "n", "", "Resource Name") + ResourceCreateCmd.Flags().StringP("username", "u", "", "Resource Username") + ResourceCreateCmd.Flags().String("uri", "", "Resource URI") + ResourceCreateCmd.Flags().StringP("password", "p", "", "Resource Password") + ResourceCreateCmd.Flags().StringP("description", "d", "", "Resource Description") + ResourceCreateCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to create the Resource") + + ResourceCreateCmd.MarkFlagRequired("name") + ResourceCreateCmd.MarkFlagRequired("password") +} + +func ResourceCreate(cmd *cobra.Command, args []string) error { + folderParentID, err := cmd.Flags().GetString("folderParentID") + if err != nil { + return err + } + name, err := cmd.Flags().GetString("name") + if err != nil { + return err + } + username, err := cmd.Flags().GetString("username") + if err != nil { + return err + } + uri, err := cmd.Flags().GetString("uri") + if err != nil { + return err + } + password, err := cmd.Flags().GetString("password") + if err != nil { + return err + } + description, err := cmd.Flags().GetString("description") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + id, err := helper.CreateResource( + ctx, + client, + folderParentID, + name, + username, + uri, + password, + description, + ) + if err != nil { + return fmt.Errorf("Creating Resource: %w", err) + } + + fmt.Printf("ResourceID: %v\n", id) + return nil +} diff --git a/resource/delete.go b/resource/delete.go new file mode 100644 index 0000000..b75e49b --- /dev/null +++ b/resource/delete.go @@ -0,0 +1,43 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/spf13/cobra" +) + +// ResourceDeleteCmd Deletes a Resource +var ResourceDeleteCmd = &cobra.Command{ + Use: "resource", + Short: "Deletes a Passbolt Resource", + Long: `Deletes a Passbolt Resource`, + RunE: ResourceDelete, +} + +func ResourceDelete(cmd *cobra.Command, args []string) error { + resourceID, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + + if resourceID == "" { + return fmt.Errorf("No ID to Delete Provided") + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + client.DeleteResource(ctx, resourceID) + if err != nil { + return fmt.Errorf("Deleting Resource: %w", err) + } + return nil +} diff --git a/resource/get.go b/resource/get.go new file mode 100644 index 0000000..ce6863a --- /dev/null +++ b/resource/get.go @@ -0,0 +1,56 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// ResourceGetCmd Gets a Passbolt Resource +var ResourceGetCmd = &cobra.Command{ + Use: "resource", + Short: "Gets a Passbolt Resource", + Long: `Gets a Passbolt Resource`, + RunE: ResourceGet, +} + +func init() { + ResourceGetCmd.Flags().String("id", "", "id of Resource to Get") + + ResourceGetCmd.MarkFlagRequired("id") +} + +func ResourceGet(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + folderParentID, name, username, uri, password, description, err := helper.GetResource( + ctx, + client, + id, + ) + if err != nil { + return fmt.Errorf("Getting Resource: %w", err) + } + fmt.Printf("FolderParentID: %v\n", folderParentID) + fmt.Printf("Name: %v\n", name) + fmt.Printf("Username: %v\n", username) + fmt.Printf("URI: %v\n", uri) + fmt.Printf("Password: %v\n", password) + fmt.Printf("Description: %v\n", description) + return nil +} diff --git a/resource/list.go b/resource/list.go new file mode 100644 index 0000000..68e88e1 --- /dev/null +++ b/resource/list.go @@ -0,0 +1,117 @@ +package resource + +import ( + "context" + "fmt" + "strings" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/api" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" + + "github.com/pterm/pterm" +) + +// ResourceListCmd Lists a Passbolt Resource +var ResourceListCmd = &cobra.Command{ + Use: "resource", + Short: "Lists Passbolt Resources", + Long: `Lists Passbolt Resources`, + Aliases: []string{"resources"}, + RunE: ResourceList, +} + +func init() { + ResourceListCmd.Flags().Bool("favorite", false, "Resources that are maked as favorite") + ResourceListCmd.Flags().Bool("own", false, "Resources that are owned by me") + + ResourceListCmd.Flags().StringArrayP("group", "g", []string{}, "Resources that are shared with group") + ResourceListCmd.Flags().StringArrayP("folder", "f", []string{}, "Resources that are in folder") + + ResourceListCmd.Flags().StringArrayP("columns", "c", []string{"ID", "FolderParentID", "Name", "Username", "URI"}, "Columns to return, possible Columns:\nID, FolderParentID, Name, Username, URI, Password, Description") +} + +func ResourceList(cmd *cobra.Command, args []string) error { + favorite, err := cmd.Flags().GetBool("favorite") + if err != nil { + return err + } + own, err := cmd.Flags().GetBool("own") + if err != nil { + return err + } + groups, err := cmd.Flags().GetStringArray("group") + if err != nil { + return err + } + folderParents, err := cmd.Flags().GetStringArray("folder") + if err != nil { + return err + } + columns, err := cmd.Flags().GetStringArray("columns") + if err != nil { + return err + } + if len(columns) == 0 { + return fmt.Errorf("You need to specify atleast one column to return") + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + resources, err := client.GetResources(ctx, &api.GetResourcesOptions{ + FilterIsFavorite: favorite, + FilterIsOwnedByMe: own, + FilterIsSharedWithGroup: groups, + FilterHasParent: folderParents, + }) + if err != nil { + return fmt.Errorf("Listing Resource: %w", err) + } + + data := pterm.TableData{columns} + + for _, resource := range resources { + entry := make([]string, len(columns)) + for i := range columns { + switch strings.ToLower(columns[i]) { + case "id": + entry[i] = resource.ID + case "folderparentid": + entry[i] = resource.FolderParentID + case "name": + entry[i] = resource.Name + case "username": + entry[i] = resource.Username + case "uri": + entry[i] = resource.URI + case "password": + _, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID) + if err != nil { + return fmt.Errorf("Get Resource %w", err) + } + entry[i] = pass + case "description": + _, _, _, _, _, desc, err := helper.GetResource(ctx, client, resource.ID) + if err != nil { + return fmt.Errorf("Get Resource %w", err) + } + entry[i] = desc + default: + cmd.SilenceUsage = false + return fmt.Errorf("Unknown Column: %v", columns[i]) + } + } + data = append(data, entry) + } + + pterm.DefaultTable.WithHasHeader().WithData(data).Render() + return nil +} diff --git a/resource/move.go b/resource/move.go new file mode 100644 index 0000000..be835b6 --- /dev/null +++ b/resource/move.go @@ -0,0 +1,57 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// ResourceMoveCmd Moves a Passbolt Resource +var ResourceMoveCmd = &cobra.Command{ + Use: "resource", + Short: "Moves a Passbolt Resource into a Folder", + Long: `Moves a Passbolt Resource into a Folder`, + RunE: ResourceMove, +} + +func init() { + ResourceMoveCmd.Flags().String("id", "", "id of Resource to Move") + ResourceMoveCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to Move the Resource") + + ResourceMoveCmd.MarkFlagRequired("id") + ResourceMoveCmd.MarkFlagRequired("folderParentID") +} + +func ResourceMove(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + folderParentID, err := cmd.Flags().GetString("folderParentID") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.MoveResource( + ctx, + client, + id, + folderParentID, + ) + if err != nil { + return fmt.Errorf("Moving Resource: %w", err) + } + return nil +} diff --git a/resource/share.go b/resource/share.go new file mode 100644 index 0000000..1f64a83 --- /dev/null +++ b/resource/share.go @@ -0,0 +1,69 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// ResourceShareCmd Shares a Passbolt Resource +var ResourceShareCmd = &cobra.Command{ + Use: "resource", + Short: "Shares a Passbolt Resource", + Long: `Shares a Passbolt Resource`, + RunE: ResourceShare, +} + +func init() { + ResourceShareCmd.Flags().String("id", "", "id of Resource to Share") + ResourceShareCmd.Flags().IntP("type", "t", 1, "Permission Type (1 Read Only, 7 Can Update, 15 Owner)") + ResourceShareCmd.Flags().StringArrayP("users", "u", []string{}, "User id's to share with") + ResourceShareCmd.Flags().StringArrayP("groups", "g", []string{}, "Group id's to share with") + + ResourceShareCmd.MarkFlagRequired("id") + ResourceShareCmd.MarkFlagRequired("type") +} + +func ResourceShare(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + pType, err := cmd.Flags().GetInt("type") + if err != nil { + return err + } + users, err := cmd.Flags().GetStringArray("users") + if err != nil { + return err + } + groups, err := cmd.Flags().GetStringArray("groups") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.ShareResourceWithUsersAndGroups( + ctx, + client, + id, + users, + groups, + pType, + ) + if err != nil { + return fmt.Errorf("Sharing Resource: %w", err) + } + return nil +} diff --git a/resource/update.go b/resource/update.go new file mode 100644 index 0000000..47cdbc6 --- /dev/null +++ b/resource/update.go @@ -0,0 +1,80 @@ +package resource + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// ResourceUpdateCmd Updates a Passbolt Resource +var ResourceUpdateCmd = &cobra.Command{ + Use: "resource", + Short: "Updates a Passbolt Resource", + Long: `Updates a Passbolt Resource`, + RunE: ResourceUpdate, +} + +func init() { + ResourceUpdateCmd.Flags().String("id", "", "id of Resource to Update") + ResourceUpdateCmd.Flags().StringP("name", "n", "", "Resource Name") + ResourceUpdateCmd.Flags().StringP("username", "u", "", "Resource Username") + ResourceUpdateCmd.Flags().String("uri", "", "Resource URI") + ResourceUpdateCmd.Flags().StringP("password", "p", "", "Resource Password") + ResourceUpdateCmd.Flags().StringP("description", "d", "", "Resource Description") + + ResourceUpdateCmd.MarkFlagRequired("id") +} + +func ResourceUpdate(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + name, err := cmd.Flags().GetString("name") + if err != nil { + return err + } + username, err := cmd.Flags().GetString("username") + if err != nil { + return err + } + uri, err := cmd.Flags().GetString("uri") + if err != nil { + return err + } + password, err := cmd.Flags().GetString("password") + if err != nil { + return err + } + description, err := cmd.Flags().GetString("description") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.UpdateResource( + ctx, + client, + id, + name, + username, + uri, + password, + description, + ) + if err != nil { + return fmt.Errorf("Updating Resource: %w", err) + } + return nil +}