Skip to content

Commit

Permalink
✨ Add FromQueryPackBundle, allowing conversion of an explorer bundle …
Browse files Browse the repository at this point in the history
…to a policy bundle.

Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev committed Nov 11, 2023
1 parent 3ed6c68 commit c30ff8d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 6 deletions.
24 changes: 21 additions & 3 deletions policy/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
}
46 changes: 43 additions & 3 deletions policy/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func TestBundleCompile_ConvertQueryPacks(t *testing.T) {
email: [email protected]
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:
Expand All @@ -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{
Expand All @@ -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)

Expand All @@ -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: [email protected]
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
Expand Down

0 comments on commit c30ff8d

Please sign in to comment.