Skip to content

Commit

Permalink
✨ new cnspec policy command
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuskimmina committed Dec 15, 2023
1 parent 4a0a690 commit 5499c58
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions apps/cnspec/cmd/bundle.go → apps/cnspec/cmd/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,40 @@ import (
"go.mondoo.com/cnquery/v9/providers-sdk/v1/upstream"
"go.mondoo.com/cnspec/v9/internal/bundle"
"go.mondoo.com/cnspec/v9/policy"
"go.mondoo.com/ranger-rpc"
)

const (
defaultRegistryUrl = "https://registry.api.mondoo.com"
)

func init() {
// policy init
policyBundlesCmd.AddCommand(policyInitCmd)
policyCmd.AddCommand(policyInitCmd)

// policy list
policyCmd.AddCommand(policyListCmd)

// validate
policyLintCmd.Flags().StringP("output", "o", "cli", "Set output format: compact, sarif")
policyLintCmd.Flags().String("output-file", "", "Set output file")
policyBundlesCmd.AddCommand(policyLintCmd)
policyCmd.AddCommand(policyLintCmd)

// fmt
policyFmtCmd.Flags().Bool("sort", false, "sort the bundle.")
policyBundlesCmd.AddCommand(policyFmtCmd)
policyCmd.AddCommand(policyFmtCmd)

// docs
policyDocsCmd.Flags().Bool("no-code", false, "enable/disable code blocks inside of docs")
policyDocsCmd.Flags().Bool("no-ids", false, "enable/disable the printing of ID fields")
policyBundlesCmd.AddCommand(policyDocsCmd)
policyCmd.AddCommand(policyDocsCmd)

// publish
policyPublishCmd.Flags().Bool("no-lint", false, "Disable linting of the bundle before publishing.")
policyPublishCmd.Flags().String("policy-version", "", "Override the version of each policy in the bundle.")
policyBundlesCmd.AddCommand(policyPublishCmd)
policyCmd.AddCommand(policyPublishCmd)

rootCmd.AddCommand(policyBundlesCmd)
rootCmd.AddCommand(policyCmd)
}

// ensureProviders ensures that all providers are locally installed
Expand All @@ -56,17 +64,50 @@ func ensureProviders() error {
return nil
}

var policyBundlesCmd = &cobra.Command{
Use: "bundle",
Short: "Manage policy bundles.",
var policyCmd = &cobra.Command{
Use: "policy",
Short: "Manage policies.",
}

//go:embed policy-example.mql.yaml
var embedPolicyTemplate []byte

var policyListCmd = &cobra.Command{
Use: "list",
Short: "list currently active policies in the connected space",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
registryEndpoint := os.Getenv("REGISTRY_URL")
if registryEndpoint == "" {
registryEndpoint = defaultRegistryUrl
}

// Note, this does not use the proxy config override from the mondoo.yml since we only get here when
// it is used without upstream config
client, err := policy.NewPolicyHubClient(registryEndpoint, ranger.DefaultHttpClient())
if err != nil {
log.Fatal().Err(err)
}

listReq := policy.ListReq{}
policyList, err := client.List(context.Background(), &listReq)
if err != nil {
log.Fatal().Err(err)
}
for _, policy := range policyList.Items {
// Printing policy name and version
fmt.Println(policy.Name + " " + policy.Version)

// Printing policy MRN in gray
fmt.Printf("\033[90m %s\033[0m\n", policy.Mrn)
}

},
}

var policyInitCmd = &cobra.Command{
Use: "init [path]",
Short: "Create an example policy bundle that you can use as a starting point. If you don't provide a filename, cnspec uses `example-policy.mql.yml`.",
Short: "Create an example policy that you can use as a starting point. If you don't provide a filename, cnspec uses `example-policy.mql.yml`.",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
name := "example-policy.mql.yaml"
Expand All @@ -90,7 +131,7 @@ var policyInitCmd = &cobra.Command{
var policyLintCmd = &cobra.Command{
Use: "lint [path]",
Aliases: []string{"validate"},
Short: "Lint a policy bundle.",
Short: "Lint a policy.",
Args: cobra.ExactArgs(1),
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlag("output", cmd.Flags().Lookup("output"))
Expand Down

0 comments on commit 5499c58

Please sign in to comment.