Skip to content

Commit

Permalink
⭐️ allow filtering of query packs by short mrns (#1145)
Browse files Browse the repository at this point in the history
This change adds support to filter the query packs available in
https://mondoo.com/registry by short mrns:

```
cnquery scan okta --organization dev-12345.okta.com --token $OKTA_TOKEN --querypack mondoohq/mondoo-okta-incident-response
```
  • Loading branch information
chris-rock authored Apr 11, 2023
1 parent 04a6590 commit 175c10c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
18 changes: 17 additions & 1 deletion explorer/query_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"errors"
"os"
"path"

"go.mondoo.com/cnquery/mrn"

"go.mondoo.com/ranger-rpc"

Expand All @@ -13,10 +16,23 @@ import (
"go.opentelemetry.io/otel"
)

const defaultRegistryUrl = "https://registry.api.mondoo.com"
const (
defaultRegistryUrl = "https://registry.api.mondoo.com"
RegistryServiceName = "registry.mondoo.com"
CollectionIDNamespace = "namespace"
CollectionIDQueryPacks = "querypacks"
)

var tracer = otel.Tracer("go.mondoo.com/cnquery/explorer")

func NewQueryPackMrn(namespace string, uid string) string {
m := &mrn.MRN{
ServiceName: RegistryServiceName,
RelativeResourceName: path.Join(CollectionIDNamespace, namespace, CollectionIDQueryPacks, uid),
}
return m.String()
}

// ValidateBundle and check queries, relationships, MRNs, and versions
func (s *LocalServices) ValidateBundle(ctx context.Context, bundle *Bundle) (*Empty, error) {
_, err := bundle.Compile(ctx)
Expand Down
19 changes: 19 additions & 0 deletions explorer/query_hub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package explorer

import (
"testing"

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

func TestQueryPackMrn(t *testing.T) {
// given
namespace := "test-namespace"
uid := "test-uid"

// when
mrn := NewQueryPackMrn(namespace, uid)

// then
assert.Equal(t, "//registry.mondoo.com/namespace/test-namespace/querypacks/test-uid", mrn)
}
23 changes: 22 additions & 1 deletion explorer/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ func (s *LocalScanner) RunIncognito(ctx context.Context, job *Job) (*explorer.Re
return reports, nil
}

// preprocessPolicyFilters expends short registry mrns into full mrns
func preprocessQueryPackFilters(filters []string) []string {
res := make([]string, len(filters))
for i := range filters {
f := filters[i]
if strings.HasPrefix(f, "//") {
res[i] = f
continue
}

// expand short registry mrns
m := strings.Split(f, "/")
if len(m) == 2 {
res[i] = explorer.NewQueryPackMrn(m[0], m[1])
} else {
res[i] = f
}
}
return res
}

func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstreamConfig resources.UpstreamConfig) (*explorer.ReportCollection, bool, error) {
log.Info().Msgf("discover related assets for %d asset(s)", len(job.Inventory.Spec.Assets))
im, err := inventory.New(inventory.WithInventory(job.Inventory))
Expand Down Expand Up @@ -246,7 +267,7 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstreamConf
Asset: asset,
Bundle: job.Bundle,
Props: job.Props,
QueryPackFilters: job.QueryPackFilters,
QueryPackFilters: preprocessQueryPackFilters(job.QueryPackFilters),
Ctx: ctx,
CredsResolver: im.GetCredsResolver(),
Reporter: reporter,
Expand Down
26 changes: 26 additions & 0 deletions explorer/scan/local_scanner_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package scan

import (
"testing"

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

func TestFilterPreprocess(t *testing.T) {
// given
filters := []string{
"namespace1/pack1",
"namespace2/pack2",
"//registry.mondoo.com/namespace/namespace3/querypacks/pack3",
}

// when
preprocessed := preprocessQueryPackFilters(filters)

// then
assert.Equal(t, []string{
"//registry.mondoo.com/namespace/namespace1/querypacks/pack1",
"//registry.mondoo.com/namespace/namespace2/querypacks/pack2",
"//registry.mondoo.com/namespace/namespace3/querypacks/pack3",
}, preprocessed)
}

0 comments on commit 175c10c

Please sign in to comment.