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

mindev: Add new subcommand to validate ruletype updates #4790

Merged
merged 1 commit into from
Oct 23, 2024
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
1 change: 1 addition & 0 deletions cmd/dev/app/rule_type/ruletype.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func CmdRuleType() *cobra.Command {

rtCmd.AddCommand(CmdTest())
rtCmd.AddCommand(CmdLint())
rtCmd.AddCommand(CmdValidateUpdate())

return rtCmd
}
76 changes: 76 additions & 0 deletions cmd/dev/app/rule_type/validate_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-FileCopyrightText: Copyright 2023 The Minder Authors
// SPDX-License-Identifier: Apache-2.0

package rule_type

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/mindersec/minder/internal/util/schemaupdate"
)

// CmdValidateUpdate is the command for validating an update of a rule type definition
func CmdValidateUpdate() *cobra.Command {
var vuCmd = &cobra.Command{
Use: "validate-update",
Aliases: []string{"vu"},
Short: "validate an update of a rule type definition",
Long: `The 'ruletype validate-update' subcommand allows you to validate an update of a rule type
definition`,
RunE: vuCmdRun,
SilenceUsage: true,
}
vuCmd.Flags().StringP("before", "b", "", "file to read rule type definition from")
vuCmd.Flags().StringP("after", "a", "", "file to read rule type definition from")

if err := vuCmd.MarkFlagRequired("before"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}
if err := vuCmd.MarkFlagRequired("after"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}

return vuCmd
}

func vuCmdRun(cmd *cobra.Command, _ []string) error {
beforePath := cmd.Flag("before").Value.String()
afterPath := cmd.Flag("after").Value.String()

beforeRt, err := readRuleTypeFromFile(beforePath)
if err != nil {
return fmt.Errorf("error reading rule type from %s: %w", beforePath, err)
}

afterRt, err := readRuleTypeFromFile(afterPath)
if err != nil {
return fmt.Errorf("error reading rule type from %s: %w", afterPath, err)
}

// We only validate the after rule type because the before rule type is assumed to be valid
if err := afterRt.Validate(); err != nil {
return fmt.Errorf("error validating rule type %s: %w", afterPath, err)
}

if beforeRt.GetName() != afterRt.GetName() {
return fmt.Errorf("rule type name cannot be changed")
}

beforeDef := beforeRt.GetDef()
afterDef := afterRt.GetDef()

if err := schemaupdate.ValidateSchemaUpdate(beforeDef.GetRuleSchema(), afterDef.GetRuleSchema()); err != nil {
return fmt.Errorf("error validating rule schema update: %w", err)
}

if err := schemaupdate.ValidateSchemaUpdate(beforeDef.GetParamSchema(), afterDef.GetParamSchema()); err != nil {
return fmt.Errorf("error validating param schema update: %w", err)
}

return nil
}