code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/permissions_list.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 listPermissionsLong = cli.LongDesc(` 35 List all permitted hostnames for the specified wallet. 36 `) 37 38 listPermissionsExample = cli.Examples(` 39 # List all permitted hostnames for the specified wallet 40 {{.Software}} permissions list --wallet WALLET 41 `) 42 ) 43 44 type ListPermissionsHandler func(api.AdminListPermissionsParams, string) (api.AdminListPermissionsResult, error) 45 46 func NewCmdListPermissions(w io.Writer, rf *RootFlags) *cobra.Command { 47 h := func(params api.AdminListPermissionsParams, passphrase string) (api.AdminListPermissionsResult, error) { 48 ctx := context.Background() 49 50 walletStore, err := wallets.InitialiseStore(rf.Home, false) 51 if err != nil { 52 return api.AdminListPermissionsResult{}, 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.AdminListPermissionsResult{}, errors.New(errDetails.Data) 61 } 62 63 rawResult, errDetails := api.NewAdminListPermissions(walletStore).Handle(ctx, params) 64 if errDetails != nil { 65 return api.AdminListPermissionsResult{}, errors.New(errDetails.Data) 66 } 67 return rawResult.(api.AdminListPermissionsResult), nil 68 } 69 70 return BuildCmdListPermissions(w, h, rf) 71 } 72 73 func BuildCmdListPermissions(w io.Writer, handler ListPermissionsHandler, rf *RootFlags) *cobra.Command { 74 f := &ListPermissionsFlags{} 75 76 cmd := &cobra.Command{ 77 Use: "list", 78 Short: "List all permitted hostnames for the specified wallet", 79 Long: listPermissionsLong, 80 Example: listPermissionsExample, 81 RunE: func(_ *cobra.Command, _ []string) error { 82 req, pass, err := f.Validate() 83 if err != nil { 84 return err 85 } 86 87 resp, err := handler(req, pass) 88 if err != nil { 89 return err 90 } 91 92 switch rf.Output { 93 case flags.InteractiveOutput: 94 PrintListPermissionsResponse(w, resp) 95 case flags.JSONOutput: 96 return printer.FprintJSON(w, resp) 97 } 98 99 return nil 100 }, 101 } 102 103 cmd.Flags().StringVarP(&f.Wallet, 104 "wallet", "w", 105 "", 106 "Name of the wallet", 107 ) 108 cmd.Flags().StringVarP(&f.PassphraseFile, 109 "passphrase-file", "p", 110 "", 111 "Path to the file containing the wallet's passphrase", 112 ) 113 114 autoCompleteWallet(cmd, rf.Home, "wallet") 115 116 return cmd 117 } 118 119 type ListPermissionsFlags struct { 120 Wallet string 121 PassphraseFile string 122 } 123 124 func (f *ListPermissionsFlags) Validate() (api.AdminListPermissionsParams, string, error) { 125 if len(f.Wallet) == 0 { 126 return api.AdminListPermissionsParams{}, "", flags.MustBeSpecifiedError("wallet") 127 } 128 129 passphrase, err := flags.GetPassphrase(f.PassphraseFile) 130 if err != nil { 131 return api.AdminListPermissionsParams{}, "", err 132 } 133 134 return api.AdminListPermissionsParams{ 135 Wallet: f.Wallet, 136 }, passphrase, nil 137 } 138 139 func PrintListPermissionsResponse(w io.Writer, resp api.AdminListPermissionsResult) { 140 p := printer.NewInteractivePrinter(w) 141 142 str := p.String() 143 defer p.Print(str) 144 145 if len(resp.Permissions) == 0 { 146 str.InfoText("No permission has been given to any hostname").NextLine() 147 return 148 } 149 150 for hostname, permissions := range resp.Permissions { 151 str.Text(fmt.Sprintf("* %s", hostname)).NextLine() 152 for scope, access := range permissions { 153 str.Pad().Text(fmt.Sprintf("- %s: %s", scope, access)).NextLine() 154 } 155 str.NextLine() 156 } 157 }