code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/permissions_revoke.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  	revokePermissionsLong = cli.LongDesc(`
    36  	    Revoke the permissions of the specified hostname
    37  	`)
    38  
    39  	revokePermissionsExample = cli.Examples(`
    40  		# Revoke the permissions for the specified hostname
    41  		{{.Software}} network revoke --wallet WALLET --hostname HOSTNAME
    42  
    43  		# Revoke the permissions for the specified hostname without 
    44          # asking for confirmation
    45  		{{.Software}} network revoke --wallet WALLET --hostname HOSTNAME --force
    46  	`)
    47  )
    48  
    49  type RevokePermissionsHandler func(api.AdminRevokePermissionsParams, string) error
    50  
    51  func NewCmdRevokePermissions(w io.Writer, rf *RootFlags) *cobra.Command {
    52  	h := func(params api.AdminRevokePermissionsParams, 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  		if _, errDetails := api.NewAdminRevokePermissions(walletStore).Handle(ctx, params); errDetails != nil {
    69  			return errors.New(errDetails.Data)
    70  		}
    71  		return nil
    72  	}
    73  
    74  	return BuildCmdRevokePermissions(w, h, rf)
    75  }
    76  
    77  func BuildCmdRevokePermissions(w io.Writer, handler RevokePermissionsHandler, rf *RootFlags) *cobra.Command {
    78  	f := &RevokePermissionsFlags{}
    79  	cmd := &cobra.Command{
    80  		Use:     "revoke",
    81  		Short:   "Revoke the permissions for the specified hostname",
    82  		Long:    revokePermissionsLong,
    83  		Example: revokePermissionsExample,
    84  		RunE: func(_ *cobra.Command, _ []string) error {
    85  			req, pass, err := f.Validate()
    86  			if err != nil {
    87  				return err
    88  			}
    89  
    90  			if !f.Force && vgterm.HasTTY() {
    91  				if !flags.AreYouSure() {
    92  					return nil
    93  				}
    94  			}
    95  
    96  			if err = handler(req, pass); err != nil {
    97  				return err
    98  			}
    99  
   100  			if rf.Output == flags.InteractiveOutput {
   101  				PrintRevokePermissionsResponse(w, req)
   102  			}
   103  
   104  			return nil
   105  		},
   106  	}
   107  
   108  	cmd.Flags().StringVarP(&f.Wallet,
   109  		"wallet", "w",
   110  		"",
   111  		"Name of the wallet",
   112  	)
   113  	cmd.Flags().StringVar(&f.Hostname,
   114  		"hostname",
   115  		"",
   116  		"Hostname from which access is revoked",
   117  	)
   118  	cmd.Flags().StringVarP(&f.PassphraseFile,
   119  		"passphrase-file", "p",
   120  		"",
   121  		"Path to the file containing the wallet's passphrase",
   122  	)
   123  	cmd.Flags().BoolVarP(&f.Force,
   124  		"force", "f",
   125  		false,
   126  		"Do not ask for confirmation",
   127  	)
   128  
   129  	autoCompleteWallet(cmd, rf.Home, "wallet")
   130  
   131  	return cmd
   132  }
   133  
   134  type RevokePermissionsFlags struct {
   135  	Wallet         string
   136  	Hostname       string
   137  	Force          bool
   138  	PassphraseFile string
   139  }
   140  
   141  func (f *RevokePermissionsFlags) Validate() (api.AdminRevokePermissionsParams, string, error) {
   142  	if len(f.Wallet) == 0 {
   143  		return api.AdminRevokePermissionsParams{}, "", flags.MustBeSpecifiedError("wallet")
   144  	}
   145  
   146  	if len(f.Hostname) == 0 {
   147  		return api.AdminRevokePermissionsParams{}, "", flags.MustBeSpecifiedError("hostname")
   148  	}
   149  
   150  	passphrase, err := flags.GetPassphrase(f.PassphraseFile)
   151  	if err != nil {
   152  		return api.AdminRevokePermissionsParams{}, "", err
   153  	}
   154  
   155  	return api.AdminRevokePermissionsParams{
   156  		Wallet:   f.Wallet,
   157  		Hostname: f.Hostname,
   158  	}, passphrase, nil
   159  }
   160  
   161  func PrintRevokePermissionsResponse(w io.Writer, req api.AdminRevokePermissionsParams) {
   162  	p := printer.NewInteractivePrinter(w)
   163  	str := p.String()
   164  	defer p.Print(str)
   165  	str.CheckMark().SuccessText("Permissions for hostname ").SuccessBold(req.Hostname).SuccessText(" has been revoked from wallet ").SuccessBold(req.Wallet).SuccessText(".").NextLine()
   166  }