Files
ziti/controller/db/util.go
T
Paul Lorenz f8be418b33 Evaluate policies against candidate entities and policies instead of scanning. Fixes #4513
- selects the entities a policy change can affect from its ids, the entity role attribute index and its current links
- selects the policies an entity change can affect from its current links and the policy role attribute index
- indexes the #all wildcard in the policy role attribute indexes and skips it in the role attribute usage query
- adds DB version 50, which backfills the wildcard entries
- adds transition tests across all three policy types
2026-11-04 11:24:11 -05:00

106 lines
3.4 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 db
import (
"fmt"
"strings"
"github.com/openziti/foundation/v2/errorz"
"github.com/openziti/foundation/v2/stringz"
"github.com/openziti/ziti/v2/controller/storage/boltz"
"github.com/pkg/errors"
)
const (
RolePrefix = "#"
EntityPrefix = "@"
AllRole = "#all"
// AllRoleValue is AllRole without its prefix: how the wildcard appears once roles are split from
// their prefixes, and its key in the policy role-attribute indexes.
AllRoleValue = "all"
)
func validateRolesAndIds(field string, values []string) error {
if len(values) > 1 && stringz.Contains(values, AllRole) {
return errorz.NewFieldError(fmt.Sprintf("if using %v, it should be the only role specified", AllRole), field, values)
}
var invalidKeys []string
for _, entry := range values {
if !strings.HasPrefix(entry, RolePrefix) && !strings.HasPrefix(entry, EntityPrefix) {
invalidKeys = append(invalidKeys, entry)
}
}
if len(invalidKeys) > 0 {
return errorz.NewFieldError("role entries must prefixed with # (to indicate role attributes) or @ (to indicate a name or id)", field, invalidKeys)
}
return nil
}
func splitRolesAndIds(values []string) ([]string, []string, error) {
var roles []string
var ids []string
for _, entry := range values {
if strings.HasPrefix(entry, RolePrefix) {
entry = strings.TrimPrefix(entry, RolePrefix)
roles = append(roles, entry)
} else if strings.HasPrefix(entry, EntityPrefix) {
entry = strings.TrimPrefix(entry, EntityPrefix)
ids = append(ids, entry)
} else {
return nil, nil, errors.Errorf("'%v' is neither role attribute (prefixed with %v) or an entity id or name (prefixed with %v)",
entry, RolePrefix, EntityPrefix)
}
}
return roles, ids, nil
}
// roleAttributeOnlyTransform is a boltz SetIndex value transform that keeps
// only role entries (values prefixed with RolePrefix, with a non-empty
// suffix) and strips the prefix. It's used to build derived set indexes
// over policy role fields that mix role-attribute refs ("#attr") and entity
// refs ("@id").
//
// The "#all" wildcard is indexed under AllRoleValue. It is not a reference
// to an attribute named "all", and a policy cannot reference one, so readers
// that enumerate attributes skip that key. Policy evaluation reads it to find
// the policies that select every entity.
var roleAttributeOnlyTransform boltz.SetIndexValueTransform = func(ft boltz.FieldType, v []byte) (bool, boltz.FieldType, []byte) {
if ft != boltz.TypeString || len(v) < 2 || v[0] != RolePrefix[0] {
return false, ft, v
}
return true, ft, v[1:]
}
func FieldValuesToIds(new []boltz.FieldTypeAndValue) []string {
var entityRoles []string
for _, fv := range new {
entityRoles = append(entityRoles, string(fv.Value))
}
return entityRoles
}
func roleRef(val string) string {
return RolePrefix + val
}
func entityRef(val string) string {
return EntityPrefix + val
}