-
Notifications
You must be signed in to change notification settings - Fork 123
/
builder.go
349 lines (281 loc) · 9.36 KB
/
builder.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
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
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package index
import (
"context"
"fmt"
"io"
"io/fs"
"path"
"go.opencensus.io/stats"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1"
runtimev1 "github.com/cerbos/cerbos/api/genpb/cerbos/runtime/v1"
internaljsonschema "github.com/cerbos/cerbos/internal/jsonschema"
"github.com/cerbos/cerbos/internal/namer"
"github.com/cerbos/cerbos/internal/observability/metrics"
"github.com/cerbos/cerbos/internal/policy"
"github.com/cerbos/cerbos/internal/schema"
"github.com/cerbos/cerbos/internal/util"
)
// errSchemasInWrongDir signals that schemas folder is in the wrong place.
var errSchemasInWrongDir = fmt.Errorf("%s directory must be under the root of the storage directory", util.SchemasDirectory)
const maxLoggableBuildErrors = 5
// BuildError is an error type that contains details about the failures encountered during the index build.
type BuildError struct {
*runtimev1.IndexBuildErrors
nErr int
}
func (ibe *BuildError) Error() string {
return fmt.Sprintf("failed to build index: missing imports=%d, missing scopes=%d, duplicate definitions=%d, load failures=%d",
len(ibe.MissingImports), len(ibe.MissingScopes), len(ibe.DuplicateDefs), len(ibe.LoadFailures))
}
type buildOptions struct {
rootDir string
buildFailureLogLevel zapcore.Level
}
type BuildOpt func(*buildOptions)
func WithBuildFailureLogLevel(level zapcore.Level) BuildOpt {
return func(o *buildOptions) {
o.buildFailureLogLevel = level
}
}
func WithRootDir(rootDir string) BuildOpt {
return func(o *buildOptions) {
o.rootDir = rootDir
}
}
func mkBuildOpts(opts ...BuildOpt) buildOptions {
o := buildOptions{
buildFailureLogLevel: zap.ErrorLevel,
rootDir: ".",
}
for _, optFn := range opts {
optFn(&o)
}
return o
}
// Build builds an index from the policy files stored in a directory.
func Build(ctx context.Context, fsys fs.FS, opts ...BuildOpt) (Index, error) {
return build(ctx, fsys, mkBuildOpts(opts...))
}
func build(ctx context.Context, fsys fs.FS, opts buildOptions) (Index, error) {
if err := checkValidDir(fsys, opts.rootDir); err != nil {
return nil, err
}
ib := newIndexBuilder()
err := fs.WalkDir(fsys, opts.rootDir, func(filePath string, d fs.DirEntry, err error) error {
if err := ctx.Err(); err != nil {
return err
}
if err != nil {
return err
}
if d.IsDir() {
if filePath == path.Join(opts.rootDir, schema.Directory) ||
d.Name() == util.TestDataDirectory ||
util.IsHidden(d.Name()) {
return fs.SkipDir
} else if d.Name() == schema.Directory {
ib.addLoadFailure(filePath, errSchemasInWrongDir)
return fs.SkipDir
}
return nil
}
if !util.IsSupportedFileType(d.Name()) ||
util.IsSupportedTestFile(d.Name()) ||
util.IsHidden(d.Name()) {
return nil
}
if err := internaljsonschema.ValidatePolicy(fsys, filePath); err != nil {
ib.addLoadFailure(filePath, err)
return nil
}
p := &policyv1.Policy{}
if err := util.LoadFromJSONOrYAML(fsys, filePath, p); err != nil {
ib.addLoadFailure(filePath, err)
return nil
}
if err := policy.Validate(p); err != nil {
ib.addLoadFailure(filePath, err)
return nil
}
if p.Disabled {
ib.addDisabled(filePath)
return nil
}
ib.addPolicy(filePath, policy.Wrap(policy.WithMetadata(p, filePath, nil, filePath)))
return nil
})
if err != nil {
if c, ok := fsys.(io.Closer); ok {
_ = c.Close()
}
return nil, err
}
return ib.build(fsys, opts)
}
type indexBuilder struct {
executables map[namer.ModuleID]struct{}
modIDToFile map[namer.ModuleID]string
fileToModID map[string]namer.ModuleID
dependents map[namer.ModuleID]map[namer.ModuleID]struct{}
dependencies map[namer.ModuleID]map[namer.ModuleID]struct{}
missing map[namer.ModuleID][]*runtimev1.IndexBuildErrors_MissingImport
missingScopes map[namer.ModuleID]string
stats *statsCollector
duplicates []*runtimev1.IndexBuildErrors_DuplicateDef
loadFailures []*runtimev1.IndexBuildErrors_LoadFailure
disabled []string
}
func newIndexBuilder() *indexBuilder {
return &indexBuilder{
executables: make(map[namer.ModuleID]struct{}),
modIDToFile: make(map[namer.ModuleID]string),
fileToModID: make(map[string]namer.ModuleID),
dependents: make(map[namer.ModuleID]map[namer.ModuleID]struct{}),
dependencies: make(map[namer.ModuleID]map[namer.ModuleID]struct{}),
missing: make(map[namer.ModuleID][]*runtimev1.IndexBuildErrors_MissingImport),
missingScopes: make(map[namer.ModuleID]string),
stats: newStatsCollector(),
}
}
func (idx *indexBuilder) addLoadFailure(file string, err error) {
idx.loadFailures = append(idx.loadFailures, &runtimev1.IndexBuildErrors_LoadFailure{File: file, Error: err.Error()})
}
func (idx *indexBuilder) addDisabled(file string) {
idx.disabled = append(idx.disabled, file)
}
func (idx *indexBuilder) addPolicy(file string, p policy.Wrapper) {
// Is this is a duplicate of another file?
if otherFile, ok := idx.modIDToFile[p.ID]; ok && (otherFile != file) {
idx.duplicates = append(idx.duplicates, &runtimev1.IndexBuildErrors_DuplicateDef{
File: file,
OtherFile: otherFile,
})
return
}
idx.fileToModID[file] = p.ID
idx.modIDToFile[p.ID] = file
delete(idx.missing, p.ID)
delete(idx.missingScopes, p.ID)
idx.stats.add(p)
switch p.Kind {
case policy.ResourceKind, policy.PrincipalKind:
idx.executables[p.ID] = struct{}{}
case policy.DerivedRolesKind, policy.ExportVariablesKind:
// not executable
}
for _, dep := range policy.Dependencies(p.Policy) {
depID := namer.GenModuleIDFromFQN(dep)
idx.addDep(p.ID, depID)
// the dependent may not have been loaded by the indexer yet because it's still walking the directory.
if _, exists := idx.modIDToFile[depID]; !exists {
kind := policy.KindFromFQN(dep)
var desc string
switch kind {
case policy.DerivedRolesKind:
desc = "Derived roles"
case policy.ExportVariablesKind:
desc = "Variables"
default:
panic(fmt.Errorf("unexpected import kind %s", kind))
}
idx.missing[depID] = append(idx.missing[depID], &runtimev1.IndexBuildErrors_MissingImport{
ImportingFile: file,
Desc: fmt.Sprintf("%s import '%s' not found", desc, namer.SimpleName(dep)),
})
}
}
ancestors := policy.RequiredAncestors(p.Policy)
for aID, a := range ancestors {
if _, ok := idx.modIDToFile[aID]; !ok {
idx.missingScopes[aID] = a
}
}
}
func (idx *indexBuilder) addDep(child, parent namer.ModuleID) {
// When we compile a policy, we need to load the dependencies (imported variables and derived roles).
if _, ok := idx.dependencies[child]; !ok {
idx.dependencies[child] = make(map[namer.ModuleID]struct{})
}
idx.dependencies[child][parent] = struct{}{}
// if a derived role or variable export changes, we need to recompile all the policies that import it (dependents).
if _, ok := idx.dependents[parent]; !ok {
idx.dependents[parent] = make(map[namer.ModuleID]struct{})
}
idx.dependents[parent][child] = struct{}{}
}
func (idx *indexBuilder) build(fsys fs.FS, opts buildOptions) (*index, error) {
logger := zap.L().Named("index")
nErr := len(idx.missing) + len(idx.duplicates) + len(idx.loadFailures) + len(idx.missingScopes)
if nErr > 0 {
err := &BuildError{
IndexBuildErrors: &runtimev1.IndexBuildErrors{
Disabled: idx.disabled,
DuplicateDefs: idx.duplicates,
LoadFailures: idx.loadFailures,
},
nErr: nErr,
}
for _, missing := range idx.missing {
err.MissingImports = append(err.MissingImports, missing...)
}
for _, ms := range idx.missingScopes {
err.MissingScopes = append(err.MissingScopes, namer.PolicyKeyFromFQN(ms))
}
logBuildFailure(logger, opts.buildFailureLogLevel, err)
return nil, err
}
logger.Info(fmt.Sprintf("Found %d executable policies", len(idx.executables)))
stats.Record(context.Background(), metrics.IndexEntryCount.M(int64(len(idx.modIDToFile))))
return &index{
fsys: fsys,
executables: idx.executables,
modIDToFile: idx.modIDToFile,
fileToModID: idx.fileToModID,
dependents: idx.dependents,
dependencies: idx.dependencies,
buildOpts: opts,
schemaLoader: NewSchemaLoader(fsys, opts.rootDir),
stats: idx.stats.collate(),
}, nil
}
func logBuildFailure(logger *zap.Logger, level zapcore.Level, err *BuildError) {
ce := logger.Check(level, "Index build failed")
if ce == nil {
return
}
if err.nErr > maxLoggableBuildErrors && !logger.Core().Enabled(zap.DebugLevel) {
ce.Write(zap.String("error", "too many errors; run `cerbos compile` to see a full list"))
return
}
var fields []zapcore.Field
if len(err.MissingImports) > 0 {
fields = append(fields, zap.Any("missing", err.MissingImports))
}
if len(err.MissingScopes) > 0 {
fields = append(fields, zap.Any("missing_scopes", err.MissingScopes))
}
if len(err.LoadFailures) > 0 {
fields = append(fields, zap.Any("load_failures", err.LoadFailures))
}
if len(err.DuplicateDefs) > 0 {
fields = append(fields, zap.Any("duplicates", err.DuplicateDefs))
}
if len(err.Disabled) > 0 {
fields = append(fields, zap.Strings("disabled", err.Disabled))
}
ce.Write(fields...)
}
func checkValidDir(fsys fs.FS, dir string) error {
finfo, err := fs.Stat(fsys, dir)
if err != nil {
return fmt.Errorf("failed to stat %s: %w", dir, err)
}
if !finfo.IsDir() {
return fmt.Errorf("not a directory: %s", dir)
}
return nil
}