Skip to content

Commit 5d24307

Browse files
committed
add package name
1 parent 43824a3 commit 5d24307

2 files changed

Lines changed: 106 additions & 67 deletions

File tree

cli/cage/audit/printer.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,19 @@ func (p *printer) logImageScanFindings(
9898
for _, c := range containers {
9999
containerList = append(containerList, color.Bold(c))
100100
}
101-
p.logger.Printf("- %s \n", strings.Join(containerList, ", "))
102-
p.logger.Printf(" %s (%s)\n", *cve.Name, *cve.Uri)
101+
var packageName string = "unknown"
102+
var packageVersion string = "unknown"
103+
for _, attr := range cve.Attributes {
104+
switch *attr.Key {
105+
case "package_name":
106+
packageName = *attr.Value
107+
case "package_version":
108+
packageVersion = *attr.Value
109+
}
110+
}
111+
p.logger.Printf("- %s %s \n", *cve.Name, strings.Join(containerList, ", "))
112+
p.logger.Printf(" %s::%s (%s)\n",
113+
packageName, packageVersion, *cve.Uri)
103114
if p.logDetail {
104115
p.logger.Printf("\n%s\n", *cve.Description)
105116
}

cli/cage/audit/printer_test.go

Lines changed: 93 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func TestPrinter_Print(t *testing.T) {
149149
}
150150

151151
func TestPrinter_logImageScanFindings(t *testing.T) {
152-
t.Run("does nothing when findings are empty", func(t *testing.T) {
152+
t.Run("returns early when no findings", func(t *testing.T) {
153153
logger := &mockLogger{}
154154
printer := NewPrinter(logger, true, false)
155155
agg := NewAggregater()
@@ -165,32 +165,44 @@ func TestPrinter_logImageScanFindings(t *testing.T) {
165165
logger := &mockLogger{}
166166
printer := NewPrinter(logger, true, false)
167167
agg := NewAggregater()
168-
result := makeScanResult(ecrtypes.FindingSeverityCritical)
169-
agg.Add(result[0])
170168

171-
findings := result[0].ImageScanFindings.Findings
169+
findings := []ecrtypes.ImageScanFinding{
170+
{
171+
Name: aws.String("CVE-2023-0001"),
172+
Uri: aws.String("http://example.com"),
173+
Description: aws.String("Test description"),
174+
Attributes: []ecrtypes.Attribute{},
175+
},
176+
}
177+
172178
printer.logImageScanFindings(ecrtypes.FindingSeverityCritical, findings, agg)
173179

174180
headerFound := false
175181
for _, log := range logger.logs {
176-
if containsString(log, "=== CRITICAL ===") {
182+
if containsString(log, "CRITICAL") && containsString(log, "===") {
177183
headerFound = true
178184
break
179185
}
180186
}
181187
if !headerFound {
182-
t.Error("Expected severity header to be printed")
188+
t.Error("Expected severity header with CRITICAL")
183189
}
184190
})
185191

186192
t.Run("prints CVE name and URI", func(t *testing.T) {
187193
logger := &mockLogger{}
188194
printer := NewPrinter(logger, true, false)
189195
agg := NewAggregater()
190-
result := makeScanResult(ecrtypes.FindingSeverityHigh)
191-
agg.Add(result[0])
192196

193-
findings := result[0].ImageScanFindings.Findings
197+
findings := []ecrtypes.ImageScanFinding{
198+
{
199+
Name: aws.String("CVE-2023-0001"),
200+
Uri: aws.String("http://example.com/cve"),
201+
Description: aws.String("Test description"),
202+
Attributes: []ecrtypes.Attribute{},
203+
},
204+
}
205+
194206
printer.logImageScanFindings(ecrtypes.FindingSeverityHigh, findings, agg)
195207

196208
cveFound := false
@@ -199,114 +211,130 @@ func TestPrinter_logImageScanFindings(t *testing.T) {
199211
if containsString(log, "CVE-2023-0001") {
200212
cveFound = true
201213
}
202-
if containsString(log, "http://example.com") {
214+
if containsString(log, "http://example.com/cve") {
203215
uriFound = true
204216
}
205217
}
206218
if !cveFound {
207-
t.Error("Expected CVE name to be printed")
219+
t.Error("Expected CVE name in output")
208220
}
209221
if !uriFound {
210-
t.Error("Expected CVE URI to be printed")
222+
t.Error("Expected CVE URI in output")
211223
}
212224
})
213225

214-
t.Run("prints container names", func(t *testing.T) {
226+
t.Run("extracts package name and version from attributes", func(t *testing.T) {
215227
logger := &mockLogger{}
216228
printer := NewPrinter(logger, true, false)
217229
agg := NewAggregater()
218-
result := makeScanResult(ecrtypes.FindingSeverityMedium)
219-
agg.Add(result[0])
220230

221-
findings := result[0].ImageScanFindings.Findings
231+
findings := []ecrtypes.ImageScanFinding{
232+
{
233+
Name: aws.String("CVE-2023-0001"),
234+
Uri: aws.String("http://example.com"),
235+
Description: aws.String("Test description"),
236+
Attributes: []ecrtypes.Attribute{
237+
{Key: aws.String("package_name"), Value: aws.String("test-package")},
238+
{Key: aws.String("package_version"), Value: aws.String("1.2.3")},
239+
},
240+
},
241+
}
242+
222243
printer.logImageScanFindings(ecrtypes.FindingSeverityMedium, findings, agg)
223244

224-
containerFound := false
245+
packageFound := false
225246
for _, log := range logger.logs {
226-
if containsString(log, "test-container") {
227-
containerFound = true
247+
if containsString(log, "test-package::1.2.3") {
248+
packageFound = true
228249
break
229250
}
230251
}
231-
if !containerFound {
232-
t.Error("Expected container name to be printed")
252+
if !packageFound {
253+
t.Error("Expected package name and version in output")
233254
}
234255
})
235256

236-
t.Run("prints description when logDetail is true", func(t *testing.T) {
257+
t.Run("uses unknown for missing package info", func(t *testing.T) {
237258
logger := &mockLogger{}
238-
printer := NewPrinter(logger, true, true) // logDetail = true
259+
printer := NewPrinter(logger, true, false)
239260
agg := NewAggregater()
240-
result := makeScanResult(ecrtypes.FindingSeverityCritical)
241-
agg.Add(result[0])
242261

243-
findings := result[0].ImageScanFindings.Findings
244-
printer.logImageScanFindings(ecrtypes.FindingSeverityCritical, findings, agg)
262+
findings := []ecrtypes.ImageScanFinding{
263+
{
264+
Name: aws.String("CVE-2023-0001"),
265+
Uri: aws.String("http://example.com"),
266+
Description: aws.String("Test description"),
267+
Attributes: []ecrtypes.Attribute{},
268+
},
269+
}
270+
271+
printer.logImageScanFindings(ecrtypes.FindingSeverityLow, findings, agg)
245272

246-
descriptionFound := false
273+
unknownFound := false
247274
for _, log := range logger.logs {
248-
if containsString(log, "Test vulnerability description") {
249-
descriptionFound = true
275+
if containsString(log, "unknown::unknown") {
276+
unknownFound = true
250277
break
251278
}
252279
}
253-
if !descriptionFound {
254-
t.Error("Expected description to be printed when logDetail is true")
280+
if !unknownFound {
281+
t.Error("Expected unknown::unknown for missing package info")
255282
}
256283
})
257284

258-
t.Run("does not print description when logDetail is false", func(t *testing.T) {
285+
t.Run("prints description when logDetail is true", func(t *testing.T) {
259286
logger := &mockLogger{}
260-
printer := NewPrinter(logger, true, false) // logDetail = false
287+
printer := NewPrinter(logger, true, true) // logDetail = true
261288
agg := NewAggregater()
262-
result := makeScanResult(ecrtypes.FindingSeverityCritical)
263-
agg.Add(result[0])
264289

265-
findings := result[0].ImageScanFindings.Findings
266-
printer.logImageScanFindings(ecrtypes.FindingSeverityCritical, findings, agg)
290+
findings := []ecrtypes.ImageScanFinding{
291+
{
292+
Name: aws.String("CVE-2023-0001"),
293+
Uri: aws.String("http://example.com"),
294+
Description: aws.String("Detailed vulnerability description"),
295+
Attributes: []ecrtypes.Attribute{},
296+
},
297+
}
267298

268-
descriptionFound := false
299+
printer.logImageScanFindings(ecrtypes.FindingSeverityHigh, findings, agg)
300+
301+
descFound := false
269302
for _, log := range logger.logs {
270-
if containsString(log, "Test vulnerability description") {
271-
descriptionFound = true
303+
if containsString(log, "Detailed vulnerability description") {
304+
descFound = true
272305
break
273306
}
274307
}
275-
if descriptionFound {
276-
t.Error("Expected description NOT to be printed when logDetail is false")
308+
if !descFound {
309+
t.Error("Expected description in output when logDetail is true")
277310
}
278311
})
279312

280-
t.Run("handles multiple findings", func(t *testing.T) {
313+
t.Run("does not print description when logDetail is false", func(t *testing.T) {
281314
logger := &mockLogger{}
282-
printer := NewPrinter(logger, true, false)
315+
printer := NewPrinter(logger, true, false) // logDetail = false
283316
agg := NewAggregater()
284-
result := makeScanResult(
285-
ecrtypes.FindingSeverityHigh,
286-
ecrtypes.FindingSeverityHigh,
287-
ecrtypes.FindingSeverityHigh,
288-
)
289-
agg.Add(result[0])
290317

291-
findings := result[0].ImageScanFindings.Findings
318+
findings := []ecrtypes.ImageScanFinding{
319+
{
320+
Name: aws.String("CVE-2023-0001"),
321+
Uri: aws.String("http://example.com"),
322+
Description: aws.String("Detailed vulnerability description"),
323+
Attributes: []ecrtypes.Attribute{},
324+
},
325+
}
326+
292327
printer.logImageScanFindings(ecrtypes.FindingSeverityHigh, findings, agg)
293328

294-
cve1Found := false
295-
cve2Found := false
296-
cve3Found := false
329+
descFound := false
297330
for _, log := range logger.logs {
298-
if containsString(log, "CVE-2023-0001") {
299-
cve1Found = true
300-
}
301-
if containsString(log, "CVE-2023-0002") {
302-
cve2Found = true
303-
}
304-
if containsString(log, "CVE-2023-0003") {
305-
cve3Found = true
331+
if containsString(log, "Detailed vulnerability description") {
332+
descFound = true
333+
break
306334
}
307335
}
308-
if !cve1Found || !cve2Found || !cve3Found {
309-
t.Error("Expected all three CVEs to be printed")
336+
if descFound {
337+
t.Error("Expected no description in output when logDetail is false")
310338
}
311339
})
312340
}

0 commit comments

Comments
 (0)