Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add permissioned keys cli #2644

Merged
merged 2 commits into from
Dec 11, 2024
Merged

Add permissioned keys cli #2644

merged 2 commits into from
Dec 11, 2024

Conversation

jayy04
Copy link
Contributor

@jayy04 jayy04 commented Dec 11, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced a command-line interface (CLI) for querying functionalities in the accountplus module.
    • Added commands for querying module parameters and authenticators.
    • Implemented transaction commands for adding and removing authenticators.
  • Bug Fixes

    • Enhanced error handling for command execution and context retrieval.
  • Documentation

    • Updated module structure to reflect new command functionalities.

@jayy04 jayy04 requested a review from a team as a code owner December 11, 2024 00:19
Copy link
Contributor

coderabbitai bot commented Dec 11, 2024

Walkthrough

The changes introduce new command-line interface (CLI) functionalities for the accountplus module in the Cosmos SDK. Two new files, query.go and tx.go, define commands for querying and transaction operations related to authenticators. The module.go file is updated to integrate these commands into the module's structure, allowing for user-submitted transactions and queries. The new commands include functionalities for adding, removing, and retrieving authenticators, with appropriate error handling implemented throughout.

Changes

File Change Summary
protocol/x/accountplus/client/cli/query.go Added CLI commands for querying authenticators: GetQueryCmd, CmdQueryParam, CmdQueryGetAuthenticator, and CmdQueryGetAllAuthenticators.
protocol/x/accountplus/client/cli/tx.go Added CLI commands for transaction operations: GetTxCmd, CmdAddAuthenticator, and CmdRemoveAuthenticator.
protocol/x/accountplus/module.go Updated GetTxCmd and GetQueryCmd methods to return commands from the cli package, integrating new CLI functionalities into the accountplus module.

Possibly related PRs

Suggested labels

proto

Suggested reviewers

  • vincentwschau

Poem

🐰 In the land of code where rabbits play,
New commands hop in, brightening the day.
Query and transact, oh what a delight,
With authenticators, we take flight!
So gather 'round, let’s cheer and sing,
For the CLI magic this change will bring! 🌟


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4beed22 and 00e8976.

📒 Files selected for processing (2)
  • protocol/x/accountplus/client/cli/query.go (1 hunks)
  • protocol/x/accountplus/client/cli/tx.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • protocol/x/accountplus/client/cli/tx.go
  • protocol/x/accountplus/client/cli/query.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 7da33a6 and 4beed22.

📒 Files selected for processing (3)
  • protocol/x/accountplus/client/cli/query.go (1 hunks)
  • protocol/x/accountplus/client/cli/tx.go (1 hunks)
  • protocol/x/accountplus/module.go (2 hunks)
🔇 Additional comments (2)
protocol/x/accountplus/module.go (2)

75-75: LGTM: CLI commands properly integrated

The integration of CLI commands follows the standard Cosmos SDK module patterns.

Also applies to: 81-81


Line range hint 1-112: Verify command naming and documentation consistency across the module

Let's ensure the command naming and documentation are consistent across the codebase.

✅ Verification successful

The previous results showed some command patterns, but we need a broader view of CLI commands across the codebase to verify consistency. This script will help us analyze the command naming patterns, documentation styles, and argument handling across all CLI files in the codebase.


Command naming and documentation patterns are consistent across the module

Based on the comprehensive analysis of CLI commands across the codebase:

  1. The accountplus module's command naming follows consistent patterns:

    • Query commands use the get- prefix (e.g., get-authenticator, get-all-authenticators)
    • Action commands use clear verbs (e.g., add-authenticator, remove-authenticator)
  2. Documentation is complete and follows consistent patterns:

    • All commands have clear Use and Short descriptions
    • Arguments are properly defined using cobra.ExactArgs
    • Command structure aligns with other modules in the codebase (e.g., similar to affiliate, vault, and other modules)
  3. The command argument handling is consistent:

    • Uses cobra.ExactArgs with appropriate argument counts
    • Follows the same pattern as other modules in the codebase
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for consistent command naming and documentation patterns
echo "Checking command naming patterns..."
rg -A 2 'Use:.*authenticator' 

echo "Checking for command documentation..."
rg 'Short:.*authenticator'

echo "Checking for similar commands in other modules..."
rg -g '*/client/cli/*.go' 'func Cmd.*Authenticator'

Length of output: 1866


Script:

#!/bin/bash
# Let's verify command naming patterns more comprehensively
echo "Checking command naming patterns in CLI files..."
fd -e go -p 'cli' --exec rg -A 1 'Use:.*".*"' {}

echo "Checking command documentation patterns..."
fd -e go -p 'cli' --exec rg -A 1 'Short:.*".*"' {}

echo "Checking command argument patterns..."
fd -e go -p 'cli' --exec rg 'Args:\s+cobra.ExactArgs' {}

Length of output: 20686

