Skip to content

Commit 61b08b3

Browse files
empty folder fix
1 parent 4b509b9 commit 61b08b3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

internal/commands/scan.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,17 @@ func getUploadURLFromSource(cmd *cobra.Command, uploadsWrapper wrappers.UploadsW
16561656
logger.PrintIfVerbose("Single container scan triggered: compressing only the container resolution file")
16571657
containerResolutionFilePath := filepath.Join(directoryPath, containerResolutionFileName)
16581658
zipFilePath, dirPathErr = util.CompressFile(containerResolutionFilePath, containerResolutionFileName, directoryCreationPrefix)
1659+
} else if isSingleContainerScanTriggered() && containerImagesFlag != "" {
1660+
logger.PrintIfVerbose("Single container scan with external images: creating minimal zip file")
1661+
// For container scans with external images, we need to create a minimal zip file
1662+
// since the API requires an upload URL even for container-only scans
1663+
zipFilePath, dirPathErr = createMinimalZipFile()
1664+
if unzip {
1665+
dirRemovalErr := cleanTempUnzipDirectory(directoryPath)
1666+
if dirRemovalErr != nil {
1667+
return "", "", dirRemovalErr
1668+
}
1669+
}
16591670
} else {
16601671
zipFilePath, dirPathErr = compressFolder(directoryPath, sourceDirFilter, userIncludeFilter, scaResolver)
16611672
}
@@ -3013,3 +3024,30 @@ func parseArgs(input string) []string {
30133024

30143025
return args
30153026
}
3027+
3028+
// createMinimalZipFile creates a minimal zip file for container scans with external images
3029+
// The API requires an upload URL even for container-only scans, so we create a minimal placeholder
3030+
func createMinimalZipFile() (string, error) {
3031+
outputFile, err := os.CreateTemp(os.TempDir(), "cx-container-*.zip")
3032+
if err != nil {
3033+
return "", errors.Wrapf(err, "Cannot create temp file for container-only scan")
3034+
}
3035+
defer outputFile.Close()
3036+
3037+
zipWriter := zip.NewWriter(outputFile)
3038+
defer zipWriter.Close()
3039+
3040+
// Create a minimal placeholder file
3041+
f, err := zipWriter.Create(".container-scan")
3042+
if err != nil {
3043+
return "", errors.Wrapf(err, "Cannot create placeholder file in zip")
3044+
}
3045+
3046+
// Write minimal content (just a single byte)
3047+
_, err = f.Write([]byte("1"))
3048+
if err != nil {
3049+
return "", errors.Wrapf(err, "Cannot write to placeholder file")
3050+
}
3051+
3052+
return outputFile.Name(), nil
3053+
}

0 commit comments

Comments
 (0)