Skip to content

Commit

Permalink
🧹 ensure example query packs pass our own lint requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Jan 21, 2024
1 parent 65df0ae commit 18de21e
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 46 deletions.
49 changes: 3 additions & 46 deletions apps/cnquery/cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
_ "embed"
"fmt"
"os"
"strconv"

"github.com/cockroachdb/errors"
"github.com/rs/zerolog/log"
Expand All @@ -17,6 +16,7 @@ import (
"go.mondoo.com/cnquery/v10/cli/config"
cli_errors "go.mondoo.com/cnquery/v10/cli/errors"
"go.mondoo.com/cnquery/v10/explorer"
"go.mondoo.com/cnquery/v10/internal/bundle"
"go.mondoo.com/cnquery/v10/providers"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/upstream"
"go.mondoo.com/cnquery/v10/utils/stringx"
Expand Down Expand Up @@ -69,49 +69,6 @@ var queryPackInitCmd = &cobra.Command{
},
}

func validate(queryPackBundle *explorer.Bundle) []string {
errors := []string{}

// check that we have uids for packs and queries
for i := range queryPackBundle.Packs {
pack := queryPackBundle.Packs[i]
packId := strconv.Itoa(i)

if pack.Uid == "" {
errors = append(errors, fmt.Sprintf("pack %s does not define a uid", packId))
} else {
packId = pack.Uid
}

if pack.Name == "" {
errors = append(errors, fmt.Sprintf("pack %s does not define a name", packId))
}

for j := range pack.Queries {
query := pack.Queries[j]
queryId := strconv.Itoa(j)
if query.Uid == "" {
errors = append(errors, fmt.Sprintf("query %s/%s does not define a uid", packId, queryId))
} else {
queryId = query.Uid
}

if query.Title == "" {
errors = append(errors, fmt.Sprintf("query %s/%s does not define a name", packId, queryId))
}
}
}

// we compile after the checks because it removes the uids and replaces it with mrns
schema := providers.DefaultRuntime().Schema()
_, err := queryPackBundle.Compile(context.Background(), schema)
if err != nil {
errors = append(errors, "could not compile the query pack bundle", err.Error())
}

return errors
}

// ensureProviders ensures that all providers are locally installed
func ensureProviders() error {
for _, v := range providers.DefaultProviders {
Expand Down Expand Up @@ -139,7 +96,7 @@ var queryPackLintCmd = &cobra.Command{
return cli_errors.NewCommandError(errors.Wrap(err, "could not load query pack"), 1)
}

errors := validate(queryPackBundle)
errors := bundle.Lint(queryPackBundle)
if len(errors) > 0 {
log.Error().Msg("could not validate query pack")
for i := range errors {
Expand Down Expand Up @@ -177,7 +134,7 @@ var queryPackPublishCmd = &cobra.Command{
return cli_errors.NewCommandError(errors.Wrap(err, "could not load query pack bundle"), 1)
}

bundleErrors := validate(queryPackBundle)
bundleErrors := bundle.Lint(queryPackBundle)
if len(bundleErrors) > 0 {
log.Error().Msg("could not validate query pack")
for i := range bundleErrors {
Expand Down
1 change: 1 addition & 0 deletions examples/complex.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# show off some of the more advanced features. It is meant as a demo only.
packs:
- uid: mixed-os
name: Sample OS Query Pack for Linux and macOS
filters:
- asset.family.contains("unix")

Expand Down
1 change: 1 addition & 0 deletions examples/k8s.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

packs:
- uid: kubernetes-pod-security-info
name: Kubernetes Pod Security Info
filters:
- asset.platform == "k8s-pod"
queries:
Expand Down
20 changes: 20 additions & 0 deletions examples/lint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package examples

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.mondoo.com/cnquery/v10/explorer"
"go.mondoo.com/cnquery/v10/internal/bundle"
"testing"
)

func TestExampleLint(t *testing.T) {
queryPackBundle, err := explorer.BundleFromPaths(".")
require.NoError(t, err)

lintErr := bundle.Lint(queryPackBundle)
assert.Equal(t, []string{}, lintErr)
}
1 change: 1 addition & 0 deletions examples/os.mql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

packs:
- uid: linux-mixed-queries
name: Linux Mixed Queries
filters:
- asset.family.contains("unix")

Expand Down
55 changes: 55 additions & 0 deletions internal/bundle/lint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package bundle

import (
"context"
"fmt"
"go.mondoo.com/cnquery/v10/explorer"
"go.mondoo.com/cnquery/v10/providers"
"strconv"
)

func Lint(queryPackBundle *explorer.Bundle) []string {
errors := []string{}

// check that we have uids for packs and queries
for i := range queryPackBundle.Packs {
pack := queryPackBundle.Packs[i]
packId := strconv.Itoa(i)

if pack.Uid == "" {
errors = append(errors, fmt.Sprintf("pack %s does not define a uid", packId))
} else {
packId = pack.Uid
}

if pack.Name == "" {
errors = append(errors, fmt.Sprintf("pack %s does not define a name", packId))
}

for j := range pack.Queries {
query := pack.Queries[j]
queryId := strconv.Itoa(j)
if query.Uid == "" {
errors = append(errors, fmt.Sprintf("query %s/%s does not define a uid", packId, queryId))
} else {
queryId = query.Uid
}

if query.Title == "" {
errors = append(errors, fmt.Sprintf("query %s/%s does not define a name", packId, queryId))
}
}
}

// we compile after the checks because it removes the uids and replaces it with mrns
schema := providers.DefaultRuntime().Schema()
_, err := queryPackBundle.Compile(context.Background(), schema)
if err != nil {
errors = append(errors, "could not compile the query pack bundle", err.Error())
}

return errors
}

0 comments on commit 18de21e

Please sign in to comment.