-
Notifications
You must be signed in to change notification settings - Fork 123
/
verify.go
110 lines (91 loc) · 3.4 KB
/
verify.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
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package verify
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1"
runtimev1 "github.com/cerbos/cerbos/api/genpb/cerbos/runtime/v1"
internalcompile "github.com/cerbos/cerbos/internal/compile"
"github.com/cerbos/cerbos/internal/engine"
"github.com/cerbos/cerbos/internal/schema"
"github.com/cerbos/cerbos/internal/storage/bundle"
"github.com/cerbos/cerbos/internal/storage/disk"
"github.com/cerbos/cerbos/internal/storage/index"
"github.com/cerbos/cerbos/internal/verify"
"github.com/cerbos/cerbos/private/compile"
enginev1 "github.com/cerbos/cerbos/api/genpb/cerbos/engine/v1"
"go.uber.org/zap"
)
// Files runs tests using the policy files in the given file system.
func Files(ctx context.Context, fsys fs.FS, idx compile.Index) (*policyv1.TestResults, error) {
if idx == nil {
var err error
idx, err = index.Build(ctx, fsys, index.WithBuildFailureLogLevel(zap.DebugLevel))
if err != nil {
idxErrs := new(index.BuildError)
if errors.As(err, &idxErrs) {
return nil, &compile.Errors{
Errors: &runtimev1.Errors{
Kind: &runtimev1.Errors_IndexBuildErrors{IndexBuildErrors: idxErrs.IndexBuildErrors},
},
}
}
return nil, fmt.Errorf("failed to build index: %w", err)
}
}
store := disk.NewFromIndexWithConf(idx, &disk.Conf{})
schemaMgr := schema.NewFromConf(ctx, store, schema.NewConf(schema.EnforcementReject))
compiler := internalcompile.NewManagerFromDefaultConf(ctx, store, schemaMgr)
eng, err := engine.NewEphemeral(compiler, schemaMgr)
if err != nil {
return nil, fmt.Errorf("failed to create engine: %w", err)
}
results, err := verify.Verify(ctx, fsys, eng, verify.Config{Trace: true})
if err != nil {
return nil, fmt.Errorf("failed to run tests: %w", err)
}
return results, nil
}
type BundleParams struct {
BundlePath string
TestsDir string
WorkDir string
}
// Bundle runs tests using the given policy bundle.
func Bundle(ctx context.Context, params BundleParams) (*policyv1.TestResults, error) {
bundleSrc, err := bundle.NewLocalSource(bundle.LocalParams{
BundlePath: params.BundlePath,
TempDir: params.WorkDir,
})
if err != nil {
return nil, fmt.Errorf("failed to create local bundle source from %q: %w", params.BundlePath, err)
}
schemaMgr := schema.NewFromConf(ctx, bundleSrc, schema.NewConf(schema.EnforcementReject))
eng, err := engine.NewEphemeral(bundleSrc, schemaMgr)
if err != nil {
return nil, fmt.Errorf("failed to create engine: %w", err)
}
results, err := verify.Verify(ctx, os.DirFS(params.TestsDir), eng, verify.Config{Trace: true})
if err != nil {
return nil, fmt.Errorf("failed to run tests: %w", err)
}
return results, nil
}
type simpleChecker interface {
Check(ctx context.Context, inputs []*enginev1.CheckInput) ([]*enginev1.CheckOutput, error)
}
func WithCustomChecker(ctx context.Context, fsys fs.FS, eng simpleChecker) (*policyv1.TestResults, error) {
results, err := verify.Verify(ctx, fsys, checkFunc(eng.Check), verify.Config{})
if err != nil {
return nil, fmt.Errorf("failed to run tests: %w", err)
}
return results, nil
}
type checkFunc func(ctx context.Context, inputs []*enginev1.CheckInput) ([]*enginev1.CheckOutput, error)
func (f checkFunc) Check(ctx context.Context, inputs []*enginev1.CheckInput, _ ...engine.CheckOpt) ([]*enginev1.CheckOutput, error) {
return f(ctx, inputs)
}