Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Nov 15, 2024
1 parent 8fa87cd commit 49a86d6
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions pkg/profiles/rule_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/santhosh-tekuri/jsonschema/v6"
"google.golang.org/protobuf/types/known/structpb"

minderv1 "github.com/mindersec/minder/pkg/api/protobuf/go/minder/v1"
)
Expand All @@ -25,20 +26,20 @@ type RuleValidator struct {

// NewRuleValidator creates a new rule validator
func NewRuleValidator(rt *minderv1.RuleType) (*RuleValidator, error) {
if rt.GetDef().GetRuleSchema() == nil {
return nil, fmt.Errorf("rule type %s does not have a rule schema", rt.Name)
}
// Create a new schema compiler
// Compile the main rule schema
mainSchema, err := compileSchema(rt.GetDef().GetRuleSchema().AsMap())
mainSchema, err := compileSchemaFromPB(rt.GetDef().GetRuleSchema())
if err != nil {
return nil, fmt.Errorf("cannot create json schema: %w", err)
}

// Compile the parameter schema if it exists
var paramSchema *jsonschema.Schema
if rt.Def.ParamSchema != nil {
paramSchema, err = compileSchema(rt.GetDef().GetParamSchema().AsMap())
if err != nil {
return nil, fmt.Errorf("cannot create json schema for params: %w", err)
}
paramSchema, err := compileSchemaFromPB(rt.GetDef().GetParamSchema())
if err != nil {
return nil, fmt.Errorf("cannot create json schema for params: %w", err)
}

return &RuleValidator{
Expand Down Expand Up @@ -77,7 +78,15 @@ func (r *RuleValidator) ValidateParamsAgainstSchema(params map[string]any) error
return nil
}

func compileSchema(schemaData interface{}) (*jsonschema.Schema, error) {
func compileSchemaFromPB(schemaData *structpb.Struct) (*jsonschema.Schema, error) {
if schemaData == nil {
return nil, nil
}

return compileSchemaFromMap(schemaData.AsMap())
}

func compileSchemaFromMap(schemaData map[string]any) (*jsonschema.Schema, error) {
compiler := jsonschema.NewCompiler()
if err := compiler.AddResource("schema.json", schemaData); err != nil {
return nil, fmt.Errorf("invalid schema: %w", err)
Expand All @@ -87,7 +96,10 @@ func compileSchema(schemaData interface{}) (*jsonschema.Schema, error) {

func validateAgainstSchema(schema *jsonschema.Schema, obj map[string]any) error {
if err := schema.Validate(obj); err != nil {
return buildValidationError(err.(*jsonschema.ValidationError).Causes)
if verror, ok := err.(*jsonschema.ValidationError); ok {
return buildValidationError(verror.Causes)
}
return fmt.Errorf("invalid json schema: %s", err)
}
return nil
}
Expand Down

0 comments on commit 49a86d6

Please sign in to comment.