/
schema.go
293 lines (238 loc) · 8.5 KB
/
schema.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
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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package schema
import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"strings"
jsonschema "github.com/santhosh-tekuri/jsonschema/v5"
"github.com/tidwall/gjson"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
enginev1 "github.com/cerbos/cerbos/api/genpb/cerbos/engine/v1"
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1"
privatev1 "github.com/cerbos/cerbos/api/genpb/cerbos/private/v1"
"github.com/cerbos/cerbos/internal/cache"
"github.com/cerbos/cerbos/internal/observability/logging"
"github.com/cerbos/cerbos/internal/observability/tracing"
"github.com/cerbos/cerbos/internal/storage"
"github.com/cerbos/cerbos/internal/util"
)
const (
Directory = "_schemas"
URLScheme = "cerbos"
attrPath = "attr"
)
var alwaysValidResult = &ValidationResult{Reject: false}
type ValidationResult struct {
Errors ValidationErrorList
Reject bool
}
func (vr *ValidationResult) add(errs ...ValidationError) {
vr.Errors = append(vr.Errors, errs...)
}
type Manager interface {
ValidateCheckInput(context.Context, *policyv1.Schemas, *enginev1.CheckInput) (*ValidationResult, error)
ValidatePlanResourcesInput(context.Context, *policyv1.Schemas, *enginev1.PlanResourcesInput) (*ValidationResult, error)
CheckSchema(context.Context, string) error
}
type Loader interface {
LoadSchema(context.Context, string) (io.ReadCloser, error)
}
func NewNopManager() NopManager {
return NopManager{}
}
type NopManager struct{}
func (NopManager) ValidateCheckInput(_ context.Context, _ *policyv1.Schemas, _ *enginev1.CheckInput) (*ValidationResult, error) {
return alwaysValidResult, nil
}
func (NopManager) ValidatePlanResourcesInput(_ context.Context, _ *policyv1.Schemas, _ *enginev1.PlanResourcesInput) (*ValidationResult, error) {
return alwaysValidResult, nil
}
func (NopManager) CheckSchema(_ context.Context, _ string) error {
return nil
}
type manager struct {
conf *Conf
log *zap.Logger
loader Loader
cache *cache.Cache[string, *cacheEntry]
}
func New(ctx context.Context, loader Loader) (Manager, error) {
conf, err := GetConf()
if err != nil {
return nil, fmt.Errorf("failed to get config section %q: %w", confKey, err)
}
return NewFromConf(ctx, loader, conf), nil
}
func NewFromConf(_ context.Context, loader Loader, conf *Conf) Manager {
if conf.Enforcement == EnforcementNone {
return NopManager{}
}
mgr := &manager{
conf: conf,
log: zap.L().Named("schema"),
loader: loader,
cache: cache.New[string, *cacheEntry]("schema", conf.CacheSize),
}
if s, ok := loader.(storage.Subscribable); ok {
s.Subscribe(mgr)
}
return mgr
}
func (m *manager) CheckSchema(ctx context.Context, url string) error {
_, err := m.loadSchema(ctx, url)
return err
}
func (m *manager) ValidateCheckInput(ctx context.Context, schemas *policyv1.Schemas, input *enginev1.CheckInput) (*ValidationResult, error) {
log := logging.FromContext(ctx).With(zap.Any("input", input))
return m.validate(ctx, log, schemas, input.Principal.Attr, input.Resource.Attr, input.Actions, nil)
}
func (m *manager) ValidatePlanResourcesInput(ctx context.Context, schemas *policyv1.Schemas, input *enginev1.PlanResourcesInput) (*ValidationResult, error) {
log := logging.FromContext(ctx).With(zap.Any("input", input))
return m.validate(ctx, log, schemas, input.Principal.Attr, input.Resource.Attr, []string{input.Action}, func(err *jsonschema.ValidationError) bool {
// resource attributes are optional for query planning, so ignore errors from required properties
return !strings.HasSuffix(err.KeywordLocation, "/required")
})
}
func (m *manager) validate(ctx context.Context, log *zap.Logger, schemas *policyv1.Schemas, principalAttr, resourceAttr map[string]*structpb.Value, actions []string, resourceErrorFilter validationErrorFilter) (*ValidationResult, error) {
result := &ValidationResult{Reject: m.conf.Enforcement == EnforcementReject}
if schemas == nil {
return result, nil
}
ctx, span := tracing.StartSpan(ctx, "schema.Validate")
defer span.End()
if err := m.validateAttr(ctx, ErrSourcePrincipal, schemas.PrincipalSchema, principalAttr, actions, nil); err != nil {
var principalErrs ValidationErrorList
if ok := errors.As(err, &principalErrs); !ok {
return result, fmt.Errorf("failed to validate the principal: %w", err)
}
result.add(principalErrs...)
}
if err := m.validateAttr(ctx, ErrSourceResource, schemas.ResourceSchema, resourceAttr, actions, resourceErrorFilter); err != nil {
var resourceErrs ValidationErrorList
if ok := errors.As(err, &resourceErrs); !ok {
return result, fmt.Errorf("failed to validate the resource: %w", err)
}
result.add(resourceErrs...)
}
if len(result.Errors) > 0 {
log.Warn("Validation failed", zap.Strings("errors", result.Errors.ErrorMessages()))
}
return result, nil
}
func (m *manager) validateAttr(ctx context.Context, src ErrSource, schemaRef *policyv1.Schemas_Schema, attr map[string]*structpb.Value, actions []string, errorFilter validationErrorFilter) error {
if schemaRef == nil || schemaRef.Ref == "" {
return nil
}
// check whether the current actions are excluded from validation
if ignore := schemaRef.IgnoreWhen; ignore != nil && len(ignore.Actions) > 0 {
toValidate := filterActionsToValidate(ignore.Actions, actions)
if len(toValidate) == 0 {
return nil
}
if len(toValidate) != len(actions) {
m.log.Warn("Schema validation is enabled for some actions but disabled for others",
zap.Strings("all_actions", actions),
zap.Strings("actions_requiring_validation", toValidate))
}
}
schema, err := m.loadSchema(ctx, schemaRef.Ref)
if err != nil {
m.log.Warn("Failed to load schema", zap.String("schema", schemaRef.Ref), zap.Error(err))
return newSchemaLoadErr(src, schemaRef.Ref)
}
attrJSON, err := attrToJSONObject(src, attr)
if err != nil {
return err
}
if err := schema.Validate(attrJSON); err != nil {
var validationErr *jsonschema.ValidationError
if ok := errors.As(err, &validationErr); !ok {
return fmt.Errorf("unable to validate %s: %w", src, err)
}
return newValidationErrorList(validationErr, src, errorFilter)
}
return nil
}
func attrToJSONObject(src ErrSource, attr map[string]*structpb.Value) (interface{}, error) {
if attr == nil {
return map[string]interface{}{}, nil
}
jsonBytes, err := protojson.Marshal(&privatev1.AttrWrapper{Attr: attr})
if err != nil {
return nil, fmt.Errorf("failed to marshal %s: %w", src, err)
}
return gjson.GetBytes(jsonBytes, attrPath).Value(), nil
}
func (m *manager) loadSchema(ctx context.Context, url string) (*jsonschema.Schema, error) {
entry, ok := m.cache.Get(url)
if ok {
return entry.schema, entry.err
}
e := &cacheEntry{}
e.schema, e.err = m.loadSchemaFromStore(ctx, url)
if e.err != nil && errors.Is(e.err, fs.ErrNotExist) {
e.err = fmt.Errorf("schema %q does not exist in the store", url)
}
m.cache.Set(url, e)
return e.schema, e.err
}
func (m *manager) loadSchemaFromStore(ctx context.Context, schemaURL string) (*jsonschema.Schema, error) {
compiler := jsonschema.NewCompiler()
compiler.AssertFormat = true
compiler.AssertContent = true
compiler.LoadURL = func(path string) (io.ReadCloser, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
}
if u.Scheme == "" || u.Scheme == URLScheme {
relativePath := strings.TrimPrefix(u.Path, "/")
return m.loader.LoadSchema(ctx, relativePath)
}
loader, ok := jsonschema.Loaders[u.Scheme]
if !ok {
return nil, jsonschema.LoaderNotFoundError(path)
}
return loader(path)
}
return compiler.Compile(schemaURL)
}
func (m *manager) SubscriberID() string {
return "schema.manager"
}
func (m *manager) OnStorageEvent(events ...storage.Event) {
for _, event := range events {
//nolint:exhaustive
switch event.Kind {
case storage.EventAddOrUpdateSchema:
cacheKey := fmt.Sprintf("%s:///%s", URLScheme, event.SchemaFile)
_ = m.cache.Remove(cacheKey)
m.log.Debug("Handled schema add/update event", zap.String("schema", cacheKey))
case storage.EventDeleteSchema:
cacheKey := fmt.Sprintf("%s:///%s", URLScheme, event.SchemaFile)
_ = m.cache.Remove(cacheKey)
m.log.Warn("Handled schema delete event", zap.String("schema", cacheKey))
}
}
}
type cacheEntry struct {
schema *jsonschema.Schema
err error
}
func filterActionsToValidate(ignore, actions []string) []string {
filtered := actions
for _, glob := range ignore {
filtered = util.FilterGlobNotMatches(glob, filtered)
if len(filtered) == 0 {
return nil
}
}
return filtered
}