Skip to content

Commit

Permalink
🐛 Try to make mquery checksums stable (#2592)
Browse files Browse the repository at this point in the history
* 🐛 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
  • Loading branch information
jaym authored Nov 21, 2023
1 parent 4fd1e5e commit 2f217fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions explorer/mquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions explorer/mquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 2f217fa

Please sign in to comment.