2023-11-07 18:29:51 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
2024-01-18 08:01:39 +00:00
|
|
|
"bytes"
|
2023-11-07 18:29:51 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2023-12-21 08:13:22 +00:00
|
|
|
"sync"
|
2023-11-07 18:29:51 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/util"
|
|
|
|
)
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
type targetToChain map[engine.Target][]*chain.Chain
|
2023-11-07 18:29:51 +00:00
|
|
|
|
|
|
|
type inmemoryLocalStorage struct {
|
2024-01-18 08:01:39 +00:00
|
|
|
usedChainID map[string]struct{}
|
2023-11-07 18:29:51 +00:00
|
|
|
nameToResourceChains map[chain.Name]targetToChain
|
2023-12-21 08:13:22 +00:00
|
|
|
guard *sync.RWMutex
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewInmemoryLocalStorage() engine.LocalOverrideStorage {
|
|
|
|
return &inmemoryLocalStorage{
|
2024-01-18 08:01:39 +00:00
|
|
|
usedChainID: map[string]struct{}{},
|
2023-11-07 18:29:51 +00:00
|
|
|
nameToResourceChains: make(map[chain.Name]targetToChain),
|
2023-12-21 08:13:22 +00:00
|
|
|
guard: &sync.RWMutex{},
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
func (s *inmemoryLocalStorage) generateChainID(name chain.Name, target engine.Target) chain.ID {
|
2023-11-07 18:29:51 +00:00
|
|
|
var id chain.ID
|
|
|
|
for {
|
|
|
|
suffix := rand.Uint32() % 100
|
2023-12-01 13:08:52 +00:00
|
|
|
sid := fmt.Sprintf("%s:%s/%d", name, target.Name, suffix)
|
2023-11-07 18:29:51 +00:00
|
|
|
sid = strings.ReplaceAll(sid, "*", "")
|
|
|
|
sid = strings.ReplaceAll(sid, "/", ":")
|
|
|
|
sid = strings.ReplaceAll(sid, "::", ":")
|
2024-01-18 08:01:39 +00:00
|
|
|
_, ok := s.usedChainID[sid]
|
2023-11-07 18:29:51 +00:00
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-18 08:01:39 +00:00
|
|
|
s.usedChainID[sid] = struct{}{}
|
|
|
|
|
|
|
|
id = chain.ID(sid)
|
2023-11-07 18:29:51 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
func (s *inmemoryLocalStorage) AddOverride(name chain.Name, target engine.Target, c *chain.Chain) (chain.ID, error) {
|
2023-12-21 08:13:22 +00:00
|
|
|
s.guard.Lock()
|
|
|
|
defer s.guard.Unlock()
|
|
|
|
|
2023-11-07 18:29:51 +00:00
|
|
|
// AddOverride assigns generated chain ID if it has not been assigned.
|
2024-01-18 08:01:39 +00:00
|
|
|
if len(c.ID) == 0 {
|
2023-12-01 13:08:52 +00:00
|
|
|
c.ID = s.generateChainID(name, target)
|
2023-11-07 18:29:51 +00:00
|
|
|
}
|
|
|
|
if s.nameToResourceChains[name] == nil {
|
|
|
|
s.nameToResourceChains[name] = make(targetToChain)
|
|
|
|
}
|
|
|
|
rc := s.nameToResourceChains[name]
|
2023-12-01 15:12:57 +00:00
|
|
|
for i := range rc[target] {
|
2024-01-18 08:01:39 +00:00
|
|
|
if bytes.Equal(rc[target][i].ID, c.ID) {
|
2023-12-01 15:12:57 +00:00
|
|
|
rc[target][i] = c
|
|
|
|
return c.ID, nil
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 13:08:52 +00:00
|
|
|
rc[target] = append(rc[target], c)
|
2023-11-07 18:29:51 +00:00
|
|
|
return c.ID, nil
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
func (s *inmemoryLocalStorage) GetOverride(name chain.Name, target engine.Target, chainID chain.ID) (*chain.Chain, error) {
|
2023-12-21 08:13:22 +00:00
|
|
|
s.guard.RLock()
|
|
|
|
defer s.guard.RUnlock()
|
|
|
|
|
2023-11-07 18:29:51 +00:00
|
|
|
if _, ok := s.nameToResourceChains[name]; !ok {
|
|
|
|
return nil, engine.ErrChainNameNotFound
|
|
|
|
}
|
2023-12-01 13:08:52 +00:00
|
|
|
chains, ok := s.nameToResourceChains[name][target]
|
2023-11-07 18:29:51 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, engine.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
for _, c := range chains {
|
2024-01-18 08:01:39 +00:00
|
|
|
if bytes.Equal(c.ID, chainID) {
|
2023-11-07 18:29:51 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, engine.ErrChainNotFound
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
func (s *inmemoryLocalStorage) RemoveOverride(name chain.Name, target engine.Target, chainID chain.ID) error {
|
2023-12-21 08:13:22 +00:00
|
|
|
s.guard.Lock()
|
|
|
|
defer s.guard.Unlock()
|
|
|
|
|
2023-11-07 18:29:51 +00:00
|
|
|
if _, ok := s.nameToResourceChains[name]; !ok {
|
|
|
|
return engine.ErrChainNameNotFound
|
|
|
|
}
|
2023-12-01 13:08:52 +00:00
|
|
|
chains, ok := s.nameToResourceChains[name][target]
|
2023-11-07 18:29:51 +00:00
|
|
|
if !ok {
|
|
|
|
return engine.ErrResourceNotFound
|
|
|
|
}
|
|
|
|
for i, c := range chains {
|
2024-01-18 08:01:39 +00:00
|
|
|
if bytes.Equal(c.ID, chainID) {
|
2023-12-01 13:08:52 +00:00
|
|
|
s.nameToResourceChains[name][target] = append(chains[:i], chains[i+1:]...)
|
2023-11-07 18:29:51 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return engine.ErrChainNotFound
|
|
|
|
}
|
|
|
|
|
2023-12-01 13:08:52 +00:00
|
|
|
func (s *inmemoryLocalStorage) ListOverrides(name chain.Name, target engine.Target) ([]*chain.Chain, error) {
|
2023-12-21 08:13:22 +00:00
|
|
|
s.guard.RLock()
|
|
|
|
defer s.guard.RUnlock()
|
|
|
|
|
2023-11-07 18:29:51 +00:00
|
|
|
rcs, ok := s.nameToResourceChains[name]
|
|
|
|
if !ok {
|
|
|
|
return []*chain.Chain{}, nil
|
|
|
|
}
|
2023-12-01 13:08:52 +00:00
|
|
|
for t, chains := range rcs {
|
|
|
|
if t.Type != target.Type {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !util.GlobMatch(target.Name, t.Name) {
|
2023-11-07 18:29:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return chains, nil
|
|
|
|
}
|
|
|
|
return []*chain.Chain{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemoryLocalStorage) DropAllOverrides(name chain.Name) error {
|
2023-12-21 08:13:22 +00:00
|
|
|
s.guard.Lock()
|
|
|
|
defer s.guard.Unlock()
|
|
|
|
|
2023-11-07 18:29:51 +00:00
|
|
|
s.nameToResourceChains[name] = make(targetToChain)
|
|
|
|
return nil
|
|
|
|
}
|
2024-01-26 14:25:59 +00:00
|
|
|
|
|
|
|
func (s *inmemoryLocalStorage) ListOverrideDefinedTargets(name chain.Name) ([]engine.Target, error) {
|
|
|
|
s.guard.RLock()
|
|
|
|
defer s.guard.RUnlock()
|
|
|
|
ttc := s.nameToResourceChains[name]
|
|
|
|
var keys []engine.Target
|
|
|
|
for k := range ttc {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
|
|
|
return keys, nil
|
|
|
|
}
|