Skip to content

Commit

Permalink
🐛 compute checksum for properties of queries (#1825)
Browse files Browse the repository at this point in the history
We had missed to process checksums and MRNs 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 <dominik.richter@gmail.com>
  • Loading branch information
arlimus authored Sep 21, 2023
1 parent 1c83a8d commit 8a9c7ce
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/complex.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 10 additions & 0 deletions explorer/mquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func (m *Mquery) RefreshMRN(ownerMRN string) error {

m.Mrn = nu
m.Uid = ""

for i := range m.Props {
if err := m.Props[i].RefreshMRN(ownerMRN); err != nil {
return err
}
}

return nil
}

Expand Down Expand Up @@ -118,6 +125,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")
}
Expand Down
29 changes: 29 additions & 0 deletions explorer/mquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,42 @@
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_Refresh(t *testing.T) {
a := &Mquery{
Mql: "mondoo.version != props.world",
Uid: "my-id0",
Props: []*Property{{Mql: "'hi'", Uid: "world"}},
}

err := a.RefreshMRN("//owner")
require.NoError(t, err)
assert.Equal(t, "//owner/queries/my-id0", a.Mrn)
assert.Empty(t, a.Uid)
assert.Equal(t, "//owner/queries/world", a.Props[0].Mrn)
assert.Empty(t, a.Props[0].Uid)

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, "5KkJ/lLHnBM=", a.Checksum)
assert.Equal(t, "9NhbOk30tEg=", a.Props[0].Checksum)
}

func TestMqueryMerge(t *testing.T) {
a := &Mquery{
Mql: "base",
Expand Down

0 comments on commit 8a9c7ce

Please sign in to comment.