Skip to content

Commit e4b5e94

Browse files
Refactor container scan command by removing severity and package types flags. Default values for severity and package types are now hardcoded in the buildTrivyArgs function. Updated related tests to reflect these changes.
1 parent df0ffea commit e4b5e94

File tree

2 files changed

+8
-95
lines changed

2 files changed

+8
-95
lines changed

cmd/container_scan.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,10 @@ func getExitCode(err error) int {
7575

7676
// Flag variables for container-scan command
7777
var (
78-
severityFlag string
79-
pkgTypesFlag string
8078
ignoreUnfixedFlag bool
8179
)
8280

8381
func init() {
84-
containerScanCmd.Flags().StringVar(&severityFlag, "severity", "", "Comma-separated list of severities to scan for (default: HIGH,CRITICAL)")
85-
containerScanCmd.Flags().StringVar(&pkgTypesFlag, "pkg-types", "", "Comma-separated list of package types to scan (default: os)")
8682
containerScanCmd.Flags().BoolVar(&ignoreUnfixedFlag, "ignore-unfixed", true, "Ignore unfixed vulnerabilities")
8783
rootCmd.AddCommand(containerScanCmd)
8884
}
@@ -93,19 +89,13 @@ var containerScanCmd = &cobra.Command{
9389
Long: `Scan a container image for vulnerabilities using Trivy.
9490
9591
By default, scans for HIGH and CRITICAL vulnerabilities in OS packages,
96-
ignoring unfixed issues. Use flags to override these defaults.
92+
ignoring unfixed issues.
9793
9894
The --exit-code 1 flag is always applied (not user-configurable) to ensure
9995
the command fails when vulnerabilities are found.`,
10096
Example: ` # Scan an image
10197
codacy-cli container-scan myapp:latest
10298
103-
# Scan only for CRITICAL vulnerabilities
104-
codacy-cli container-scan --severity CRITICAL myapp:latest
105-
106-
# Scan all severities and package types
107-
codacy-cli container-scan --severity LOW,MEDIUM,HIGH,CRITICAL --pkg-types os,library myapp:latest
108-
10999
# Include unfixed vulnerabilities
110100
codacy-cli container-scan --ignore-unfixed=false myapp:latest`,
111101
Args: cobra.ExactArgs(1),
@@ -266,19 +256,8 @@ func buildTrivyArgs(imageName string) []string {
266256
args = append(args, "--ignore-unfixed")
267257
}
268258

269-
// Apply --severity (use default if not specified)
270-
severity := severityFlag
271-
if severity == "" {
272-
severity = "HIGH,CRITICAL"
273-
}
274-
args = append(args, "--severity", severity)
275-
276-
// Apply --pkg-types (use default if not specified)
277-
pkgTypes := pkgTypesFlag
278-
if pkgTypes == "" {
279-
pkgTypes = "os"
280-
}
281-
args = append(args, "--pkg-types", pkgTypes)
259+
// Fixed severity and package types (not user-configurable)
260+
args = append(args, "--severity", "HIGH,CRITICAL", "--pkg-types", "os")
282261

283262
// Always apply --exit-code 1 (not user-configurable)
284263
args = append(args, "--exit-code", "1")

cmd/container_scan_test.go

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010

1111
// MockCommandRunner is a mock implementation of CommandRunner for testing
1212
type MockCommandRunner struct {
13-
RunFunc func(name string, args []string) error
13+
RunFunc func(name string, args []string) error
1414
RunWithStderrFunc func(name string, args []string, stderr io.Writer) error
15-
Calls []struct {
15+
Calls []struct {
1616
Name string
1717
Args []string
1818
}
@@ -41,8 +41,6 @@ type testState struct {
4141
getTrivyPathResolver func() (string, error)
4242
exitFunc func(code int)
4343
commandRunner CommandRunner
44-
severityFlag string
45-
pkgTypesFlag string
4644
ignoreUnfixed bool
4745
}
4846

@@ -51,8 +49,6 @@ func saveState() testState {
5149
getTrivyPathResolver: getTrivyPathResolver,
5250
exitFunc: exitFunc,
5351
commandRunner: commandRunner,
54-
severityFlag: severityFlag,
55-
pkgTypesFlag: pkgTypesFlag,
5652
ignoreUnfixed: ignoreUnfixedFlag,
5753
}
5854
}
@@ -61,8 +57,6 @@ func (s testState) restore() {
6157
getTrivyPathResolver = s.getTrivyPathResolver
6258
exitFunc = s.exitFunc
6359
commandRunner = s.commandRunner
64-
severityFlag = s.severityFlag
65-
pkgTypesFlag = s.pkgTypesFlag
6660
ignoreUnfixedFlag = s.ignoreUnfixed
6761
}
6862

@@ -114,8 +108,6 @@ func TestExecuteContainerScan_Success(t *testing.T) {
114108
commandRunner = mockRunner
115109

116110
// Reset flags to defaults
117-
severityFlag = ""
118-
pkgTypesFlag = ""
119111
ignoreUnfixedFlag = true
120112

121113
exitCode := executeContainerScan("alpine:latest")
@@ -154,8 +146,6 @@ func TestExecuteContainerScan_VulnerabilitiesFound(t *testing.T) {
154146
}
155147
commandRunner = mockRunner
156148

157-
severityFlag = ""
158-
pkgTypesFlag = ""
159149
ignoreUnfixedFlag = true
160150

161151
exitCode := executeContainerScan("alpine:latest")
@@ -207,8 +197,6 @@ func TestExecuteContainerScan_TrivyExecutionError(t *testing.T) {
207197
}
208198
commandRunner = mockRunner
209199

210-
severityFlag = ""
211-
pkgTypesFlag = ""
212200
ignoreUnfixedFlag = true
213201

214202
exitCode := executeContainerScan("alpine:latest")
@@ -235,8 +223,6 @@ func TestExecuteContainerScan_ScanFailureExit1(t *testing.T) {
235223
}
236224
commandRunner = mockRunner
237225

238-
severityFlag = ""
239-
pkgTypesFlag = ""
240226
ignoreUnfixedFlag = true
241227

242228
exitCode := executeContainerScan("random-string")
@@ -271,8 +257,6 @@ func TestHandleTrivyNotFound(t *testing.T) {
271257
type trivyArgsTestCase struct {
272258
name string
273259
imageName string
274-
severity string
275-
pkgTypes string
276260
ignoreUnfixed bool
277261
expectedArgs []string
278262
expectedContains []string
@@ -283,70 +267,35 @@ var trivyArgsTestCases = []trivyArgsTestCase{
283267
{
284268
name: "default flags",
285269
imageName: "myapp:latest",
286-
severity: "",
287-
pkgTypes: "",
288270
ignoreUnfixed: true,
289271
expectedArgs: []string{
290272
"image", "--scanners", "vuln", "--ignore-unfixed",
291273
"--severity", "HIGH,CRITICAL", "--pkg-types", "os",
292274
"--exit-code", "1", "myapp:latest",
293275
},
294276
},
295-
{
296-
name: "custom severity only",
297-
imageName: "codacy/engine:1.0.0",
298-
severity: "CRITICAL",
299-
pkgTypes: "",
300-
ignoreUnfixed: true,
301-
expectedContains: []string{"--severity", "CRITICAL", "--pkg-types", "os", "--ignore-unfixed", "codacy/engine:1.0.0"},
302-
expectedNotContains: []string{"HIGH,CRITICAL"},
303-
},
304-
{
305-
name: "custom pkg-types only",
306-
imageName: "nginx:alpine",
307-
severity: "",
308-
pkgTypes: "os,library",
309-
ignoreUnfixed: true,
310-
expectedContains: []string{"--severity", "HIGH,CRITICAL", "--pkg-types", "os,library", "nginx:alpine"},
311-
},
312-
{
313-
name: "all custom flags",
314-
imageName: "ubuntu:22.04",
315-
severity: "LOW,MEDIUM,HIGH,CRITICAL",
316-
pkgTypes: "os,library",
317-
ignoreUnfixed: true,
318-
expectedContains: []string{"--severity", "LOW,MEDIUM,HIGH,CRITICAL", "--pkg-types", "os,library", "--ignore-unfixed", "ubuntu:22.04"},
319-
},
320277
{
321278
name: "ignore-unfixed disabled",
322279
imageName: "alpine:latest",
323-
severity: "",
324-
pkgTypes: "",
325280
ignoreUnfixed: false,
326281
expectedContains: []string{"--severity", "HIGH,CRITICAL", "--pkg-types", "os", "alpine:latest"},
327282
expectedNotContains: []string{"--ignore-unfixed"},
328283
},
329284
{
330285
name: "exit-code always present",
331286
imageName: "test:v1",
332-
severity: "MEDIUM",
333-
pkgTypes: "library",
334287
ignoreUnfixed: false,
335288
expectedContains: []string{"--exit-code", "1"},
336289
},
337290
{
338291
name: "image with registry prefix",
339292
imageName: "ghcr.io/codacy/codacy-cli:latest",
340-
severity: "",
341-
pkgTypes: "",
342293
ignoreUnfixed: true,
343294
expectedContains: []string{"ghcr.io/codacy/codacy-cli:latest"},
344295
},
345296
{
346297
name: "image with digest",
347298
imageName: "nginx@sha256:abc123",
348-
severity: "",
349-
pkgTypes: "",
350299
ignoreUnfixed: true,
351300
expectedContains: []string{"nginx@sha256:abc123"},
352301
},
@@ -355,8 +304,6 @@ var trivyArgsTestCases = []trivyArgsTestCase{
355304
func TestBuildTrivyArgs(t *testing.T) {
356305
for _, tt := range trivyArgsTestCases {
357306
t.Run(tt.name, func(t *testing.T) {
358-
severityFlag = tt.severity
359-
pkgTypesFlag = tt.pkgTypes
360307
ignoreUnfixedFlag = tt.ignoreUnfixed
361308

362309
args := buildTrivyArgs(tt.imageName)
@@ -386,8 +333,6 @@ func assertTrivyArgsBaseRequirements(t *testing.T, args []string, imageName stri
386333
}
387334

388335
func TestBuildTrivyArgsOrder(t *testing.T) {
389-
severityFlag = ""
390-
pkgTypesFlag = ""
391336
ignoreUnfixedFlag = true
392337

393338
args := buildTrivyArgs("test:latest")
@@ -428,16 +373,9 @@ func TestContainerScanCommandRequiresArg(t *testing.T) {
428373
}
429374

430375
func TestContainerScanFlagDefaults(t *testing.T) {
431-
severityFlagDef := containerScanCmd.Flags().Lookup("severity")
432-
pkgTypesFlagDef := containerScanCmd.Flags().Lookup("pkg-types")
433376
ignoreUnfixedFlagDef := containerScanCmd.Flags().Lookup("ignore-unfixed")
434377

435-
assert.NotNil(t, severityFlagDef, "severity flag should exist")
436-
assert.NotNil(t, pkgTypesFlagDef, "pkg-types flag should exist")
437378
assert.NotNil(t, ignoreUnfixedFlagDef, "ignore-unfixed flag should exist")
438-
439-
assert.Equal(t, "", severityFlagDef.DefValue, "severity default should be empty (uses HIGH,CRITICAL in buildTrivyArgs)")
440-
assert.Equal(t, "", pkgTypesFlagDef.DefValue, "pkg-types default should be empty (uses 'os' in buildTrivyArgs)")
441379
assert.Equal(t, "true", ignoreUnfixedFlagDef.DefValue, "ignore-unfixed default should be true")
442380
}
443381

@@ -498,26 +436,22 @@ func TestValidateImageNameInvalid(t *testing.T) {
498436
}
499437

500438
func TestBuildTrivyArgsDefaultsApplied(t *testing.T) {
501-
severityFlag = ""
502-
pkgTypesFlag = ""
503439
ignoreUnfixedFlag = true
504440

505441
args := buildTrivyArgs("test:latest")
506442

507443
severityIdx := findArgIndex(args, "--severity")
508444
assert.NotEqual(t, -1, severityIdx, "--severity should be present")
509-
assert.Equal(t, "HIGH,CRITICAL", args[severityIdx+1], "Default severity should be HIGH,CRITICAL")
445+
assert.Equal(t, "HIGH,CRITICAL", args[severityIdx+1], "Severity should be HIGH,CRITICAL")
510446

511447
pkgTypesIdx := findArgIndex(args, "--pkg-types")
512448
assert.NotEqual(t, -1, pkgTypesIdx, "--pkg-types should be present")
513-
assert.Equal(t, "os", args[pkgTypesIdx+1], "Default pkg-types should be 'os'")
449+
assert.Equal(t, "os", args[pkgTypesIdx+1], "Pkg-types should be 'os'")
514450

515451
assert.Contains(t, args, "--ignore-unfixed", "--ignore-unfixed should be present when enabled")
516452
}
517453

518454
func TestBuildTrivyArgsWithDifferentImages(t *testing.T) {
519-
severityFlag = "CRITICAL"
520-
pkgTypesFlag = ""
521455
ignoreUnfixedFlag = true
522456

523457
images := []string{"alpine:latest", "nginx:1.21", "redis:7"}
@@ -526,7 +460,7 @@ func TestBuildTrivyArgsWithDifferentImages(t *testing.T) {
526460
args := buildTrivyArgs(img)
527461
assert.Equal(t, img, args[len(args)-1], "Image name should be last argument")
528462
assert.Contains(t, args, "--severity", "Should contain severity flag")
529-
assert.Contains(t, args, "CRITICAL", "Should use configured severity")
463+
assert.Contains(t, args, "HIGH,CRITICAL", "Should use fixed severity HIGH,CRITICAL")
530464
}
531465
}
532466

0 commit comments

Comments
 (0)