summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/internal/relationships/errors.go
blob: 3237e0b0e3d1c02151a1d36639bf5a87f94839cc (plain)
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
package relationships

import (
	"fmt"
	"maps"
	"sort"
	"strings"

	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"

	v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
	"github.com/lithammer/fuzzysearch/fuzzy"

	"github.com/authzed/spicedb/pkg/genutil/mapz"
	core "github.com/authzed/spicedb/pkg/proto/core/v1"
	"github.com/authzed/spicedb/pkg/schema"
	"github.com/authzed/spicedb/pkg/spiceerrors"
	"github.com/authzed/spicedb/pkg/tuple"
)

// InvalidSubjectTypeError indicates that a write was attempted with a subject type which is not
// allowed on relation.
type InvalidSubjectTypeError struct {
	error
	relationship      tuple.Relationship
	relationType      *core.AllowedRelation
	additionalDetails map[string]string
}

// NewInvalidSubjectTypeError constructs a new error for attempting to write an invalid subject type.
func NewInvalidSubjectTypeError(
	relationship tuple.Relationship,
	relationType *core.AllowedRelation,
	definition *schema.Definition,
) error {
	allowedTypes, err := definition.AllowedDirectRelationsAndWildcards(relationship.Resource.Relation)
	if err != nil {
		return err
	}

	// Special case: if the subject is uncaveated but only a caveated version is allowed, return
	// a more descriptive error.
	if relationship.OptionalCaveat == nil {
		allowedCaveatsForSubject := mapz.NewSet[string]()

		for _, allowedType := range allowedTypes {
			if allowedType.RequiredCaveat != nil &&
				allowedType.RequiredCaveat.CaveatName != "" &&
				allowedType.Namespace == relationship.Subject.ObjectType &&
				allowedType.GetRelation() == relationship.Subject.Relation &&
				(allowedType.RequiredExpiration != nil) == (relationship.OptionalExpiration != nil) {
				allowedCaveatsForSubject.Add(allowedType.RequiredCaveat.CaveatName)
			}
		}

		if !allowedCaveatsForSubject.IsEmpty() {
			return InvalidSubjectTypeError{
				error: fmt.Errorf(
					"subjects of type `%s` are not allowed on relation `%s#%s` without one of the following caveats: %s",
					schema.SourceForAllowedRelation(relationType),
					relationship.Resource.ObjectType,
					relationship.Resource.Relation,
					strings.Join(allowedCaveatsForSubject.AsSlice(), ","),
				),
				relationship: relationship,
				relationType: relationType,
				additionalDetails: map[string]string{
					"allowed_caveats": strings.Join(allowedCaveatsForSubject.AsSlice(), ","),
				},
			}
		}
	}

	allowedTypeStrings := make([]string, 0, len(allowedTypes))
	for _, allowedType := range allowedTypes {
		allowedTypeStrings = append(allowedTypeStrings, schema.SourceForAllowedRelation(allowedType))
	}

	matches := fuzzy.RankFind(schema.SourceForAllowedRelation(relationType), allowedTypeStrings)
	sort.Sort(matches)
	if len(matches) > 0 {
		return InvalidSubjectTypeError{
			error: fmt.Errorf(
				"subjects of type `%s` are not allowed on relation `%s#%s`; did you mean `%s`?",
				schema.SourceForAllowedRelation(relationType),
				relationship.Resource.ObjectType,
				relationship.Resource.Relation,
				matches[0].Target,
			),
			relationship:      relationship,
			relationType:      relationType,
			additionalDetails: nil,
		}
	}

	return InvalidSubjectTypeError{
		error: fmt.Errorf(
			"subjects of type `%s` are not allowed on relation `%s#%s`",
			schema.SourceForAllowedRelation(relationType),
			relationship.Resource.ObjectType,
			relationship.Resource.Relation,
		),
		relationship:      relationship,
		relationType:      relationType,
		additionalDetails: nil,
	}
}

// GRPCStatus implements retrieving the gRPC status for the error.
func (err InvalidSubjectTypeError) GRPCStatus() *status.Status {
	details := map[string]string{
		"definition_name": err.relationship.Resource.ObjectType,
		"relation_name":   err.relationship.Resource.Relation,
		"subject_type":    schema.SourceForAllowedRelation(err.relationType),
	}

	if err.additionalDetails != nil {
		maps.Copy(details, err.additionalDetails)
	}

	return spiceerrors.WithCodeAndDetails(
		err,
		codes.InvalidArgument,
		spiceerrors.ForReason(
			v1.ErrorReason_ERROR_REASON_INVALID_SUBJECT_TYPE,
			details,
		),
	)
}

// CannotWriteToPermissionError indicates that a write was attempted on a permission.
type CannotWriteToPermissionError struct {
	error
	rel tuple.Relationship
}

// NewCannotWriteToPermissionError constructs a new error for attempting to write to a permission.
func NewCannotWriteToPermissionError(rel tuple.Relationship) CannotWriteToPermissionError {
	return CannotWriteToPermissionError{
		error: fmt.Errorf(
			"cannot write a relationship to permission `%s` under definition `%s`",
			rel.Resource.Relation,
			rel.Resource.ObjectType,
		),
		rel: rel,
	}
}

// GRPCStatus implements retrieving the gRPC status for the error.
func (err CannotWriteToPermissionError) GRPCStatus() *status.Status {
	return spiceerrors.WithCodeAndDetails(
		err,
		codes.InvalidArgument,
		spiceerrors.ForReason(
			v1.ErrorReason_ERROR_REASON_CANNOT_UPDATE_PERMISSION,
			map[string]string{
				"definition_name": err.rel.Resource.ObjectType,
				"permission_name": err.rel.Resource.Relation,
			},
		),
	)
}

// CaveatNotFoundError indicates that a caveat referenced in a relationship update was not found.
type CaveatNotFoundError struct {
	error
	relationship tuple.Relationship
}

// NewCaveatNotFoundError constructs a new caveat not found error.
func NewCaveatNotFoundError(relationship tuple.Relationship) CaveatNotFoundError {
	return CaveatNotFoundError{
		error: fmt.Errorf(
			"the caveat `%s` was not found for relationship `%s`",
			relationship.OptionalCaveat.CaveatName,
			tuple.MustString(relationship),
		),
		relationship: relationship,
	}
}

// GRPCStatus implements retrieving the gRPC status for the error.
func (err CaveatNotFoundError) GRPCStatus() *status.Status {
	return spiceerrors.WithCodeAndDetails(
		err,
		codes.FailedPrecondition,
		spiceerrors.ForReason(
			v1.ErrorReason_ERROR_REASON_UNKNOWN_CAVEAT,
			map[string]string{
				"caveat_name": err.relationship.OptionalCaveat.CaveatName,
			},
		),
	)
}