Skip to content

Commit

Permalink
Add resource_type to rule properties (#4)
Browse files Browse the repository at this point in the history
This PR adds the `resource_type` field from the rule metadata to the
output of passed rules, and further to the rule properties in the Sarif
report.

This field is useful to know which resource rules were applied to, in
particular for rules without violations where we have no way of knowing
this otherwise.
  • Loading branch information
snakster authored Sep 6, 2024
2 parents 0b185f0 + ac7ecb7 commit e553f7a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
11 changes: 6 additions & 5 deletions pkg/policy/opa/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,12 @@ func (e *Engine) reportViolation(regoData *policy.RegoData, resource *output.Res
// reportPassed Adds a passed rule which wasn't violated by all the resources
func (e *Engine) reportPassed(regoData *policy.RegoData) {
passedRule := results.PassedRule{
RuleName: regoData.Metadata.Name,
Description: regoData.Metadata.Description,
RuleID: regoData.Metadata.ID,
Severity: regoData.Metadata.Severity,
Category: regoData.Metadata.Category,
RuleName: regoData.Metadata.Name,
Description: regoData.Metadata.Description,
RuleID: regoData.Metadata.ID,
Severity: regoData.Metadata.Severity,
Category: regoData.Metadata.Category,
ResourceType: regoData.Metadata.ResourceType,
}

e.results.ViolationStore.AddPassedRule(&passedRule)
Expand Down
11 changes: 6 additions & 5 deletions pkg/results/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ type Violation struct {

// PassedRule contains information of a passed rule
type PassedRule struct {
RuleName string `json:"rule_name" yaml:"rule_name" xml:"rule_name,attr"`
Description string `json:"description" yaml:"description" xml:"description,attr"`
RuleID string `json:"rule_id" yaml:"rule_id" xml:"rule_id,attr"`
Severity string `json:"severity" yaml:"severity" xml:"severity,attr"`
Category string `json:"category" yaml:"category" xml:"category,attr"`
RuleName string `json:"rule_name" yaml:"rule_name" xml:"rule_name,attr"`
Description string `json:"description" yaml:"description" xml:"description,attr"`
RuleID string `json:"rule_id" yaml:"rule_id" xml:"rule_id,attr"`
Severity string `json:"severity" yaml:"severity" xml:"severity,attr"`
Category string `json:"category" yaml:"category" xml:"category,attr"`
ResourceType string `json:"resource_type" yaml:"resource_type" xml:"resource_type,attr"`
}

// ViolationStore Storage area for violation data
Expand Down
1 change: 1 addition & 0 deletions pkg/writer/github_sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const violationTemplateForGH = `{
},
"properties": {
"category": "S3",
"resource_type": "aws_s3_bucket",
"severity": "HIGH"
}
}
Expand Down
22 changes: 12 additions & 10 deletions pkg/writer/human_readable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ var (
ViolationStore: &results.ViolationStore{
PassedRules: []*results.PassedRule{
{
RuleName: "s3EnforceUserACL",
Description: "S3 bucket Access is allowed to all AWS Account Users.",
RuleID: "AWS.S3Bucket.DS.High.1043",
Severity: "HIGH",
Category: "S3",
RuleName: "s3EnforceUserACL",
Description: "S3 bucket Access is allowed to all AWS Account Users.",
RuleID: "AWS.S3Bucket.DS.High.1043",
Severity: "HIGH",
Category: "S3",
ResourceType: "aws_s3_bucket",
},
},
Summary: summaryWithNoViolations,
Expand All @@ -53,11 +54,12 @@ var (
},
PassedRules: []*results.PassedRule{
{
RuleName: "s3EnforceUserACL",
Description: "S3 bucket Access is allowed to all AWS Account Users.",
RuleID: "AWS.S3Bucket.DS.High.1043",
Severity: "HIGH",
Category: "S3",
RuleName: "s3EnforceUserACL",
Description: "S3 bucket Access is allowed to all AWS Account Users.",
RuleID: "AWS.S3Bucket.DS.High.1043",
Severity: "HIGH",
Category: "S3",
ResourceType: "aws_s3_bucket",
},
},
Summary: summaryWithNoViolations,
Expand Down
2 changes: 2 additions & 0 deletions pkg/writer/sarif.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func writeSarif(data interface{}, writers []io.Writer, forGitHub bool) error {
for _, passedRule := range outputData.PassedRules {
m := sarif.NewPropertyBag()
m.Properties["category"] = passedRule.Category
m.Properties["resource_type"] = passedRule.ResourceType
m.Properties["severity"] = passedRule.Severity

run.AddRule(passedRule.RuleID).
Expand All @@ -67,6 +68,7 @@ func writeSarif(data interface{}, writers []io.Writer, forGitHub bool) error {
for _, violation := range outputData.Violations {
m := sarif.NewPropertyBag()
m.Properties["category"] = violation.Category
m.Properties["resource_type"] = violation.ResourceType
m.Properties["severity"] = violation.Severity

rule := run.AddRule(violation.RuleID).
Expand Down
5 changes: 4 additions & 1 deletion pkg/writer/sarif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const violationTemplate = `{
},
"properties": {
"category": "S3",
"severity": "HIGH"
"severity": "HIGH",
"resource_type": "aws_s3_bucket"
}
}
],
Expand Down Expand Up @@ -113,6 +114,7 @@ var expectedSarifOutput3 = fmt.Sprintf(`{
},
"properties": {
"category": "S3",
"resource_type": "aws_s3_bucket",
"severity": "HIGH"
}
}
Expand Down Expand Up @@ -143,6 +145,7 @@ var expectedSarifOutput4 = fmt.Sprintf(`{
},
"properties": {
"category": "S3",
"resource_type": "aws_s3_bucket",
"severity": "HIGH"
}
}
Expand Down

0 comments on commit e553f7a

Please sign in to comment.