Skip to content

Commit

Permalink
🐛 avoid accidental empty properties
Browse files Browse the repository at this point in the history
A friend accidentally the `query` (which we used pre v9) attribute for
the property, instead of the `mql` field. This led to an empty property,
which in this case is never valid. Properties must be non-empty.

Guarantee non-empty props + add more testing.

Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Mar 2, 2024
1 parent 2f1c166 commit 6b9d43f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
20 changes: 16 additions & 4 deletions explorer/property.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,27 @@ func (p *Property) Compile(props map[string]*llx.Primitive, conf mqlc.CompilerCo
return mqlc.Compile(p.Mql, props, conf)
}

// id gets any valid ID for the property, prioritizing uid > mrn > title
func (p *Property) id() string {
if p.Uid != "" {
return p.Uid
}
if p.Mrn != "" {
return p.Mrn
}
// last resort
return p.Title
}

// RefreshChecksumAndType by compiling the query and updating the Checksum field
func (p *Property) RefreshChecksumAndType(conf mqlc.CompilerConfig) (*llx.CodeBundle, error) {
return p.refreshChecksumAndType(conf)
}
if p.Mql == "" {
return nil, errors.New("property must not be empty (property '" + p.id() + "')")
}

func (p *Property) refreshChecksumAndType(conf mqlc.CompilerConfig) (*llx.CodeBundle, error) {
bundle, err := p.Compile(nil, conf)
if err != nil {
return bundle, multierr.Wrap(err, "failed to compile property '"+p.Mql+"'")
return bundle, multierr.Wrap(err, "failed to compile property '"+p.id()+"', mql: '"+p.Mql+"'")
}

if bundle.GetCodeV2().GetId() == "" {
Expand Down
41 changes: 41 additions & 0 deletions explorer/property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v10"
"go.mondoo.com/cnquery/v10/mqlc"
"go.mondoo.com/cnquery/v10/providers"
)

func TestProperty_RefreshMrn(t *testing.T) {
Expand All @@ -26,3 +29,41 @@ func TestProperty_RefreshMrn(t *testing.T) {
assert.Equal(t, "", in.For[0].Uid)
assert.Equal(t, "//my.owner/queries/uid2", in.For[0].Mrn)
}

func TestProperty_MustNotBeEmpty(t *testing.T) {
in := &Property{
Uid: "uid1",
Mql: "",
}

schema := providers.DefaultRuntime().Schema()
conf := mqlc.NewConfig(schema, cnquery.DefaultFeatures)
_, err := in.RefreshChecksumAndType(conf)

assert.Error(t, err)
}

func TestProperty_MustCompile(t *testing.T) {
schema := providers.DefaultRuntime().Schema()
conf := mqlc.NewConfig(schema, cnquery.DefaultFeatures)

t.Run("fails on invalid MQL", func(t *testing.T) {
in := &Property{
Uid: "uid1",
Mql: "ruri.ryu",
}

_, err := in.RefreshChecksumAndType(conf)
assert.Error(t, err)
})

t.Run("works with a valid query", func(t *testing.T) {
in := &Property{
Uid: "uid1",
Mql: "mondoo.version",
}

_, err := in.RefreshChecksumAndType(conf)
assert.NoError(t, err)
})
}

0 comments on commit 6b9d43f

Please sign in to comment.