Use bytes in control service proto definition for chainID #885

Merged
fyrchik merged 2 commits from dkirillov/frostfs-node:feature/control_api_make_chain_id_bytes into master 2024-01-11 07:24:27 +00:00
10 changed files with 45 additions and 12 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{
@ -38,7 +46,7 @@ func getRule(cmd *cobra.Command, _ []string) {
Name: cidStr, Name: cidStr,
Type: control.ChainTarget_CONTAINER, Type: control.ChainTarget_CONTAINER,
}, },
ChainId: chainID, ChainId: []byte(chainID),
}, },
} }
@ -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: 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")
} }

Binary file not shown.

View file

@ -7,7 +7,7 @@ import (
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/services/control"
apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain" apechain "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
engine "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine" "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
) )
@ -51,7 +51,7 @@ func (s *Server) AddChainLocalOverride(_ context.Context, req *control.AddChainL
resp := &control.AddChainLocalOverrideResponse{ resp := &control.AddChainLocalOverrideResponse{
Body: &control.AddChainLocalOverrideResponse_Body{ Body: &control.AddChainLocalOverrideResponse_Body{
ChainId: string(chain.ID), ChainId: []byte(chain.ID),
}, },
} }
err = SignMessage(s.key, resp) err = SignMessage(s.key, resp)

Binary file not shown.

View file

@ -446,7 +446,7 @@ message AddChainLocalOverrideResponse {
// Chain ID assigned for the added rule chain. // Chain ID assigned for the added rule chain.
// If chain ID is left empty in the request, then // If chain ID is left empty in the request, then
// it will be generated. // it will be generated.
string chain_id = 1; bytes chain_id = 1;
} }
Body body = 1; Body body = 1;
@ -461,7 +461,7 @@ message GetChainLocalOverrideRequest {
ChainTarget target = 1; ChainTarget target = 1;
// Chain ID assigned for the added rule chain. // Chain ID assigned for the added rule chain.
string chain_id = 2; bytes chain_id = 2;
} }
Body body = 1; Body body = 1;
@ -511,7 +511,7 @@ message RemoveChainLocalOverrideRequest {
ChainTarget target = 1; ChainTarget target = 1;
// Chain ID assigned for the added rule chain. // Chain ID assigned for the added rule chain.
string chain_id = 2; bytes chain_id = 2;
} }
Body body = 1; Body body = 1;

Binary file not shown.

Binary file not shown.