From c30ff8d1604d8d6344b91ca8feedc2f2f6bab803 Mon Sep 17 00:00:00 2001 From: Preslav Date: Mon, 6 Nov 2023 13:28:30 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20FromQueryPackBundle,=20allowi?= =?UTF-8?q?ng=20conversion=20of=20an=20explorer=20bundle=20to=20a=20policy?= =?UTF-8?q?=20bundle.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Preslav --- policy/bundle.go | 24 +++++++++++++++++++--- policy/bundle_test.go | 46 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 6 deletions(-) diff --git a/policy/bundle.go b/policy/bundle.go index 6497de9f..bb00eb3d 100644 --- a/policy/bundle.go +++ b/policy/bundle.go @@ -120,7 +120,7 @@ func aggregateFilesToBundle(paths []string) (*Bundle, error) { return nil, errors.Wrap(err, "could not load file: "+path) } - mergedBundle = aggregateBundles(mergedBundle, bundle) + mergedBundle = Merge(mergedBundle, bundle) } return mergedBundle, nil @@ -136,9 +136,9 @@ func bundleFromSingleFile(path string) (*Bundle, error) { return BundleFromYAML(bundleData) } -// aggregateBundles combines two PolicyBundle and merges the data additive into one +// Merge combines two PolicyBundle and merges the data additive into one // single PolicyBundle structure -func aggregateBundles(a *Bundle, b *Bundle) *Bundle { +func Merge(a *Bundle, b *Bundle) *Bundle { res := &Bundle{} res.OwnerMrn = a.OwnerMrn @@ -1112,3 +1112,21 @@ func translateGroupUIDs(ownerMrn string, policyObj *Policy, uid2mrn map[string]s return nil } + +// Takes a query pack bundle and converts it to a policy bundle. +// It copies over the owner, the packs, the props and the queries from the bundle +// and converts all query packs into data-only policies. +func FromQueryPackBundle(bundle *explorer.Bundle) *Bundle { + if bundle == nil { + return nil + } + b := &Bundle{ + OwnerMrn: bundle.OwnerMrn, + Packs: bundle.Packs, + Props: bundle.Props, + Queries: bundle.Queries, + } + b.ConvertQuerypacks() + + return b +} diff --git a/policy/bundle_test.go b/policy/bundle_test.go index 66d541e2..2c41aa52 100644 --- a/policy/bundle_test.go +++ b/policy/bundle_test.go @@ -90,9 +90,9 @@ func TestBundleCompile_ConvertQueryPacks(t *testing.T) { email: author@author.com filters: 2 == 2 queries: - - uid: built-in q + - uid: built-in-q mql: 1 == 1 - title: built-in q + title: built-in-q groups: - filters: "true" queries: @@ -116,6 +116,7 @@ func TestBundleCompile_ConvertQueryPacks(t *testing.T) { }, } require.Equal(t, expectedAuthors, bundle.Policies[0].Authors) + require.Equal(t, explorer.ScoringSystem_DATA_ONLY, bundle.Policies[0].ScoringSystem) // built in group expectedBuiltInFilters := &explorer.Filters{ @@ -128,7 +129,7 @@ func TestBundleCompile_ConvertQueryPacks(t *testing.T) { require.Equal(t, 1, len(bundle.Policies[0].Groups[0].Queries)) require.Equal(t, "Default Queries", bundle.Policies[0].Groups[0].Title) - require.Equal(t, "built-in q", bundle.Policies[0].Groups[0].Queries[0].Title) + require.Equal(t, "built-in-q", bundle.Policies[0].Groups[0].Queries[0].Title) require.Equal(t, "1 == 1", bundle.Policies[0].Groups[0].Queries[0].Mql) require.Equal(t, expectedBuiltInFilters, bundle.Policies[0].Groups[0].Filters) @@ -145,6 +146,45 @@ func TestBundleCompile_ConvertQueryPacks(t *testing.T) { require.Equal(t, expectedGrpFilters, bundle.Policies[0].Groups[1].Filters) } +func TestBundleCompile_FromQueryPackBundle(t *testing.T) { + // this bundle has both built-in queries and group queries + qBundleStr := ` + owner_mrn: //test.sth + packs: + - uid: pack-1 + authors: + - name: author1 + email: author@author.com + filters: 2 == 2 + queries: + - uid: built-in-q + mql: 1 == 1 + title: built-in-q + groups: + - filters: "true" + queries: + - uid: check-1 + mql: 1 == 2 + - uid: check-2 + queries: + - uid: check-2 + mql: 3 == 3 + title: check-2 +` + + qBundle, err := explorer.BundleFromYAML([]byte(qBundleStr)) + require.NoError(t, err) + require.Equal(t, 1, len(qBundle.Packs)) + require.Equal(t, 1, len(qBundle.Queries)) + + converted := policy.FromQueryPackBundle(qBundle) + require.Equal(t, 1, len(converted.Packs)) + require.Equal(t, 1, len(converted.Policies)) + require.Equal(t, 1, len(converted.Queries)) + // built-in group + group from pack + require.Equal(t, 2, len(converted.Policies[0].Groups)) +} + func TestBundleCompile_RemoveFailingQueries(t *testing.T) { bundleStr := ` owner_mrn: //test.sth