From 5aea249eb4e62d5fbd478bd59cb184f0b07a05ec Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Wed, 20 Sep 2023 22:36:18 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20compute=20checksum=20for=20prope?= =?UTF-8?q?rties=20of=20queries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We had missed to process checksums for properties of queries with this call. Since semantically it is supposed to refresh the entire checksum of the Mquery object, it needs to cover properties as well. Signed-off-by: Dominik Richter --- examples/complex.mql.yaml | 2 +- explorer/mquery.go | 3 +++ explorer/mquery_test.go | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/examples/complex.mql.yaml b/examples/complex.mql.yaml index 3ba2a542e4..13188ed539 100644 --- a/examples/complex.mql.yaml +++ b/examples/complex.mql.yaml @@ -52,7 +52,7 @@ packs: # This MQL uses the property defined above. You can override it via # e.g. --props "home='/home/user'" mql: | - file( props.home ) { * } + file( props.home ) { basename user group } # These are shared queries that can be used in any querypack queries: diff --git a/explorer/mquery.go b/explorer/mquery.go index 171fbd5b82..738e196a85 100644 --- a/explorer/mquery.go +++ b/explorer/mquery.go @@ -118,6 +118,9 @@ func (m *Mquery) RefreshChecksum( for i := range m.Props { prop := m.Props[i] + if _, err := prop.RefreshChecksumAndType(schema); err != nil { + return err + } if prop.Checksum == "" { return errors.New("referenced property '" + prop.Mrn + "' checksum is empty") } diff --git a/explorer/mquery_test.go b/explorer/mquery_test.go index 20f6e606bc..1d47d5cf5f 100644 --- a/explorer/mquery_test.go +++ b/explorer/mquery_test.go @@ -4,13 +4,33 @@ package explorer import ( + "context" "encoding/json" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "go.mondoo.com/cnquery/providers-sdk/v1/testutils" ) +func TestMquery_RefreshChecksum(t *testing.T) { + a := &Mquery{ + Mql: "mondoo.version != props.world", + Props: []*Property{{Mql: "'hi'", Uid: "world"}}, + } + x := testutils.LinuxMock() + err := a.RefreshChecksum( + context.Background(), + x.Schema(), + func(ctx context.Context, mrn string) (*Mquery, error) { + return nil, nil + }, + ) + require.NoError(t, err) + assert.Equal(t, "t2slvX2k58s=", a.Checksum) + assert.Equal(t, "0xgs2tmERsM=", a.Props[0].Checksum) +} + func TestMqueryMerge(t *testing.T) { a := &Mquery{ Mql: "base",