@@ -2,7 +2,9 @@ package containersResolver
2
2
3
3
import (
4
4
"os"
5
+ "os/exec"
5
6
"path/filepath"
7
+ "runtime"
6
8
7
9
"github.com/Checkmarx/containers-images-extractor/pkg/imagesExtractor"
8
10
"github.com/Checkmarx/containers-syft-packages-extractor/pkg/syftPackagesExtractor"
@@ -55,20 +57,21 @@ func (cr *ContainersResolver) Resolve(scanPath string, resolutionFolderPath stri
55
57
return err
56
58
}
57
59
58
- //4 . get images resolution
60
+ //3 . get images resolution
59
61
resolutionResult , err := cr .AnalyzeImagesWithPlatform (imagesToAnalyze , "linux/amd64" )
60
62
if err != nil {
61
63
log .Err (err ).Msg ("Could not analyze images." )
62
64
return err
63
65
}
64
66
65
- //5 . save to resolution file path (now using .checkmarx folder)
67
+ //4 . save to resolution file path (now using .checkmarx folder)
66
68
err = cr .SaveObjectToFile (checkmarxPath , resolutionResult )
67
69
if err != nil {
68
70
log .Err (err ).Msg ("Could not save resolution result." )
69
71
return err
70
72
}
71
- //6. cleanup files generated folder
73
+
74
+ //5. cleanup files generated folder
72
75
err = cleanup (resolutionFolderPath , outputPath , checkmarxPath )
73
76
if err != nil {
74
77
log .Err (err ).Msg ("Could not cleanup resources." )
@@ -83,27 +86,56 @@ func validate(resolutionFolderPath string) (string, error) {
83
86
return "" , err
84
87
}
85
88
86
- checkmarxPath := filepath .Join (resolutionFolderPath , ".checkmarx" , "containers" )
89
+ checkmarxFolderPath := filepath .Join (resolutionFolderPath , ".checkmarx" )
90
+ checkmarxPath := filepath .Join (checkmarxFolderPath , "containers" )
87
91
88
92
err = os .MkdirAll (checkmarxPath , 0755 )
89
93
if err != nil {
90
94
return "" , err
91
95
}
92
96
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
+
93
105
return checkmarxPath , nil
94
106
}
95
107
96
108
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
100
110
111
+ // Clean up output path if it's different from original
112
+ if outputPath != "" && outputPath != originalPath {
113
+ err = imagesExtractor .DeleteDirectory (outputPath )
101
114
if err != nil {
102
- return err
115
+ log . Warn (). Err ( err ). Msg ( "Could not delete output directory" )
103
116
}
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 )
104
123
if cxErr != nil {
105
- return cxErr
124
+ log . Warn (). Err ( cxErr ). Msg ( "Could not delete containers directory inside .checkmarx folder" )
106
125
}
107
126
}
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 ()
109
141
}
0 commit comments