Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,8 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
if unzip {
_ = cleanTempUnzipDirectory(directoryPath)
}
// Clean up .checkmarx/containers directory on container scan error
_ = cleanCheckmarxContainersDirectory(directoryPath)
return "", "", containerResolverError
}
}
Expand All @@ -1950,12 +1952,26 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
logger.PrintIfVerbose("Single container scan triggered: compressing only the container resolution file")
containerResolutionFilePath := filepath.Join(directoryPath, ".checkmarx", "containers", containerResolutionFileName)
zipFilePath, dirPathErr = util.CompressFile(containerResolutionFilePath, containerResolutionFileName, directoryCreationPrefix)

// Clean up .checkmarx/containers directory after successful container scan compression
if dirPathErr == nil {
_ = cleanCheckmarxContainersDirectory(directoryPath)
}
} else {
if !isSbom {
zipFilePath, dirPathErr = compressFolder(directoryPath, sourceDirFilter, userIncludeFilter, scaResolver)
}

// Clean up .checkmarx/containers directory after successful mixed scan (including containers) compression
if dirPathErr == nil && containerScanTriggered && containerResolveLocally {
_ = cleanCheckmarxContainersDirectory(directoryPath)
}
}
if dirPathErr != nil {
// Clean up .checkmarx/containers directory on compression error if container scan was involved
if containerScanTriggered && containerResolveLocally {
_ = cleanCheckmarxContainersDirectory(directoryPath)
}
return "", "", dirPathErr
}

Expand All @@ -1975,6 +1991,24 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
return preSignedURL, zipFilePath, nil
}

// cleanCheckmarxContainersDirectory removes only the .checkmarx/containers directory after container scan completion
func cleanCheckmarxContainersDirectory(directoryPath string) error {
containersPath := filepath.Join(directoryPath, ".checkmarx", "containers")
if _, err := os.Stat(containersPath); os.IsNotExist(err) {
logger.PrintIfVerbose("No .checkmarx/containers directory found to clean up")
return nil
}

logger.PrintIfVerbose("Cleaning up .checkmarx/containers directory after container scan")
err := os.RemoveAll(containersPath)
if err != nil {
logger.PrintIfVerbose(fmt.Sprintf("Warning: Failed to clean up .checkmarx/containers directory: %s", err.Error()))
return errors.Wrapf(err, "Failed to clean up .checkmarx/containers directory")
}
logger.PrintIfVerbose("Successfully cleaned up .checkmarx/containers directory")
return nil
}

func runContainerResolver(cmd *cobra.Command, directoryPath, containerImageFlag string, containerResolveLocally bool) error {
debug, _ := cmd.Flags().GetBool(commonParams.DebugFlag)
var containerImagesList []string
Expand Down
Loading