forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.go
1347 lines (1129 loc) · 37.7 KB
/
compile.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package compile implements bundles compilation and linking.
package compile
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/internal/compiler/wasm"
"github.com/open-policy-agent/opa/internal/debug"
"github.com/open-policy-agent/opa/internal/planner"
"github.com/open-policy-agent/opa/internal/ref"
initload "github.com/open-policy-agent/opa/internal/runtime/init"
"github.com/open-policy-agent/opa/internal/wasm/encoding"
"github.com/open-policy-agent/opa/ir"
"github.com/open-policy-agent/opa/loader"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/storage/inmem"
)
const (
// TargetRego is the default target. The source rego is copied (potentially
// rewritten for optimization purpsoes) into the bundle. The target supports
// base documents.
TargetRego = "rego"
// TargetWasm is an alternative target that compiles the policy into a wasm
// module instead of Rego. The target supports base documents.
TargetWasm = "wasm"
// TargetPlan is an altertive target that compiles the policy into an
// imperative query plan that can be further transpiled or interpreted.
TargetPlan = "plan"
)
// Targets contains the list of targets supported by the compiler.
var Targets = []string{
TargetRego,
TargetWasm,
TargetPlan,
}
const resultVar = ast.Var("result")
// Compiler implements bundle compilation and linking.
type Compiler struct {
capabilities *ast.Capabilities // the capabilities that compiled policies may require
bundle *bundle.Bundle // the bundle that the compiler operates on
revision *string // the revision to set on the output bundle
asBundle bool // whether to assume bundle layout on file loading or not
pruneUnused bool // whether to extend the entrypoint set for semantic equivalence of built bundles
filter loader.Filter // filter to apply to file loader
paths []string // file paths to load. TODO(tsandall): add support for supplying readers for embedded users.
entrypoints orderedStringSet // policy entrypoints required for optimization and certain targets
roots []string // optionally, bundle roots can be provided
useRegoAnnotationEntrypoints bool // allow compiler to late-bind entrypoints from annotated rules in policies.
optimizationLevel int // how aggressive should optimization be
target string // target type (wasm, rego, etc.)
output *io.Writer // output stream to write bundle to
entrypointrefs []*ast.Term // validated entrypoints computed from default decision or manually supplied entrypoints
compiler *ast.Compiler // rego ast compiler used for semantic checks and rewriting
policy *ir.Policy // planner output when wasm or plan targets are enabled
debug debug.Debug // optionally outputs debug information produced during build
enablePrintStatements bool // optionally enable rego print statements
bvc *bundle.VerificationConfig // represents the key configuration used to verify a signed bundle
bsc *bundle.SigningConfig // represents the key configuration used to generate a signed bundle
keyID string // represents the name of the default key used to verify a signed bundle
metadata *map[string]interface{} // represents additional data included in .manifest file
fsys fs.FS // file system to use when loading paths
ns string
regoVersion ast.RegoVersion
followSymlinks bool // optionally follow symlinks in the bundle directory when building the bundle
}
// New returns a new compiler instance that can be invoked.
func New() *Compiler {
return &Compiler{
asBundle: false,
optimizationLevel: 0,
target: TargetRego,
debug: debug.Discard(),
}
}
// WithRevision sets the revision to include in the output bundle manifest.
func (c *Compiler) WithRevision(r string) *Compiler {
c.revision = &r
return c
}
// WithAsBundle sets file loading mode on the compiler.
func (c *Compiler) WithAsBundle(enabled bool) *Compiler {
c.asBundle = enabled
return c
}
// WithPruneUnused will make rules be ignored that are defined on the same
// package as the entrypoint, but that are not in the entrypoint set.
//
// Notably this includes functions (they can't be entrypoints) and causes
// the built bundle to no longer be semantically equivalent to the bundle built
// without wasm.
//
// This affects the 'wasm' and 'plan' targets only. It has no effect on
// building 'rego' bundles, i.e., "ordinary bundles".
func (c *Compiler) WithPruneUnused(enabled bool) *Compiler {
c.pruneUnused = enabled
return c
}
// WithEntrypoints sets the policy entrypoints on the compiler. Entrypoints tell the
// compiler what rules to expect and where optimizations can be targeted. The wasm
// target requires at least one entrypoint as does optimization.
func (c *Compiler) WithEntrypoints(e ...string) *Compiler {
c.entrypoints = c.entrypoints.Append(e...)
return c
}
// WithRegoAnnotationEntrypoints allows the compiler to late-bind entrypoints, based
// on Rego entrypoint annotations. The rules tagged with entrypoint annotations are
// added to the global list of entrypoints before optimizations/target compilation.
func (c *Compiler) WithRegoAnnotationEntrypoints(enabled bool) *Compiler {
c.useRegoAnnotationEntrypoints = enabled
return c
}
// WithOptimizationLevel sets the optimization level on the compiler. By default
// optimizations are disabled. Higher levels apply more aggressive optimizations
// but can take longer.
func (c *Compiler) WithOptimizationLevel(n int) *Compiler {
c.optimizationLevel = n
return c
}
// WithTarget sets the output target type to use.
func (c *Compiler) WithTarget(t string) *Compiler {
c.target = t
return c
}
// WithOutput sets the output stream to write the bundle to.
func (c *Compiler) WithOutput(w io.Writer) *Compiler {
c.output = &w
return c
}
// WithDebug sets the output stream to write debug info to.
func (c *Compiler) WithDebug(sink io.Writer) *Compiler {
if sink != nil {
c.debug = debug.New(sink)
}
return c
}
// WithEnablePrintStatements enables print statements inside of modules compiled
// by the compiler. If print statements are not enabled, calls to print() are
// erased at compile-time.
func (c *Compiler) WithEnablePrintStatements(yes bool) *Compiler {
c.enablePrintStatements = yes
return c
}
// WithPaths adds input filepaths to read policy and data from.
func (c *Compiler) WithPaths(p ...string) *Compiler {
c.paths = append(c.paths, p...)
return c
}
// WithFilter sets the loader filter to use when reading non-bundle input files.
func (c *Compiler) WithFilter(filter loader.Filter) *Compiler {
c.filter = filter
return c
}
// WithBundle sets the input bundle to compile. This should be used as an
// alternative to reading from paths. This function overrides any file
// loading options.
func (c *Compiler) WithBundle(b *bundle.Bundle) *Compiler {
c.bundle = b
return c
}
// WithBundleVerificationConfig sets the key configuration to use to verify a signed bundle
func (c *Compiler) WithBundleVerificationConfig(config *bundle.VerificationConfig) *Compiler {
c.bvc = config
return c
}
// WithBundleSigningConfig sets the key configuration to use to generate a signed bundle
func (c *Compiler) WithBundleSigningConfig(config *bundle.SigningConfig) *Compiler {
c.bsc = config
return c
}
// WithBundleVerificationKeyID sets the key to use to verify a signed bundle.
// If provided, the "keyid" claim in the bundle signature, will be set to this value
func (c *Compiler) WithBundleVerificationKeyID(keyID string) *Compiler {
c.keyID = keyID
return c
}
// WithCapabilities sets the capabilities to use while checking policies.
func (c *Compiler) WithCapabilities(capabilities *ast.Capabilities) *Compiler {
c.capabilities = capabilities
return c
}
// WithFollowSymlinks sets whether or not to follow symlinks in the bundle directory when building the bundle
func (c *Compiler) WithFollowSymlinks(yes bool) *Compiler {
c.followSymlinks = yes
return c
}
// WithMetadata sets the additional data to be included in .manifest
func (c *Compiler) WithMetadata(metadata *map[string]interface{}) *Compiler {
c.metadata = metadata
return c
}
// WithRoots sets the roots to include in the output bundle manifest.
func (c *Compiler) WithRoots(r ...string) *Compiler {
c.roots = append(c.roots, r...)
return c
}
// WithFS sets the file system to use when loading paths
func (c *Compiler) WithFS(fsys fs.FS) *Compiler {
c.fsys = fsys
return c
}
// WithPartialNamespace sets the namespace to use for partial evaluation results
func (c *Compiler) WithPartialNamespace(ns string) *Compiler {
c.ns = ns
return c
}
func (c *Compiler) WithRegoVersion(v ast.RegoVersion) *Compiler {
c.regoVersion = v
return c
}
func addEntrypointsFromAnnotations(c *Compiler, ar []*ast.AnnotationsRef) error {
for _, ref := range ar {
var entrypoint ast.Ref
scope := ref.Annotations.Scope
if ref.Annotations.Entrypoint {
// Build up the entrypoint path from either package path or rule.
switch scope {
case "package":
if p := ref.GetPackage(); p != nil {
entrypoint = p.Path
}
case "rule":
if r := ref.GetRule(); r != nil {
entrypoint = r.Ref().GroundPrefix()
}
default:
continue // Wrong scope type. Bail out early.
}
// Get a slash-based path, as with a CLI-provided entrypoint.
escapedPath, err := storage.NewPathForRef(entrypoint)
if err != nil {
return err
}
slashPath := strings.Join(escapedPath, "/")
// Add new entrypoints to the appropriate places.
c.entrypoints = c.entrypoints.Append(slashPath)
c.entrypointrefs = append(c.entrypointrefs, ast.NewTerm(entrypoint))
}
}
return nil
}
// Build compiles and links the input files and outputs a bundle to the writer.
func (c *Compiler) Build(ctx context.Context) error {
if err := c.init(); err != nil {
return err
}
// Fail early if not using Rego annotation entrypoints.
if !c.useRegoAnnotationEntrypoints {
if err := c.checkNumEntrypoints(); err != nil {
return err
}
}
if err := c.initBundle(false); err != nil {
return err
}
// Extract annotations, and generate new entrypoints as needed.
if c.useRegoAnnotationEntrypoints {
moduleList := make([]*ast.Module, 0, len(c.bundle.Modules))
for _, modfile := range c.bundle.Modules {
moduleList = append(moduleList, modfile.Parsed)
}
as, errs := ast.BuildAnnotationSet(moduleList)
if len(errs) > 0 {
return errs
}
ar := as.Flatten()
// Patch in entrypoints from Rego annotations.
err := addEntrypointsFromAnnotations(c, ar)
if err != nil {
return err
}
}
// Ensure we have at least one valid entrypoint, or fail before compilation.
if err := c.checkNumEntrypoints(); err != nil {
return err
}
// Dedup entrypoint refs, if both CLI and entrypoint metadata annotations
// were used.
if err := c.dedupEntrypointRefs(); err != nil {
return err
}
if err := c.optimize(ctx); err != nil {
return err
}
switch c.target {
case TargetWasm:
if err := c.compileWasm(ctx); err != nil {
return err
}
case TargetPlan:
if err := c.compilePlan(ctx); err != nil {
return err
}
bs, err := json.Marshal(c.policy)
if err != nil {
return err
}
c.bundle.PlanModules = append(c.bundle.PlanModules, bundle.PlanModuleFile{
Path: bundle.PlanFile,
URL: bundle.PlanFile,
Raw: bs,
})
case TargetRego:
// nop
}
if c.revision != nil {
c.bundle.Manifest.Revision = *c.revision
}
if c.metadata != nil {
c.bundle.Manifest.Metadata = *c.metadata
}
if c.regoVersion == ast.RegoV1 {
if err := c.bundle.FormatModulesForRegoVersion(c.regoVersion, true, false); err != nil {
return err
}
} else {
if err := c.bundle.FormatModules(false); err != nil {
return err
}
}
if c.bsc != nil {
if err := c.bundle.GenerateSignature(c.bsc, c.keyID, false); err != nil {
return err
}
}
if c.output == nil {
return nil
}
return bundle.NewWriter(*c.output).Write(*c.bundle)
}
func (c *Compiler) init() error {
if c.capabilities == nil {
c.capabilities = ast.CapabilitiesForThisVersion()
}
var found bool
for _, t := range Targets {
if c.target == t {
found = true
break
}
}
if !found {
return fmt.Errorf("invalid target %q", c.target)
}
for _, e := range c.entrypoints {
r, err := ref.ParseDataPath(e)
if err != nil {
return fmt.Errorf("entrypoint %v not valid: use <package>/<rule>", e)
}
c.entrypointrefs = append(c.entrypointrefs, ast.NewTerm(r))
}
return nil
}
// Once the bundle has been loaded, we can check the entrypoint counts.
func (c *Compiler) checkNumEntrypoints() error {
if c.optimizationLevel > 0 && len(c.entrypointrefs) == 0 {
return errors.New("bundle optimizations require at least one entrypoint")
}
// Rego target does not require an entrypoint. Others currently do.
if c.target != TargetRego && len(c.entrypointrefs) == 0 {
return fmt.Errorf("%s compilation requires at least one entrypoint", c.target)
}
return nil
}
// Note(philipc): When an entrypoint is provided on the CLI and from an
// entrypoint annotation, it can lead to duplicates in the slice of
// entrypoint refs. This can cause panics down the line due to c.entrypoints
// being a different length than c.entrypointrefs. As a result, we have to
// trim out the duplicates.
func (c *Compiler) dedupEntrypointRefs() error {
// Build list of entrypoint refs, without duplicates.
newEntrypointRefs := make([]*ast.Term, 0, len(c.entrypointrefs))
entrypointRefSet := make(map[string]struct{}, len(c.entrypointrefs))
for i, r := range c.entrypointrefs {
refString := r.String()
// Store only the first index in the list that matches.
if _, ok := entrypointRefSet[refString]; !ok {
entrypointRefSet[refString] = struct{}{}
newEntrypointRefs = append(newEntrypointRefs, c.entrypointrefs[i])
}
}
c.entrypointrefs = newEntrypointRefs
return nil
}
// Bundle returns the compiled bundle. This function can be called to retrieve the
// output of the compiler (as an alternative to having the bundle written to a stream.)
func (c *Compiler) Bundle() *bundle.Bundle {
return c.bundle
}
func (c *Compiler) initBundle(usePath bool) error {
// If the bundle is already set, skip file loading.
if c.bundle != nil {
return nil
}
// TODO(tsandall): the metrics object should passed through here so we that
// we can track read and parse times.
load, err := initload.LoadPathsForRegoVersion(
c.regoVersion,
c.paths,
c.filter,
c.asBundle,
c.bvc,
false,
c.useRegoAnnotationEntrypoints,
c.followSymlinks,
c.capabilities,
c.fsys)
if err != nil {
return fmt.Errorf("load error: %w", err)
}
if c.asBundle {
var names []string
for k := range load.Bundles {
names = append(names, k)
}
sort.Strings(names)
var bundles []*bundle.Bundle
for _, k := range names {
bundles = append(bundles, load.Bundles[k])
}
result, err := bundle.MergeWithRegoVersion(bundles, c.regoVersion, usePath)
if err != nil {
return fmt.Errorf("bundle merge failed: %v", err)
}
c.bundle = result
return nil
}
// TODO(tsandall): roots could be automatically inferred based on the packages and data
// contents. That would require changes to the loader to preserve the
// locations where base documents were mounted under data.
result := &bundle.Bundle{}
result.SetRegoVersion(c.regoVersion)
if len(c.roots) > 0 {
result.Manifest.Roots = &c.roots
}
result.Manifest.Init()
result.Data = load.Files.Documents
modules := make([]string, 0, len(load.Files.Modules))
for k := range load.Files.Modules {
modules = append(modules, k)
}
sort.Strings(modules)
for _, module := range modules {
path := filepath.ToSlash(load.Files.Modules[module].Name)
result.Modules = append(result.Modules, bundle.ModuleFile{
URL: path,
Path: path,
Parsed: load.Files.Modules[module].Parsed,
Raw: load.Files.Modules[module].Raw,
})
}
c.bundle = result
return nil
}
func (c *Compiler) optimize(ctx context.Context) error {
if c.optimizationLevel <= 0 {
var err error
c.compiler, err = compile(c.capabilities, c.bundle, c.debug, c.enablePrintStatements)
return err
}
o := newOptimizer(c.capabilities, c.bundle).
WithEntrypoints(c.entrypointrefs).
WithDebug(c.debug.Writer()).
WithShallowInlining(c.optimizationLevel <= 1).
WithEnablePrintStatements(c.enablePrintStatements).
WithRegoVersion(c.regoVersion)
if c.ns != "" {
o = o.WithPartialNamespace(c.ns)
}
err := o.Do(ctx)
if err != nil {
return err
}
c.bundle = o.Bundle()
return nil
}
func (c *Compiler) compilePlan(context.Context) error {
// Lazily compile the modules if needed. If optimizations were run, the
// AST compiler will not be set because the default target does not require it.
if c.compiler == nil {
var err error
c.compiler, err = compile(c.capabilities, c.bundle, c.debug, c.enablePrintStatements)
if err != nil {
return err
}
}
if !c.pruneUnused {
// Find transitive dependents of entrypoints and add them to the set to compile.
//
// NOTE(tsandall): We compile entrypoints because the evaluator does not support
// evaluation of wasm-compiled rules when 'with' statements are in-scope. Compiling
// out the dependents avoids the need to support that case for now.
deps := map[*ast.Rule]struct{}{}
for i := range c.entrypointrefs {
transitiveDocumentDependents(c.compiler, c.entrypointrefs[i], deps)
}
extras := ast.NewSet()
for rule := range deps {
extras.Add(ast.NewTerm(rule.Path()))
}
sorted := extras.Sorted()
for i := 0; i < sorted.Len(); i++ {
p, err := sorted.Elem(i).Value.(ast.Ref).Ptr()
if err != nil {
return err
}
if !c.entrypoints.Contains(p) {
c.entrypoints = append(c.entrypoints, p)
c.entrypointrefs = append(c.entrypointrefs, sorted.Elem(i))
}
}
}
// Create query sets for each of the entrypoints.
resultSym := ast.NewTerm(resultVar)
queries := make([]planner.QuerySet, len(c.entrypointrefs))
var unmappedEntrypoints []string
for i := range c.entrypointrefs {
qc := c.compiler.QueryCompiler()
query := ast.NewBody(ast.Equality.Expr(resultSym, c.entrypointrefs[i]))
compiled, err := qc.Compile(query)
if err != nil {
return err
}
if len(c.compiler.GetRules(c.entrypointrefs[i].Value.(ast.Ref))) == 0 {
unmappedEntrypoints = append(unmappedEntrypoints, c.entrypoints[i])
}
queries[i] = planner.QuerySet{
Name: c.entrypoints[i],
Queries: []ast.Body{compiled},
RewrittenVars: qc.RewrittenVars(),
}
}
if len(unmappedEntrypoints) > 0 {
return fmt.Errorf("entrypoint %q does not refer to a rule or policy decision", unmappedEntrypoints[0])
}
// Prepare modules and builtins for the planner.
modules := make([]*ast.Module, 0, len(c.compiler.Modules))
for _, module := range c.compiler.Modules {
modules = append(modules, module)
}
builtins := make(map[string]*ast.Builtin, len(c.capabilities.Builtins))
for _, bi := range c.capabilities.Builtins {
builtins[bi.Name] = bi
}
// Plan the query sets.
p := planner.New().
WithQueries(queries).
WithModules(modules).
WithBuiltinDecls(builtins).
WithDebug(c.debug.Writer())
policy, err := p.Plan()
if err != nil {
return err
}
// dump policy IR (if "debug" wasn't requested, debug.Writer will discard it)
err = ir.Pretty(c.debug.Writer(), policy)
if err != nil {
return err
}
c.policy = policy
return nil
}
func (c *Compiler) compileWasm(ctx context.Context) error {
compiler := wasm.New()
found := false
have := compiler.ABIVersion()
if c.capabilities.WasmABIVersions == nil { // discern nil from len=0
c.debug.Printf("no wasm ABI versions in capabilities, building for %v", have)
found = true
}
for _, v := range c.capabilities.WasmABIVersions {
if v.Version == have.Version && v.Minor <= have.Minor {
found = true
break
}
}
if !found {
return fmt.Errorf("compiler ABI version not in capabilities (have %v, want %d)",
c.capabilities.WasmABIVersions,
compiler.ABIVersion(),
)
}
if err := c.compilePlan(ctx); err != nil {
return err
}
// Compile the policy into a wasm binary.
m, err := compiler.WithPolicy(c.policy).WithDebug(c.debug.Writer()).Compile()
if err != nil {
return err
}
var buf bytes.Buffer
if err := encoding.WriteModule(&buf, m); err != nil {
return err
}
modulePath := bundle.WasmFile
c.bundle.WasmModules = []bundle.WasmModuleFile{{
URL: modulePath,
Path: modulePath,
Raw: buf.Bytes(),
}}
flattenedAnnotations := c.compiler.GetAnnotationSet().Flatten()
// Each entrypoint needs an entry in the manifest
for i, e := range c.entrypointrefs {
entrypointPath := c.entrypoints[i]
var annotations []*ast.Annotations
if !c.isPackage(e) {
annotations = findAnnotationsForTerm(e, flattenedAnnotations)
}
c.bundle.Manifest.WasmResolvers = append(c.bundle.Manifest.WasmResolvers, bundle.WasmResolver{
Module: "/" + strings.TrimLeft(modulePath, "/"),
Entrypoint: entrypointPath,
Annotations: annotations,
})
}
// Remove the entrypoints from remaining source rego files
return pruneBundleEntrypoints(c.bundle, c.entrypointrefs)
}
func (c *Compiler) isPackage(term *ast.Term) bool {
for _, m := range c.compiler.Modules {
if m.Package.Path.Equal(term.Value) {
return true
}
}
return false
}
// findAnnotationsForTerm returns a slice of all annotations directly associated with the given term.
func findAnnotationsForTerm(term *ast.Term, annotationRefs []*ast.AnnotationsRef) []*ast.Annotations {
r, ok := term.Value.(ast.Ref)
if !ok {
return nil
}
var result []*ast.Annotations
for _, ar := range annotationRefs {
if r.Equal(ar.Path) {
result = append(result, ar.Annotations)
}
}
return result
}
// pruneBundleEntrypoints will modify modules in the provided bundle to remove
// rules matching the entrypoints along with injecting import statements to
// preserve their ability to compile.
func pruneBundleEntrypoints(b *bundle.Bundle, entrypointrefs []*ast.Term) error {
// For each package path keep a list of new imports to add.
requiredImports := map[string][]*ast.Import{}
for _, entrypoint := range entrypointrefs {
for i := 0; i < len(b.Modules); i++ {
mf := &b.Modules[i]
// Drop any rules that match the entrypoint path.
var rules []*ast.Rule
for _, rule := range mf.Parsed.Rules {
rulePath := rule.Path()
if !rulePath.Equal(entrypoint.Value) {
rules = append(rules, rule)
} else {
pkgPath := rule.Module.Package.Path.String()
newImport := &ast.Import{Path: ast.NewTerm(rulePath)}
shouldAdd := true
currentImports := requiredImports[pkgPath]
for _, imp := range currentImports {
if imp.Equal(newImport) {
shouldAdd = false
break
}
}
if shouldAdd {
requiredImports[pkgPath] = append(currentImports, newImport)
}
}
}
// Drop any Annotations for rules matching the entrypoint path
var annotations []*ast.Annotations
var prunedAnnotations []*ast.Annotations
for _, annotation := range mf.Parsed.Annotations {
p := annotation.GetTargetPath()
// We prune annotations of dropped rules, but not packages, as the Rego file is always retained
if p.Equal(entrypoint.Value) && !mf.Parsed.Package.Path.Equal(entrypoint.Value) {
prunedAnnotations = append(prunedAnnotations, annotation)
} else {
annotations = append(annotations, annotation)
}
}
// Drop comments associated with pruned annotations
var comments []*ast.Comment
for _, comment := range mf.Parsed.Comments {
pruned := false
for _, annotation := range prunedAnnotations {
if comment.Location.Row >= annotation.Location.Row &&
comment.Location.Row <= annotation.EndLoc().Row {
pruned = true
break
}
}
if !pruned {
comments = append(comments, comment)
}
}
// If any rules or annotations were dropped update the module accordingly
if len(rules) != len(mf.Parsed.Rules) || len(comments) != len(mf.Parsed.Comments) {
mf.Parsed.Rules = rules
mf.Parsed.Annotations = annotations
mf.Parsed.Comments = comments
// Remove the original raw source, we're editing the AST
// directly, so it won't be in sync anymore.
mf.Raw = nil
}
}
}
// Any packages which had rules removed need an import injected for the
// removed rule to keep the policies valid.
for i := 0; i < len(b.Modules); i++ {
mf := &b.Modules[i]
pkgPath := mf.Parsed.Package.Path.String()
if imports, ok := requiredImports[pkgPath]; ok {
mf.Raw = nil
mf.Parsed.Imports = append(mf.Parsed.Imports, imports...)
}
}
return nil
}
type undefinedEntrypointErr struct {
Entrypoint *ast.Term
}
func (err undefinedEntrypointErr) Error() string {
return fmt.Sprintf("undefined entrypoint %v", err.Entrypoint)
}
type optimizer struct {
capabilities *ast.Capabilities
bundle *bundle.Bundle
compiler *ast.Compiler
entrypoints []*ast.Term
nsprefix string
resultsymprefix string
outputprefix string
shallow bool
debug debug.Debug
enablePrintStatements bool
regoVersion ast.RegoVersion
}
func newOptimizer(c *ast.Capabilities, b *bundle.Bundle) *optimizer {
return &optimizer{
capabilities: c,
bundle: b,
nsprefix: "partial",
resultsymprefix: ast.WildcardPrefix,
outputprefix: "optimized",
debug: debug.Discard(),
}
}
func (o *optimizer) WithDebug(sink io.Writer) *optimizer {
if sink != nil {
o.debug = debug.New(sink)
}
return o
}
func (o *optimizer) WithEnablePrintStatements(yes bool) *optimizer {
o.enablePrintStatements = yes
return o
}
func (o *optimizer) WithEntrypoints(es []*ast.Term) *optimizer {
o.entrypoints = es
return o
}
func (o *optimizer) WithShallowInlining(yes bool) *optimizer {
o.shallow = yes
return o
}
func (o *optimizer) WithPartialNamespace(ns string) *optimizer {
o.nsprefix = ns
return o
}
func (o *optimizer) WithRegoVersion(regoVersion ast.RegoVersion) *optimizer {
o.regoVersion = regoVersion
return o
}
func (o *optimizer) Do(ctx context.Context) error {
// NOTE(tsandall): if there are multiple entrypoints, copy the bundle because
// if any of the optimization steps fail, we do not want to leave the caller's
// bundle in a partially modified state.
if len(o.entrypoints) > 1 {
cpy := o.bundle.Copy()
o.bundle = &cpy
}
// initialize other inputs to the optimization process (store, symbols, etc.)
data := o.bundle.Data
if data == nil {
data = map[string]interface{}{}
}
store := inmem.NewFromObjectWithOpts(data, inmem.OptRoundTripOnWrite(false))
resultsym := ast.VarTerm(o.resultsymprefix + "__result__")
usedFilenames := map[string]int{}
var unknowns []*ast.Term
// NOTE(tsandall): the entrypoints are optimized in order so that the optimization
// of entrypoint[1] sees the optimization of entrypoint[0] and so on. This is needed
// because otherwise the optimization outputs (e.g., support rules) would have to
// merged somehow. Instead of dealing with that, just run the optimizations in the
// order the user supplied the entrypoints in.
for i, e := range o.entrypoints {
var err error
o.compiler, err = compile(o.capabilities, o.bundle, o.debug, o.enablePrintStatements)
if err != nil {
return err
}
if unknowns == nil {
unknowns = o.findUnknowns()
}
required := o.findRequiredDocuments(e)
r := rego.New(
rego.ParsedQuery(ast.NewBody(ast.Equality.Expr(resultsym, e))),
rego.PartialNamespace(o.nsprefix),
rego.DisableInlining(required),
rego.ShallowInlining(o.shallow),
rego.SkipPartialNamespace(true),
rego.ParsedUnknowns(unknowns),
rego.Compiler(o.compiler),
rego.Store(store),
rego.Capabilities(o.capabilities),
rego.SetRegoVersion(o.regoVersion),
)
o.debug.Printf("optimizer: entrypoint: %v", e)
o.debug.Printf(" partial-namespace: %v", o.nsprefix)
o.debug.Printf(" disable-inlining: %v", required)
o.debug.Printf(" shallow-inlining: %v", o.shallow)
for i := range unknowns {
o.debug.Printf(" unknown: %v", unknowns[i])
}
pq, err := r.Partial(ctx)
if err != nil {
return err