mirror of
https://github.com/openziti/ziti.git
synced 2026-10-13 15:51:21 +00:00
Merge pull request openziti/fabric#233 from openziti/remove-xtv
Remove eXtensible Terminator Validator framework (xtv) as it's now un…
This commit is contained in:
@@ -31,14 +31,12 @@ import (
|
||||
"github.com/openziti/fabric/controller/xt_random"
|
||||
"github.com/openziti/fabric/controller/xt_smartrouting"
|
||||
"github.com/openziti/fabric/controller/xt_weighted"
|
||||
"github.com/openziti/fabric/controller/xtv"
|
||||
"github.com/openziti/fabric/controller/xweb"
|
||||
"github.com/openziti/fabric/events"
|
||||
"github.com/openziti/foundation/channel2"
|
||||
"github.com/openziti/foundation/common"
|
||||
"github.com/openziti/foundation/profiler"
|
||||
"github.com/openziti/foundation/util/concurrenz"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -68,10 +66,6 @@ func NewController(cfg *Config, versionProvider common.VersionProvider) (*Contro
|
||||
}
|
||||
|
||||
c.registerXts()
|
||||
if err := c.loadXtvMappings(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.loadEventHandlers()
|
||||
|
||||
if n, err := network.NewNetwork(cfg.Id, cfg.Network, cfg.Db, cfg.Metrics, versionProvider, c.shutdownC); err == nil {
|
||||
@@ -205,29 +199,6 @@ func (c *Controller) startProfiling() {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) loadXtvMappings() error {
|
||||
if t, ok := c.config.src["terminator"]; ok {
|
||||
if tm, ok := t.(map[interface{}]interface{}); ok {
|
||||
if vals, ok := tm["validators"]; ok {
|
||||
if valMap, ok := vals.(map[interface{}]interface{}); ok {
|
||||
for k, v := range valMap {
|
||||
binding, ok := k.(string)
|
||||
if !ok {
|
||||
return errors.Errorf("invalid binding in terminator.validators configuration: %v", binding)
|
||||
}
|
||||
validatorId, ok := v.(string)
|
||||
if !ok {
|
||||
return errors.Errorf("invalid validator id in terminator.validators configuration: %v", binding)
|
||||
}
|
||||
xtv.GetRegistry().RegisterBinding(binding, validatorId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) loadEventHandlers() {
|
||||
|
||||
if e, ok := c.config.src["events"]; ok {
|
||||
|
||||
@@ -20,7 +20,6 @@ import (
|
||||
"encoding/binary"
|
||||
"github.com/michaelquigley/pfxlog"
|
||||
"github.com/openziti/fabric/controller/xt"
|
||||
"github.com/openziti/fabric/controller/xtv"
|
||||
"github.com/openziti/foundation/storage/ast"
|
||||
"github.com/openziti/foundation/storage/boltz"
|
||||
"github.com/openziti/foundation/util/sequence"
|
||||
@@ -181,7 +180,7 @@ func (entity *Terminator) GetEntityType() string {
|
||||
type TerminatorStore interface {
|
||||
boltz.CrudStore
|
||||
LoadOneById(tx *bbolt.Tx, id string) (*Terminator, error)
|
||||
GetTerminatorsInIdentityGroup(tx *bbolt.Tx, terminator xtv.Terminator, create bool) ([]*Terminator, error)
|
||||
GetTerminatorsInIdentityGroup(tx *bbolt.Tx, terminatorId string) ([]*Terminator, error)
|
||||
}
|
||||
|
||||
func newTerminatorStore(stores *stores) *terminatorStoreImpl {
|
||||
@@ -244,24 +243,7 @@ func (store *terminatorStoreImpl) Create(ctx boltz.MutateContext, entity boltz.E
|
||||
}
|
||||
entity.SetId(id)
|
||||
}
|
||||
if err := store.baseStore.Create(ctx, entity); err != nil {
|
||||
return err
|
||||
}
|
||||
return xtv.Validate(ctx.Tx(), entity.(*Terminator), true)
|
||||
}
|
||||
|
||||
func (store *terminatorStoreImpl) Update(ctx boltz.MutateContext, entity boltz.Entity, checker boltz.FieldChecker) error {
|
||||
if err := store.baseStore.Update(ctx, entity, checker); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// load from database, as entity used to persist may be incomplete or have out of sync data, for example when patching
|
||||
// or when fields become immutable after create
|
||||
terminator, err := store.LoadOneById(ctx.Tx(), entity.GetId())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return xtv.Validate(ctx.Tx(), terminator, false)
|
||||
return store.baseStore.Create(ctx, entity)
|
||||
}
|
||||
|
||||
func (store *terminatorStoreImpl) DeleteById(ctx boltz.MutateContext, id string) error {
|
||||
@@ -291,25 +273,20 @@ func (store *terminatorStoreImpl) DeleteById(ctx boltz.MutateContext, id string)
|
||||
return store.baseStore.DeleteById(ctx, id)
|
||||
}
|
||||
|
||||
func (store *terminatorStoreImpl) GetTerminatorsInIdentityGroup(tx *bbolt.Tx, entity xtv.Terminator, create bool) ([]*Terminator, error) {
|
||||
serviceId := ""
|
||||
if create { // don't allow service to be changed
|
||||
serviceId = entity.GetServiceId()
|
||||
} else {
|
||||
terminator, err := store.LoadOneById(tx, entity.GetId())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceId = terminator.GetServiceId()
|
||||
func (store *terminatorStoreImpl) GetTerminatorsInIdentityGroup(tx *bbolt.Tx, termnatorId string) ([]*Terminator, error) {
|
||||
terminator, err := store.LoadOneById(tx, termnatorId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
serviceId := terminator.GetServiceId()
|
||||
|
||||
identity := entity.GetIdentity()
|
||||
identity := terminator.GetIdentity()
|
||||
|
||||
terminatorIds := store.stores.service.GetRelatedEntitiesIdList(tx, serviceId, EntityTypeTerminators)
|
||||
var identityTerminators []*Terminator
|
||||
for _, terminatorId := range terminatorIds {
|
||||
if terminatorId != entity.GetId() {
|
||||
if terminator, _ := store.LoadOneById(tx, terminatorId); terminator != nil {
|
||||
for _, siblingId := range terminatorIds {
|
||||
if siblingId != termnatorId {
|
||||
if terminator, _ := store.LoadOneById(tx, siblingId); terminator != nil {
|
||||
if identity == terminator.Identity {
|
||||
identityTerminators = append(identityTerminators, terminator)
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func (ctrl *ServiceController) Create(s *Service) error {
|
||||
}
|
||||
for _, terminator := range s.Terminators {
|
||||
terminator.Service = s.Id
|
||||
if _, err := ctrl.Terminators.createInTx(ctx, terminator); err != nil {
|
||||
if _, err := ctrl.Terminators.CreateInTx(ctx, terminator); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func (ctrl *TerminatorController) Create(s *Terminator) (string, error) {
|
||||
var id string
|
||||
var err error
|
||||
err = ctrl.db.Update(func(tx *bbolt.Tx) error {
|
||||
id, err = ctrl.createInTx(boltz.NewMutateContext(tx), s)
|
||||
id, err = ctrl.CreateInTx(boltz.NewMutateContext(tx), s)
|
||||
return err
|
||||
})
|
||||
return id, err
|
||||
@@ -160,7 +160,7 @@ func (ctrl *TerminatorController) checkBinding(terminator *Terminator) {
|
||||
}
|
||||
}
|
||||
|
||||
func (ctrl *TerminatorController) createInTx(ctx boltz.MutateContext, terminator *Terminator) (string, error) {
|
||||
func (ctrl *TerminatorController) CreateInTx(ctx boltz.MutateContext, terminator *Terminator) (string, error) {
|
||||
ctrl.checkBinding(terminator)
|
||||
boltTerminator := terminator.toBolt()
|
||||
if err := ctrl.GetStore().Create(ctx, boltTerminator); err != nil {
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package xtv
|
||||
|
||||
import (
|
||||
"go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
type DefaultValidator struct{}
|
||||
|
||||
func (d DefaultValidator) Validate(*bbolt.Tx, Terminator, bool) error {
|
||||
return nil
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
package xtv
|
||||
|
||||
import (
|
||||
"github.com/michaelquigley/pfxlog"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
func init() {
|
||||
globalRegistry = ®istry{
|
||||
validators: copyOnWriteValidatorMap{
|
||||
value: &atomic.Value{},
|
||||
lock: &sync.Mutex{},
|
||||
},
|
||||
bindings: copyOnWriteValidatorMap{
|
||||
value: &atomic.Value{},
|
||||
lock: &sync.Mutex{},
|
||||
},
|
||||
mappings: copyOnWriteStringStringMap{
|
||||
value: &atomic.Value{},
|
||||
lock: &sync.Mutex{},
|
||||
},
|
||||
defaultValidator: DefaultValidator{},
|
||||
}
|
||||
|
||||
globalRegistry.validators.value.Store(map[string]Validator{})
|
||||
globalRegistry.bindings.value.Store(map[string]Validator{})
|
||||
globalRegistry.mappings.value.Store(map[string]string{})
|
||||
|
||||
}
|
||||
|
||||
func GetRegistry() Registry {
|
||||
return globalRegistry
|
||||
}
|
||||
|
||||
var globalRegistry *registry
|
||||
|
||||
type registry struct {
|
||||
validators copyOnWriteValidatorMap
|
||||
bindings copyOnWriteValidatorMap
|
||||
mappings copyOnWriteStringStringMap
|
||||
defaultValidator Validator
|
||||
}
|
||||
|
||||
func (r *registry) RegisterBinding(binding, validatorId string) {
|
||||
r.mappings.put(binding, validatorId)
|
||||
}
|
||||
|
||||
func (r *registry) RegisterValidator(id string, validator Validator) {
|
||||
r.validators.put(id, validator)
|
||||
}
|
||||
|
||||
func (r *registry) GetValidatorForBinding(binding string) Validator {
|
||||
validator := r.bindings.get(binding)
|
||||
if validator == nil {
|
||||
pfxlog.Logger().Debugf("using default validator for binding %v", binding)
|
||||
validator = r.defaultValidator
|
||||
r.bindings.put(binding, validator)
|
||||
}
|
||||
return validator
|
||||
}
|
||||
|
||||
type copyOnWriteValidatorMap struct {
|
||||
value *atomic.Value
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func (m *copyOnWriteValidatorMap) put(key string, value Validator) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
var current = m.value.Load().(map[string]Validator)
|
||||
mapCopy := map[string]Validator{}
|
||||
for k, v := range current {
|
||||
mapCopy[k] = v
|
||||
}
|
||||
mapCopy[key] = value
|
||||
m.value.Store(mapCopy)
|
||||
}
|
||||
|
||||
func (m *copyOnWriteValidatorMap) get(key string) Validator {
|
||||
var current = m.value.Load().(map[string]Validator)
|
||||
return current[key]
|
||||
}
|
||||
|
||||
type copyOnWriteStringStringMap struct {
|
||||
value *atomic.Value
|
||||
lock *sync.Mutex
|
||||
}
|
||||
|
||||
func (m *copyOnWriteStringStringMap) put(key string, value string) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
var current = m.value.Load().(map[string]string)
|
||||
mapCopy := map[string]string{}
|
||||
for k, v := range current {
|
||||
mapCopy[k] = v
|
||||
}
|
||||
mapCopy[key] = value
|
||||
m.value.Store(mapCopy)
|
||||
}
|
||||
|
||||
func (m *copyOnWriteStringStringMap) get(key string) string {
|
||||
var current = m.value.Load().(map[string]string)
|
||||
return current[key]
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package xtv
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"go.etcd.io/bbolt"
|
||||
)
|
||||
|
||||
type Terminator interface {
|
||||
GetId() string
|
||||
GetServiceId() string
|
||||
GetRouterId() string
|
||||
GetBinding() string
|
||||
GetAddress() string
|
||||
GetIdentity() string
|
||||
GetIdentitySecret() []byte
|
||||
}
|
||||
|
||||
type Validator interface {
|
||||
Validate(tx *bbolt.Tx, terminator Terminator, create bool) error
|
||||
}
|
||||
|
||||
type Registry interface {
|
||||
RegisterBinding(binding, validatorId string)
|
||||
RegisterValidator(id string, validator Validator)
|
||||
GetValidatorForBinding(binding string) Validator
|
||||
}
|
||||
|
||||
func RegisterValidator(id string, validator Validator) {
|
||||
globalRegistry.RegisterValidator(id, validator)
|
||||
}
|
||||
|
||||
func Validate(tx *bbolt.Tx, terminator Terminator, create bool) error {
|
||||
validator := globalRegistry.GetValidatorForBinding(terminator.GetBinding())
|
||||
return validator.Validate(tx, terminator, create)
|
||||
}
|
||||
|
||||
func InitializeMappings() error {
|
||||
globalRegistry.mappings.lock.Lock()
|
||||
defer globalRegistry.mappings.lock.Unlock()
|
||||
mappings := globalRegistry.mappings.value.Load().(map[string]string)
|
||||
for binding, validatorId := range mappings {
|
||||
validator := globalRegistry.validators.get(validatorId)
|
||||
if validator == nil {
|
||||
return errors.Errorf("no validator %v found for terminator binding %v", validatorId, binding)
|
||||
}
|
||||
globalRegistry.bindings.put(binding, validator)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user