Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ mqueries.RefreshChecksums + migrate tests #59

Merged
merged 1 commit into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions policy/cnspec_policy.proto
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ message ScoreDistribution {

service PolicyHub {
rpc SetBundle(Bundle) returns (Empty) {}
rpc DeletePolicy(Mrn) returns (Empty) {}
rpc ValidateBundle(Bundle) returns (Empty) {}
rpc GetPolicy(Mrn) returns (Policy) {}
rpc GetBundle(Mrn) returns (Bundle) {}
rpc GetPolicy(Mrn) returns (Policy) {}
rpc DeletePolicy(Mrn) returns (Empty) {}
rpc GetPolicyFilters(Mrn) returns (Mqueries) {}
rpc List(ListReq) returns (Policies) {}
rpc DefaultPolicies(DefaultPoliciesReq) returns (URLs) {}
Expand Down
10 changes: 10 additions & 0 deletions policy/mquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,13 @@ func ChecksumAssetFilters(queries []*Mquery) (string, error) {

return afc.String(), nil
}

// RefreshChecksums of all queries
func (m *Mqueries) RefreshChecksums(props map[string]*llx.Primitive) error {
for i := range m.Items {
if _, err := m.Items[i].RefreshChecksumAndType(props); err != nil {
return err
}
}
return nil
}
45 changes: 45 additions & 0 deletions policy/mquery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package policy

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMquery_Whitespaces(t *testing.T) {
mq := Mquery{
Query: " mondoo { version \n} \t\n ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arlimus should this be mondoo or cnspec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% that we need to migrate the mondoo resource. I think the better name is mql since the resource is available in cnquery and cnspec.

Since this PR just adds tests, I propose we do this change in a followup PR

}

mqexpect := Mquery{
Query: "mondoo { version \n}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this...mondoo or cnspec?

}

bundle, err := mq.RefreshChecksumAndType(nil)
assert.NoError(t, err)
assert.NotNil(t, bundle)

bundle, err = mqexpect.RefreshChecksumAndType(nil)
assert.NoError(t, err)
assert.NotNil(t, bundle)

assert.Equal(t, mqexpect.CodeId, mq.CodeId)
}

func TestMquery_CodeIDs(t *testing.T) {
mqAssetFilter := Mquery{
Query: "mondoo { version \n}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this...mondoo or cnspec?

}

mqReg := Mquery{
Query: "mondoo { version \n}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this...mondoo or cnspec?

}

_, err := mqAssetFilter.RefreshAsAssetFilter("//some.mrn")
assert.NoError(t, err)

_, err = mqReg.RefreshChecksumAndType(nil)
assert.NoError(t, err)

assert.Equal(t, mqReg.CodeId, mqAssetFilter.CodeId)
}
4 changes: 2 additions & 2 deletions policy/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (s *LocalServices) tryResolve(ctx context.Context, policyMrn string, assetF
return nil, err
}
if len(matchingFilters) == 0 {
return nil, newPolicyAssetMatchError(assetFilters, policyObj)
return nil, NewPolicyAssetMatchError(assetFilters, policyObj)
}

assetFiltersMap := make(map[string]struct{}, len(matchingFilters))
Expand Down Expand Up @@ -545,7 +545,7 @@ func (s *LocalServices) tryResolve(ctx context.Context, policyMrn string, assetF
return &resolvedPolicy, nil
}

func newPolicyAssetMatchError(assetFilters []*Mquery, p *Policy) error {
func NewPolicyAssetMatchError(assetFilters []*Mquery, p *Policy) error {
if len(assetFilters) == 0 {
// send a proto error with details, so that the agent can render it properly
msg := "asset does not match any of the activated policies"
Expand Down