[#885] cli: Support hex chain id in control API

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-12-21 15:55:09 +03:00 committed by Evgenii Stratonikov
parent c19396d203
commit 4a4c790ec1
4 changed files with 39 additions and 6 deletions

View file

@ -3,6 +3,7 @@ package control
import ( import (
"bytes" "bytes"
"crypto/sha256" "crypto/sha256"
"encoding/hex"
"encoding/json" "encoding/json"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
@ -42,6 +43,15 @@ func addRule(cmd *cobra.Command, _ []string) {
pk := key.Get(cmd) pk := key.Get(cmd)
chainID, _ := cmd.Flags().GetString(chainIDFlag) 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 var cnr cid.ID
cidStr, _ := cmd.Flags().GetString(commonflags.CIDFlag) cidStr, _ := cmd.Flags().GetString(commonflags.CIDFlag)
@ -54,7 +64,7 @@ func addRule(cmd *cobra.Command, _ []string) {
chain := new(apechain.Chain) chain := new(apechain.Chain)
commonCmd.ExitOnErr(cmd, "parser error: %w", util.ParseAPEChain(chain, []string{rule})) commonCmd.ExitOnErr(cmd, "parser error: %w", util.ParseAPEChain(chain, []string{rule}))
chain.ID = apechain.ID(chainID) chain.ID = apechain.ID(chainIDRaw)
serializedChain := chain.Bytes() serializedChain := chain.Bytes()
cmd.Println("CID: " + cidStr) cmd.Println("CID: " + cidStr)
@ -84,7 +94,8 @@ func addRule(cmd *cobra.Command, _ []string) {
verifyResponse(cmd, resp.GetSignature(), resp.GetBody()) 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() { func initControlAddRuleCmd() {
@ -94,4 +105,5 @@ func initControlAddRuleCmd() {
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
ff.String(ruleFlag, "", "Rule statement") ff.String(ruleFlag, "", "Rule statement")
ff.String(chainIDFlag, "", "Assign ID to the parsed chain") ff.String(chainIDFlag, "", "Assign ID to the parsed chain")
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
} }

View file

@ -2,6 +2,7 @@ package control
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/hex"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
@ -31,6 +32,13 @@ func getRule(cmd *cobra.Command, _ []string) {
cnr.Encode(rawCID) cnr.Encode(rawCID)
chainID, _ := cmd.Flags().GetString(chainIDFlag) 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{ req := &control.GetChainLocalOverrideRequest{
Body: &control.GetChainLocalOverrideRequest_Body{ 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())) commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(resp.GetBody().GetChain()))
// TODO (aarifullin): make pretty-formatted output for chains. // 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() { func initControGetRuleCmd() {
@ -69,4 +77,5 @@ func initControGetRuleCmd() {
ff := getRuleCmd.Flags() ff := getRuleCmd.Flags()
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
ff.String(chainIDFlag, "", "Chain id") ff.String(chainIDFlag, "", "Chain id")
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
} }

View file

@ -63,7 +63,7 @@ func listRules(cmd *cobra.Command, _ []string) {
// TODO (aarifullin): make pretty-formatted output for chains. // TODO (aarifullin): make pretty-formatted output for chains.
var chain apechain.Chain var chain apechain.Chain
commonCmd.ExitOnErr(cmd, "decode error: %w", chain.DecodeBytes(c)) 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()))
} }
} }

View file

@ -2,6 +2,7 @@ package control
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/hex"
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client" "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/rpc/client"
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags" "git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
@ -13,7 +14,8 @@ import (
) )
const ( const (
chainIDFlag = "chain-id" chainIDFlag = "chain-id"
chainIDHexFlag = "chain-id-hex"
) )
var removeRuleCmd = &cobra.Command{ var removeRuleCmd = &cobra.Command{
@ -34,6 +36,15 @@ func removeRule(cmd *cobra.Command, _ []string) {
cnr.Encode(rawCID) cnr.Encode(rawCID)
chainID, _ := cmd.Flags().GetString(chainIDFlag) 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{ req := &control.RemoveChainLocalOverrideRequest{
Body: &control.RemoveChainLocalOverrideRequest_Body{ Body: &control.RemoveChainLocalOverrideRequest_Body{
@ -41,7 +52,7 @@ func removeRule(cmd *cobra.Command, _ []string) {
Name: cidStr, Name: cidStr,
Type: control.ChainTarget_CONTAINER, Type: control.ChainTarget_CONTAINER,
}, },
ChainId: []byte(chainID), ChainId: chainIDRaw,
}, },
} }
@ -72,4 +83,5 @@ func initControlRemoveRuleCmd() {
ff := removeRuleCmd.Flags() ff := removeRuleCmd.Flags()
ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage) ff.String(commonflags.CIDFlag, "", commonflags.CIDFlagUsage)
ff.String(chainIDFlag, "", "Chain id") ff.String(chainIDFlag, "", "Chain id")
ff.Bool(chainIDHexFlag, false, "Flag to parse chain ID as hex")
} }