Files
ziti/controller/handler_ctrl/update_terminator.go
T
Paul Lorenz 1aa6ee324a Scope terminator operations to the requesting router. Fixes #4237
- rejects a remove or update request whose terminator is owned by a different
  router, and drops unowned ids from batch removals, so a router can only
  operate on its own terminators
- keeps the existing leader-side presence pre-filter in the batch handler and
  applies the ownership filter after it
- adds unit tests for the ownership filter and the single-terminator check
- adds an end-to-end test that drives the fabric control channel from an
  enrolled router against a second router's terminator, covering single remove,
  batch remove, and re-weight, plus a control that a router can still remove its
  own
2026-08-07 14:53:27 -04:00

119 lines
3.7 KiB
Go

/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package handler_ctrl
import (
"fmt"
"math"
"github.com/openziti/channel/v4"
"github.com/openziti/ziti/common/handler_common"
"github.com/openziti/ziti/common/pb/ctrl_pb"
"github.com/openziti/ziti/controller/db"
"github.com/openziti/ziti/controller/fields"
"github.com/openziti/ziti/controller/model"
"github.com/openziti/ziti/controller/network"
"github.com/openziti/ziti/controller/xt"
log "github.com/sirupsen/logrus"
"google.golang.org/protobuf/proto"
)
type updateTerminatorHandler struct {
baseHandler
}
func newUpdateTerminatorHandler(network *network.Network, router *model.Router) *updateTerminatorHandler {
return &updateTerminatorHandler{
baseHandler: baseHandler{
router: router,
network: network,
},
}
}
func (self *updateTerminatorHandler) ContentType() int32 {
return int32(ctrl_pb.ContentType_UpdateTerminatorRequestType)
}
func (self *updateTerminatorHandler) HandleReceive(msg *channel.Message, ch channel.Channel) {
request := &ctrl_pb.UpdateTerminatorRequest{}
if err := proto.Unmarshal(msg.Body, request); err != nil {
log.WithError(err).Error("failed to unmarshal update terminator message")
return
}
go self.handleUpdateTerminator(msg, ch, request)
}
func (self *updateTerminatorHandler) handleUpdateTerminator(msg *channel.Message, ch channel.Channel, request *ctrl_pb.UpdateTerminatorRequest) {
terminator, err := self.network.Terminator.Read(request.TerminatorId)
if err != nil {
handler_common.SendFailure(msg, ch, err.Error())
return
}
if !self.ownsTerminator(terminator) {
log.
WithField("routerId", self.router.Id).
WithField("terminator", request.TerminatorId).
WithField("terminatorRouterId", terminator.Router).
Warn("router attempted to update a terminator it does not own; rejected")
handler_common.SendFailure(msg, ch, "terminator not owned by requesting router")
return
}
if !request.UpdateCost && !request.UpdatePrecedence {
// nothing to do
handler_common.SendSuccess(msg, ch, "")
return
}
checker := fields.UpdatedFieldsMap{}
if request.UpdateCost {
if request.Cost > math.MaxUint16 {
handler_common.SendFailure(msg, ch, fmt.Sprintf("invalid static cost %v. Must be less than %v", request.Cost, math.MaxUint16))
return
}
terminator.Cost = uint16(request.Cost)
checker[db.FieldTerminatorCost] = struct{}{}
}
if request.UpdatePrecedence {
if request.UpdatePrecedence {
if request.Precedence == ctrl_pb.TerminatorPrecedence_Default {
terminator.Precedence = xt.Precedences.Default
} else if request.Precedence == ctrl_pb.TerminatorPrecedence_Required {
terminator.Precedence = xt.Precedences.Required
} else if request.Precedence == ctrl_pb.TerminatorPrecedence_Failed {
terminator.Precedence = xt.Precedences.Failed
} else {
handler_common.SendFailure(msg, ch, fmt.Sprintf("invalid precedence: %v", request.Precedence))
return
}
}
checker[db.FieldTerminatorPrecedence] = struct{}{}
}
if err := self.network.Terminator.Update(terminator, checker, self.newChangeContext(ch, "fabric.update.terminator")); err != nil {
handler_common.SendFailure(msg, ch, err.Error())
return
}
handler_common.SendSuccess(msg, ch, "")
}