code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/permissions_describe.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package cmd
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"fmt"
    22  	"io"
    23  
    24  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/cli"
    25  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags"
    26  	"code.vegaprotocol.io/vega/cmd/vegawallet/commands/printer"
    27  	"code.vegaprotocol.io/vega/wallet/api"
    28  	"code.vegaprotocol.io/vega/wallet/wallets"
    29  
    30  	"github.com/spf13/cobra"
    31  )
    32  
    33  var (
    34  	describePermissionsLong = cli.LongDesc(`
    35  	    Describe the permissions associated to a given hostname.
    36  	`)
    37  
    38  	describePermissionsExample = cli.Examples(`
    39  		# Describe the permissions
    40  		{{.Software}} permissions describe --wallet WALLET --hostname HOSTNAME
    41  	`)
    42  )
    43  
    44  type DescribePermissionsHandler func(api.AdminDescribePermissionsParams, string) (api.AdminDescribePermissionsResult, error)
    45  
    46  func NewCmdDescribePermissions(w io.Writer, rf *RootFlags) *cobra.Command {
    47  	h := func(params api.AdminDescribePermissionsParams, passphrase string) (api.AdminDescribePermissionsResult, error) {
    48  		ctx := context.Background()
    49  
    50  		walletStore, err := wallets.InitialiseStore(rf.Home, false)
    51  		if err != nil {
    52  			return api.AdminDescribePermissionsResult{}, fmt.Errorf("couldn't initialise wallets store: %w", err)
    53  		}
    54  		defer walletStore.Close()
    55  
    56  		if _, errDetails := api.NewAdminUnlockWallet(walletStore).Handle(ctx, api.AdminUnlockWalletParams{
    57  			Wallet:     params.Wallet,
    58  			Passphrase: passphrase,
    59  		}); errDetails != nil {
    60  			return api.AdminDescribePermissionsResult{}, errors.New(errDetails.Data)
    61  		}
    62  
    63  		rawResult, errDetails := api.NewAdminDescribePermissions(walletStore).Handle(ctx, params)
    64  		if errDetails != nil {
    65  			return api.AdminDescribePermissionsResult{}, errors.New(errDetails.Data)
    66  		}
    67  		return rawResult.(api.AdminDescribePermissionsResult), nil
    68  	}
    69  
    70  	return BuildCmdDescribePermissions(w, h, rf)
    71  }
    72  
    73  func BuildCmdDescribePermissions(w io.Writer, handler DescribePermissionsHandler, rf *RootFlags) *cobra.Command {
    74  	f := &DescribePermissionsFlags{}
    75  	cmd := &cobra.Command{
    76  		Use:     "describe",
    77  		Short:   "Describe the permissions associated to the specified hostname",
    78  		Long:    describePermissionsLong,
    79  		Example: describePermissionsExample,
    80  		RunE: func(_ *cobra.Command, _ []string) error {
    81  			req, pass, err := f.Validate()
    82  			if err != nil {
    83  				return err
    84  			}
    85  			resp, err := handler(req, pass)
    86  			if err != nil {
    87  				return err
    88  			}
    89  
    90  			switch rf.Output {
    91  			case flags.InteractiveOutput:
    92  				PrintDescribePermissionsResult(w, resp)
    93  			case flags.JSONOutput:
    94  				return printer.FprintJSON(w, resp)
    95  			}
    96  
    97  			return nil
    98  		},
    99  	}
   100  
   101  	cmd.Flags().StringVarP(&f.Wallet,
   102  		"wallet", "w",
   103  		"",
   104  		"Name of the wallet",
   105  	)
   106  	cmd.Flags().StringVar(&f.Hostname,
   107  		"hostname",
   108  		"",
   109  		"Hostname to describe",
   110  	)
   111  	cmd.Flags().StringVarP(&f.PassphraseFile,
   112  		"passphrase-file", "p",
   113  		"",
   114  		"Path to the file containing the wallet's passphrase",
   115  	)
   116  
   117  	autoCompleteWallet(cmd, rf.Home, "wallet")
   118  
   119  	return cmd
   120  }
   121  
   122  type DescribePermissionsFlags struct {
   123  	Wallet         string
   124  	Hostname       string
   125  	PassphraseFile string
   126  }
   127  
   128  func (f *DescribePermissionsFlags) Validate() (api.AdminDescribePermissionsParams, string, error) {
   129  	if len(f.Wallet) == 0 {
   130  		return api.AdminDescribePermissionsParams{}, "", flags.MustBeSpecifiedError("wallet")
   131  	}
   132  
   133  	if len(f.Hostname) == 0 {
   134  		return api.AdminDescribePermissionsParams{}, "", flags.MustBeSpecifiedError("hostname")
   135  	}
   136  
   137  	passphrase, err := flags.GetPassphrase(f.PassphraseFile)
   138  	if err != nil {
   139  		return api.AdminDescribePermissionsParams{}, "", err
   140  	}
   141  
   142  	return api.AdminDescribePermissionsParams{
   143  		Wallet:   f.Wallet,
   144  		Hostname: f.Hostname,
   145  	}, passphrase, nil
   146  }
   147  
   148  func PrintDescribePermissionsResult(w io.Writer, resp api.AdminDescribePermissionsResult) {
   149  	p := printer.NewInteractivePrinter(w)
   150  
   151  	str := p.String()
   152  	defer p.Print(str)
   153  
   154  	str.Text("Public keys: ").NextLine()
   155  	str.Text("  Access mode: ").WarningText(fmt.Sprintf("%v", resp.Permissions.PublicKeys.Access)).NextLine()
   156  	if len(resp.Permissions.PublicKeys.AllowedKeys) != 0 {
   157  		str.Text("  Allowed keys: ").NextLine()
   158  		for _, k := range resp.Permissions.PublicKeys.AllowedKeys {
   159  			str.ListItem().Text("- ").WarningText(k).NextLine()
   160  		}
   161  	}
   162  }