[#1] chain: Fix ID type from string to bytes

ID may be non UTF-8 string, so from developers POV
it is just byte slice.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2024-01-18 11:01:39 +03:00
parent 5f13d91c0d
commit dd0f582fc3
8 changed files with 57 additions and 60 deletions

View file

@ -10,7 +10,7 @@ import (
) )
// ID is the ID of rule chain. // ID is the ID of rule chain.
type ID string type ID []byte
// MatchType is the match type for chain rules. // MatchType is the match type for chain rules.
type MatchType uint8 type MatchType uint8

View file

@ -37,7 +37,12 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(in *jl
} }
switch key { switch key {
case "ID": case "ID":
(out.ID).UnmarshalEasyJSON(in) if in.IsNull() {
in.Skip()
out.ID = nil
} else {
out.ID = in.Bytes()
}
case "Rules": case "Rules":
if in.IsNull() { if in.IsNull() {
in.Skip() in.Skip()
@ -54,9 +59,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(in *jl
out.Rules = (out.Rules)[:0] out.Rules = (out.Rules)[:0]
} }
for !in.IsDelim(']') { for !in.IsDelim(']') {
var v1 Rule var v2 Rule
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in, &v1) easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in, &v2)
out.Rules = append(out.Rules, v1) out.Rules = append(out.Rules, v2)
in.WantComma() in.WantComma()
} }
in.Delim(']') in.Delim(']')
@ -80,7 +85,7 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(out *j
{ {
const prefix string = ",\"ID\":" const prefix string = ",\"ID\":"
out.RawString(prefix[1:]) out.RawString(prefix[1:])
(in.ID).MarshalEasyJSON(out) out.Base64Bytes(in.ID)
} }
{ {
const prefix string = ",\"Rules\":" const prefix string = ",\"Rules\":"
@ -89,11 +94,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain(out *j
out.RawString("null") out.RawString("null")
} else { } else {
out.RawByte('[') out.RawByte('[')
for v2, v3 := range in.Rules { for v5, v6 := range in.Rules {
if v2 > 0 { if v5 > 0 {
out.RawByte(',') out.RawByte(',')
} }
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out, v3) easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out, v6)
} }
out.RawByte(']') out.RawByte(']')
} }
@ -172,9 +177,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(in *j
out.Condition = (out.Condition)[:0] out.Condition = (out.Condition)[:0]
} }
for !in.IsDelim(']') { for !in.IsDelim(']') {
var v4 Condition var v7 Condition
easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(in, &v4) easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(in, &v7)
out.Condition = append(out.Condition, v4) out.Condition = append(out.Condition, v7)
in.WantComma() in.WantComma()
} }
in.Delim(']') in.Delim(']')
@ -220,11 +225,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain1(out *
out.RawString("null") out.RawString("null")
} else { } else {
out.RawByte('[') out.RawByte('[')
for v5, v6 := range in.Condition { for v8, v9 := range in.Condition {
if v5 > 0 { if v8 > 0 {
out.RawByte(',') out.RawByte(',')
} }
easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(out, v6) easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain4(out, v9)
} }
out.RawByte(']') out.RawByte(']')
} }
@ -331,9 +336,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain3(in *j
out.Names = (out.Names)[:0] out.Names = (out.Names)[:0]
} }
for !in.IsDelim(']') { for !in.IsDelim(']') {
var v7 string var v10 string
v7 = string(in.String()) v10 = string(in.String())
out.Names = append(out.Names, v7) out.Names = append(out.Names, v10)
in.WantComma() in.WantComma()
} }
in.Delim(']') in.Delim(']')
@ -364,11 +369,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain3(out *
out.RawString("null") out.RawString("null")
} else { } else {
out.RawByte('[') out.RawByte('[')
for v8, v9 := range in.Names { for v11, v12 := range in.Names {
if v8 > 0 { if v11 > 0 {
out.RawByte(',') out.RawByte(',')
} }
out.String(string(v9)) out.String(string(v12))
} }
out.RawByte(']') out.RawByte(']')
} }
@ -412,9 +417,9 @@ func easyjsonE2758465DecodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain2(in *j
out.Names = (out.Names)[:0] out.Names = (out.Names)[:0]
} }
for !in.IsDelim(']') { for !in.IsDelim(']') {
var v10 string var v13 string
v10 = string(in.String()) v13 = string(in.String())
out.Names = append(out.Names, v10) out.Names = append(out.Names, v13)
in.WantComma() in.WantComma()
} }
in.Delim(']') in.Delim(']')
@ -445,11 +450,11 @@ func easyjsonE2758465EncodeGitFrostfsInfoTrueCloudLabPolicyEnginePkgChain2(out *
out.RawString("null") out.RawString("null")
} else { } else {
out.RawByte('[') out.RawByte('[')
for v11, v12 := range in.Names { for v14, v15 := range in.Names {
if v11 > 0 { if v14 > 0 {
out.RawByte(',') out.RawByte(',')
} }
out.String(string(v12)) out.String(string(v15))
} }
out.RawByte(']') out.RawByte(']')
} }

View file

@ -19,7 +19,7 @@ var (
func (c *Chain) MarshalBinary() ([]byte, error) { func (c *Chain) MarshalBinary() ([]byte, error) {
s := marshal.UInt8Size // Marshaller version s := marshal.UInt8Size // Marshaller version
s += marshal.UInt8Size // Chain version s += marshal.UInt8Size // Chain version
s += marshal.StringSize(string(c.ID)) s += marshal.SliceSize(c.ID, func(byte) int { return marshal.ByteSize })
s += marshal.SliceSize(c.Rules, ruleSize) s += marshal.SliceSize(c.Rules, ruleSize)
s += marshal.UInt8Size // MatchType s += marshal.UInt8Size // MatchType
@ -34,7 +34,7 @@ func (c *Chain) MarshalBinary() ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
offset, err = marshal.StringMarshal(buf, offset, string(c.ID)) offset, err = marshal.SliceMarshal(buf, offset, c.ID, marshal.ByteMarshal)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -72,11 +72,11 @@ func (c *Chain) UnmarshalBinary(data []byte) error {
return fmt.Errorf("unsupported chain version %d", chainVersion) return fmt.Errorf("unsupported chain version %d", chainVersion)
} }
idStr, offset, err := marshal.StringUnmarshal(data, offset) idBytes, offset, err := marshal.SliceUnmarshal(data, offset, marshal.ByteUnmarshal)
if err != nil { if err != nil {
return err return err
} }
c.ID = ID(idStr) c.ID = ID(idBytes)
c.Rules, offset, err = marshal.SliceUnmarshal(data, offset, unmarshalRule) c.Rules, offset, err = marshal.SliceUnmarshal(data, offset, unmarshalRule)
if err != nil { if err != nil {

View file

@ -143,11 +143,3 @@ func (ct *ConditionType) UnmarshalEasyJSON(l *jlexer.Lexer) {
} }
*ct = ConditionType(v) *ct = ConditionType(v)
} }
func (id ID) MarshalEasyJSON(w *jwriter.Writer) {
w.Base64Bytes([]byte(id))
}
func (id *ID) UnmarshalEasyJSON(l *jlexer.Lexer) {
*id = ID(l.Bytes())
}

View file

@ -38,11 +38,11 @@ func TestMatchTypeJson(t *testing.T) {
data, err := chain.MarshalJSON() data, err := chain.MarshalJSON()
require.NoError(t, err) require.NoError(t, err)
if mt == MatchTypeDenyPriority { if mt == MatchTypeDenyPriority {
require.Equal(t, []byte("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"DenyPriority\"}"), data) require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"DenyPriority\"}"), data)
} else if mt == MatchTypeFirstMatch { } else if mt == MatchTypeFirstMatch {
require.Equal(t, []byte("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"FirstMatch\"}"), data) require.Equal(t, []byte("{\"ID\":null,\"Rules\":null,\"MatchType\":\"FirstMatch\"}"), data)
} else { } else {
require.Equal(t, []byte(fmt.Sprintf("{\"ID\":\"\",\"Rules\":null,\"MatchType\":\"%d\"}", mt)), data) require.Equal(t, []byte(fmt.Sprintf("{\"ID\":null,\"Rules\":null,\"MatchType\":\"%d\"}", mt)), data)
} }
var parsed Chain var parsed Chain
@ -55,7 +55,7 @@ func TestMatchTypeJson(t *testing.T) {
func TestJsonEnums(t *testing.T) { func TestJsonEnums(t *testing.T) {
chain := Chain{ chain := Chain{
ID: "2cca5ae7-cee8-428d-b45f-567fb1d03f01", // will be encoded to base64 ID: []byte("2cca5ae7-cee8-428d-b45f-567fb1d03f01"), // will be encoded to base64
MatchType: MatchTypeFirstMatch, MatchType: MatchTypeFirstMatch,
Rules: []Rule{ Rules: []Rule{
{ {

View file

@ -1,6 +1,7 @@
package inmemory package inmemory
import ( import (
"bytes"
"fmt" "fmt"
"math/rand" "math/rand"
"strings" "strings"
@ -14,14 +15,14 @@ import (
type targetToChain map[engine.Target][]*chain.Chain type targetToChain map[engine.Target][]*chain.Chain
type inmemoryLocalStorage struct { type inmemoryLocalStorage struct {
usedChainID map[chain.ID]struct{} usedChainID map[string]struct{}
nameToResourceChains map[chain.Name]targetToChain nameToResourceChains map[chain.Name]targetToChain
guard *sync.RWMutex guard *sync.RWMutex
} }
func NewInmemoryLocalStorage() engine.LocalOverrideStorage { func NewInmemoryLocalStorage() engine.LocalOverrideStorage {
return &inmemoryLocalStorage{ return &inmemoryLocalStorage{
usedChainID: map[chain.ID]struct{}{}, usedChainID: map[string]struct{}{},
nameToResourceChains: make(map[chain.Name]targetToChain), nameToResourceChains: make(map[chain.Name]targetToChain),
guard: &sync.RWMutex{}, guard: &sync.RWMutex{},
} }
@ -35,12 +36,13 @@ func (s *inmemoryLocalStorage) generateChainID(name chain.Name, target engine.Ta
sid = strings.ReplaceAll(sid, "*", "") sid = strings.ReplaceAll(sid, "*", "")
sid = strings.ReplaceAll(sid, "/", ":") sid = strings.ReplaceAll(sid, "/", ":")
sid = strings.ReplaceAll(sid, "::", ":") sid = strings.ReplaceAll(sid, "::", ":")
id = chain.ID(sid) _, ok := s.usedChainID[sid]
_, ok := s.usedChainID[id]
if ok { if ok {
continue continue
} }
s.usedChainID[id] = struct{}{} s.usedChainID[sid] = struct{}{}
id = chain.ID(sid)
break break
} }
return id return id
@ -51,7 +53,7 @@ func (s *inmemoryLocalStorage) AddOverride(name chain.Name, target engine.Target
defer s.guard.Unlock() defer s.guard.Unlock()
// AddOverride assigns generated chain ID if it has not been assigned. // AddOverride assigns generated chain ID if it has not been assigned.
if c.ID == "" { if len(c.ID) == 0 {
c.ID = s.generateChainID(name, target) c.ID = s.generateChainID(name, target)
} }
if s.nameToResourceChains[name] == nil { if s.nameToResourceChains[name] == nil {
@ -59,7 +61,7 @@ func (s *inmemoryLocalStorage) AddOverride(name chain.Name, target engine.Target
} }
rc := s.nameToResourceChains[name] rc := s.nameToResourceChains[name]
for i := range rc[target] { for i := range rc[target] {
if rc[target][i].ID == c.ID { if bytes.Equal(rc[target][i].ID, c.ID) {
rc[target][i] = c rc[target][i] = c
return c.ID, nil return c.ID, nil
} }
@ -80,7 +82,7 @@ func (s *inmemoryLocalStorage) GetOverride(name chain.Name, target engine.Target
return nil, engine.ErrResourceNotFound return nil, engine.ErrResourceNotFound
} }
for _, c := range chains { for _, c := range chains {
if c.ID == chainID { if bytes.Equal(c.ID, chainID) {
return c, nil return c, nil
} }
} }
@ -99,7 +101,7 @@ func (s *inmemoryLocalStorage) RemoveOverride(name chain.Name, target engine.Tar
return engine.ErrResourceNotFound return engine.ErrResourceNotFound
} }
for i, c := range chains { for i, c := range chains {
if c.ID == chainID { if bytes.Equal(c.ID, chainID) {
s.nameToResourceChains[name][target] = append(chains[:i], chains[i+1:]...) s.nameToResourceChains[name][target] = append(chains[:i], chains[i+1:]...)
return nil return nil
} }

View file

@ -14,9 +14,7 @@ const (
nonExistChainId = "ingress:LxGyWyL" nonExistChainId = "ingress:LxGyWyL"
) )
var ( var resrc = engine.ContainerTarget(container)
resrc = engine.ContainerTarget(container)
)
func testInmemLocalStorage() *inmemoryLocalStorage { func testInmemLocalStorage() *inmemoryLocalStorage {
return NewInmemoryLocalStorage().(*inmemoryLocalStorage) return NewInmemoryLocalStorage().(*inmemoryLocalStorage)
@ -210,12 +208,12 @@ func TestGenerateID(t *testing.T) {
} }
func hasDuplicates(ids []chain.ID) bool { func hasDuplicates(ids []chain.ID) bool {
seen := make(map[chain.ID]bool) seen := make(map[string]bool)
for _, id := range ids { for _, id := range ids {
if seen[id] { if seen[string(id)] {
return true return true
} }
seen[id] = true seen[string(id)] = true
} }
return false return false
} }

View file

@ -44,7 +44,7 @@ func NewContractStorageWithSimpleActor(rpcActor actor.RPCActor, acc *wallet.Acco
} }
func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (txHash util.Uint256, vub uint32, err error) { func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (txHash util.Uint256, vub uint32, err error) {
if c.ID == "" { if len(c.ID) == 0 {
err = ErrEmptyChainID err = ErrEmptyChainID
return return
} }
@ -61,7 +61,7 @@ func (s *ContractStorage) AddMorphRuleChain(name chain.Name, target engine.Targe
} }
func (s *ContractStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (txHash util.Uint256, vub uint32, err error) { func (s *ContractStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (txHash util.Uint256, vub uint32, err error) {
if chainID == "" { if len(chainID) == 0 {
err = ErrEmptyChainID err = ErrEmptyChainID
return return
} }