From 2f217fa9496ecf773f22fbf99debb2a2176689d4 Mon Sep 17 00:00:00 2001 From: Jay Mundrawala Date: Tue, 21 Nov 2023 08:22:13 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Try=20to=20make=20mquery=20check?= =?UTF-8?q?sums=20stable=20(#2592)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 Try to make mquery checksums stable If we change a value inside the query, we need to recalculate the checksum. If checksums aren't stable, this ends up breaking parts of the resolver code in cnspec * Remove forced title for mquery filter We don't understand why it is necessary --- explorer/mquery.go | 8 ++++++-- explorer/mquery_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/explorer/mquery.go b/explorer/mquery.go index 57f31505fa..20af780d43 100644 --- a/explorer/mquery.go +++ b/explorer/mquery.go @@ -288,12 +288,16 @@ func (m *Mquery) RefreshAsFilter(mrn string, schema llx.Schema) (*llx.CodeBundle return nil, errors.New("filters require MQL snippets (no compiled code generated)") } + checksumInvalidated := false if mrn != "" { m.Mrn = mrn + "/filter/" + m.CodeId + checksumInvalidated = true } - if m.Title == "" { - m.Title = m.Query + if checksumInvalidated { + if err := m.RefreshChecksum(context.Background(), schema, nil); err != nil { + return nil, err + } } return bundle, nil diff --git a/explorer/mquery_test.go b/explorer/mquery_test.go index a95336baff..6abd696145 100644 --- a/explorer/mquery_test.go +++ b/explorer/mquery_test.go @@ -13,6 +13,24 @@ import ( "go.mondoo.com/cnquery/v9/providers-sdk/v1/testutils" ) +func TestMquery_RefreshAsAssetFilterStableChecksum(t *testing.T) { + m := &Mquery{ + Mql: "true", + Uid: "my-id0", + } + + x := testutils.LinuxMock() + + _, err := m.RefreshAsFilter("//owner/me", x.Schema()) + require.NoError(t, err) + assert.Equal(t, "//owner/me/filter/"+m.CodeId, m.Mrn) + + cs := m.Checksum + _, err = m.RefreshAsFilter("//owner/me", x.Schema()) + require.NoError(t, err) + assert.Equal(t, cs, m.Checksum) +} + func TestMquery_Refresh(t *testing.T) { a := &Mquery{ Mql: "mondoo.version != props.world",