Skip to content

Commit 727e963

Browse files
Merge pull request #18 from Checkmarx/cx-shaked-karta-fix-issues
Fixes for AST-106096 and AST-106461 (AST-106096)
2 parents a71282b + a1136a7 commit 727e963

File tree

1 file changed

+42
-10
lines changed

1 file changed

+42
-10
lines changed

pkg/containerResolver/containerScanner.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package containersResolver
22

33
import (
44
"os"
5+
"os/exec"
56
"path/filepath"
7+
"runtime"
68

79
"github.com/Checkmarx/containers-images-extractor/pkg/imagesExtractor"
810
"github.com/Checkmarx/containers-syft-packages-extractor/pkg/syftPackagesExtractor"
@@ -55,20 +57,21 @@ func (cr *ContainersResolver) Resolve(scanPath string, resolutionFolderPath stri
5557
return err
5658
}
5759

58-
//4. get images resolution
60+
//3. get images resolution
5961
resolutionResult, err := cr.AnalyzeImagesWithPlatform(imagesToAnalyze, "linux/amd64")
6062
if err != nil {
6163
log.Err(err).Msg("Could not analyze images.")
6264
return err
6365
}
6466

65-
//5. save to resolution file path (now using .checkmarx folder)
67+
//4. save to resolution file path (now using .checkmarx folder)
6668
err = cr.SaveObjectToFile(checkmarxPath, resolutionResult)
6769
if err != nil {
6870
log.Err(err).Msg("Could not save resolution result.")
6971
return err
7072
}
71-
//6. cleanup files generated folder
73+
74+
//5. cleanup files generated folder
7275
err = cleanup(resolutionFolderPath, outputPath, checkmarxPath)
7376
if err != nil {
7477
log.Err(err).Msg("Could not cleanup resources.")
@@ -83,27 +86,56 @@ func validate(resolutionFolderPath string) (string, error) {
8386
return "", err
8487
}
8588

86-
checkmarxPath := filepath.Join(resolutionFolderPath, ".checkmarx", "containers")
89+
checkmarxFolderPath := filepath.Join(resolutionFolderPath, ".checkmarx")
90+
checkmarxPath := filepath.Join(checkmarxFolderPath, "containers")
8791

8892
err = os.MkdirAll(checkmarxPath, 0755)
8993
if err != nil {
9094
return "", err
9195
}
9296

97+
// Hide the .checkmarx folder on Windows
98+
if runtime.GOOS == "windows" {
99+
err = hideDirectoryOnWindows(checkmarxFolderPath)
100+
if err != nil {
101+
log.Warn().Err(err).Msg("Could not hide .checkmarx folder on Windows")
102+
}
103+
}
104+
93105
return checkmarxPath, nil
94106
}
95107

96108
func cleanup(originalPath string, outputPath string, checkmarxPath string) error {
97-
if outputPath != "" && outputPath != originalPath && checkmarxPath != "" {
98-
err := imagesExtractor.DeleteDirectory(outputPath)
99-
cxErr := imagesExtractor.DeleteDirectory(checkmarxPath)
109+
var err error
100110

111+
// Clean up output path if it's different from original
112+
if outputPath != "" && outputPath != originalPath {
113+
err = imagesExtractor.DeleteDirectory(outputPath)
101114
if err != nil {
102-
return err
115+
log.Warn().Err(err).Msg("Could not delete output directory")
103116
}
117+
}
118+
119+
// Clean up containers folder inside .checkmarx if checkmarxPath is provided
120+
if checkmarxPath != "" {
121+
// checkmarxPath points to .checkmarx/containers, so we delete this directory
122+
cxErr := imagesExtractor.DeleteDirectory(checkmarxPath)
104123
if cxErr != nil {
105-
return cxErr
124+
log.Warn().Err(cxErr).Msg("Could not delete containers directory inside .checkmarx folder")
106125
}
107126
}
108-
return nil
127+
128+
// Only return error from output directory cleanup, not from .checkmarx cleanup
129+
return err
130+
}
131+
132+
// hideDirectoryOnWindows sets the hidden attribute on a directory in Windows
133+
func hideDirectoryOnWindows(dirPath string) error {
134+
if runtime.GOOS != "windows" {
135+
return nil
136+
}
137+
138+
// Use the attrib command to set the hidden attribute
139+
cmd := exec.Command("attrib", "+H", dirPath)
140+
return cmd.Run()
109141
}

0 commit comments

Comments
 (0)