File size: 6,561 Bytes
e89cd08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | // _ _
// __ _____ __ ___ ___ __ _| |_ ___
// \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \
// \ V V / __/ (_| |\ V /| | (_| | || __/
// \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___|
//
// Copyright © 2016 - 2025 Weaviate B.V. All rights reserved.
//
// CONTACT: hello@weaviate.io
//
package objects
import (
"context"
"errors"
"fmt"
autherrs "github.com/weaviate/weaviate/usecases/auth/authorization/errors"
"github.com/weaviate/weaviate/usecases/auth/authorization"
"github.com/go-openapi/strfmt"
"github.com/weaviate/weaviate/entities/additional"
"github.com/weaviate/weaviate/entities/classcache"
"github.com/weaviate/weaviate/entities/models"
"github.com/weaviate/weaviate/entities/schema"
"github.com/weaviate/weaviate/entities/schema/crossref"
"github.com/weaviate/weaviate/usecases/objects/validation"
)
// AddObjectReference to an existing object. If the class contains a network
// ref, it has a side-effect on the schema: The schema will be updated to
// include this particular network ref class.
func (m *Manager) AddObjectReference(ctx context.Context, principal *models.Principal,
input *AddReferenceInput, repl *additional.ReplicationProperties, tenant string,
) *Error {
m.metrics.AddReferenceInc()
defer m.metrics.AddReferenceDec()
ctx = classcache.ContextWithClassCache(ctx)
input.Class = schema.UppercaseClassName(input.Class)
input.Class, _ = m.resolveAlias(input.Class)
if err := m.authorizer.Authorize(ctx, principal, authorization.UPDATE, authorization.ShardsData(input.Class, tenant)...); err != nil {
return &Error{err.Error(), StatusForbidden, err}
}
deprecatedEndpoint := input.Class == ""
if deprecatedEndpoint { // for backward compatibility only
if err := m.authorizer.Authorize(ctx, principal, authorization.READ, authorization.Collections()...); err != nil {
return &Error{err.Error(), StatusForbidden, err}
}
objectRes, err := m.getObjectFromRepo(ctx, "", input.ID,
additional.Properties{}, nil, tenant)
if err != nil {
errnf := ErrNotFound{} // treated as StatusBadRequest for backward comp
if errors.As(err, &errnf) {
return &Error{"source object deprecated", StatusBadRequest, err}
} else if errors.As(err, &ErrMultiTenancy{}) {
return &Error{"source object deprecated", StatusUnprocessableEntity, err}
}
return &Error{"source object deprecated", StatusInternalServerError, err}
}
input.Class = objectRes.Object().Class
}
if err := validateReferenceName(input.Class, input.Property); err != nil {
return &Error{err.Error(), StatusBadRequest, err}
}
class, schemaVersion, fetchedClass, typedErr := m.getAuthorizedFromClass(ctx, principal, input.Class)
if typedErr != nil {
return typedErr
}
validator := validation.New(m.vectorRepo.Exists, m.config, repl)
targetRef, err := input.validate(validator, class)
if err != nil {
if errors.As(err, &ErrMultiTenancy{}) {
return &Error{"validate inputs", StatusUnprocessableEntity, err}
}
var forbidden autherrs.Forbidden
if errors.As(err, &forbidden) {
return &Error{"validate inputs", StatusForbidden, err}
}
return &Error{"validate inputs", StatusBadRequest, err}
}
if input.Class != "" && targetRef.Class == "" {
toClass, toBeacon, replace, err := m.autodetectToClass(class, input.Property, targetRef)
if err != nil {
return err
}
if replace {
input.Ref.Class = toClass
input.Ref.Beacon = toBeacon
targetRef.Class = string(toClass)
}
}
if err := m.authorizer.Authorize(ctx, principal, authorization.READ, authorization.ShardsData(targetRef.Class, tenant)...); err != nil {
return &Error{err.Error(), StatusForbidden, err}
}
if err := input.validateExistence(ctx, validator, tenant, targetRef); err != nil {
return &Error{"validate existence", StatusBadRequest, err}
}
if !deprecatedEndpoint {
ok, err := m.vectorRepo.Exists(ctx, input.Class, input.ID, repl, tenant)
if err != nil {
switch {
case errors.As(err, &ErrMultiTenancy{}):
return &Error{"source object", StatusUnprocessableEntity, err}
default:
return &Error{"source object", StatusInternalServerError, err}
}
}
if !ok {
return &Error{"source object", StatusNotFound, err}
}
}
source := crossref.NewSource(schema.ClassName(input.Class),
schema.PropertyName(input.Property), input.ID)
target, err := crossref.ParseSingleRef(&input.Ref)
if err != nil {
return &Error{"parse target ref", StatusBadRequest, err}
}
if shouldValidateMultiTenantRef(tenant, source, target) {
_, err = validateReferenceMultiTenancy(ctx, principal,
m.schemaManager, m.vectorRepo, source, target, tenant, fetchedClass)
if err != nil {
switch {
case errors.As(err, &autherrs.Forbidden{}):
return &Error{"validation", StatusForbidden, err}
default:
return &Error{"multi-tenancy violation", StatusInternalServerError, err}
}
}
}
// Ensure that the local schema has caught up to the version we used to validate
if err := m.schemaManager.WaitForUpdate(ctx, schemaVersion); err != nil {
return &Error{
Msg: fmt.Sprintf("error waiting for local schema to catch up to version %d", schemaVersion),
Code: StatusInternalServerError,
Err: err,
}
}
if err := m.vectorRepo.AddReference(ctx, source, target, repl, tenant, schemaVersion); err != nil {
return &Error{"add reference to repo", StatusInternalServerError, err}
}
if err := m.updateRefVector(ctx, principal, input.Class, input.ID, tenant, class, schemaVersion); err != nil {
return &Error{"update ref vector", StatusInternalServerError, err}
}
return nil
}
func shouldValidateMultiTenantRef(tenant string, source *crossref.RefSource, target *crossref.Ref) bool {
return tenant != "" || (source != nil && target != nil && source.Class != "" && target.Class != "")
}
// AddReferenceInput represents required inputs to add a reference to an existing object.
type AddReferenceInput struct {
// Class name
Class string
// ID of an object
ID strfmt.UUID
// Property name
Property string
// Ref cross reference
Ref models.SingleRef
}
func (req *AddReferenceInput) validate(
v *validation.Validator,
class *models.Class,
) (*crossref.Ref, error) {
ref, err := v.ValidateSingleRef(&req.Ref)
if err != nil {
return nil, err
}
return ref, validateReferenceSchema(class, req.Property)
}
func (req *AddReferenceInput) validateExistence(
ctx context.Context,
v *validation.Validator, tenant string, ref *crossref.Ref,
) error {
return v.ValidateExistence(ctx, ref, "validate reference", tenant)
}
|