summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/spicedb/internal/namespace/caveats.go
blob: 5ddfa9d739305d05bde139a5a655213486708300 (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
package namespace

import (
	"fmt"

	"golang.org/x/exp/maps"

	"github.com/authzed/spicedb/pkg/caveats"
	caveattypes "github.com/authzed/spicedb/pkg/caveats/types"
	core "github.com/authzed/spicedb/pkg/proto/core/v1"
	"github.com/authzed/spicedb/pkg/schema"
)

// ValidateCaveatDefinition validates the parameters and types within the given caveat
// definition, including usage of the parameters.
func ValidateCaveatDefinition(ts *caveattypes.TypeSet, caveat *core.CaveatDefinition) error {
	// Ensure all parameters are used by the caveat expression itself.
	parameterTypes, err := caveattypes.DecodeParameterTypes(ts, caveat.ParameterTypes)
	if err != nil {
		return schema.NewTypeWithSourceError(
			fmt.Errorf("could not decode caveat parameters `%s`: %w", caveat.Name, err),
			caveat,
			caveat.Name,
		)
	}

	deserialized, err := caveats.DeserializeCaveatWithTypeSet(ts, caveat.SerializedExpression, parameterTypes)
	if err != nil {
		return schema.NewTypeWithSourceError(
			fmt.Errorf("could not decode caveat `%s`: %w", caveat.Name, err),
			caveat,
			caveat.Name,
		)
	}

	if len(caveat.ParameterTypes) == 0 {
		return schema.NewTypeWithSourceError(
			fmt.Errorf("caveat `%s` must have at least one parameter defined", caveat.Name),
			caveat,
			caveat.Name,
		)
	}

	referencedNames, err := deserialized.ReferencedParameters(maps.Keys(caveat.ParameterTypes))
	if err != nil {
		return err
	}

	for paramName, paramType := range caveat.ParameterTypes {
		_, err := caveattypes.DecodeParameterType(ts, paramType)
		if err != nil {
			return schema.NewTypeWithSourceError(
				fmt.Errorf("type error for parameter `%s` for caveat `%s`: %w", paramName, caveat.Name, err),
				caveat,
				paramName,
			)
		}

		if !referencedNames.Has(paramName) {
			return schema.NewTypeWithSourceError(
				NewUnusedCaveatParameterErr(caveat.Name, paramName),
				caveat,
				paramName,
			)
		}
	}

	return nil
}