Skip to content

Commit 773b42a

Browse files
committed
fix duplicate container image in report
1 parent ad4ec10 commit 773b42a

File tree

3 files changed

+86
-52
lines changed

3 files changed

+86
-52
lines changed

internal/applicationsnapshot/report.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,17 @@ func NewVSAReport(components []VSAComponent) VSAReport {
551551
if failingRulesField.Kind() == reflect.Slice {
552552
for i := 0; i < failingRulesField.Len(); i++ {
553553
rule := failingRulesField.Index(i)
554+
555+
// Extract the specific container image for this violation from the FailingRule
556+
specificImageRef := extractSpecificImageRefFromFailingRule(rule)
557+
558+
// If no specific image is found, fall back to the component's container image
559+
if specificImageRef == "" {
560+
specificImageRef = comp.ContainerImage
561+
}
562+
554563
violation := VSAViolation{
555-
ImageRef: comp.ContainerImage,
564+
ImageRef: specificImageRef,
556565
}
557566

558567
// Extract rule fields using reflection
@@ -591,3 +600,18 @@ func NewVSAReport(components []VSAComponent) VSAReport {
591600
Components: components, // Keep for backward compatibility
592601
}
593602
}
603+
604+
// extractSpecificImageRefFromFailingRule extracts the specific container image reference for a violation
605+
// from the FailingRule's ComponentImage field
606+
func extractSpecificImageRefFromFailingRule(rule reflect.Value) string {
607+
// Try to get the ComponentImage field from the FailingRule
608+
if componentImageField := rule.FieldByName("ComponentImage"); componentImageField.IsValid() {
609+
if componentImage := componentImageField.String(); componentImage != "" {
610+
return componentImage
611+
}
612+
}
613+
614+
// Fallback: if ComponentImage is not available, return empty string
615+
// This will be handled by the calling code
616+
return ""
617+
}

internal/validate/vsa/validation.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,10 @@ func extractRuleResultsFromPredicate(predicate *Predicate) map[string][]RuleResu
126126
ruleID := extractRuleID(success)
127127
if ruleID != "" {
128128
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
129-
RuleID: ruleID,
130-
Status: "success",
131-
Message: success.Message,
129+
RuleID: ruleID,
130+
Status: "success",
131+
Message: success.Message,
132+
ComponentImage: component.ContainerImage,
132133
})
133134
}
134135
}
@@ -138,12 +139,13 @@ func extractRuleResultsFromPredicate(predicate *Predicate) map[string][]RuleResu
138139
ruleID := extractRuleID(violation)
139140
if ruleID != "" {
140141
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
141-
RuleID: ruleID,
142-
Status: "failure",
143-
Message: violation.Message,
144-
Title: extractMetadataString(violation, "title"),
145-
Description: extractMetadataString(violation, "description"),
146-
Solution: extractMetadataString(violation, "solution"),
142+
RuleID: ruleID,
143+
Status: "failure",
144+
Message: violation.Message,
145+
Title: extractMetadataString(violation, "title"),
146+
Description: extractMetadataString(violation, "description"),
147+
Solution: extractMetadataString(violation, "solution"),
148+
ComponentImage: component.ContainerImage,
147149
})
148150
}
149151
}
@@ -153,9 +155,10 @@ func extractRuleResultsFromPredicate(predicate *Predicate) map[string][]RuleResu
153155
ruleID := extractRuleID(warning)
154156
if ruleID != "" {
155157
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
156-
RuleID: ruleID,
157-
Status: "warning",
158-
Message: warning.Message,
158+
RuleID: ruleID,
159+
Status: "warning",
160+
Message: warning.Message,
161+
ComponentImage: component.ContainerImage,
159162
})
160163
}
161164
}
@@ -220,13 +223,14 @@ func compareRules(vsaRuleResults map[string][]RuleResult, requiredRules map[stri
220223
if ruleResult.Status == "failure" {
221224
// Rule failed validation - this is a failure
222225
result.FailingRules = append(result.FailingRules, FailingRule{
223-
RuleID: ruleID,
224-
Package: extractPackageFromCode(ruleID),
225-
Message: ruleResult.Message,
226-
Reason: ruleResult.Message,
227-
Title: ruleResult.Title,
228-
Description: ruleResult.Description,
229-
Solution: ruleResult.Solution,
226+
RuleID: ruleID,
227+
Package: extractPackageFromCode(ruleID),
228+
Message: ruleResult.Message,
229+
Reason: ruleResult.Message,
230+
Title: ruleResult.Title,
231+
Description: ruleResult.Description,
232+
Solution: ruleResult.Solution,
233+
ComponentImage: ruleResult.ComponentImage,
230234
})
231235
} else if ruleResult.Status == "success" || ruleResult.Status == "warning" {
232236
// Rule passed or has warning - both are acceptable

internal/validate/vsa/validator.go

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,25 @@ type MissingRule struct {
8888

8989
// FailingRule represents a rule that is present in the VSA but failed validation
9090
type FailingRule struct {
91-
RuleID string `json:"rule_id"`
92-
Package string `json:"package"`
93-
Message string `json:"message"`
94-
Reason string `json:"reason"`
95-
Title string `json:"title,omitempty"`
96-
Description string `json:"description,omitempty"`
97-
Solution string `json:"solution,omitempty"`
91+
RuleID string `json:"rule_id"`
92+
Package string `json:"package"`
93+
Message string `json:"message"`
94+
Reason string `json:"reason"`
95+
Title string `json:"title,omitempty"`
96+
Description string `json:"description,omitempty"`
97+
Solution string `json:"solution,omitempty"`
98+
ComponentImage string `json:"component_image,omitempty"` // The specific container image this violation relates to
9899
}
99100

100101
// RuleResult represents a rule result extracted from the VSA
101102
type RuleResult struct {
102-
RuleID string `json:"rule_id"`
103-
Status string `json:"status"` // "success", "failure", "warning", "skipped", "exception"
104-
Message string `json:"message"`
105-
Title string `json:"title,omitempty"`
106-
Description string `json:"description,omitempty"`
107-
Solution string `json:"solution,omitempty"`
103+
RuleID string `json:"rule_id"`
104+
Status string `json:"status"` // "success", "failure", "warning", "skipped", "exception"
105+
Message string `json:"message"`
106+
Title string `json:"title,omitempty"`
107+
Description string `json:"description,omitempty"`
108+
Solution string `json:"solution,omitempty"`
109+
ComponentImage string `json:"component_image,omitempty"` // The specific container image this result relates to
108110
}
109111

110112
// VSARuleValidatorImpl implements VSARuleValidator with comprehensive validation logic
@@ -161,9 +163,10 @@ func (v *VSARuleValidatorImpl) extractRuleResults(vsaRecords []VSARecord) (map[s
161163
ruleID := v.extractRuleID(success)
162164
if ruleID != "" {
163165
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
164-
RuleID: ruleID,
165-
Status: "success",
166-
Message: success.Message,
166+
RuleID: ruleID,
167+
Status: "success",
168+
Message: success.Message,
169+
ComponentImage: component.ContainerImage,
167170
})
168171
}
169172
}
@@ -173,12 +176,13 @@ func (v *VSARuleValidatorImpl) extractRuleResults(vsaRecords []VSARecord) (map[s
173176
ruleID := v.extractRuleID(violation)
174177
if ruleID != "" {
175178
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
176-
RuleID: ruleID,
177-
Status: "failure",
178-
Message: violation.Message,
179-
Title: v.extractMetadataString(violation, "title"),
180-
Description: v.extractMetadataString(violation, "description"),
181-
Solution: v.extractMetadataString(violation, "solution"),
179+
RuleID: ruleID,
180+
Status: "failure",
181+
Message: violation.Message,
182+
Title: v.extractMetadataString(violation, "title"),
183+
Description: v.extractMetadataString(violation, "description"),
184+
Solution: v.extractMetadataString(violation, "solution"),
185+
ComponentImage: component.ContainerImage,
182186
})
183187
}
184188
}
@@ -188,9 +192,10 @@ func (v *VSARuleValidatorImpl) extractRuleResults(vsaRecords []VSARecord) (map[s
188192
ruleID := v.extractRuleID(warning)
189193
if ruleID != "" {
190194
ruleResults[ruleID] = append(ruleResults[ruleID], RuleResult{
191-
RuleID: ruleID,
192-
Status: "warning",
193-
Message: warning.Message,
195+
RuleID: ruleID,
196+
Status: "warning",
197+
Message: warning.Message,
198+
ComponentImage: component.ContainerImage,
194199
})
195200
}
196201
}
@@ -287,13 +292,14 @@ func (v *VSARuleValidatorImpl) compareRules(vsaRuleResults map[string][]RuleResu
287292
if ruleResult.Status == "failure" {
288293
// Rule failed validation - this is a failure
289294
result.FailingRules = append(result.FailingRules, FailingRule{
290-
RuleID: ruleID,
291-
Package: v.extractPackageFromRuleID(ruleID),
292-
Message: ruleResult.Message,
293-
Reason: "Rule failed validation in VSA",
294-
Title: ruleResult.Title,
295-
Description: ruleResult.Description,
296-
Solution: ruleResult.Solution,
295+
RuleID: ruleID,
296+
Package: v.extractPackageFromRuleID(ruleID),
297+
Message: ruleResult.Message,
298+
Reason: "Rule failed validation in VSA",
299+
Title: ruleResult.Title,
300+
Description: ruleResult.Description,
301+
Solution: ruleResult.Solution,
302+
ComponentImage: ruleResult.ComponentImage,
297303
})
298304
} else if ruleResult.Status == "success" || ruleResult.Status == "warning" {
299305
// Rule passed or has warning - both are acceptable

0 commit comments

Comments
 (0)