summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/pkg/development/resolver.go
blob: ddb8a671ca8ee8a4a7b136b3c6f6309c97c93b45 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package development

import (
	"context"
	"fmt"
	"strings"

	"github.com/ccoveille/go-safecast"

	log "github.com/authzed/spicedb/internal/logging"
	"github.com/authzed/spicedb/pkg/caveats"
	caveattypes "github.com/authzed/spicedb/pkg/caveats/types"
	"github.com/authzed/spicedb/pkg/namespace"
	core "github.com/authzed/spicedb/pkg/proto/core/v1"
	"github.com/authzed/spicedb/pkg/schema"
	"github.com/authzed/spicedb/pkg/schemadsl/compiler"
	"github.com/authzed/spicedb/pkg/schemadsl/dslshape"
	"github.com/authzed/spicedb/pkg/schemadsl/generator"
	"github.com/authzed/spicedb/pkg/schemadsl/input"
)

// ReferenceType is the type of reference.
type ReferenceType int

const (
	ReferenceTypeUnknown ReferenceType = iota
	ReferenceTypeDefinition
	ReferenceTypeCaveat
	ReferenceTypeRelation
	ReferenceTypePermission
	ReferenceTypeCaveatParameter
)

// SchemaReference represents a reference to a schema node.
type SchemaReference struct {
	// Source is the source of the reference.
	Source input.Source

	// Position is the position of the reference in the source.
	Position input.Position

	// Text is the text of the reference.
	Text string

	// ReferenceType is the type of reference.
	ReferenceType ReferenceType

	// ReferenceMarkdown is the markdown representation of the reference.
	ReferenceMarkdown string

	// TargetSource is the source of the target node, if any.
	TargetSource *input.Source

	// TargetPosition is the position of the target node, if any.
	TargetPosition *input.Position

	// TargetSourceCode is the source code representation of the target, if any.
	TargetSourceCode string

	// TargetNamePositionOffset is the offset from the target position from where the
	// *name* of the target is found.
	TargetNamePositionOffset int
}

// Resolver resolves references to schema nodes from source positions.
type Resolver struct {
	schema     *compiler.CompiledSchema
	typeSystem *schema.TypeSystem
}

// NewResolver creates a new resolver for the given schema.
func NewResolver(compiledSchema *compiler.CompiledSchema) (*Resolver, error) {
	resolver := schema.ResolverForCompiledSchema(*compiledSchema)
	ts := schema.NewTypeSystem(resolver)
	return &Resolver{schema: compiledSchema, typeSystem: ts}, nil
}

