Skip to content

Commit

Permalink
🐛 Fix resolved policy root pruning bug
Browse files Browse the repository at this point in the history
In #1036, code was added to prune nodes that were being used. However,
this happens before the frameworks get a say in what happens. This meant
it was possible to prune the root reporting job casuing the resolved
policy to be broken
  • Loading branch information
jaym committed Feb 1, 2024
1 parent 91b8e48 commit ba663ff
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions policy/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,11 @@ func (s *LocalServices) tryResolve(ctx context.Context, bundleMrn string, assetF
reportingJobUUIDs := topologicalSortReportingJobs(collectorJob.ReportingJobs)
for _, rjUUID := range reportingJobUUIDs {
rj := collectorJob.ReportingJobs[rjUUID]
if rj.QrId == "root" {
// If we prune the root, we're going to get a broken resolved policy.
// For example, the framework code that follows wants to report to it.
continue
}
if rj.Type == ReportingJob_POLICY && (len(rj.ChildJobs)+len(rj.Datapoints) == 0) {
logCtx.Debug().
Str("policy", bundleMrn).
Expand Down
43 changes: 42 additions & 1 deletion policy/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ policies:
})
require.NoError(t, err)
require.NotNil(t, rp)
require.Len(t, rp.CollectorJob.ReportingJobs, 0)
require.Len(t, rp.CollectorJob.ReportingJobs, 1)
}

func TestResolve_IgnoredQuery(t *testing.T) {
Expand Down Expand Up @@ -1364,3 +1364,44 @@ queries:
require.Nil(t, qrIdToRj[policyMrn("pack2")])
})
}

func TestResolve_NeverPruneRoot(t *testing.T) {
b := parseBundle(t, `
owner_mrn: //test.sth
policies:
- uid: policy1
groups:
- type: chapter
filters: "false"
checks:
- uid: check1
queries:
- uid: check1
title: check1
filters: |
true
mql: |
1 == 1
`)

srv := initResolver(t, []*testAsset{
{asset: "asset1", policies: []string{policyMrn("policy1")}},
}, []*policy.Bundle{b})

rp, err := srv.Resolve(context.Background(), &policy.ResolveReq{
PolicyMrn: "asset1",
AssetFilters: []*explorer.Mquery{{Mql: "true"}},
})
require.NoError(t, err)
require.NotNil(t, rp)

require.Len(t, rp.CollectorJob.ReportingJobs, 1)

qrIdToRj := map[string]*policy.ReportingJob{}
for _, rj := range rp.CollectorJob.ReportingJobs {
qrIdToRj[rj.QrId] = rj
}
require.NotNil(t, qrIdToRj["root"])

}

0 comments on commit ba663ff

Please sign in to comment.