Files
Paul Lorenz b353d2d9b6 Scope terminator operations to the requesting router. Fixes #4234
- rejects a remove or update request whose terminator is owned by a different
  router, on the fabric control channel handlers
- drops ids the requesting router does not own from batch removals, keeping
  absent ids so a delete racing a not-yet-applied create is still ordered after
  it
- adds unit tests for the ownership filter and for 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:27:58 -04:00

100 lines
3.3 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 (
"errors"
"testing"
"github.com/openziti/ziti/v2/controller/model"
"github.com/openziti/ziti/v2/controller/models"
"github.com/stretchr/testify/require"
)
func Test_filterOwnedTerminators(t *testing.T) {
const me = "router-me"
// mine: present, owned by the requesting router; other: present, owned by a different router;
// gone: not present; err: lookup fails.
lookup := func(id string) (string, bool, error) {
switch id {
case "mine":
return me, true, nil
case "other":
return "router-other", true, nil
case "err":
return "", false, errors.New("boom")
default: // "gone" and anything else
return "", false, nil
}
}
t.Run("keeps owned and absent ids", func(t *testing.T) {
req := require.New(t)
kept, rejected := filterOwnedTerminators([]string{"mine", "gone"}, me, lookup)
req.Equal([]string{"mine", "gone"}, kept)
req.Zero(rejected)
})
t.Run("rejects ids owned by another router", func(t *testing.T) {
req := require.New(t)
kept, rejected := filterOwnedTerminators([]string{"other", "mine"}, me, lookup)
req.Equal([]string{"mine"}, kept, "a router may only remove terminators it owns")
req.Equal(1, rejected)
})
t.Run("a lookup error keeps the id", func(t *testing.T) {
req := require.New(t)
kept, rejected := filterOwnedTerminators([]string{"err"}, me, lookup)
req.Equal([]string{"err"}, kept, "an unresolved owner must not be treated as an ownership violation")
req.Zero(rejected)
})
t.Run("mixes kept and rejected", func(t *testing.T) {
req := require.New(t)
kept, rejected := filterOwnedTerminators([]string{"mine", "other", "gone", "err"}, me, lookup)
req.Equal([]string{"mine", "gone", "err"}, kept)
req.Equal(1, rejected)
})
t.Run("rejects every id when none are owned", func(t *testing.T) {
req := require.New(t)
kept, rejected := filterOwnedTerminators([]string{"other", "other"}, me, lookup)
req.Empty(kept, "an all-foreign batch must delete nothing")
req.Equal(2, rejected)
})
}
// Test_ownsTerminator covers the single-terminator check used by the remove and update handlers,
// which reject outright rather than filtering.
func Test_ownsTerminator(t *testing.T) {
handler := &baseHandler{router: &model.Router{BaseEntity: models.BaseEntity{Id: "router-me"}}}
t.Run("owned by the requesting router", func(t *testing.T) {
require.True(t, handler.ownsTerminator(&model.Terminator{Router: "router-me"}))
})
t.Run("owned by a different router", func(t *testing.T) {
require.False(t, handler.ownsTerminator(&model.Terminator{Router: "router-other"}))
})
t.Run("unset owner", func(t *testing.T) {
require.False(t, handler.ownsTerminator(&model.Terminator{}),
"a terminator with no owner must not be treated as owned by the requester")
})
}