From df15b38c63a59a8a4754f42ebec4d0b8069b0bea Mon Sep 17 00:00:00 2001 From: aarifullin Date: Wed, 29 Nov 2023 16:36:54 +0300 Subject: [PATCH] [#3] engine: Refactor MorphRuleChainStorage * Make changing state methods like AddMorphRuleChain, RemoveMorphRuleChain return transaction hash and VUB. Signed-off-by: Airat Arifullin --- go.mod | 5 ++++- go.sum | 3 +++ iam/converter_test.go | 4 ++-- pkg/engine/inmemory/morph_storage.go | 12 +++++++----- pkg/engine/interface.go | 8 ++++++-- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 6008d15..ff9f5ad 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.frostfs.info/TrueCloudLab/policy-engine go 1.20 -require github.com/stretchr/testify v1.8.4 +require ( + github.com/nspcc-dev/neo-go v0.103.0 + github.com/stretchr/testify v1.8.4 +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 07efd47..f3f7b8c 100644 --- a/go.sum +++ b/go.sum @@ -7,10 +7,13 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/nspcc-dev/neo-go v0.103.0 h1:UVyWPhzZdfYFG35ORP3FRDLh8J/raRQ6m8SptDdlgfM= +github.com/nspcc-dev/neo-go v0.103.0/go.mod h1:x+wmcYqpZYJwLp1l/pHZrqNp3RSWlkMymWGDij3/OPo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/iam/converter_test.go b/iam/converter_test.go index 4373e30..7ec177d 100644 --- a/iam/converter_test.go +++ b/iam/converter_test.go @@ -669,7 +669,7 @@ func TestComplexNativeConditions(t *testing.T) { requireChainRulesMatch(t, expected.Rules, nativeChain.Rules) s := inmemory.NewInMemory() - err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), nativeChain) + _, _, err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), nativeChain) require.NoError(t, err) for _, tc := range []struct { @@ -933,7 +933,7 @@ func TestComplexS3Conditions(t *testing.T) { requireChainRulesMatch(t, expected.Rules, s3Chain.Rules) s := inmemory.NewInMemory() - err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), s3Chain) + _, _, err = s.MorphRuleChainStorage().AddMorphRuleChain("name", engine.NamespaceTarget("ns"), s3Chain) require.NoError(t, err) for _, tc := range []struct { diff --git a/pkg/engine/inmemory/morph_storage.go b/pkg/engine/inmemory/morph_storage.go index cd4b5a9..72b5439 100644 --- a/pkg/engine/inmemory/morph_storage.go +++ b/pkg/engine/inmemory/morph_storage.go @@ -3,6 +3,7 @@ package inmemory import ( "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain" "git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine" + "github.com/nspcc-dev/neo-go/pkg/util" ) type inmemoryMorphRuleChainStorage struct { @@ -17,7 +18,7 @@ func NewInmemoryMorphRuleChainStorage() engine.MorphRuleChainStorage { } } -func (s *inmemoryMorphRuleChainStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (err error) { +func (s *inmemoryMorphRuleChainStorage) AddMorphRuleChain(name chain.Name, target engine.Target, c *chain.Chain) (_ util.Uint256, _ uint32, err error) { switch target.Type { case engine.Namespace: _, err = s.nameToNamespaceChains.AddOverride(name, target, c) @@ -29,15 +30,16 @@ func (s *inmemoryMorphRuleChainStorage) AddMorphRuleChain(name chain.Name, targe return } -func (s *inmemoryMorphRuleChainStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) error { +func (s *inmemoryMorphRuleChainStorage) RemoveMorphRuleChain(name chain.Name, target engine.Target, chainID chain.ID) (_ util.Uint256, _ uint32, err error) { switch target.Type { case engine.Namespace: - return s.nameToNamespaceChains.RemoveOverride(name, target, chainID) + err = s.nameToNamespaceChains.RemoveOverride(name, target, chainID) case engine.Container: - return s.nameToContainerChains.RemoveOverride(name, target, chainID) + err = s.nameToContainerChains.RemoveOverride(name, target, chainID) default: - return engine.ErrUnknownTarget + err = engine.ErrUnknownTarget } + return } func (s *inmemoryMorphRuleChainStorage) ListMorphRuleChains(name chain.Name, target engine.Target) ([]*chain.Chain, error) { diff --git a/pkg/engine/interface.go b/pkg/engine/interface.go index 14c1301..8d11466 100644 --- a/pkg/engine/interface.go +++ b/pkg/engine/interface.go @@ -3,6 +3,7 @@ package engine import ( "git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain" "git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource" + "github.com/nspcc-dev/neo-go/pkg/util" ) type ChainRouter interface { @@ -93,10 +94,13 @@ func ContainerTarget(container string) Target { // MorphRuleChainStorage is the interface to manage chains from the chain storage. // Basically, this implies that the storage manages rules stored in policy contract. type MorphRuleChainStorage interface { - AddMorphRuleChain(name chain.Name, target Target, c *chain.Chain) error + // AddMorphRuleChain adds a chain rule to the policy contract and returns transaction hash, VUB and error. + AddMorphRuleChain(name chain.Name, target Target, c *chain.Chain) (util.Uint256, uint32, error) - RemoveMorphRuleChain(name chain.Name, target Target, chainID chain.ID) error + // RemoveMorphRuleChain removes a chain rule to the policy contract and returns transaction hash, VUB and error. + RemoveMorphRuleChain(name chain.Name, target Target, chainID chain.ID) (util.Uint256, uint32, error) + // ListMorphRuleChains just lists deserialized chains. ListMorphRuleChains(name chain.Name, target Target) ([]*chain.Chain, error) }