forked from TrueCloudLab/policy-engine
[#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:
parent
5f13d91c0d
commit
dd0f582fc3
8 changed files with 26 additions and 34 deletions
|
@ -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
|
||||||
|
|
Binary file not shown.
|
@ -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 {
|
||||||
|
|
|
@ -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())
|
|
||||||
}
|
|
||||||
|
|
|
@ -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{
|
||||||
{
|
{
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue