Skip to content

Commit 2f877b3

Browse files
authored
Fix/remove generation time (#493)
* few updates on policy structure, wrap long col values * new column for total rules applied * fix linting issue, remove unused var
1 parent fb9c522 commit 2f877b3

File tree

5 files changed

+163
-118
lines changed

5 files changed

+163
-118
lines changed

pkg/policy/engine.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,34 @@ func Engine(ctx context.Context, policyConfig *Params, policies []Policy) error
4848

4949
log.Debugf("field mapping done via extractor")
5050

51-
var results []Result
51+
var policyResults []PolicyResult
5252

5353
log.Debugf("Evaluation of policy against SBOM begins...")
5454

5555
// Evaluate policies
5656
for _, policy := range policies {
5757
log.Debugf("Evaluating policy: ", policy.Name)
5858

59+
// evaluate each policy one by one against SBOM
5960
result, err := EvaluatePolicyAgainstSBOMs(ctx, policy, doc, fieldExtractor)
6061
if err != nil {
6162
return fmt.Errorf("policy %s evaluation failed: %w", policy.Name, err)
6263
}
63-
results = append(results, result)
64+
policyResults = append(policyResults, result)
6465
}
6566

6667
// Reporting
6768
switch strings.ToLower(policyConfig.OutputFmt) {
6869
case "json":
69-
if err := ReportJSON(ctx, results); err != nil {
70+
if err := ReportJSON(ctx, policyResults); err != nil {
7071
return fmt.Errorf("failed to write json output: %w", err)
7172
}
7273
case "table":
73-
if err := ReportTable(ctx, results); err != nil {
74+
if err := ReportTable(ctx, policyResults); err != nil {
7475
return fmt.Errorf("failed to write yaml output: %w", err)
7576
}
7677
default:
77-
if err := ReportBasic(ctx, results); err != nil {
78+
if err := ReportBasic(ctx, policyResults); err != nil {
7879
return fmt.Errorf("failed to write table output: %w", err)
7980
}
8081
}

pkg/policy/evaluator.go

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,29 @@ package policy
1717
import (
1818
"context"
1919
"regexp"
20-
"time"
2120

2221
"github.com/interlynk-io/sbomqs/pkg/logger"
2322
"github.com/interlynk-io/sbomqs/pkg/sbom"
2423
)
2524

2625
// EvaluatePolicyAgainstSBOMs evaluates a single policy against a SBOMs.
27-
func EvaluatePolicyAgainstSBOMs(ctx context.Context, p Policy, doc sbom.Document, fieldExtractor *Extractor) (Result, error) {
26+
func EvaluatePolicyAgainstSBOMs(ctx context.Context, policy Policy, doc sbom.Document, fieldExtractor *Extractor) (PolicyResult, error) {
2827
log := logger.FromContext(ctx)
29-
log.Debugf("processing policy evaluation: %s", p.Name, p.Type)
28+
log.Debugf("processing policy evaluation: %s", policy.Name, policy.Type)
3029

31-
result := NewResult(p)
32-
result.GeneratedAt = time.Now().UTC()
30+
policyResult := NewPolicyResult(policy)
3331

32+
totalChecks := 0
3433
components := doc.Components()
35-
result.TotalChecked = len(components)
34+
policyResult.TotalComponents = len(components)
3635

3736
// compile regex present in pattern rules
38-
compiledRules, err := compilePatternRules(p)
37+
compiledRules, err := compilePatternRules(policy)
3938
if err != nil {
40-
return Result{}, err
39+
return PolicyResult{}, err
4140
}
4241

43-
policyResults := make([]PolicyResult, 0, len(components)*len(compiledRules))
42+
policyResults := make([]RuleResult, 0, len(components)*len(compiledRules))
4443

4544
// evaluate components against list of all rules in a single policy
4645
for _, comp := range components {
@@ -54,6 +53,7 @@ func EvaluatePolicyAgainstSBOMs(ctx context.Context, p Policy, doc sbom.Document
5453

5554
// evaluate each component against list of all rules
5655
for _, compileRule := range compiledRules {
56+
totalChecks++
5757
// evaluate rule
5858

5959
declaredRule := compileRule.Rule
@@ -66,43 +66,43 @@ func EvaluatePolicyAgainstSBOMs(ctx context.Context, p Policy, doc sbom.Document
6666
actualValues := fieldExtractor.RetrieveValues(comp, declaredField)
6767

6868
// default outcome/pass reason
69-
outcome := "pass"
69+
result := "pass"
7070
reason := "present"
7171

7272
// required rule: presence check
73-
if RULE_TYPE(p.Type) == REQUIRED {
73+
if RULE_TYPE(policy.Type) == REQUIRED {
7474
ok := fieldExtractor.HasField(comp, declaredField)
7575
if !ok {
76-
outcome = "fail"
76+
result = "fail"
7777
reason = "missing field"
7878
}
7979

8080
} else {
8181
// for whitelist/blacklist do matching
8282
matched := anyMatch(actualValues, declaredValues, patterns)
8383

84-
switch RULE_TYPE(p.Type) {
84+
switch RULE_TYPE(policy.Type) {
8585
case WHITELIST:
8686
if !matched {
87-
outcome = "fail"
87+
result = "fail"
8888
reason = "value not in whitelist"
8989
}
9090
case BLACKLIST:
9191
if matched {
92-
outcome = "fail"
92+
result = "fail"
9393
reason = "value in blacklist"
9494
}
9595
default:
9696
// if unknown type, treat as pass (or change to fail depending on your policy)
9797
}
9898
}
9999

100-
pr := PolicyResult{
100+
pr := RuleResult{
101101
ComponentID: compID,
102102
ComponentName: compName,
103-
Field: declaredField,
104-
Actual: actualValues,
105-
Outcome: outcome,
103+
DeclaredField: declaredField,
104+
ActualValues: actualValues,
105+
Result: result,
106106
Reason: reason,
107107
}
108108

@@ -112,32 +112,33 @@ func EvaluatePolicyAgainstSBOMs(ctx context.Context, p Policy, doc sbom.Document
112112
}
113113

114114
// assign results
115-
result.PolicyResults = policyResults
115+
policyResult.RuleResults = policyResults
116+
policyResult.TotalChecks = totalChecks
116117

117118
// compute ViolationCnt (failed outcomes)
118119
violationCount := 0
119120
for _, pr := range policyResults {
120-
if pr.Outcome == "fail" {
121+
if pr.Result == "fail" {
121122
violationCount++
122123
}
123124
}
124-
result.ViolationCnt = violationCount
125+
policyResult.ViolationCnt = violationCount
125126

126127
// Decide outcome
127-
if result.ViolationCnt == 0 {
128-
result.Result = "pass"
128+
if policyResult.ViolationCnt == 0 {
129+
policyResult.OverallResult = "pass"
129130
} else {
130-
switch p.Action {
131+
switch policy.Action {
131132
case "warn":
132-
result.Result = "warn"
133+
policyResult.OverallResult = "warn"
133134
case "pass":
134-
result.Result = "pass"
135+
policyResult.OverallResult = "pass"
135136
default:
136-
result.Result = "fail"
137+
policyResult.OverallResult = "fail"
137138
}
138139
}
139140

140-
return *result, nil
141+
return *policyResult, nil
141142
}
142143

143144
// anyMatch returns true if at least one of the actual values

pkg/policy/policy.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ type Params struct {
3434

3535
// Output
3636
OutputFmt string
37-
38-
// Debug
39-
debug bool
4037
}
4138

4239
// PolicyFile represents the top-level YAML structure

0 commit comments

Comments
 (0)