code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/permissions_purge.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  	vgterm "code.vegaprotocol.io/vega/libs/term"
    28  	"code.vegaprotocol.io/vega/wallet/api"
    29  	"code.vegaprotocol.io/vega/wallet/wallets"
    30  
    31  	"github.com/spf13/cobra"
    32  )
    33  
    34  var (
    35  	purgePermissionsLong = cli.LongDesc(`
    36  	    Purge all the permissions of the specified wallet
    37  	`)
    38  
    39  	purgePermissionsExample = cli.Examples(`
    40  		# Purge all the permissions of the specified wallet
    41  		{{.Software}} network purge --wallet WALLET
    42  
    43  		# Purge all the permissions of the specified wallet without 
    44          # asking for confirmation
    45  		{{.Software}} network purge --wallet WALLET --force
    46  	`)
    47  )
    48  
    49  type PurgePermissionsHandler func(api.AdminPurgePermissionsParams, string) error
    50  
    51  func NewCmdPurgePermissions(w io.Writer, rf *RootFlags) *cobra.Command {
    52  	h := func(params api.AdminPurgePermissionsParams, passphrase string) error {
    53  		ctx := context.Background()
    54  
    55  		walletStore, err := wallets.InitialiseStore(rf.Home, false)
    56  		if err != nil {
    57  			return fmt.Errorf("couldn't initialise wallets store: %w", err)
    58  		}
    59  		defer walletStore.Close()
    60  
    61  		if _, errDetails := api.NewAdminUnlockWallet(walletStore).Handle(ctx, api.AdminUnlockWalletParams{
    62  			Wallet:     params.Wallet,
    63  			Passphrase: passphrase,
    64  		}); errDetails != nil {
    65  			return errors.New(errDetails.Data)
    66  		}
    67  
    68  		_, errDetails := api.NewAdminPurgePermissions(walletStore).Handle(ctx, params)
    69  		if errDetails != nil {
    70  			return errors.New(errDetails.Data)
    71  		}
    72  		return nil
    73  	}
    74  
    75  	return BuildCmdPurgePermissions(w, h, rf)
    76  }
    77  
    78  func BuildCmdPurgePermissions(w io.Writer, handler PurgePermissionsHandler, rf *RootFlags) *cobra.Command {
    79  	f := &PurgePermissionsFlags{}
    80  	cmd := &cobra.Command{
    81  		Use:     "purge",
    82  		Short:   "Purge the permissions for the specified hostname",
    83  		Long:    purgePermissionsLong,
    84  		Example: purgePermissionsExample,
    85  		RunE: func(_ *cobra.Command, _ []string) error {
    86  			req, pass, err := f.Validate()
    87  			if err != nil {
    88  				return err
    89  			}
    90  
    91  			if !f.Force && vgterm.HasTTY() {
    92  				if !flags.AreYouSure() {
    93  					return nil
    94  				}
    95  			}
    96  
    97  			if err = handler(req, pass); err != nil {
    98  				return err
    99  			}
   100  
   101  			if rf.Output == flags.InteractiveOutput {
   102  				PrintPurgePermissionsResponse(w, f.Wallet)
   103  			}
   104  
   105  			return nil
   106  		},
   107  	}
   108  
   109  	cmd.Flags().StringVarP(&f.Wallet,
   110  		"wallet", "w",
   111  		"",
   112  		"Name of the wallet to purge",
   113  	)
   114  	cmd.Flags().BoolVarP(&f.Force,
   115  		"force", "f",
   116  		false,
   117  		"Do not ask for confirmation",
   118  	)
   119  	cmd.Flags().StringVarP(&f.PassphraseFile,
   120  		"passphrase-file", "p",
   121  		"",
   122  		"Path to the file containing the wallet's passphrase",
   123  	)
   124  
   125  	autoCompleteWallet(cmd, rf.Home, "wallet")
   126  
   127  	return cmd
   128  }
   129  
   130  type PurgePermissionsFlags struct {
   131  	Wallet         string
   132  	PassphraseFile string
   133  	Force          bool
   134  }
   135  
   136  func (f *PurgePermissionsFlags) Validate() (api.AdminPurgePermissionsParams, string, error) {
   137  	if len(f.Wallet) == 0 {
   138  		return api.AdminPurgePermissionsParams{}, "", flags.MustBeSpecifiedError("wallet")
   139  	}
   140  
   141  	passphrase, err := flags.GetPassphrase(f.PassphraseFile)
   142  	if err != nil {
   143  		return api.AdminPurgePermissionsParams{}, "", err
   144  	}
   145  
   146  	return api.AdminPurgePermissionsParams{
   147  		Wallet: f.Wallet,
   148  	}, passphrase, nil
   149  }
   150  
   151  func PrintPurgePermissionsResponse(w io.Writer, wallet string) {
   152  	p := printer.NewInteractivePrinter(w)
   153  	p.Print(p.String().CheckMark().SuccessText("All permissions on wallet ").SuccessBold(wallet).SuccessText(" have been purged.").NextLine())
   154  }