// ReferenceAtPosition returns the reference to the schema node at the given position in the source, if any.
func (r *Resolver) ReferenceAtPosition(source input.Source, position input.Position) (*SchemaReference, error) {
	nodeChain, err := compiler.PositionToAstNodeChain(r.schema, source, position)
	if err != nil {
		return nil, err
	}

	if nodeChain == nil {
		return nil, nil
	}

	relationReference := func(relation *core.Relation, def *schema.Definition) (*SchemaReference, error) {
		// NOTE: zeroes are fine here to mean "unknown"
		lineNumber, err := safecast.ToInt(relation.SourcePosition.ZeroIndexedLineNumber)
		if err != nil {
			log.Err(err).Msg("could not cast lineNumber to uint32")
		}
		columnPosition, err := safecast.ToInt(relation.SourcePosition.ZeroIndexedColumnPosition)
		if err != nil {
			log.Err(err).Msg("could not cast columnPosition to uint32")
		}
		relationPosition := input.Position{
			LineNumber:     lineNumber,
			ColumnPosition: columnPosition,
		}

		targetSourceCode, err := generator.GenerateRelationSource(relation, caveattypes.Default.TypeSet)
		if err != nil {
			return nil, err
		}

		if def.IsPermission(relation.Name) {
			return &SchemaReference{
				Source:   source,
				Position: position,
				Text:     relation.Name,

				ReferenceType:     ReferenceTypePermission,
				ReferenceMarkdown: fmt.Sprintf("permission %s", relation.Name),

				TargetSource:             &source,
				TargetPosition:           &relationPosition,
				TargetSourceCode:         targetSourceCode,
				TargetNamePositionOffset: len("permission "),
			}, nil
		}

		return &SchemaReference{
			Source:   source,
			Position: position,
			Text:     relation.Name,

			ReferenceType:     ReferenceTypeRelation,
			ReferenceMarkdown: fmt.Sprintf("relation %s", relation.Name),

			TargetSource:             &source,
			TargetPosition:           &relationPosition,
			TargetSourceCode:         targetSourceCode,
			TargetNamePositionOffset: len("relation "),
		}, nil
	}

	// Type reference.
	if ts, relation, ok := r.typeReferenceChain(nodeChain); ok {
		if relation != nil {
			return relationReference(relation, ts)
		}

		def := ts.Namespace()

		// NOTE: zeroes are fine here to mean "unknown"
		lineNumber, err := safecast.ToInt(def.SourcePosition.ZeroIndexedLineNumber)
		if err != nil {
			log.Err(err).Msg("could not cast lineNumber to uint32")
		}
		columnPosition, err := safecast.ToInt(def.SourcePosition.ZeroIndexedColumnPosition)
		if err != nil {
			log.Err(err).Msg("could not cast columnPosition to uint32")
		}

		defPosition := input.Position{
			LineNumber:     lineNumber,
			ColumnPosition: columnPosition,
		}

		docComment := ""
		comments := namespace.GetComments(def.Metadata)
		if len(comments) > 0 {
			docComment = strings.Join(comments, "\n") + "\n"
		}

		targetSourceCode := fmt.Sprintf("%sdefinition %s {\n\t// ...\n}", docComment, def.Name)
		if len(def.Relation) == 0 {
			targetSourceCode = fmt.Sprintf("%sdefinition %s {}", docComment, def.Name)
		}

		return &SchemaReference{
			Source:   source,
			Position: position,
			Text:     def.Name,

			ReferenceType:     ReferenceTypeDefinition,
			ReferenceMarkdown: fmt.Sprintf("definition %s", def.Name),

			TargetSource:             &source,
			TargetPosition:           &defPosition,
			TargetSourceCode:         targetSourceCode,
			TargetNamePositionOffset: len("definition "),
		}, nil
	}

	// Caveat Type reference.
	if caveatDef, ok := r.caveatTypeReferenceChain(nodeChain); ok {
		// NOTE: zeroes are fine here to mean "unknown"
		lineNumber, err := safecast.ToInt(caveatDef.SourcePosition.ZeroIndexedLineNumber)
		if err != nil {
			log.Err(err).Msg("could not cast lineNumber to uint32")
		}
		columnPosition, err := safecast.ToInt(caveatDef.SourcePosition.ZeroIndexedColumnPosition)
		if err != nil {
			log.Err(err).Msg("could not cast columnPosition to uint32")
		}

		defPosition := input.Position{
			LineNumber:     lineNumber,
			ColumnPosition: columnPosition,
		}

		var caveatSourceCode strings.Builder
		caveatSourceCode.WriteString(fmt.Sprintf("caveat %s(", caveatDef.Name))
		index := 0
		for paramName, paramType := range caveatDef.ParameterTypes {
			if index > 0 {
				caveatSourceCode.WriteString(", ")
			}

			caveatSourceCode.WriteString(fmt.Sprintf("%s %s", paramName, caveats.ParameterTypeString(paramType)))
			index++
		}
		caveatSourceCode.WriteString(") {\n\t// ...\n}")

		return &SchemaReference{
			Source:   source,
			Position: position,
			Text:     caveatDef.Name,

			ReferenceType:     ReferenceTypeCaveat,
			ReferenceMarkdown: fmt.Sprintf("caveat %s", caveatDef.Name),

			TargetSource:             &source,
			TargetPosition:           &defPosition,
			TargetSourceCode:         caveatSourceCode.String(),
			TargetNamePositionOffset: len("caveat "),
		}, nil
	}

	// Relation reference.
	if relation, ts, ok := r.relationReferenceChain(nodeChain); ok {
		return relationReference(relation, ts)
	}

	// Caveat parameter used in expression.
	if caveatParamName, caveatDef, ok := r.caveatParamChain(nodeChain, source, position); ok {
		targetSourceCode := fmt.Sprintf("%s %s", caveatParamName, caveats.ParameterTypeString(caveatDef.ParameterTypes[caveatParamName]))

		return &SchemaReference{
			Source:   source,
			Position: position,
			Text:     caveatParamName,

			ReferenceType:     ReferenceTypeCaveatParameter,
			ReferenceMarkdown: targetSourceCode,

			TargetSource:     &source,
			TargetSourceCode: targetSourceCode,
		}, nil
	}

	return nil, nil
}

func (r *Resolver) lookupCaveat(caveatName string) (*core.CaveatDefinition, bool) {
	for _, caveatDef := range r.schema.CaveatDefinitions {
		if caveatDef.Name == caveatName {
			return caveatDef, true
		}
	}

	return nil, false
}

func (r *Resolver) lookupRelation(defName, relationName string) (*core.Relation, *schema.Definition, bool) {
	ts, err := r.typeSystem.GetDefinition(context.Background(), defName)
	if err != nil {
		return nil, nil, false
	}

	rel, ok := ts.GetRelation(relationName)
	if !ok {
		return nil, nil, false
	}

	return rel, ts, true
}

