2023-12-05 09:12:35 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
2023-12-08 07:44:13 +00:00
|
|
|
policycontract "git.frostfs.info/TrueCloudLab/frostfs-contract/policy"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/handler"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-12-05 09:12:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine/inmemory"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource"
|
2023-12-08 07:44:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"go.uber.org/zap"
|
2023-12-05 09:12:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Storage struct {
|
|
|
|
router engine.ChainRouter
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
morph handler.MorphRuleChainStorage
|
2023-12-05 09:12:35 +00:00
|
|
|
|
|
|
|
local engine.LocalOverrideStorage
|
2023-12-08 07:44:13 +00:00
|
|
|
|
|
|
|
policy handler.PolicyStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageConfig struct {
|
|
|
|
Contract Contract
|
|
|
|
Cache *cache.MorphPolicyCache
|
|
|
|
Log *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type Contract interface {
|
|
|
|
AddChain(kind policycontract.Kind, entity string, name []byte, chain []byte) (util.Uint256, uint32, error)
|
|
|
|
GetChain(kind policycontract.Kind, entity string, name []byte) ([]byte, error)
|
|
|
|
RemoveChain(kind policycontract.Kind, entity string, name []byte) (util.Uint256, uint32, error)
|
|
|
|
ListChains(kind policycontract.Kind, entity string, name []byte) ([][]byte, error)
|
|
|
|
Wait(tx util.Uint256, vub uint32, err error) error
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
var _ handler.APE = (*Storage)(nil)
|
2023-12-05 09:12:35 +00:00
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func NewStorage(cfg StorageConfig) *Storage {
|
|
|
|
// todo use thread safe inmemory https://git.frostfs.info/TrueCloudLab/policy-engine/issues/35
|
2023-12-05 09:12:35 +00:00
|
|
|
local := inmemory.NewInmemoryLocalStorage()
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
morph := NewMorphRuleChainStorage(&MorphRuleChainStorageConfig{
|
|
|
|
Contract: cfg.Contract,
|
|
|
|
Cache: cfg.Cache,
|
|
|
|
Log: cfg.Log,
|
|
|
|
})
|
|
|
|
|
|
|
|
policyStorage := NewMorphPolicyStorage(&MorphPolicyStorageConfig{
|
|
|
|
Contract: cfg.Contract,
|
|
|
|
Log: cfg.Log,
|
|
|
|
})
|
|
|
|
|
2023-12-05 09:12:35 +00:00
|
|
|
return &Storage{
|
|
|
|
router: engine.NewDefaultChainRouterWithLocalOverrides(morph, local),
|
|
|
|
morph: morph,
|
|
|
|
local: local,
|
2023-12-08 07:44:13 +00:00
|
|
|
policy: policyStorage,
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (s *Storage) IsAllowed(name chain.Name, target engine.RequestTarget, r resource.Request) (status chain.Status, found bool, err error) {
|
2023-12-05 09:12:35 +00:00
|
|
|
return s.router.IsAllowed(name, target, r)
|
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (s *Storage) LocalStorage() engine.LocalOverrideStorage {
|
|
|
|
return s.local
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-08 07:44:13 +00:00
|
|
|
func (s *Storage) AddChain(target engine.Target, policyChain *chain.Chain) error {
|
|
|
|
return s.morph.AddChain(target, policyChain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) RemoveChain(target engine.Target, chainID chain.ID) error {
|
|
|
|
return s.morph.RemoveChain(target, chainID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) ListChains(target engine.Target) ([]*chain.Chain, error) {
|
|
|
|
return s.morph.ListChains(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) PutPolicy(namespace string, cnrID cid.ID, policy []byte) error {
|
|
|
|
return s.policy.PutPolicy(namespace, cnrID, policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) GetPolicy(namespace string, cnrID cid.ID) ([]byte, error) {
|
|
|
|
return s.policy.GetPolicy(namespace, cnrID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) DeletePolicy(namespace string, cnrID cid.ID) error {
|
|
|
|
return s.policy.DeletePolicy(namespace, cnrID)
|
2023-12-05 09:12:35 +00:00
|
|
|
}
|