From dbb53d25902ea2f2e5073f6a2ee962929efc98b6 Mon Sep 17 00:00:00 2001 From: Preslav Date: Tue, 28 Nov 2023 23:17:53 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=A7=B9=20Add=20sortContents=20to=20ya?= =?UTF-8?q?c=20bundle=20struct.=20Simplify=20formatting.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/bundle/bundle_ext.yac.go | 49 +++++++++++++++++++++++++++++++ internal/bundle/fmt.go | 33 +++++++-------------- internal/bundle/fmt_test.go | 11 +++---- 3 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 internal/bundle/bundle_ext.yac.go diff --git a/internal/bundle/bundle_ext.yac.go b/internal/bundle/bundle_ext.yac.go new file mode 100644 index 00000000..eb9e00ee --- /dev/null +++ b/internal/bundle/bundle_ext.yac.go @@ -0,0 +1,49 @@ +package bundle + +import "sort" + +// 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 + }) + } + } + } +} diff --git a/internal/bundle/fmt.go b/internal/bundle/fmt.go index 9401940a..c4ba8ec5 100644 --- a/internal/bundle/fmt.go +++ b/internal/bundle/fmt.go @@ -74,26 +74,16 @@ func FormatFile(filename string, sort bool) error { if err != nil { return err } - - if sort { - b, err := policy.BundleFromYAML(data) - if err != nil { - return err - } - - b.SortContents() - data, err = b.ToYAML() - if err != nil { - return err - } + yacB, err := ParseYaml(data) + if err != nil { + return err } - - data, err = FormatBundleData(data) + fmtData, err := FormatBundle(yacB, sort) if err != nil { return err } - err = os.WriteFile(filename, data, 0o644) + err = os.WriteFile(filename, fmtData, 0o644) if err != nil { return err } @@ -101,13 +91,8 @@ func FormatFile(filename string, sort bool) error { return nil } -// Format formats the .mql.yaml bundle -func FormatBundleData(data []byte) ([]byte, error) { - b, err := ParseYaml(data) - if err != nil { - return nil, err - } - +// Format formats the ßbundle +func FormatBundle(b *Bundle, sort bool) ([]byte, error) { // to improve the formatting we need to remove the whitespace at the end of the lines for i := range b.Queries { query := b.Queries[i] @@ -138,5 +123,9 @@ func FormatBundleData(data []byte) ([]byte, error) { } } + if sort { + b.SortContents() + } + return Format(b) } diff --git a/internal/bundle/fmt_test.go b/internal/bundle/fmt_test.go index 7a347a9f..50585f4e 100644 --- a/internal/bundle/fmt_test.go +++ b/internal/bundle/fmt_test.go @@ -8,7 +8,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "go.mondoo.com/cnspec/v9/policy" ) func TestBundleFormatter(t *testing.T) { @@ -43,7 +42,8 @@ queries: title: Ensure Secure Boot is enabled ` - formatted, err := FormatBundleData([]byte(data)) + b, err := ParseYaml([]byte(data)) + formatted, err := FormatBundle(b, false) require.NoError(t, err) expected := `policies: @@ -128,12 +128,9 @@ queries: title: Ensure Secure Boot is enabled ` - b, err := policy.BundleFromYAML([]byte(data)) + b, err := ParseYaml([]byte(data)) require.NoError(t, err) - b.SortContents() - byteData, err := b.ToYAML() - require.NoError(t, err) - formatted, err := FormatBundleData(byteData) + formatted, err := FormatBundle(b, true) require.NoError(t, err) expected := `policies: - uid: sshd-server-policy From 44bfba22fa14257cb16d7f056a2fe4b19c2f41f0 Mon Sep 17 00:00:00 2001 From: Preslav Date: Wed, 29 Nov 2023 09:07:32 +0100 Subject: [PATCH 2/3] add license header, fix lint issues. --- internal/bundle/bundle_ext.yac.go | 3 +++ internal/bundle/fmt.go | 6 +++--- internal/bundle/fmt_test.go | 1 + 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/internal/bundle/bundle_ext.yac.go b/internal/bundle/bundle_ext.yac.go index eb9e00ee..6e6e70cd 100644 --- a/internal/bundle/bundle_ext.yac.go +++ b/internal/bundle/bundle_ext.yac.go @@ -1,3 +1,6 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + package bundle import "sort" diff --git a/internal/bundle/fmt.go b/internal/bundle/fmt.go index c4ba8ec5..a4208357 100644 --- a/internal/bundle/fmt.go +++ b/internal/bundle/fmt.go @@ -74,11 +74,11 @@ func FormatFile(filename string, sort bool) error { if err != nil { return err } - yacB, err := ParseYaml(data) + b, err := ParseYaml(data) if err != nil { return err } - fmtData, err := FormatBundle(yacB, sort) + fmtData, err := FormatBundle(b, sort) if err != nil { return err } @@ -91,7 +91,7 @@ func FormatFile(filename string, sort bool) error { return nil } -// Format formats the ßbundle +// Format formats the Bundle func FormatBundle(b *Bundle, sort bool) ([]byte, error) { // to improve the formatting we need to remove the whitespace at the end of the lines for i := range b.Queries { diff --git a/internal/bundle/fmt_test.go b/internal/bundle/fmt_test.go index 50585f4e..183d7a7d 100644 --- a/internal/bundle/fmt_test.go +++ b/internal/bundle/fmt_test.go @@ -43,6 +43,7 @@ queries: ` b, err := ParseYaml([]byte(data)) + require.NoError(t, err) formatted, err := FormatBundle(b, false) require.NoError(t, err) From 2122097d84fdc5ec31fd7432d3c3c35240d0d428 Mon Sep 17 00:00:00 2001 From: Preslav Date: Wed, 29 Nov 2023 09:11:24 +0100 Subject: [PATCH 3/3] Extend test with a comment. --- internal/bundle/fmt_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/bundle/fmt_test.go b/internal/bundle/fmt_test.go index 183d7a7d..448f14cf 100644 --- a/internal/bundle/fmt_test.go +++ b/internal/bundle/fmt_test.go @@ -12,6 +12,7 @@ import ( func TestBundleFormatter(t *testing.T) { data := ` +# This is a comment policies: - uid: sshd-server-policy authors: @@ -47,7 +48,8 @@ queries: formatted, err := FormatBundle(b, false) require.NoError(t, err) - expected := `policies: + expected := `# This is a comment +policies: - uid: sshd-server-policy name: SSH Server Policy version: 1.0.0