Skip to content

Commit 68f4c81

Browse files
author
Checkmarx Automation
committed
Enhance container security scan functionality with detailed validation and helper functions
- Introduce comprehensive validation for container image formats, including image:tag, tar files, and various prefixes (docker:, podman:, etc.). - Add detailed comments to clarify the purpose and functionality of key functions related to container image processing. - Implement helper functions for prefix extraction and validation, improving code readability and maintainability. - Ensure all new functions are aligned with container-security scan-type requirements.
1 parent 5e2934c commit 68f4c81

2 files changed

Lines changed: 47 additions & 7 deletions

File tree

internal/commands/scan.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,10 @@ func addScaScan(cmd *cobra.Command, resubmitConfig []wrappers.Config, hasContain
11871187
return nil
11881188
}
11891189

1190+
// addContainersScan creates the container security scan configuration with validation.
1191+
// Container-security scan-type related function.
1192+
// This function validates all --container-images inputs including tar files, image:tag formats,
1193+
// and various prefixed formats (docker:, podman:, file:, etc.) before creating the scan config.
11901194
func addContainersScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) (map[string]interface{}, error) {
11911195
if !scanTypeEnabled(commonParams.ContainersType) {
11921196
return nil, nil
@@ -1255,6 +1259,8 @@ func addContainersScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) (ma
12551259
return containerMapConfig, nil
12561260
}
12571261

1262+
// initializeContainersConfigWithResubmitValues populates container config from previous scan settings.
1263+
// Container-security scan-type related function.
12581264
func initializeContainersConfigWithResubmitValues(resubmitConfig []wrappers.Config, containerConfig *wrappers.ContainerConfig, containerResolveLocally, isGitScan bool) {
12591265
for _, config := range resubmitConfig {
12601266
if config.Type != commonParams.ContainersType {
@@ -2015,7 +2021,9 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
20152021
return preSignedURL, zipFilePath, nil
20162022
}
20172023

2018-
// cleanCheckmarxContainersDirectory removes only the .checkmarx/containers directory after container scan completion
2024+
// cleanCheckmarxContainersDirectory removes only the .checkmarx/containers directory after container scan completion.
2025+
// Container-security scan-type related function.
2026+
// This function performs cleanup of temporary container scan artifacts.
20192027
func cleanCheckmarxContainersDirectory(directoryPath string) error {
20202028
containersPath := filepath.Join(directoryPath, ".checkmarx", "containers")
20212029
if _, err := os.Stat(containersPath); os.IsNotExist(err) {
@@ -2033,6 +2041,9 @@ func cleanCheckmarxContainersDirectory(directoryPath string) error {
20332041
return nil
20342042
}
20352043

2044+
// runContainerResolver executes the container resolver to analyze container images locally.
2045+
// Container-security scan-type related function.
2046+
// This function processes and normalizes container image inputs before passing them to the resolver.
20362047
func runContainerResolver(cmd *cobra.Command, directoryPath, containerImageFlag string, containerResolveLocally bool) error {
20372048
debug, _ := cmd.Flags().GetBool(commonParams.DebugFlag)
20382049
var containerImagesList []string
@@ -2065,7 +2076,10 @@ func runContainerResolver(cmd *cobra.Command, directoryPath, containerImageFlag
20652076
return nil
20662077
}
20672078

2068-
// processContainerImagesForSyft processes container image references using syft's scheme extraction logic
2079+
// processContainerImagesForSyft processes container image references using syft's scheme extraction logic.
2080+
// Container-security scan-type related function.
2081+
// This function strips known prefixes (docker:, podman:, file:, etc.) from image references
2082+
// to match syft/stereoscope's expected input format.
20692083
func processContainerImagesForSyft(images []string) []string {
20702084
var processedImages []string
20712085

@@ -2094,7 +2108,9 @@ func processContainerImagesForSyft(images []string) []string {
20942108
return processedImages
20952109
}
20962110

2097-
// extractSchemeSource mimics stereoscope.ExtractSchemeSource behavior
2111+
// extractSchemeSource mimics stereoscope.ExtractSchemeSource behavior.
2112+
// Container-security scan-type related function.
2113+
// This function extracts and validates source prefixes from container image references.
20982114
func extractSchemeSource(userInput string, sources []string) (source, newInput string) {
20992115
const SchemeSeparator = ":"
21002116
parts := strings.SplitN(userInput, SchemeSeparator, 2)
@@ -3400,6 +3416,13 @@ func validateCreateScanFlags(cmd *cobra.Command) error {
34003416
return nil
34013417
}
34023418

3419+
// validateContainerImageFormat validates container image references for the --container-images flag.
3420+
// Container-security scan-type related function.
3421+
// This function implements comprehensive validation logic for all supported container image formats:
3422+
// - Standard image:tag format
3423+
// - Tar files (.tar)
3424+
// - Prefixed formats (docker:, podman:, containerd:, registry:, docker-archive:, oci-archive:, oci-dir:, file:)
3425+
// It provides helpful error messages and hints for common user mistakes.
34033426
func validateContainerImageFormat(containerImage string) error {
34043427
// Define known sources (prefixes) for container image references
34053428
knownSources := []string{
@@ -3487,7 +3510,9 @@ func validateContainerImageFormat(containerImage string) error {
34873510
return errors.Errorf("--container-images flag error: image does not have a tag")
34883511
}
34893512

3490-
// Helper function to get the prefix from input
3513+
// getPrefixFromInput extracts the prefix from a container image reference.
3514+
// Container-security scan-type related function.
3515+
// Helper function to identify which known prefix is used in the input.
34913516
func getPrefixFromInput(input string, prefixes []string) string {
34923517
for _, prefix := range prefixes {
34933518
if strings.HasPrefix(input, prefix) {
@@ -3497,6 +3522,10 @@ func getPrefixFromInput(input string, prefixes []string) string {
34973522
return ""
34983523
}
34993524

3525+
// validatePrefixedContainerImage validates container image references with specific prefixes.
3526+
// Container-security scan-type related function.
3527+
// This function handles prefix-specific validation for archive types (file:, docker-archive:, oci-archive:),
3528+
// daemon types (docker:, podman:, containerd:), registry types, and oci-dir types.
35003529
func validatePrefixedContainerImage(containerImage, prefix string) error {
35013530
// Remove the prefix to get the actual image reference
35023531
imageRef := strings.TrimPrefix(containerImage, prefix)

internal/commands/scan_test.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,9 @@ func Test_validateThresholds(t *testing.T) {
21772177
}
21782178
}
21792179

2180+
// TestValidateContainerImageFormat tests the basic validation logic for container image formats.
2181+
// Container-security scan-type related test function.
2182+
// This test covers traditional image:tag formats, tar files, and various error cases.
21802183
func TestValidateContainerImageFormat(t *testing.T) {
21812184
var traditionalErrorMessage = "Invalid value for --container-images flag. The value must be in the format <image-name>:<image-tag>, <image-name>.tar, or use a supported prefix (docker:, podman:, containerd:, registry:, docker-archive:, oci-archive:, oci-dir:, file:)"
21822185

@@ -2456,7 +2459,10 @@ func TestValidateContainerImageFormat(t *testing.T) {
24562459
}
24572460

24582461
// TestValidateContainerImageFormat_Comprehensive tests the complete validation logic
2459-
// including input normalization, helpful hints, and all error cases
2462+
// including input normalization, helpful hints, and all error cases.
2463+
// Container-security scan-type related test function.
2464+
// This test validates all supported container image formats, prefixes, tar files,
2465+
// error messages, and helpful hints for the --container-images flag.
24602466
func TestValidateContainerImageFormat_Comprehensive(t *testing.T) {
24612467
testCases := []struct {
24622468
name string
@@ -2733,7 +2739,10 @@ func TestValidateContainerImageFormat_Comprehensive(t *testing.T) {
27332739
}
27342740
}
27352741

2736-
// TestInputNormalization tests the space and quote trimming logic
2742+
// TestInputNormalization tests the space and quote trimming logic.
2743+
// Container-security scan-type related test function.
2744+
// This test validates input normalization for comma-separated container image lists,
2745+
// including space trimming, quote handling, and empty entry filtering.
27372746
func TestInputNormalization(t *testing.T) {
27382747
testCases := []struct {
27392748
name string
@@ -2823,7 +2832,9 @@ func TestInputNormalization(t *testing.T) {
28232832
}
28242833
}
28252834

2826-
// setupTestFilesAndDirs creates temporary files and directories for testing
2835+
// setupTestFilesAndDirs creates temporary files and directories for testing.
2836+
// Container-security scan-type related test helper function.
2837+
// This helper creates test files (like .tar files) and directories needed for container image validation tests.
28272838
func setupTestFilesAndDirs(t *testing.T, files []string, dirs []string) []func() {
28282839
var cleanupFuncs []func()
28292840

0 commit comments

Comments
 (0)