diff --git a/go.mod b/go.mod index a5cffc28c..07097c547 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( git.frostfs.info/TrueCloudLab/frostfs-observability v0.0.0-20231101111734-b3ad3335ff65 git.frostfs.info/TrueCloudLab/frostfs-sdk-go v0.0.0-20240126141009-65b4525b3bf0 git.frostfs.info/TrueCloudLab/hrw v1.2.1 - git.frostfs.info/TrueCloudLab/policy-engine v0.0.0-20240122104724-06cbfe8691ad + git.frostfs.info/TrueCloudLab/policy-engine v0.0.0-20240129064140-8d21ab2d99d9 git.frostfs.info/TrueCloudLab/tzhash v1.8.0 github.com/cheggaaa/pb v1.0.29 github.com/chzyer/readline v1.5.1 @@ -76,8 +76,10 @@ require ( github.com/holiman/uint256 v1.2.4 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/ipfs/go-cid v0.4.1 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/cpuid/v2 v2.2.6 // indirect github.com/magiconair/properties v1.8.7 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/minio/sha256-simd v1.0.1 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect diff --git a/go.sum b/go.sum index 4462c3575..5243ac435 100644 Binary files a/go.sum and b/go.sum differ diff --git a/pkg/ape/chainbase/boltdb.go b/pkg/ape/chainbase/boltdb.go index 4e15050b3..c26156c7a 100644 --- a/pkg/ape/chainbase/boltdb.go +++ b/pkg/ape/chainbase/boltdb.go @@ -167,8 +167,8 @@ func getTargetBucketCreateIfEmpty(tx *bbolt.Tx, name chain.Name, target policyen } func (cs *boltLocalOverrideStorage) AddOverride(name chain.Name, target policyengine.Target, c *chain.Chain) (chain.ID, error) { - if c.ID == "" { - return "", fmt.Errorf("chain ID is not set") + if len(c.ID) == 0 { + return chain.ID{}, fmt.Errorf("chain ID is not set") } serializedChain := c.Bytes() @@ -264,3 +264,48 @@ func (cs *boltLocalOverrideStorage) DropAllOverrides(name chain.Name) error { return tx.DeleteBucket([]byte(name)) }) } + +func (cs *boltLocalOverrideStorage) ListOverrideDefinedTargets(name chain.Name) ([]policyengine.Target, error) { + var targets []policyengine.Target + if err := cs.db.View(func(tx *bbolt.Tx) error { + var err error + targets, err = getTargets(tx, name) + if err != nil { + return err + } + return nil + }); err != nil { + return nil, err + } + return targets, nil +} + +func getTargets(tx *bbolt.Tx, name chain.Name) ([]policyengine.Target, error) { + var targets []policyengine.Target + cbucket := tx.Bucket(chainBucket) + if cbucket == nil { + return nil, ErrRootBucketNotFound + } + + nbucket := cbucket.Bucket([]byte(name)) + if nbucket == nil { + return nil, fmt.Errorf("%w: %w: %s", policyengine.ErrChainNotFound, ErrGlobalNamespaceBucketNotFound, name) + } + + if err := nbucket.ForEachBucket(func(k []byte) error { + ttype := policyengine.TargetType(k[0]) + if err := nbucket.Bucket(k).ForEachBucket(func(k []byte) error { + targets = append(targets, policyengine.Target{ + Type: ttype, + Name: string(slice.Copy(k)), + }) + return nil + }); err != nil { + return err + } + return nil + }); err != nil { + return nil, err + } + return targets, nil +} diff --git a/pkg/services/control/server/policy_engine.go b/pkg/services/control/server/policy_engine.go index 6bf600748..bd7f35f46 100644 --- a/pkg/services/control/server/policy_engine.go +++ b/pkg/services/control/server/policy_engine.go @@ -44,7 +44,7 @@ func (s *Server) AddChainLocalOverride(_ context.Context, req *control.AddChainL s.apeChainCounter.Add(1) // TODO (aarifullin): the such chain id is not well-designed yet. - if chain.ID == "" { + if len(chain.ID) == 0 { chain.ID = apechain.ID(fmt.Sprintf("%s:%d", apechain.Ingress, s.apeChainCounter.Load())) }