Comment on lines 28 to 48
func CmdAddAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "add-authenticator account authenticator_type data",
Short: "Registers an authenticator",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
msg := types.MsgAddAuthenticator{
Sender: args[0],
AuthenticatorType: args[1],
Data: []byte(args[2]),
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add input validation for authenticator_type and data parameters

The command accepts raw input for authenticator_type and data without validation. This could lead to security issues if malformed or malicious data is provided.

Consider:

  1. Validating authenticator_type against a predefined list of allowed types
  2. Adding size limits and format validation for the data parameter
  3. Adding documentation about accepted formats
 func CmdAddAuthenticator() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "add-authenticator account authenticator_type data",
-		Short: "Registers an authenticator",
+		Short: "Registers an authenticator with the specified type and data",
+		Long: `Registers an authenticator for an account.
+Supported authenticator types: [list supported types]
+Data format: [specify format requirements]`,
 		Args:  cobra.ExactArgs(3),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientTxContext(cmd)
 			if err != nil {
 				return err
 			}
+			// Validate authenticator type
+			if !types.IsValidAuthenticatorType(args[1]) {
+				return fmt.Errorf("invalid authenticator type: %s", args[1])
+			}
+			// Validate data format and size
+			if err := types.ValidateAuthenticatorData(args[2]); err != nil {
+				return fmt.Errorf("invalid authenticator data: %v", err)
+			}
 			msg := types.MsgAddAuthenticator{
 				Sender:            args[0],
 				AuthenticatorType: args[1],

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +50 to +73
func CmdRemoveAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "remove-authenticator [sender] [authenticator_id]",
Short: "Removes an authenticator",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
msg := types.MsgRemoveAuthenticator{
Sender: args[0],
Id: id,
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add confirmation prompt for authenticator removal

Removing an authenticator is a sensitive operation that should require confirmation to prevent accidental removals.

Add a confirmation prompt using the SDK's input package:

 func CmdRemoveAuthenticator() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "remove-authenticator [sender] [authenticator_id]",
-		Short: "Removes an authenticator",
+		Short: "Removes an authenticator (requires confirmation)",
 		Args:  cobra.ExactArgs(2),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientTxContext(cmd)
 			if err != nil {
 				return err
 			}
 			id, err := cast.ToUint64E(args[1])
 			if err != nil {
 				return err
 			}
+			buf := bufio.NewReader(cmd.InOrStdin())
+			prompt := fmt.Sprintf("Are you sure you want to remove authenticator %d? (y/N): ", id)
+			if !input.GetConfirmation(prompt, buf) {
+				return fmt.Errorf("operation cancelled")
+			}
 			msg := types.MsgRemoveAuthenticator{

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines 57 to 86
func CmdQueryGetAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "get-authenticator [account] [authenticator_id]",
Short: "Get authenticator",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticator(
context.Background(),
&types.GetAuthenticatorRequest{
Account: args[0],
AuthenticatorId: id,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix argument count mismatch in get-authenticator command

The command's Use string indicates it needs 2 arguments, but Args: cobra.ExactArgs(3) requires 3 arguments.

Fix the argument count:

 func CmdQueryGetAuthenticator() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "get-authenticator [account] [authenticator_id]",
 		Short: "Get authenticator",
-		Args:  cobra.ExactArgs(3),
+		Args:  cobra.ExactArgs(2),
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func CmdQueryGetAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "get-authenticator [account] [authenticator_id]",
Short: "Get authenticator",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticator(
context.Background(),
&types.GetAuthenticatorRequest{
Account: args[0],
AuthenticatorId: id,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}
func CmdQueryGetAuthenticator() *cobra.Command {
cmd := &cobra.Command{
Use: "get-authenticator [account] [authenticator_id]",
Short: "Get authenticator",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
id, err := cast.ToUint64E(args[1])
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticator(
context.Background(),
&types.GetAuthenticatorRequest{
Account: args[0],
AuthenticatorId: id,
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}

Comment on lines 88 to 112
func CmdQueryGetAllAuthenticators() *cobra.Command {
cmd := &cobra.Command{
Use: "get-all-authenticators [account]",
Short: "Get all authenticators for an account",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)
res, err := queryClient.GetAuthenticators(
context.Background(),
&types.GetAuthenticatorsRequest{
Account: args[0],
},
)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
return cmd
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add pagination support for get-all-authenticators command

For accounts with many authenticators, the response should be paginated to prevent overwhelming responses.

Add pagination flags and logic:

 func CmdQueryGetAllAuthenticators() *cobra.Command {
 	cmd := &cobra.Command{
 		Use:   "get-all-authenticators [account]",
 		Short: "Get all authenticators for an account",
 		Args:  cobra.ExactArgs(1),
 		RunE: func(cmd *cobra.Command, args []string) error {
 			clientCtx, err := client.GetClientQueryContext(cmd)
 			if err != nil {
 				return err
 			}
+			pageReq, err := client.ReadPageRequest(cmd.Flags())
+			if err != nil {
+				return err
+			}
 			queryClient := types.NewQueryClient(clientCtx)
 			res, err := queryClient.GetAuthenticators(
 				context.Background(),
 				&types.GetAuthenticatorsRequest{
 					Account: args[0],
+					Pagination: pageReq,
 				},
 			)
 			if err != nil {
 				return err
 			}
 			return clientCtx.PrintProto(res)
 		},
 	}
+	flags.AddPaginationFlagsToCmd(cmd, "authenticators")
 	return cmd
 }

Committable suggestion skipped: line range outside the PR's diff.

@jayy04 jayy04 merged commit 03c53ce into main Dec 11, 2024
22 checks passed
@jayy04 jayy04 deleted the jy/permissioned-keys-cli branch December 11, 2024 21:24
@jayy04
Copy link
Contributor Author

jayy04 commented Dec 11, 2024

@Mergifyio backport release/protocol/v8.x

Copy link
Contributor

mergify bot commented Dec 11, 2024

backport release/protocol/v8.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Dec 11, 2024
(cherry picked from commit 03c53ce)
jayy04 added a commit that referenced this pull request Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

2 participants