Skip to content

Commit 90eeb94

Browse files
Merge pull request #1305 from Checkmarx/bug/containers-temp-folder-delete
Containers - checkmarx folder deleted (AST-106461)
2 parents 9cd3a2c + 64c6512 commit 90eeb94

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

internal/commands/scan.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,8 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
19421942
if unzip {
19431943
_ = cleanTempUnzipDirectory(directoryPath)
19441944
}
1945+
// Clean up .checkmarx/containers directory on container scan error
1946+
_ = cleanCheckmarxContainersDirectory(directoryPath)
19451947
return "", "", containerResolverError
19461948
}
19471949
}
@@ -1950,12 +1952,26 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
19501952
logger.PrintIfVerbose("Single container scan triggered: compressing only the container resolution file")
19511953
containerResolutionFilePath := filepath.Join(directoryPath, ".checkmarx", "containers", containerResolutionFileName)
19521954
zipFilePath, dirPathErr = util.CompressFile(containerResolutionFilePath, containerResolutionFileName, directoryCreationPrefix)
1955+
1956+
// Clean up .checkmarx/containers directory after successful container scan compression
1957+
if dirPathErr == nil {
1958+
_ = cleanCheckmarxContainersDirectory(directoryPath)
1959+
}
19531960
} else {
19541961
if !isSbom {
19551962
zipFilePath, dirPathErr = compressFolder(directoryPath, sourceDirFilter, userIncludeFilter, scaResolver)
19561963
}
1964+
1965+
// Clean up .checkmarx/containers directory after successful mixed scan (including containers) compression
1966+
if dirPathErr == nil && containerScanTriggered && containerResolveLocally {
1967+
_ = cleanCheckmarxContainersDirectory(directoryPath)
1968+
}
19571969
}
19581970
if dirPathErr != nil {
1971+
// Clean up .checkmarx/containers directory on compression error if container scan was involved
1972+
if containerScanTriggered && containerResolveLocally {
1973+
_ = cleanCheckmarxContainersDirectory(directoryPath)
1974+
}
19591975
return "", "", dirPathErr
19601976
}
19611977

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

1994+
// cleanCheckmarxContainersDirectory removes only the .checkmarx/containers directory after container scan completion
1995+
func cleanCheckmarxContainersDirectory(directoryPath string) error {
1996+
containersPath := filepath.Join(directoryPath, ".checkmarx", "containers")
1997+
if _, err := os.Stat(containersPath); os.IsNotExist(err) {
1998+
logger.PrintIfVerbose("No .checkmarx/containers directory found to clean up")
1999+
return nil
2000+
}
2001+
2002+
logger.PrintIfVerbose("Cleaning up .checkmarx/containers directory after container scan")
2003+
err := os.RemoveAll(containersPath)
2004+
if err != nil {
2005+
logger.PrintIfVerbose(fmt.Sprintf("Warning: Failed to clean up .checkmarx/containers directory: %s", err.Error()))
2006+
return errors.Wrapf(err, "Failed to clean up .checkmarx/containers directory")
2007+
}
2008+
logger.PrintIfVerbose("Successfully cleaned up .checkmarx/containers directory")
2009+
return nil
2010+
}
2011+
19782012
func runContainerResolver(cmd *cobra.Command, directoryPath, containerImageFlag string, containerResolveLocally bool) error {
19792013
debug, _ := cmd.Flags().GetBool(commonParams.DebugFlag)
19802014
var containerImagesList []string

0 commit comments

Comments
 (0)