Files
ziti/controller/handler_ctrl/base_test.go
T
Paul Lorenz 455bb79a94 Scope terminator operations to the requesting router. Fixes #4236
- rejects a remove or update request whose terminator is owned by a different
  router, closing the two fabric handlers the batch fix did not cover
- adds a unit test for the single-terminator ownership 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:50:29 -04:00

93 lines
3.0 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)
})
}
// 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")
})
}