Skip to content
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
5 changes: 5 additions & 0 deletions cmd/policy_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cloudquery/cloudquery/pkg/ui/console"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const policyRunHelpMsg = "Executes a policy on CloudQuery database"
Expand Down Expand Up @@ -47,6 +48,10 @@ func init() {
flags := policyRunCmd.Flags()
flags.StringVar(&outputDir, "output-dir", "", "Generates a new file for each policy at the given dir with the output")
flags.BoolVar(&noResults, "no-results", false, "Do not show policies results")
flags.Bool("disable-fetch-check", false, "Disable checking if a respective fetch happened before running policies")
Comment thread
shimonp21 marked this conversation as resolved.
Comment thread
shimonp21 marked this conversation as resolved.

_ = viper.BindPFlag("disable-fetch-check", flags.Lookup("disable-fetch-check"))

policyRunCmd.SetUsageTemplate(usageTemplateWithFlags)
policyCmd.AddCommand(policyRunCmd)
}
8 changes: 6 additions & 2 deletions pkg/policy/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-version"
"github.com/spf13/afero"
"github.com/spf13/viper"
)

var ErrPolicyOrQueryNotFound = errors.New("selected policy/query not found")
Expand Down Expand Up @@ -136,8 +137,11 @@ func (e *Executor) Execute(ctx context.Context, req *ExecuteRequest, policy *Pol
if err := e.checkVersions(policy.Config, req.ProviderVersions); err != nil {
return nil, fmt.Errorf("%s: %w", policy.Name, err)
}
if err := e.checkFetches(ctx, policy.Config); err != nil {
return nil, fmt.Errorf("%s: %w, please run `cloudquery fetch` before running policy", policy.Name, err)

if !viper.GetBool("disable-fetch-check") {
if err := e.checkFetches(ctx, policy.Config); err != nil {
return nil, fmt.Errorf("%s: %w, please run `cloudquery fetch` before running policy", policy.Name, err)
}
}

for _, p := range policy.Policies {
Expand Down
76 changes: 76 additions & 0 deletions pkg/policy/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/cloudquery/cq-provider-sdk/provider/execution"
"github.com/google/uuid"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-version"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -405,6 +407,80 @@ func setupCheckFetchDatabase(db execution.QueryExecer, summary *meta_storage.Fet
}, nil
}

func TestExecuter_DisbleFetchCheckFlag(t *testing.T) {
db, err := sdkdb.New(context.Background(), hclog.NewNullLogger(), testDBConnection)
assert.NoError(t, err)

metaStorage := meta_storage.NewClient(db, hclog.NewNullLogger())

_, de, err := database.GetExecutor(hclog.NewNullLogger(), testDBConnection, &history.Config{})
if err != nil {
t.Fatal(fmt.Errorf("getExecutor: %w", err))
}

err = metaStorage.MigrateCore(context.Background(), de)
assert.NoError(t, err)

executor := NewExecutor(db, hclog.Default(), nil)

policy := &Policy{
Name: "test",
Policies: nil,
Checks: []*Check{{
Query: "SELECT 1 as result;",
ExpectOutput: true,
}},
Config: &Configuration{
Providers: []*Provider{
{
Type: "testProvider",
Version: ">0.0.0",
},
},
},
}

testCases := []struct {
Name string
DisableFetchCheck bool
ExpectedError error
}{{
Name: "fetch_check_enabled",
DisableFetchCheck: false,
ExpectedError: errors.New("could not find a completed fetch for requested provider"),
},
{
Name: "fetch_check_disabled",
DisableFetchCheck: true,
ExpectedError: nil,
},
}

testProviderVersion, err := version.NewVersion("0.1.0")
assert.NoError(t, err)

executeRequest := &ExecuteRequest{
Policy: policy,
ProviderVersions: map[string]*version.Version{"testProvider": testProviderVersion},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
defer viper.Reset()
viper.Set("disable-fetch-check", tc.DisableFetchCheck)

_, err = executor.Execute(context.Background(), executeRequest, policy)

if tc.ExpectedError == nil {
assert.NoError(t, err)
} else {
assert.Contains(t, err.Error(), tc.ExpectedError.Error())
}
})
}

}

func TestExecutor_CheckFetches(t *testing.T) {
// create database connection
db, err := sdkdb.New(context.Background(), hclog.NewNullLogger(), testDBConnection)
Expand Down