From 4a4c790ec1d0ee2d72200c2fc9baf4df0aa67673 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Thu, 21 Dec 2023 15:55:09 +0300 Subject: [PATCH] [#885] cli: Support hex chain id in control API Signed-off-by: Denis Kirillov --- cmd/frostfs-cli/modules/control/add_rule.go | 16 ++++++++++++++-- cmd/frostfs-cli/modules/control/get_rule.go | 11 ++++++++++- cmd/frostfs-cli/modules/control/list_rules.go | 2 +- cmd/frostfs-cli/modules/control/remove_rule.go | 16 ++++++++++++++-- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/cmd/frostfs-cli/modules/control/add_rule.go b/cmd/frostfs-cli/modules/control/add_rule.go index 47233678d..4fe8194bb 100644 --- a/cmd/frostfs-cli/modules/control/add_rule.go +++ b/cmd/frostfs-cli/modules/control/add_rule.go @@ -3,6 +3,7 @@ package control import ( "bytes" "crypto/sha256" + "encoding/hex" "encoding/json" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" @@ -42,6 +43,15 @@ func addRule(cmd *cobra.Command, _ []string) { pk := key.Get(cmd) chainID, _ := cmd.Flags().GetString(chainIDFlag) + hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag) + + chainIDRaw := []byte(chainID) + + if hexEncoded { + var err error + chainIDRaw, err = hex.DecodeString(chainID) + commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err) + } var cnr cid.ID cidStr, _ := cmd.Flags().GetString(commonflags.CIDFlag) @@ -54,7 +64,7 @@ func addRule(cmd *cobra.Command, _ []string) { chain := new(apechain.Chain) commonCmd.ExitOnErr(cmd, "parser error: %w", util.ParseAPEChain(chain, []string{rule})) - chain.ID = apechain.ID(chainID) + chain.ID = apechain.ID(chainIDRaw) serializedChain := chain.Bytes() cmd.Println("CID: " + cidStr) @@ -84,7 +94,8 @@ func addRule(cmd *cobra.Command, _ []string) { verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) - cmd.Println("Rule has been added. Chain id: ", resp.GetBody().GetChainId()) + chainIDRaw = resp.GetBody().GetChainId() + cmd.Printf("Rule has been added.\nChain id: '%s'\nChain id hex: '%x'\n", string(chainIDRaw), chainIDRaw) } func initControlAddRuleCmd() { @@ -94,4 +105,5 @@ func initControlAddRuleCmd() { ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(ruleFlag, "", "Rule statement") ff.String(chainIDFlag, "", "Assign ID to the parsed chain") + ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex") } diff --git a/cmd/frostfs-cli/modules/control/get_rule.go b/cmd/frostfs-cli/modules/control/get_rule.go index 2bb6a7da2..9508850ee 100644 --- a/cmd/frostfs-cli/modules/control/get_rule.go +++ b/cmd/frostfs-cli/modules/control/get_rule.go @@ -2,6 +2,7 @@ package control import ( "crypto/sha256" + "encoding/hex" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" @@ -31,6 +32,13 @@ func getRule(cmd *cobra.Command, _ []string) { cnr.Encode(rawCID) chainID, _ := cmd.Flags().GetString(chainIDFlag) + hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag) + + if hexEncoded { + chainIDBytes, err := hex.DecodeString(chainID) + commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err) + chainID = string(chainIDBytes) + } req := &control.GetChainLocalOverrideRequest{ Body: &control.GetChainLocalOverrideRequest_Body{ @@ -60,7 +68,7 @@ func getRule(cmd *cobra.Command, _ []string) { commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(resp.GetBody().GetChain())) // TODO (aarifullin): make pretty-formatted output for chains. - cmd.Println("Parsed chain:\n" + prettyJSONFormat(cmd, chain.Bytes())) + cmd.Printf("Parsed chain (chain id hex: '%x'):\n%s\n", chain.ID, prettyJSONFormat(cmd, chain.Bytes())) } func initControGetRuleCmd() { @@ -69,4 +77,5 @@ func initControGetRuleCmd() { ff := getRuleCmd.Flags() ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(chainIDFlag, "", "Chain id") + ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex") } diff --git a/cmd/frostfs-cli/modules/control/list_rules.go b/cmd/frostfs-cli/modules/control/list_rules.go index f087f5367..ef494b135 100644 --- a/cmd/frostfs-cli/modules/control/list_rules.go +++ b/cmd/frostfs-cli/modules/control/list_rules.go @@ -63,7 +63,7 @@ func listRules(cmd *cobra.Command, _ []string) { // TODO (aarifullin): make pretty-formatted output for chains. var chain apechain.Chain commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(c)) - cmd.Println("Parsed chain:\n" + prettyJSONFormat(cmd, chain.Bytes())) + cmd.Printf("Parsed chain (chain id hex: '%x'):\n%s\n", chain.ID, prettyJSONFormat(cmd, chain.Bytes())) } } diff --git a/cmd/frostfs-cli/modules/control/remove_rule.go b/cmd/frostfs-cli/modules/control/remove_rule.go index 60e5376a1..612ab7d0a 100644 --- a/cmd/frostfs-cli/modules/control/remove_rule.go +++ b/cmd/frostfs-cli/modules/control/remove_rule.go @@ -2,6 +2,7 @@ package control import ( "crypto/sha256" + "encoding/hex" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" @@ -13,7 +14,8 @@ import ( ) const ( - chainIDFlag = "chain-id" + chainIDFlag = "chain-id" + chainIDHexFlag = "chain-id-hex" ) var removeRuleCmd = &cobra.Command{ @@ -34,6 +36,15 @@ func removeRule(cmd *cobra.Command, _ []string) { cnr.Encode(rawCID) chainID, _ := cmd.Flags().GetString(chainIDFlag) + hexEncoded, _ := cmd.Flags().GetBool(chainIDHexFlag) + + chainIDRaw := []byte(chainID) + + if hexEncoded { + var err error + chainIDRaw, err = hex.DecodeString(chainID) + commonCmd.ExitOnErr(cmd, "can't decode chain ID as hex: %w", err) + } req := &control.RemoveChainLocalOverrideRequest{ Body: &control.RemoveChainLocalOverrideRequest_Body{ @@ -41,7 +52,7 @@ func removeRule(cmd *cobra.Command, _ []string) { Name: cidStr, Type: control.ChainTarget_CONTAINER, }, - ChainId: []byte(chainID), + ChainId: chainIDRaw, }, } @@ -72,4 +83,5 @@ func initControlRemoveRuleCmd() { ff := removeRuleCmd.Flags() ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(chainIDFlag, "", "Chain id") + ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex") }