2023-11-07 18:29:51 +00:00
|
|
|
package inmemory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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 {
|
|
|
|
usedChainID map[chain.ID]struct{}
|
|
|
|
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{
|
|
|
|
usedChainID: map[chain.ID]struct{}{},
|
|
|
|
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, "::", ":")
|
|
|
|
id = chain.ID(sid)
|
|
|
|
_, ok := s.usedChainID[id]
|
|
|
|
if ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
s.usedChainID[id] = struct{}{}
|
|
|
|
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.
|
|
|
|
if c.ID == "" {
|
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] {
|
|
|
|
if rc[target][i].ID == c.ID {
|
|
|
|
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 {
|
|
|
|
if c.ID == chainID {
|
|
|
|
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 {
|
|
|
|
if 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
|
|
|
|
}
|