Skip to content

Commit

Permalink
Validate rule type alert during creation / update
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias committed Nov 19, 2024
1 parent 6bd6f55 commit 7e3eeaf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
33 changes: 33 additions & 0 deletions pkg/api/protobuf/go/minder/v1/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ func (def *RuleType_Definition) Validate() error {
return err
}

// Alert is not required and can be nil
if def.Alert != nil {
if err := def.Alert.Validate(); err != nil {
return err
}
}

return def.Eval.Validate()
}

Expand Down Expand Up @@ -286,6 +293,32 @@ func (ing *RuleType_Definition_Ingest) Validate() error {
return nil
}

// Validate validates a rule type definition alert
func (alert *RuleType_Definition_Alert) Validate() error {
if alert == nil {
return nil
}

// Not using import to avoid circular dependency
if alert.Type == "security_advisory" {
if err := alert.GetSecurityAdvisory().Validate(); err != nil {
return err
}
} else {
return fmt.Errorf("%w: alert type cannot be empty", ErrInvalidRuleTypeDefinition)
}
return nil
}

// Validate validates a rule type alert security advisory
func (sa *RuleType_Definition_Alert_AlertTypeSA) Validate() error {
if sa == nil {
return fmt.Errorf("%w: security advisory is nil", ErrInvalidRuleTypeDefinition)
}

return nil
}

// Validate validates a rule type definition ingest diff
func (diffing *DiffType) Validate() error {
if diffing == nil {
Expand Down
50 changes: 50 additions & 0 deletions pkg/api/protobuf/go/minder/v1/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,53 @@ func TestRuleType_Definition_Eval_Rego_Validate(t *testing.T) {
})
}
}

func TestRuleType_Definition_Alert_Validate(t *testing.T) {
t.Parallel()
tests := []struct {
name string
alert *RuleType_Definition_Alert
wantErr bool
}{
{
name: "valid alert definition",
alert: &RuleType_Definition_Alert{
Type: "security_advisory",
SecurityAdvisory: &RuleType_Definition_Alert_AlertTypeSA{},
},
wantErr: false,
},
{
name: "nil alert is valid",
alert: nil,
wantErr: false,
},
{
name: "empty alert type",
alert: &RuleType_Definition_Alert{
Type: "",
},
wantErr: true,
},
{
name: "invalid security advisory",
alert: &RuleType_Definition_Alert{
Type: "security_advisory",
SecurityAdvisory: nil,
},
wantErr: true,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

err := tt.alert.Validate()
if (err != nil) != tt.wantErr {
t.Errorf("Validate() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 7e3eeaf

Please sign in to comment.