func (r *Resolver) caveatParamChain(nodeChain *compiler.NodeChain, source input.Source, position input.Position) (string, *core.CaveatDefinition, bool) {
	if !nodeChain.HasHeadType(dslshape.NodeTypeCaveatExpression) {
		return "", nil, false
	}

	caveatDefNode := nodeChain.FindNodeOfType(dslshape.NodeTypeCaveatDefinition)
	if caveatDefNode == nil {
		return "", nil, false
	}

	caveatName, err := caveatDefNode.GetString(dslshape.NodeCaveatDefinitionPredicateName)
	if err != nil {
		return "", nil, false
	}

	caveatDef, ok := r.lookupCaveat(caveatName)
	if !ok {
		return "", nil, false
	}

	runePosition, err := r.schema.SourcePositionToRunePosition(source, position)
	if err != nil {
		return "", nil, false
	}

	exprRunePosition, err := nodeChain.Head().GetInt(dslshape.NodePredicateStartRune)
	if err != nil {
		return "", nil, false
	}

	if exprRunePosition > runePosition {
		return "", nil, false
	}

	relationRunePosition := runePosition - exprRunePosition

	caveatExpr, err := nodeChain.Head().GetString(dslshape.NodeCaveatExpressionPredicateExpression)
	if err != nil {
		return "", nil, false
	}

	// Split the expression into tokens and find the associated token.
	tokens := strings.FieldsFunc(caveatExpr, splitCELToken)
	currentIndex := 0
	for _, token := range tokens {
		if currentIndex <= relationRunePosition && currentIndex+len(token) >= relationRunePosition {
			if _, ok := caveatDef.ParameterTypes[token]; ok {
				return token, caveatDef, true
			}
		}
	}

	return "", caveatDef, true
}

func splitCELToken(r rune) bool {
	return r == ' ' || r == '(' || r == ')' || r == '.' || r == ',' || r == '[' || r == ']' || r == '{' || r == '}' || r == ':' || r == '='
}

func (r *Resolver) caveatTypeReferenceChain(nodeChain *compiler.NodeChain) (*core.CaveatDefinition, bool) {
	if !nodeChain.HasHeadType(dslshape.NodeTypeCaveatReference) {
		return nil, false
	}

	caveatName, err := nodeChain.Head().GetString(dslshape.NodeCaveatPredicateCaveat)
	if err != nil {
		return nil, false
	}

	return r.lookupCaveat(caveatName)
}

func (r *Resolver) typeReferenceChain(nodeChain *compiler.NodeChain) (*schema.Definition, *core.Relation, bool) {
	if !nodeChain.HasHeadType(dslshape.NodeTypeSpecificTypeReference) {
		return nil, nil, false
	}

	defName, err := nodeChain.Head().GetString(dslshape.NodeSpecificReferencePredicateType)
	if err != nil {
		return nil, nil, false
	}

	def, err := r.typeSystem.GetDefinition(context.Background(), defName)
	if err != nil {
		return nil, nil, false
	}

	relationName, err := nodeChain.Head().GetString(dslshape.NodeSpecificReferencePredicateRelation)
	if err != nil {
		return def, nil, true
	}

	startingRune, err := nodeChain.Head().GetInt(dslshape.NodePredicateStartRune)
	if err != nil {
		return def, nil, true
	}

	// If hover over the definition name, return the definition.
	if nodeChain.ForRunePosition() < startingRune+len(defName) {
		return def, nil, true
	}

	relation, ok := def.GetRelation(relationName)
	if !ok {
		return nil, nil, false
	}

	return def, relation, true
}

func (r *Resolver) relationReferenceChain(nodeChain *compiler.NodeChain) (*core.Relation, *schema.Definition, bool) {
	if !nodeChain.HasHeadType(dslshape.NodeTypeIdentifier) {
		return nil, nil, false
	}

	if arrowExpr := nodeChain.FindNodeOfType(dslshape.NodeTypeArrowExpression); arrowExpr != nil {
		// Ensure this on the left side of the arrow.
		rightExpr, err := arrowExpr.Lookup(dslshape.NodeExpressionPredicateRightExpr)
		if err != nil {
			return nil, nil, false
		}

		if rightExpr == nodeChain.Head() {
			return nil, nil, false
		}
	}

	relationName, err := nodeChain.Head().GetString(dslshape.NodeIdentiferPredicateValue)
	if err != nil {
		return nil, nil, false
	}

	parentDefNode := nodeChain.FindNodeOfType(dslshape.NodeTypeDefinition)
	if parentDefNode == nil {
		return nil, nil, false
	}

	defName, err := parentDefNode.GetString(dslshape.NodeDefinitionPredicateName)
	if err != nil {
		return nil, nil, false
	}

	return r.lookupRelation(defName, relationName)
}