-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathresolver_v2_test.go
2006 lines (1761 loc) · 59.8 KB
/
resolver_v2_test.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_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v11/explorer"
"go.mondoo.com/cnquery/v11/mqlc"
"go.mondoo.com/cnquery/v11/providers"
"go.mondoo.com/cnspec/v11/internal/datalakes/inmemory"
"go.mondoo.com/cnspec/v11/policy"
)
func collectQueriesFromRiskFactors(p *policy.Policy, query map[string]*explorer.Mquery) {
for _, rf := range p.RiskFactors {
for _, c := range rf.Checks {
query[c.Mrn] = c
}
}
}
func newResolvedPolicyTester(bundle *policy.Bundle, conf mqlc.CompilerConfig) *resolvedPolicyTester {
m := bundle.ToMap()
for _, p := range m.Policies {
collectQueriesFromRiskFactors(p, m.Queries)
}
return &resolvedPolicyTester{
bundleMap: m,
items: []resolvedPolicyTesterItem{},
conf: conf,
}
}
type resolvedPolicyTesterItem interface {
testIt(t *testing.T, resolvedPolicy *policy.ResolvedPolicy)
}
type resolvedPolicyTester struct {
bundleMap *policy.PolicyBundleMap
items []resolvedPolicyTesterItem
conf mqlc.CompilerConfig
}
func (r *resolvedPolicyTester) doTest(t *testing.T, rp *policy.ResolvedPolicy) {
for _, item := range r.items {
item.testIt(t, rp)
}
}
func (r *resolvedPolicyTester) ExecutesQuery(mrn string) *resolvedPolicyTesterExecutesQueryBuilder {
item := &resolvedPolicyTesterExecutesQueryBuilder{tester: r, mrn: mrn}
r.items = append(r.items, item)
return item
}
func (r *resolvedPolicyTester) DoesNotExecutesQuery(mrn string) {
item := &resolvedPolicyTesterExecutesQueryBuilder{tester: r, mrn: mrn, doesNotExecute: true}
r.items = append(r.items, item)
}
type resolvedPolicyTesterExecutesQueryBuilder struct {
tester *resolvedPolicyTester
mrn string
datapoints *[]string
props *map[string]string
doesNotExecute bool
}
func (r *resolvedPolicyTesterExecutesQueryBuilder) WithProps(props map[string]string) *resolvedPolicyTesterExecutesQueryBuilder {
r.props = &props
return r
}
func (r *resolvedPolicyTesterExecutesQueryBuilder) testIt(t *testing.T, rp *policy.ResolvedPolicy) {
q := r.tester.bundleMap.Queries[r.mrn]
require.NotNilf(t, q, "query not found in bundle: %s", r.mrn)
codeId := q.CodeId
require.NotEmptyf(t, codeId, "query %s doesn't have code id", r.mrn)
eq := rp.ExecutionJob.Queries[codeId]
if r.doesNotExecute {
require.Nil(t, eq, "query %s should not be executed", r.mrn)
return
}
require.NotNilf(t, eq, "query %s not found in ExecutionJob", r.mrn)
if r.datapoints != nil {
require.ElementsMatchf(t, *r.datapoints, eq.Datapoints, "datapoints mismatch for query %q", r.mrn)
}
if r.props != nil {
require.Lenf(t, eq.Properties, len(*r.props), "properties mismatch for query %q", r.mrn)
for propName, mql := range *r.props {
// Compile the property
codeBundle, err := mqlc.Compile(mql, nil, r.tester.conf)
require.NoErrorf(t, err, "failed to compile property %q for query %q", propName, r.mrn)
propCodeId := codeBundle.CodeV2.Id
require.NotEmptyf(t, propCodeId, "property %s doesn't have code id", propName)
propEq := rp.ExecutionJob.Queries[propCodeId]
require.NotNilf(t, propEq, "property %q not found in ExecutionJob with code id %q", propName, propCodeId)
require.Lenf(t, propEq.Datapoints, 1, "property %q should have exactly one datapoint", propName)
propDatapoint := propEq.Datapoints[0]
require.Equalf(t, eq.Properties[propName], propDatapoint, "property %q value mismatch", propName)
}
}
}
type resolvedPolicyTesterReportingJobNotifiesBuilder struct {
rjTester *resolvedPolicyTesterReportingJobBuilder
childMrn string
childMrnForCodeId string
parent string
impact *explorer.Impact
impactSet bool
}
func (r *resolvedPolicyTesterReportingJobNotifiesBuilder) WithImpact(impact *explorer.Impact) *resolvedPolicyTesterReportingJobNotifiesBuilder {
r.impact = impact
r.impactSet = true
return r
}
func findReportingJobByQrId(rp *policy.ResolvedPolicy, qrId string) *policy.ReportingJob {
for _, rj := range rp.CollectorJob.ReportingJobs {
if rj.QrId == qrId {
return rj
}
}
return nil
}
func (r *resolvedPolicyTesterReportingJobNotifiesBuilder) testIt(t *testing.T, rp *policy.ResolvedPolicy) {
var qrId string
var extraInfo string
mrnsMatchesQrId := false
if r.childMrn != "" {
qrId = r.childMrn
mrnsMatchesQrId = true
} else {
q := r.rjTester.tester.bundleMap.Queries[r.childMrnForCodeId]
require.NotNilf(t, q, "query not found in bundle: %s", r.childMrnForCodeId)
require.NotEmptyf(t, q.CodeId, "query %s doesn't have code id", r.childMrnForCodeId)
qrId = q.CodeId
extraInfo = " (" + r.childMrnForCodeId + ")"
}
childRj := findReportingJobByQrId(rp, qrId)
require.NotNilf(t, childRj, "child reporting job %s%s not found", qrId, extraInfo)
if mrnsMatchesQrId {
require.Equalf(t, []string{qrId}, childRj.Mrns, "child reporting job %s%s mrns mismatch", qrId, extraInfo)
}
parentRj := findReportingJobByQrId(rp, r.parent)
require.NotNilf(t, parentRj, "parent reporting job %s not found", r.parent)
require.Containsf(t, childRj.Notify, parentRj.Uuid, "child reporting job %s%s doesn't notify parent reporting job %s", qrId, extraInfo, r.parent)
require.Containsf(t, parentRj.ChildJobs, childRj.Uuid, "parent reporting job %s doesn't have child reporting job %s%s", r.parent, qrId, extraInfo)
if r.impactSet {
require.EqualExportedValuesf(t, r.impact, parentRj.ChildJobs[childRj.Uuid], "impact mismatch for child reporting job %s%s", qrId, extraInfo)
}
}
type resolvedPolicyTesterReportingJobBuilder struct {
tester *resolvedPolicyTester
mrn string
mrnForCodeId string
typ *policy.ReportingJob_Type
scoringSystem *explorer.ScoringSystem
notifies []*resolvedPolicyTesterReportingJobNotifiesBuilder
notifiesSet bool
doesNotExist bool
}
func (r *resolvedPolicyTester) CodeIdReportingJobForMrn(mrn string) *resolvedPolicyTesterReportingJobBuilder {
var item *resolvedPolicyTesterReportingJobBuilder
for _, existing := range r.items {
if existingItem, ok := existing.(*resolvedPolicyTesterReportingJobBuilder); ok && existingItem.mrnForCodeId == mrn {
item = existingItem
break
}
}
if item == nil {
item = &resolvedPolicyTesterReportingJobBuilder{tester: r, mrnForCodeId: mrn}
r.items = append(r.items, item)
}
return item
}
func (r *resolvedPolicyTester) ReportingJobByMrn(mrn string) *resolvedPolicyTesterReportingJobBuilder {
var item *resolvedPolicyTesterReportingJobBuilder
for _, existing := range r.items {
if existingItem, ok := existing.(*resolvedPolicyTesterReportingJobBuilder); ok && existingItem.mrn == mrn {
item = existingItem
break
}
}
if item == nil {
item = &resolvedPolicyTesterReportingJobBuilder{tester: r, mrn: mrn}
r.items = append(r.items, item)
}
return item
}
func (r *resolvedPolicyTesterReportingJobBuilder) DoesNotExist() {
r.doesNotExist = true
}
func (r *resolvedPolicyTesterReportingJobBuilder) WithType(typ policy.ReportingJob_Type) *resolvedPolicyTesterReportingJobBuilder {
r.typ = &typ
return r
}
func (r *resolvedPolicyTesterReportingJobBuilder) Notifies(qrId string) *resolvedPolicyTesterReportingJobNotifiesBuilder {
n := &resolvedPolicyTesterReportingJobNotifiesBuilder{rjTester: r, childMrnForCodeId: r.mrnForCodeId, childMrn: r.mrn, parent: qrId}
r.notifies = append(r.notifies, n)
r.notifiesSet = true
return n
}
func (r *resolvedPolicyTesterReportingJobBuilder) WithScoringSystem(scoringSystem explorer.ScoringSystem) *resolvedPolicyTesterReportingJobBuilder {
r.scoringSystem = &scoringSystem
return r
}
func (r *resolvedPolicyTesterReportingJobBuilder) testIt(t *testing.T, rp *policy.ResolvedPolicy) {
var qrId string
var extraInfo string
if r.mrn != "" {
qrId = r.mrn
} else {
q := r.tester.bundleMap.Queries[r.mrnForCodeId]
require.NotNilf(t, q, "query not found in bundle: %s", r.mrnForCodeId)
require.NotEmptyf(t, q.CodeId, "query %s doesn't have code id", r.mrnForCodeId)
qrId = q.CodeId
extraInfo = " (" + r.mrnForCodeId + ")"
}
rj := findReportingJobByQrId(rp, qrId)
if r.doesNotExist {
require.Nilf(t, rj, "reporting job %s%s should not exist", qrId, extraInfo)
return
}
require.NotNilf(t, rj, "reporting job %s%s not found", qrId, extraInfo)
if r.typ != nil {
require.Equalf(t, *r.typ, rj.Type, "reporting job %s%s type mismatch", qrId, extraInfo)
}
if r.scoringSystem != nil {
require.Equalf(t, *r.scoringSystem, rj.ScoringSystem, "reporting job %s%s scoring system mismatch", qrId, extraInfo)
}
if r.notifiesSet {
for _, n := range r.notifies {
n.testIt(t, rp)
}
require.Len(t, rj.Notify, len(r.notifies), "reporting job uuid=%s qrId=%s%s notify mismatch", rj.Uuid, qrId, extraInfo)
}
}
func TestResolveV2_EmptyPolicy(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- uid: policy1
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}},
}, []*policy.Bundle{b})
t.Run("resolve w/o filters", func(t *testing.T) {
_, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
})
assert.EqualError(t, err, "rpc error: code = InvalidArgument desc = asset doesn't support any policies")
})
t.Run("resolve with empty filters", func(t *testing.T) {
_, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
AssetFilters: []*explorer.Mquery{{}},
})
assert.EqualError(t, err, "failed to compile query: failed to compile query '': query is not implemented ''")
})
t.Run("resolve with random filters", func(t *testing.T) {
_, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
assert.EqualError(t, err,
"rpc error: code = InvalidArgument desc = asset isn't supported by any policies\n"+
"policies didn't provide any filters\n"+
"asset supports: true\n")
})
}
func TestResolveV2_SimplePolicy(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- uid: policy1
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == props.name
props:
- uid: name
mql: return "definitely not the asset name"
queries:
- uid: query1
mql: asset{*}
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}},
}, []*policy.Bundle{b})
t.Run("resolve with correct filters", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
require.Len(t, rp.ExecutionJob.Queries, 3)
require.Len(t, rp.Filters, 1)
require.Len(t, rp.CollectorJob.ReportingJobs, 5)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.
ExecutesQuery(queryMrn("check1")).
WithProps(map[string]string{"name": `return "definitely not the asset name"`})
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies("root")
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies("root")
rpTester.doTest(t, rp)
})
t.Run("resolve with many filters (one is correct)", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
AssetFilters: []*explorer.Mquery{
{Mql: "asset.family.contains(\"linux\")"},
{Mql: "true"},
{Mql: "asset.family.contains(\"windows\")"},
},
})
require.NoError(t, err)
require.NotNil(t, rp)
})
t.Run("resolve with incorrect filters", func(t *testing.T) {
_, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: policyMrn("policy1"),
AssetFilters: []*explorer.Mquery{
{Mql: "asset.family.contains(\"linux\")"},
{Mql: "false"},
{Mql: "asset.family.contains(\"windows\")"},
},
})
assert.EqualError(t, err,
"rpc error: code = InvalidArgument desc = asset isn't supported by any policies\n"+
"policies support: true\n"+
"asset supports: asset.family.contains(\"linux\"), asset.family.contains(\"windows\"), false\n")
})
}
func TestResolveV2_PolicyWithImpacts(t *testing.T) {
// For impacts, we always find the worst impact specified for a query in a policy bundle.
// All instances of the query use that impact
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- owner_mrn: //test.sth
mrn: //test.sth
groups:
- policies:
- uid: policy1
- uid: policy2
action: 4
- uid: policy1
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
- uid: check2
impact: 10
- uid: check3
impact: 60
queries:
- uid: query1
- uid: policy2
groups:
- type: chapter
filters: "true"
checks:
- uid: check2
impact: 5
- uid: check3
impact: 80
queries:
- uid: check1
mql: asset.name == props.name
props:
- uid: name
mql: return "definitely not the asset name"
- uid: check2
mql: true == false
impact: 70
- uid: check3
mql: true == true
impact: 9
- uid: query1
mql: asset{*}
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1"), policyMrn("policy2")}},
}, []*policy.Bundle{b})
t.Run("resolve with correct filters", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "//test.sth",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.
ExecutesQuery(queryMrn("check1")).
WithProps(map[string]string{"name": `return "definitely not the asset name"`})
rpTester.ExecutesQuery(queryMrn("check2"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check2")).Notifies(queryMrn("check2")).WithImpact(&explorer.Impact{Value: &explorer.ImpactValue{Value: 70}})
rpTester.CodeIdReportingJobForMrn(queryMrn("check3")).Notifies(queryMrn("check3")).WithImpact(&explorer.Impact{Value: &explorer.ImpactValue{Value: 80}})
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("check2")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("check3")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("check2")).Notifies(policyMrn("policy2"))
rpTester.ReportingJobByMrn(queryMrn("check3")).Notifies(policyMrn("policy2"))
rpTester.doTest(t, rp)
})
}
func TestResolveV2_PolicyWithScoringSystem(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- owner_mrn: //test.sth
mrn: //test.sth
groups:
- policies:
- uid: policy1
- uid: policy1
scoring_system: highest impact
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == props.name
props:
- uid: name
mql: return "definitely not the asset name"
queries:
- uid: query1
mql: asset{*}
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}},
}, []*policy.Bundle{b})
t.Run("resolve with correct filters", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "//test.sth",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.
ExecutesQuery(queryMrn("check1")).
WithProps(map[string]string{"name": `return "definitely not the asset name"`})
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(policyMrn("policy1")).WithScoringSystem(explorer.ScoringSystem_WORST).Notifies("root")
rpTester.doTest(t, rp)
})
}
func TestResolveV2_PolicyWithScoringSystemOverride(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- owner_mrn: //test.sth
mrn: //test.sth
groups:
- policies:
- uid: policy1
scoring_system: banded
- uid: policy1
scoring_system: highest impact
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == props.name
props:
- uid: name
mql: return "definitely not the asset name"
queries:
- uid: query1
mql: asset{*}
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}},
}, []*policy.Bundle{b})
t.Run("resolve with correct filters", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "//test.sth",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.
ExecutesQuery(queryMrn("check1")).
WithProps(map[string]string{"name": `return "definitely not the asset name"`})
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(policyMrn("policy1")).WithScoringSystem(explorer.ScoringSystem_BANDED).Notifies("root")
rpTester.doTest(t, rp)
})
}
func TestResolveV2_PolicyActionIgnore(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- owner_mrn: //test.sth
mrn: //test.sth
groups:
- policies:
- uid: policy-active
- uid: policy-ignored
action: 4
- uid: policy-active
owner_mrn: //test.sth
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == "definitely not the asset name"
queries:
- uid: query1
mql: asset.arch
- uid: policy-ignored
owner_mrn: //test.sth
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == "definitely not the asset name"
queries:
- uid: query1
mql: asset.arch
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy-active"), policyMrn("policy-ignored")}},
}, []*policy.Bundle{b})
t.Run("resolve with ignored policy", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "//test.sth",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.ExecutesQuery(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy-active"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy-active"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy-ignored"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy-ignored"))
rpTester.ReportingJobByMrn(policyMrn("policy-active")).Notifies("root")
rpTester.ReportingJobByMrn(policyMrn("policy-ignored")).Notifies("root").WithImpact(&explorer.Impact{Scoring: explorer.ScoringSystem_IGNORE_SCORE})
rpTester.doTest(t, rp)
})
}
func TestResolveV2_PolicyActionScoringSystem(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- owner_mrn: //test.sth
mrn: //test.sth
groups:
- policies:
- uid: policy-active
scoring_system: 6
- uid: policy-ignored
action: 4
- uid: policy-active
owner_mrn: //test.sth
scoring_system: 2
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == "definitely not the asset name"
queries:
- uid: query1
mql: asset.arch
- uid: policy-ignored
owner_mrn: //test.sth
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: asset.name == "definitely not the asset name"
queries:
- uid: query1
mql: asset.arch
`)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy-active"), policyMrn("policy-ignored")}},
}, []*policy.Bundle{b})
t.Run("resolve with scoring system", func(t *testing.T) {
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "//test.sth",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("query1"))
rpTester.ExecutesQuery(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("query1")).Notifies(queryMrn("query1"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy-active"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy-active"))
rpTester.ReportingJobByMrn(queryMrn("check1")).Notifies(policyMrn("policy-ignored"))
rpTester.ReportingJobByMrn(queryMrn("query1")).Notifies(policyMrn("policy-ignored"))
rpTester.ReportingJobByMrn(policyMrn("policy-active")).WithScoringSystem(explorer.ScoringSystem_BANDED).Notifies("root")
rpTester.ReportingJobByMrn(policyMrn("policy-ignored")).Notifies("root").WithImpact(&explorer.Impact{Scoring: explorer.ScoringSystem_IGNORE_SCORE})
rpTester.doTest(t, rp)
})
}
func TestResolveV2_IgnoredQuery(t *testing.T) {
ctx := context.Background()
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- uid: policy-1
owner_mrn: //test.sth
groups:
- type: chapter
filters: "true"
checks:
- uid: check1
mql: 1 == 1
- mrn: asset1
owner_mrn: //test.sth
groups:
- policies:
- uid: policy-1
- checks:
- uid: check1
action: 4
`)
_, srv, err := inmemory.NewServices(providers.DefaultRuntime(), nil)
require.NoError(t, err)
_, err = srv.SetBundle(ctx, b)
require.NoError(t, err)
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "asset1",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies(queryMrn("check1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check1")).Notifies("policy-1").WithImpact(&explorer.Impact{Scoring: explorer.ScoringSystem_IGNORE_SCORE})
rpTester.ReportingJobByMrn(policyMrn("policy-1")).Notifies("root")
}
func TestResolveV2_Frameworks(t *testing.T) {
ctx := context.Background()
bundleStr := `
owner_mrn: //test.sth
policies:
- uid: policy1
groups:
- filters: "true"
checks:
- uid: check-fail
mql: 1 == 2
- uid: check-pass-1
mql: 1 == 1
- uid: check-pass-2
mql: 2 == 2
queries:
- uid: active-query
title: users
mql: users
- uid: active-query-2
title: users length
mql: users.length
- uid: check-overlap
title: overlaps with check
mql: 1 == 1
- uid: policy-inactive
groups:
- filters: "false"
checks:
- uid: inactive-fail
mql: 1 == 2
- uid: inactive-pass
mql: 1 == 1
- uid: inactive-pass-2
mql: 2 == 2
queries:
- uid: inactive-query
title: users group
mql: users { group}
frameworks:
- uid: framework1
name: framework1
groups:
- title: group1
controls:
- uid: control1
title: control1
- uid: control2
title: control2
- uid: control3
title: control3
- uid: control4
title: control4
- uid: control5
title: control5
- uid: framework2
name: framework2
groups:
- title: group1
controls:
- uid: control1
title: control1
- uid: control2
title: control2
- uid: parent-framework
dependencies:
- mrn: ` + frameworkMrn("framework1") + `
framework_maps:
- uid: framework-map1
framework_owner:
uid: framework1
policy_dependencies:
- uid: policy1
controls:
- uid: control1
checks:
- uid: check-pass-1
queries:
- uid: active-query
- uid: active-query-2
- uid: control2
checks:
- uid: check-pass-2
- uid: check-fail
- uid: control4
controls:
- uid: control1
- uid: framework-map2
framework_owner:
uid: framework1
policy_dependencies:
- uid: policy1
controls:
- uid: control4
controls:
- uid: control1
- uid: control5
controls:
- uid: control1
`
t.Run("resolve with correct filters", func(t *testing.T) {
b := parseBundle(t, bundleStr)
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1"), policyMrn("policy-inactive")}, frameworks: []string{frameworkMrn("parent-framework")}},
}, []*policy.Bundle{b})
bundle, err := srv.GetBundle(ctx, &policy.Mrn{Mrn: "asset1"})
require.NoError(t, err)
bundleMap, err := bundle.Compile(ctx, conf.Schema, nil)
require.NoError(t, err)
mrnToQueryId := map[string]string{}
for _, q := range bundleMap.Queries {
mrnToQueryId[q.Mrn] = q.CodeId
}
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "asset1",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("check-fail"))
rpTester.ExecutesQuery(queryMrn("check-pass-1"))
rpTester.ExecutesQuery(queryMrn("check-pass-2"))
rpTester.ExecutesQuery(queryMrn("active-query"))
rpTester.ExecutesQuery(queryMrn("active-query-2"))
rpTester.ExecutesQuery(queryMrn("check-overlap"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-fail")).Notifies(queryMrn("check-fail"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-fail")).Notifies(controlMrn("control2"))
rpTester.ReportingJobByMrn(queryMrn("check-fail")).Notifies(policyMrn("policy1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-pass-1")).Notifies(queryMrn("check-pass-1"))
// This is a limitaion of the test framework. We lookup the code id from check-pass-1 because
// we need 1 tester that has all the notifies
rpTester.CodeIdReportingJobForMrn(queryMrn("check-pass-1")).Notifies(queryMrn("check-overlap"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-pass-1")).Notifies(controlMrn("control1"))
rpTester.ReportingJobByMrn(queryMrn("check-pass-1")).Notifies(policyMrn("policy1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-pass-2")).Notifies(queryMrn("check-pass-2"))
rpTester.CodeIdReportingJobForMrn(queryMrn("check-pass-2")).Notifies(controlMrn("control2"))
rpTester.ReportingJobByMrn(queryMrn("check-pass-2")).Notifies(policyMrn("policy1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("active-query")).Notifies(queryMrn("active-query"))
rpTester.CodeIdReportingJobForMrn(queryMrn("active-query")).Notifies(controlMrn("control1")).WithImpact(&explorer.Impact{Scoring: explorer.ScoringSystem_IGNORE_SCORE})
rpTester.ReportingJobByMrn(queryMrn("active-query")).Notifies(policyMrn("policy1"))
rpTester.CodeIdReportingJobForMrn(queryMrn("active-query-2")).Notifies(queryMrn("active-query-2"))
rpTester.CodeIdReportingJobForMrn(queryMrn("active-query-2")).Notifies(controlMrn("control1")).WithImpact(&explorer.Impact{Scoring: explorer.ScoringSystem_IGNORE_SCORE})
rpTester.ReportingJobByMrn(queryMrn("active-query-2")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(queryMrn("check-overlap")).Notifies(policyMrn("policy1"))
rpTester.ReportingJobByMrn(controlMrn("control1")).Notifies(controlMrn("control4"))
rpTester.ReportingJobByMrn(controlMrn("control1")).Notifies(controlMrn("control5"))
rpTester.ReportingJobByMrn(controlMrn("control1")).Notifies(frameworkMrn("framework1"))
rpTester.ReportingJobByMrn(controlMrn("control2")).Notifies(frameworkMrn("framework1"))
rpTester.ReportingJobByMrn(controlMrn("control4")).Notifies(frameworkMrn("framework1"))
rpTester.ReportingJobByMrn(policyMrn("policy1")).Notifies("root")
rpTester.ReportingJobByMrn(frameworkMrn("framework1")).Notifies(frameworkMrn("parent-framework"))
rpTester.ReportingJobByMrn(frameworkMrn("parent-framework")).Notifies("root")
rpTester.doTest(t, rp)
})
t.Run("test resolving with inactive data queries", func(t *testing.T) {
// test that creating a bundle with inactive data queries (where the packs/policies are inactive)
// will still end up in a successfully resolved policy for the asset
bundleStr := `
owner_mrn: //test.sth
policies:
- uid: policy1
groups:
- filters: "true"
queries:
- uid: active-query
title: users
mql: users
- uid: policy-inactive
groups:
- filters: "false"
queries:
- uid: inactive-query
title: users group
mql: users { group}
frameworks:
- uid: framework1
name: framework1
groups:
- title: group1
controls:
- uid: control1
title: control1
- uid: control2
title: control2
- uid: parent-framework
dependencies:
- mrn: ` + frameworkMrn("framework1") + `
framework_maps:
- uid: framework-map1
framework_owner:
uid: framework1
policy_dependencies:
- uid: policy1
- uid: policy-inactive
controls:
- uid: control1
queries:
- uid: active-query
- uid: control2
queries:
- uid: inactive-query
`
b := parseBundle(t, bundleStr)
// we do not activate policy-inactive, which means that its query should not get executed
srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}, frameworks: []string{frameworkMrn("parent-framework")}},
}, []*policy.Bundle{b})
bundle, err := srv.GetBundle(ctx, &policy.Mrn{Mrn: "asset1"})
require.NoError(t, err)
bundleMap, err := bundle.Compile(ctx, conf.Schema, nil)
require.NoError(t, err)
mrnToQueryId := map[string]string{}
for _, q := range bundleMap.Queries {
mrnToQueryId[q.Mrn] = q.CodeId
}
rp, err := srv.Resolve(ctx, &policy.ResolveReq{
PolicyMrn: "asset1",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)
rpTester := newResolvedPolicyTester(b, srv.NewCompilerConfig())
rpTester.ExecutesQuery(queryMrn("active-query"))