-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathbundle.go
1295 lines (1113 loc) · 34.2 KB
/
bundle.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 (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1
package policy
import (
"context"
"fmt"
"io"
"regexp"
"sort"
"strings"
"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v11"
"go.mondoo.com/cnquery/v11/checksums"
"go.mondoo.com/cnquery/v11/explorer"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/logger"
"go.mondoo.com/cnquery/v11/mqlc"
"go.mondoo.com/cnquery/v11/mrn"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/resources"
"go.mondoo.com/cnquery/v11/utils/multierr"
"sigs.k8s.io/yaml"
)
const (
MRN_RESOURCE_QUERY = "queries"
MRN_RESOURCE_POLICY = "policies"
MRN_RESOURCE_ASSET = "assets"
MRN_RESOURCE_FRAMEWORK = "frameworks"
MRN_RESOURCE_FRAMEWORKMAP = "frameworkmaps"
MRN_RESOURCE_CONTROL = "controls"
MRN_RESOURCE_RISK = "risks"
)
type BundleResolver interface {
Load(ctx context.Context, path string) (*Bundle, error)
IsApplicable(path string) bool
}
type BundleLoader struct {
resolvers []BundleResolver
}
func NewBundleLoader(resolvers ...BundleResolver) *BundleLoader {
return &BundleLoader{resolvers: resolvers}
}
func DefaultBundleLoader() *BundleLoader {
return NewBundleLoader(defaultS3BundleResolver(), defaultFileBundleResolver())
}
func (l *BundleLoader) getResolver(path string) (BundleResolver, error) {
for _, resolver := range l.resolvers {
if resolver.IsApplicable(path) {
return resolver, nil
}
}
return nil, fmt.Errorf("no resolver found for path '%s'", path)
}
// Deprecated: Use BundleLoader.BundleFromPaths instead
func BundleFromPaths(paths ...string) (*Bundle, error) {
defaultLoader := DefaultBundleLoader()
return defaultLoader.BundleFromPaths(paths...)
}
// iterates through all the resolvers until it finds an applicable one and then uses that to load the bundle
// from the provided path
func (l *BundleLoader) BundleFromPaths(paths ...string) (*Bundle, error) {
ctx := context.Background()
aggregatedBundle := &Bundle{}
for _, path := range paths {
resolver, err := l.getResolver(path)
if err != nil {
return nil, err
}
bundle, err := resolver.Load(ctx, path)
if err != nil {
log.Error().Err(err).Msg("could not resolve bundle files")
return nil, err
}
aggregatedBundle = Merge(aggregatedBundle, bundle)
}
logger.DebugDumpYAML("resolved_mql_bundle.mql", aggregatedBundle)
return aggregatedBundle, nil
}
// BundleExecutionChecksum creates a combined execution checksum from a policy
// and framework. Either may be nil.
func BundleExecutionChecksum(ctx context.Context, policy *Policy, framework *Framework) string {
res := checksums.New
if policy != nil {
res = res.Add(policy.GraphExecutionChecksum)
}
if framework != nil {
res = res.Add(framework.GraphExecutionChecksum)
}
// So far the checksum only includes the policy and the framework
// It does not change if any of the jobs changes, only if the policy or the framework changes
// To update the resolved policy, when we change how it is generated, change the incoporated version of the resolver
res = res.Add(RESOLVER_VERSION)
return res.String()
}
// Merge combines two PolicyBundle and merges the data additive into one
// single PolicyBundle structure
func Merge(a *Bundle, b *Bundle) *Bundle {
res := &Bundle{}
res.OwnerMrn = a.OwnerMrn
if b.OwnerMrn != "" {
res.OwnerMrn = b.OwnerMrn
}
// merge in a
res.Policies = append(res.Policies, a.Policies...)
res.Packs = append(res.Packs, a.Packs...)
res.Props = append(res.Props, a.Props...)
res.Queries = append(res.Queries, a.Queries...)
res.Frameworks = append(res.Frameworks, a.Frameworks...)
res.FrameworkMaps = append(res.FrameworkMaps, a.FrameworkMaps...)
res.MigrationGroups = append(res.MigrationGroups, a.MigrationGroups...)
// merge in b
res.Policies = append(res.Policies, b.Policies...)
res.Packs = append(res.Packs, b.Packs...)
res.Props = append(res.Props, b.Props...)
res.Queries = append(res.Queries, b.Queries...)
res.Frameworks = append(res.Frameworks, b.Frameworks...)
res.FrameworkMaps = append(res.FrameworkMaps, b.FrameworkMaps...)
res.MigrationGroups = append(res.MigrationGroups, b.MigrationGroups...)
return res
}
// BundleFromYAML create a policy bundle from yaml contents
func BundleFromYAML(data []byte) (*Bundle, error) {
var res Bundle
err := yaml.Unmarshal(data, &res)
return &res, err
}
// ToYAML returns the policy bundle as yaml
func (p *Bundle) ToYAML() ([]byte, error) {
return yaml.Marshal(p)
}
// Prepares the bundle for compilation
func (b *Bundle) Prepare() {
b.ConvertEvidence()
b.ConvertQuerypacks()
}
// ConvertQuerypacks takes any existing querypacks in the bundle
// and turns them into policies for execution by cnspec.
func (p *Bundle) ConvertQuerypacks() {
for i := range p.Packs {
pack := p.Packs[i]
policy := Policy{
Mrn: pack.Mrn,
Uid: pack.Uid,
Name: pack.Name,
Version: pack.Version,
License: pack.License,
OwnerMrn: pack.OwnerMrn,
Docs: convertQueryPackDocs(pack.Docs),
Summary: pack.Summary,
Authors: pack.Authors,
Created: pack.Created,
Modified: pack.Modified,
Tags: pack.Tags,
Props: pack.Props,
Groups: convertQueryPackGroups(pack),
// we need this to indicate that the policy was converted from a querypack
ScoringSystem: explorer.ScoringSystem_DATA_ONLY,
}
p.Policies = append(p.Policies, &policy)
}
}
func (b *Bundle) ConvertEvidence() {
for _, f := range b.Frameworks {
pol, fm := f.GenerateEvidenceObjects()
if pol != nil {
b.Policies = append(b.Policies, pol)
}
if fm != nil {
b.FrameworkMaps = append(b.FrameworkMaps, fm)
}
}
}
func convertQueryPackDocs(q *explorer.QueryPackDocs) *PolicyDocs {
if q == nil {
return nil
}
return &PolicyDocs{
Desc: q.Desc,
}
}
func convertQueryPackGroups(p *explorer.QueryPack) []*PolicyGroup {
var res []*PolicyGroup
if len(p.Queries) > 0 {
// any builtin queries need to be put into a group for policies
res = append(res, &PolicyGroup{
Queries: p.Queries,
Type: GroupType_CHAPTER,
Uid: "default-queries",
Title: "Default Queries",
Filters: p.Filters,
})
}
for i := range p.Groups {
g := p.Groups[i]
res = append(res, &PolicyGroup{
Queries: g.Queries,
Type: GroupType_CHAPTER,
Filters: g.Filters,
Created: g.Created,
Modified: g.Modified,
Title: g.Title,
})
}
return res
}
func (p *Bundle) SourceHash() (string, error) {
raw, err := p.ToYAML()
if err != nil {
return "", err
}
c := checksums.New
c = c.Add(string(raw))
return c.String(), nil
}
// ToMap turns the PolicyBundle into a PolicyBundleMap
// dataLake (optional) may be used to provide queries/policies not found in the bundle
func (p *Bundle) ToMap() *PolicyBundleMap {
res := NewPolicyBundleMap(p.OwnerMrn)
for i := range p.Policies {
c := p.Policies[i]
res.Policies[c.Mrn] = c
for j := range c.RiskFactors {
r := c.RiskFactors[j]
res.RiskFactors[r.Mrn] = r
}
}
for i := range p.Frameworks {
c := p.Frameworks[i]
res.Frameworks[c.Mrn] = c
}
for i := range p.Queries {
c := p.Queries[i]
res.Queries[c.Mrn] = c
}
for i := range p.Props {
c := p.Props[i]
res.Props[c.Mrn] = c
}
return res
}
// FilterPolicies only keeps the given policy UIDs or MRNs and removes every other one.
// If a given policy has a MRN set (but no UID) it will try to get the UID from the MRN
// and also filter by that criteria.
// If the list of IDs is empty this function doesn't do anything.
// This function does not remove orphaned queries from the bundle.
func (p *Bundle) FilterPolicies(IDs []string) {
if p == nil || len(IDs) == 0 {
return
}
log.Debug().Msg("filter policies for asset")
valid := make(map[string]struct{}, len(IDs))
for i := range IDs {
valid[IDs[i]] = struct{}{}
}
var cur *Policy
var res []*Policy
for i := range p.Policies {
cur = p.Policies[i]
if cur.Mrn != "" {
if _, ok := valid[cur.Mrn]; ok {
res = append(res, cur)
continue
}
uid, _ := mrn.GetResource(cur.Mrn, MRN_RESOURCE_POLICY)
if _, ok := valid[uid]; ok {
res = append(res, cur)
continue
}
log.Debug().Str("policy", cur.Mrn).Msg("policy does not match user-provided filter")
// if we have a MRN we do not check the UID
continue
}
if _, ok := valid[cur.Uid]; ok {
res = append(res, cur)
continue
}
log.Debug().Str("policy", cur.Uid).Msg("policy does not match user-provided filter")
}
p.Policies = res
}
func (p *Bundle) RemoveOrphaned() {
panic("Not yet implemented, please open an issue at https://github.com/mondoohq/cnspec")
}
// Clean the policy bundle to turn a few nil fields into empty fields for consistency
func (p *Bundle) Clean() *Bundle {
for i := range p.Policies {
policy := p.Policies[i]
if policy.ComputedFilters == nil {
policy.ComputedFilters = &explorer.Filters{
Items: map[string]*explorer.Mquery{},
}
}
}
// consistency between db backends
if p.Props != nil && len(p.Props) == 0 {
p.Props = nil
}
return p
}
// Add another policy bundle into this. No duplicate policies, queries, or
// properties are allowed and will lead to an error. Both bundles must have
// MRNs for everything. OwnerMRNs must be identical as well.
func (p *Bundle) AddBundle(other *Bundle) error {
if p.OwnerMrn == "" {
p.OwnerMrn = other.OwnerMrn
} else if p.OwnerMrn != other.OwnerMrn {
return errors.New("when combining policy bundles the owner MRNs must be identical")
}
for i := range other.Policies {
c := other.Policies[i]
if c.Mrn == "" {
return errors.New("source policy bundle that is added has missing policy MRNs")
}
for j := range p.Policies {
if p.Policies[j].Mrn == c.Mrn {
return errors.New("cannot combine policy bundles, duplicate policy: " + c.Mrn)
}
}
p.Policies = append(p.Policies, c)
}
for i := range other.Queries {
c := other.Queries[i]
if c.Mrn == "" {
return errors.New("source policy bundle that is added has missing query MRNs")
}
for j := range p.Queries {
if p.Queries[j].Mrn == c.Mrn {
return errors.New("cannot combine policy bundles, duplicate query: " + c.Mrn)
}
}
p.Queries = append(p.Queries, c)
}
for i := range other.Props {
c := other.Props[i]
if c.Mrn == "" {
return errors.New("source policy bundle that is added has missing property MRNs")
}
for j := range p.Props {
if p.Props[j].Mrn == c.Mrn {
return errors.New("cannot combine policy bundles, duplicate property: " + c.Mrn)
}
}
p.Props = append(p.Props, c)
}
return nil
}
// PolicyMRNs in this bundle
func (p *Bundle) PolicyMRNs() []string {
mrns := []string{}
for i := range p.Policies {
// ensure a mrn is generated
p.Policies[i].RefreshMRN(p.OwnerMrn)
mrns = append(mrns, p.Policies[i].Mrn)
}
return mrns
}
// Sorts the queries, policies and queries' variants in the bundle.
func (p *Bundle) SortContents() {
sort.SliceStable(p.Queries, func(i, j int) bool {
if p.Queries[i].Mrn == "" || p.Queries[j].Mrn == "" {
return p.Queries[i].Uid < p.Queries[j].Uid
}
return p.Queries[i].Mrn < p.Queries[j].Mrn
})
sort.SliceStable(p.Policies, func(i, j int) bool {
if p.Policies[i].Mrn == "" || p.Policies[j].Mrn == "" {
return p.Policies[i].Uid < p.Policies[j].Uid
}
return p.Policies[i].Mrn < p.Policies[j].Mrn
})
for _, q := range p.Queries {
sort.SliceStable(q.Variants, func(i, j int) bool {
if q.Variants[i].Mrn == "" || q.Variants[j].Mrn == "" {
return q.Variants[i].Uid < q.Variants[j].Uid
}
return q.Variants[i].Mrn < q.Variants[j].Mrn
})
}
for _, pl := range p.Policies {
for _, g := range pl.Groups {
for _, q := range g.Queries {
sort.SliceStable(q.Variants, func(i, j int) bool {
if q.Variants[i].Mrn == "" || q.Variants[j].Mrn == "" {
return q.Variants[i].Uid < q.Variants[j].Uid
}
return q.Variants[i].Mrn < q.Variants[j].Mrn
})
}
for _, c := range g.Checks {
sort.SliceStable(c.Variants, func(i, j int) bool {
if c.Variants[i].Mrn == "" || c.Variants[j].Mrn == "" {
return c.Variants[i].Uid < c.Variants[j].Uid
}
return c.Variants[i].Mrn < c.Variants[j].Mrn
})
}
}
}
}
type docsPrinter interface {
Write(section string, data string)
}
type nocodeWriter struct {
docsWriter
}
var reCode = regexp.MustCompile("(?s)`[^`]+`|```.*```")
func (n nocodeWriter) Write(section string, data string) {
n.docsWriter.Write(section, reCode.ReplaceAllString(data, ""))
}
type docsWriter struct {
out io.Writer
}
func (n docsWriter) Write(section string, data string) {
if data == "" {
return
}
io.WriteString(n.out, section)
io.WriteString(n.out, ": ")
io.WriteString(n.out, data)
n.out.Write([]byte{'\n'})
}
func extractQueryDocs(query *explorer.Mquery, w docsPrinter, noIDs bool) {
if !noIDs {
w.Write("query ID", query.Uid)
w.Write("query Mrn", query.Mrn)
}
w.Write("query title", query.Title)
w.Write("query description", query.Desc)
if query.Docs != nil {
w.Write("query description", query.Docs.Desc)
w.Write("query audit", query.Docs.Audit)
if query.Docs.Remediation != nil {
for i := range query.Docs.Remediation.Items {
remediation := query.Docs.Remediation.Items[i]
if noIDs {
w.Write("query remediation", remediation.Desc)
} else {
w.Write("query remediation "+remediation.Id, remediation.Desc)
}
}
}
}
}
func extractPropertyDocs(prop *explorer.Property, w docsPrinter, noIDs bool) {
if !noIDs {
w.Write("property ID", prop.Uid)
w.Write("property Mrn", prop.Mrn)
}
w.Write("property title", prop.Title)
w.Write("property description", prop.Desc)
}
func extractPolicyDocs(policy *Policy, w docsPrinter, noIDs bool) {
if !noIDs {
w.Write("policy ID", policy.Uid)
w.Write("policy Mrn", policy.Mrn)
}
w.Write("policy summary", policy.Summary)
for i := range policy.Props {
extractPropertyDocs(policy.Props[i], w, noIDs)
}
if policy.Docs != nil {
w.Write("policy description", policy.Docs.Desc)
}
for g := range policy.Groups {
group := policy.Groups[g]
w.Write("group summary", group.Title)
if group.Docs != nil {
w.Write("group description", group.Docs.Desc)
}
for i := range group.Queries {
extractQueryDocs(group.Queries[i], w, noIDs)
}
for i := range group.Checks {
extractQueryDocs(group.Checks[i], w, noIDs)
}
}
}
func (p *Bundle) ExtractDocs(out io.Writer, noIDs bool, noCode bool) {
if p == nil {
return
}
var printer docsPrinter
if noCode {
printer = nocodeWriter{docsWriter: docsWriter{out}}
} else {
printer = docsWriter{out}
}
for i := range p.Queries {
extractQueryDocs(p.Queries[i], printer, noIDs)
}
for i := range p.Props {
extractPropertyDocs(p.Props[i], printer, noIDs)
}
for i := range p.Policies {
extractPolicyDocs(p.Policies[i], printer, noIDs)
}
}
func (p *bundleCache) ensureNoCyclesInVariants(queries []*explorer.Mquery) error {
// Gather all top-level queries with variants
queriesMap := map[string]*explorer.Mquery{}
for _, q := range queries {
if q == nil {
continue
}
if q.Mrn == "" {
// This should never happen. This function is called after all
// queries have their MRNs set.
panic("BUG: expected query MRN to be set for variant cycle detection")
}
queriesMap[q.Mrn] = q
}
if err := detectVariantCycles(queriesMap); err != nil {
return err
}
return nil
}
func (c *bundleCache) removeFailing(res *Bundle) {
if !c.conf.RemoveFailing {
return
}
for k := range c.removeQueries {
log.Debug().Str("query", k).Msg("removing query from bundle")
}
res.Queries = explorer.FilterQueryMRNs(c.removeQueries, res.Queries)
for i := range res.Policies {
policy := res.Policies[i]
groups := []*PolicyGroup{}
for j := range policy.Groups {
group := policy.Groups[j]
group.Queries = explorer.FilterQueryMRNs(c.removeQueries, group.Queries)
group.Checks = explorer.FilterQueryMRNs(c.removeQueries, group.Checks)
if len(group.Policies)+len(group.Queries)+len(group.Checks) > 0 {
groups = append(groups, group)
}
}
policy.Groups = groups
}
}
type nodeVisitStatus byte
const (
// NEW is the initial state of visiting a node. It means that the node has not been visited yet.
NEW nodeVisitStatus = iota
// ACTIVE means that the node is currently being visited. If we encounter a node that is in
// ACTIVE state, it means that we have a cycle.
ACTIVE
// VISITED means that the node has been visited.
VISITED
)
var ErrVariantCycleDetected = func(mrn string) error {
return fmt.Errorf("variant cycle detected in %s", mrn)
}
func detectVariantCycles(queries map[string]*explorer.Mquery) error {
statusMap := map[string]nodeVisitStatus{}
for _, query := range queries {
err := detectVariantCyclesDFS(query.Mrn, statusMap, queries)
if err != nil {
return err
}
}
return nil
}
func detectVariantCyclesDFS(mrn string, statusMap map[string]nodeVisitStatus, queries map[string]*explorer.Mquery) error {
q := queries[mrn]
if q == nil {
return nil
}
s := statusMap[mrn]
if s == VISITED {
return nil
} else if s == ACTIVE {
return ErrVariantCycleDetected(mrn)
}
statusMap[q.Mrn] = ACTIVE
for _, variant := range q.Variants {
if variant.Mrn == "" {
// This should never happen. This function is called after all
// queries have their MRNs set.
panic("BUG: expected variant MRN to be set for variant cycle detection")
}
v := queries[variant.Mrn]
if v == nil {
continue
}
err := detectVariantCyclesDFS(v.Mrn, statusMap, queries)
if err != nil {
return err
}
}
statusMap[q.Mrn] = VISITED
return nil
}
func topologicalSortQueries(queries []*explorer.Mquery) ([]*explorer.Mquery, error) {
// Gather all top-level queries with variants
queriesMap := map[string]*explorer.Mquery{}
for _, q := range queries {
if q == nil {
continue
}
if q.Mrn == "" {
// This should never happen. This function is called after all
// queries have their MRNs set.
panic("BUG: expected query MRN to be set for topological sort")
}
queriesMap[q.Mrn] = q
}
// Topologically sort the queries
sorted := &explorer.Mqueries{}
visited := map[string]struct{}{}
for _, q := range queriesMap {
err := topologicalSortQueriesDFS(q.Mrn, queriesMap, visited, sorted)
if err != nil {
return nil, err
}
}
return sorted.Items, nil
}
func topologicalSortQueriesDFS(queryMrn string, queriesMap map[string]*explorer.Mquery, visited map[string]struct{}, sorted *explorer.Mqueries) error {
if _, ok := visited[queryMrn]; ok {
return nil
}
visited[queryMrn] = struct{}{}
q := queriesMap[queryMrn]
if q == nil {
return nil
}
for _, variant := range q.Variants {
if variant.Mrn == "" {
// This should never happen. This function is called after all
// queries have their MRNs set.
panic("BUG: expected variant MRN to be set for topological sort")
}
err := topologicalSortQueriesDFS(variant.Mrn, queriesMap, visited, sorted)
if err != nil {
return err
}
}
sorted.Items = append(sorted.Items, q)
return nil
}
// Compile a bundle. See CompileExt for a full description.
func (p *Bundle) Compile(ctx context.Context, schema resources.ResourcesSchema, library Library) (*PolicyBundleMap, error) {
return p.CompileExt(ctx, BundleCompileConf{
CompilerConfig: mqlc.NewConfig(schema, cnquery.DefaultFeatures),
Library: library,
})
}
type BundleCompileConf struct {
mqlc.CompilerConfig
Library Library
RemoveFailing bool
}
// Compile PolicyBundle into a PolicyBundleMap
// Does 4 things:
// 1. turns policy bundle into a map for easier access
// 2. compile all queries. store code in the bundle map
// 3. validation of all contents
// 4. generate MRNs for all policies, queries, and properties and updates referencing local fields
// 5. snapshot all queries into the packs
// 6. make queries public that are only embedded
func (p *Bundle) CompileExt(ctx context.Context, conf BundleCompileConf) (*PolicyBundleMap, error) {
ownerMrn := p.OwnerMrn
if ownerMrn == "" {
// this only happens for local bundles where queries have no mrn yet
ownerMrn = "//local.cnspec.io/run/local-execution"
}
cache := &bundleCache{
ownerMrn: ownerMrn,
bundle: p,
conf: conf,
uid2mrn: map[string]string{},
removeQueries: map[string]struct{}{},
lookupProp: map[string]explorer.PropertyRef{},
lookupQuery: map[string]*explorer.Mquery{},
codeBundles: map[string]*llx.CodeBundle{},
}
// Process variants and inherit attributes filled from their parents
for _, query := range p.Queries {
if len(query.Variants) == 0 {
continue
}
// we do not have a bundle map yet. we need to do the check here
// so props are copied down before we compile props and queries
for i := range query.Variants {
ref := query.Variants[i]
for _, variant := range p.Queries {
if (variant.Uid != "" && variant.Uid == ref.Uid) ||
(variant.Mrn != "" && variant.Mrn == ref.Mrn) {
addBaseToVariant(query, variant)
}
}
}
}
// TODO: Make this compatible as a store for shared properties across queries.
// Also pre-compile as many as possible before returning any errors.
for i := range p.Props {
if err := cache.compileProp(p.Props[i]); err != nil {
return nil, err
}
}
if err := cache.compileQueries(p.Queries, nil); err != nil {
return nil, err
}
// Index policies + update MRNs and checksums, link properties via MRNs
for i := range p.Policies {
policy := p.Policies[i]
// make sure we get a copy of the UID before it is removed (via refresh MRN)
policyUID := policy.Uid
// !this is very important to prevent user overrides! vv
policy.InvalidateAllChecksums()
err := policy.RefreshMRN(ownerMrn)
if err != nil {
return nil, errors.New("failed to refresh policy " + policy.Mrn + ": " + err.Error())
}
if policyUID != "" {
cache.uid2mrn[policyUID] = policy.Mrn
}
// Properties
for i := range policy.Props {
if err := cache.compileProp(policy.Props[i]); err != nil {
return nil, err
}
}
// Filters: prep a data structure in case it doesn't exist yet and add
// any filters that child groups may carry with them
if policy.ComputedFilters == nil || policy.ComputedFilters.Items == nil {
policy.ComputedFilters = &explorer.Filters{Items: map[string]*explorer.Mquery{}}
}
if err = policy.ComputedFilters.Compile(ownerMrn, conf.CompilerConfig); err != nil {
return nil, multierr.Wrap(err, "failed to compile policy filters")
}
// ---- GROUPs -------------
for i := range policy.Groups {
group := policy.Groups[i]
// When filters are initially added they haven't been compiled
if err = group.Filters.Compile(ownerMrn, conf.CompilerConfig); err != nil {
return nil, multierr.Wrap(err, "failed to compile policy group filters")
}
if group.Filters != nil {
for j := range group.Filters.Items {
filter := group.Filters.Items[j]
policy.ComputedFilters.Items[filter.CodeId] = filter
}
}
for j := range group.Policies {
policyRef := group.Policies[j]
if err = policyRef.RefreshMRN(ownerMrn); err != nil {
return nil, err
}
policyRef.RefreshChecksum()
}
if err := cache.compileQueries(group.Queries, policy); err != nil {
return nil, err
}
if err := cache.compileQueries(group.Checks, policy); err != nil {
return nil, err
}
}
// ---- RISK FACTORS ---------
for i := range policy.RiskFactors {
risk := policy.RiskFactors[i]
if err := risk.RefreshMRN(ownerMrn); err != nil {
return nil, errors.New("failed to assign MRN to risk: " + err.Error())
}
risk.DetectScope()
if err := cache.compileRisk(risk, policy); err != nil {
return nil, errors.New("failed to compile risk: " + err.Error())
}
}
}
// Removing any failing queries happens after everything is compiled.
// We do this to the original bundle, because the intent is to
// clean it up with this option.
cache.removeFailing(p)
frameworksByMrn := map[string]*Framework{}
for _, framework := range p.Frameworks {
if err := framework.compile(ctx, ownerMrn, cache); err != nil {
return nil, errors.New("failed to validate framework: " + err.Error())
}
frameworksByMrn[framework.Mrn] = framework
}
for i := range p.FrameworkMaps {
fm := p.FrameworkMaps[i]
if err := fm.compile(ctx, ownerMrn, cache); err != nil {
return nil, errors.New("failed to validate framework map: " + err.Error())
}
framework, ok := frameworksByMrn[fm.FrameworkOwner.Mrn]
if !ok {
return nil, errors.New("failed to get framework in bundle (not yet supported) for " + fm.FrameworkOwner.Mrn)
}
framework.FrameworkMaps = append(framework.FrameworkMaps, fm)
}
// cannot be done before all policies and queries have their MRNs set
bundleMap := p.ToMap()
bundleMap.Library = cache.conf.Library
bundleMap.Code = cache.codeBundles
// Validate integrity of references + translate UIDs to MRNs
for i := range p.Policies {
policy := p.Policies[i]
if policy == nil {
return nil, errors.New("received null policy")
}
err := translateGroupUIDs(ownerMrn, policy, cache.uid2mrn)
if err != nil {
return nil, errors.New("failed to validate policy: " + err.Error())
}
err = bundleMap.ValidatePolicy(ctx, policy, cache.conf.CompilerConfig)
if err != nil {
return nil, errors.New("failed to validate policy: " + err.Error())
}
}
return bundleMap, cache.error()
}
// this uses a subset of the calls of Mquery.AddBase(), because we don't want
// to push all the fields into the variant, only a select few that are
// needed for context and execution.
func addBaseToVariant(base *explorer.Mquery, variant *explorer.Mquery) {
if variant == nil {
return
}
if variant.Title == "" {
variant.Title = base.Title
}
if variant.Desc == "" {
variant.Desc = base.Desc
}
if variant.Docs == nil {
variant.Docs = base.Docs
} else if base.Docs != nil {
if variant.Docs.Desc == "" {
variant.Docs.Desc = base.Docs.Desc
}
if variant.Docs.Audit == "" {
variant.Docs.Audit = base.Docs.Audit
}
if variant.Docs.Remediation == nil {
variant.Docs.Remediation = base.Docs.Remediation
}
if variant.Docs.Refs == nil {
variant.Docs.Refs = base.Docs.Refs
}
}
if variant.Impact == nil {
variant.Impact = base.Impact
}
if len(variant.Props) == 0 {
variant.Props = base.Props
}
// We are not copying filters, variants should have their own.
// We can't copy Tags because the parent query can have way more tags
// than are applicable to the variant.
}
type bundleCache struct {
ownerMrn string
lookupQuery map[string]*explorer.Mquery
lookupProp map[string]explorer.PropertyRef
uid2mrn map[string]string
removeQueries map[string]struct{}
codeBundles map[string]*llx.CodeBundle
bundle *Bundle
conf BundleCompileConf
errors []error
}