Skip to content

Commit

Permalink
Adds permission subcommand to get a list of permissions of a resource
Browse files Browse the repository at this point in the history
  • Loading branch information
froque committed Sep 30, 2024
1 parent c454e73 commit 9b2d47f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
105 changes: 105 additions & 0 deletions resource/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

"github.com/alessio/shellescape"
"github.com/passbolt/go-passbolt-cli/util"
"github.com/passbolt/go-passbolt/helper"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

Expand All @@ -19,10 +23,24 @@ var ResourceGetCmd = &cobra.Command{
RunE: ResourceGet,
}

// ResourcePermissionCmd Gets Permissions for Passbolt Resource
var ResourcePermissionCmd = &cobra.Command{
Use: "permission",
Short: "Gets Permissions for a Passbolt Resource",
Long: `Gets Permissions for a Passbolt Resource`,
Aliases: []string{"permissions"},
RunE: ResourcePermission,
}

func init() {
ResourceGetCmd.Flags().String("id", "", "id of Resource to Get")

ResourceGetCmd.MarkFlagRequired("id")

ResourceGetCmd.AddCommand(ResourcePermissionCmd)
ResourcePermissionCmd.Flags().String("id", "", "id of Resource to Get")
ResourcePermissionCmd.Flags().StringArrayP("column", "c", []string{"ID", "Aco", "AcoForeignKey", "Aro", "AroForeignKey", "Type"}, "Columns to return, possible Columns:\nID, Aco, AcoForeignKey, Aro, AroForeignKey, Type, CreatedTimestamp, ModifiedTimestamp")

}

func ResourceGet(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -76,3 +94,90 @@ func ResourceGet(cmd *cobra.Command, args []string) error {
}
return nil
}

func ResourcePermission(cmd *cobra.Command, args []string) error {
resource, err := cmd.Flags().GetString("id")
if err != nil {
return err
}
columns, err := cmd.Flags().GetStringArray("column")
if err != nil {
return err
}
if len(columns) == 0 {
return fmt.Errorf("You need to specify atleast one column to return")
}
jsonOutput, err := cmd.Flags().GetBool("json")
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

permissions, err := client.GetResourcePermissions(ctx, resource)
if err != nil {
return fmt.Errorf("Listing Permission: %w", err)
}

if jsonOutput {
outputPermissions := []PermissionJsonOutput{}
for i := range permissions {
outputPermissions = append(outputPermissions, PermissionJsonOutput{
ID: &permissions[i].ID,
Aco: &permissions[i].ACO,
AcoForeignKey: &permissions[i].ACOForeignKey,
Aro: &permissions[i].ARO,
AroForeignKey: &permissions[i].AROForeignKey,
Type: &permissions[i].Type,
CreatedTimestamp: &permissions[i].Created.Time,
ModifiedTimestamp: &permissions[i].Modified.Time,
})
}
jsonPermissions, err := json.MarshalIndent(outputPermissions, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsonPermissions))
} else {
data := pterm.TableData{columns}

for _, permission := range permissions {
entry := make([]string, len(columns))
for i := range columns {
switch strings.ToLower(columns[i]) {
case "id":
entry[i] = permission.ID
case "aco":
entry[i] = permission.ACO
case "acoforeignkey":
entry[i] = permission.ACOForeignKey
case "aro":
entry[i] = permission.ARO
case "aroforeignkey":
entry[i] = permission.AROForeignKey
case "type":
entry[i] = strconv.Itoa(permission.Type)
case "createdtimestamp":
entry[i] = permission.Created.Format(time.RFC3339)
case "modifiedtimestamp":
entry[i] = permission.Modified.Format(time.RFC3339)
default:
cmd.SilenceUsage = false
return fmt.Errorf("Unknown Column: %v", columns[i])
}
}
data = append(data, entry)
}

pterm.DefaultTable.WithHasHeader().WithData(data).Render()
}

return nil
}
11 changes: 11 additions & 0 deletions resource/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,14 @@ type ResourceJsonOutput struct {
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
}

type PermissionJsonOutput struct {
ID *string `json:"id,omitempty"`
Aco *string `json:"aco,omitempty"`
AcoForeignKey *string `json:"aco_foreign_key,omitempty"`
Aro *string `json:"aro,omitempty"`
AroForeignKey *string `json:"aro_foreign_key,omitempty"`
Type *int `json:"type,omitempty"`
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
}

0 comments on commit 9b2d47f

Please sign in to comment.