/
validating.go
179 lines (146 loc) · 5.09 KB
/
validating.go
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
package testfixtures
import (
"context"
"errors"
"fmt"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/spicedb/internal/datastore"
"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/internal/datastore/options"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
)
type validatingDatastore struct {
delegate datastore.Datastore
}
// NewValidatingDatastore creates a proxy which runs validation on all call parameters before
// passing the call onward.
func NewValidatingDatastore(delegate datastore.Datastore) datastore.Datastore {
return validatingDatastore{delegate: delegate}
}
func (vd validatingDatastore) Close() error {
return vd.delegate.Close()
}
func (vd validatingDatastore) IsReady(ctx context.Context) (bool, error) {
return vd.delegate.IsReady(ctx)
}
func (vd validatingDatastore) DeleteRelationships(ctx context.Context, preconditions []*v1.Precondition, filter *v1.RelationshipFilter) (datastore.Revision, error) {
for _, precondition := range preconditions {
err := precondition.Validate()
if err != nil {
return datastore.NoRevision, err
}
}
if err := filter.Validate(); err != nil {
return datastore.NoRevision, err
}
return vd.delegate.DeleteRelationships(ctx, preconditions, filter)
}
func (vd validatingDatastore) WriteTuples(ctx context.Context, preconditions []*v1.Precondition, mutations []*v1.RelationshipUpdate) (datastore.Revision, error) {
for _, precondition := range preconditions {
err := precondition.Validate()
if err != nil {
return datastore.NoRevision, err
}
}
err := common.ValidateUpdatesToWrite(mutations)
if err != nil {
return datastore.NoRevision, err
}
for _, mutation := range mutations {
err = mutation.Validate()
if err != nil {
return datastore.NoRevision, err
}
}
return vd.delegate.WriteTuples(ctx, preconditions, mutations)
}
func (vd validatingDatastore) OptimizedRevision(ctx context.Context) (datastore.Revision, error) {
return vd.delegate.OptimizedRevision(ctx)
}
func (vd validatingDatastore) HeadRevision(ctx context.Context) (datastore.Revision, error) {
return vd.delegate.HeadRevision(ctx)
}
func (vd validatingDatastore) Watch(ctx context.Context, afterRevision datastore.Revision) (<-chan *datastore.RevisionChanges, <-chan error) {
return vd.delegate.Watch(ctx, afterRevision)
}
func (vd validatingDatastore) WriteNamespace(ctx context.Context, newConfig *core.NamespaceDefinition) (datastore.Revision, error) {
if err := newConfig.Validate(); err != nil {
return datastore.NoRevision, err
}
return vd.delegate.WriteNamespace(ctx, newConfig)
}
func (vd validatingDatastore) ReadNamespace(
ctx context.Context,
nsName string,
revision datastore.Revision,
) (*core.NamespaceDefinition, datastore.Revision, error) {
read, createdAt, err := vd.delegate.ReadNamespace(ctx, nsName, revision)
if err != nil {
return read, createdAt, err
}
err = read.Validate()
return read, createdAt, err
}
func (vd validatingDatastore) DeleteNamespace(ctx context.Context, nsName string) (datastore.Revision, error) {
return vd.delegate.DeleteNamespace(ctx, nsName)
}
func (vd validatingDatastore) QueryTuples(ctx context.Context,
filter *v1.RelationshipFilter,
revision datastore.Revision,
opts ...options.QueryOptionsOption,
) (datastore.TupleIterator, error) {
if err := filter.Validate(); err != nil {
return nil, err
}
queryOpts := options.NewQueryOptionsWithOptions(opts...)
for _, sub := range queryOpts.Usersets {
if err := sub.Validate(); err != nil {
return nil, err
}
}
return vd.delegate.QueryTuples(ctx, filter, revision, opts...)
}
func (vd validatingDatastore) ReverseQueryTuples(ctx context.Context,
subjectFilter *v1.SubjectFilter,
revision datastore.Revision,
opts ...options.ReverseQueryOptionsOption,
) (datastore.TupleIterator, error) {
if err := subjectFilter.Validate(); err != nil {
return nil, err
}
queryOpts := options.NewReverseQueryOptionsWithOptions(opts...)
if queryOpts.ResRelation != nil {
if queryOpts.ResRelation.Namespace == "" {
return nil, errors.New("resource relation on reverse query missing namespace")
}
if queryOpts.ResRelation.Relation == "" {
return nil, errors.New("resource relation on reverse query missing relation")
}
}
return vd.delegate.ReverseQueryTuples(ctx, subjectFilter, revision, opts...)
}
func (vd validatingDatastore) CheckRevision(ctx context.Context, revision datastore.Revision) error {
return vd.delegate.CheckRevision(ctx, revision)
}
func (vd validatingDatastore) ListNamespaces(
ctx context.Context,
revision datastore.Revision,
) ([]*core.NamespaceDefinition, error) {
read, err := vd.delegate.ListNamespaces(ctx, revision)
if err != nil {
return read, err
}
for _, nsDef := range read {
err := nsDef.Validate()
if err != nil {
return nil, err
}
}
return read, err
}
func (vd validatingDatastore) NamespaceCacheKey(namespaceName string, revision datastore.Revision) (string, error) {
return fmt.Sprintf("%s@%s", namespaceName, revision), nil
}
func (vd validatingDatastore) Statistics(ctx context.Context) (datastore.Stats, error) {
return vd.delegate.Statistics(ctx)
}