Skip to content

Commit 73ecdc9

Browse files
Merge pull request #10 from Checkmarx/david/fixes-hidden-folder
Fix dockerfile regex, hidden folder, stereoscope cleanup (AST-88922)
1 parent 76fe587 commit 73ecdc9

File tree

4 files changed

+50
-24
lines changed

4 files changed

+50
-24
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/Checkmarx/containers-resolver
33
go 1.24.1
44

55
require (
6-
github.com/Checkmarx/containers-images-extractor v1.0.7
7-
github.com/Checkmarx/containers-syft-packages-extractor v1.0.10
6+
github.com/Checkmarx/containers-images-extractor v1.0.8
7+
github.com/Checkmarx/containers-syft-packages-extractor v1.0.11
88
github.com/Checkmarx/containers-types v1.0.3
99
github.com/rs/zerolog v1.34.0
1010
github.com/stretchr/testify v1.10.0
@@ -288,4 +288,4 @@ require (
288288
sigs.k8s.io/yaml v1.4.0 // indirect
289289
)
290290

291-
replace google.golang.org/protobuf => google.golang.org/protobuf v1.33.0
291+
replace google.golang.org/protobuf => google.golang.org/protobuf v1.33.0

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi
6161
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
6262
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
6363
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
64-
github.com/Checkmarx/containers-images-extractor v1.0.7 h1:lLgaDFFqz1jksN3/d/6sLXO0C0ODbt9xExbt44YMEOg=
65-
github.com/Checkmarx/containers-images-extractor v1.0.7/go.mod h1:ZtOqhzlErPr2QL9xGjMmxwGvzXUwi+G5BBeOfdY62Ug=
66-
github.com/Checkmarx/containers-syft-packages-extractor v1.0.10 h1:35n22bjH2Tx5+B8vcqIHogHeEWOQrT2lUf4uaIjoENw=
67-
github.com/Checkmarx/containers-syft-packages-extractor v1.0.10/go.mod h1:F9FFBVNmogF0wR9SVI0wRU9dZ9Ux3IZtZl3T24sQ/8E=
64+
github.com/Checkmarx/containers-images-extractor v1.0.8 h1:X9EYkQKVjhNWcm0VCEFEF4/3O2wXM2QQtSTv5bDxm/I=
65+
github.com/Checkmarx/containers-images-extractor v1.0.8/go.mod h1:ZtOqhzlErPr2QL9xGjMmxwGvzXUwi+G5BBeOfdY62Ug=
66+
github.com/Checkmarx/containers-syft-packages-extractor v1.0.11 h1:mXQMz9a68DiP2Pwi4Dwj+ysPGHtGCOdouPSeA9u/Wi0=
67+
github.com/Checkmarx/containers-syft-packages-extractor v1.0.11/go.mod h1:F9FFBVNmogF0wR9SVI0wRU9dZ9Ux3IZtZl3T24sQ/8E=
6868
github.com/Checkmarx/containers-types v1.0.3 h1:srk+RQnyPXyFKmVHA6P9SQZAtjczyndZ1aa0CWF/6/0=
6969
github.com/Checkmarx/containers-types v1.0.3/go.mod h1:F13rfevriqYHR+0ahk3W9H8uLK0Msbts012f1pIxJb0=
7070
github.com/CycloneDX/cyclonedx-go v0.9.2 h1:688QHn2X/5nRezKe2ueIVCt+NRqf7fl3AVQk+vaFcIo=

pkg/containerResolver/containerScanner.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package containersResolver
22

33
import (
4+
"os"
5+
"path/filepath"
6+
47
"github.com/Checkmarx/containers-images-extractor/pkg/imagesExtractor"
58
"github.com/Checkmarx/containers-syft-packages-extractor/pkg/syftPackagesExtractor"
69
"github.com/Checkmarx/containers-types/types"
@@ -30,10 +33,10 @@ func (cr *ContainersResolver) Resolve(scanPath string, resolutionFolderPath stri
3033
}
3134
log.Debug().Msgf("Resolve func parameters: scanPath=%s, resolutionFolderPath=%s, images=%s, isDebug=%t", scanPath, resolutionFolderPath, images, isDebug)
3235

33-
// 0. validate input
34-
err := validate(resolutionFolderPath)
36+
// 0. validate input and create .checkmarx folder
37+
checkmarxPath, err := validate(resolutionFolderPath)
3538
if err != nil {
36-
log.Err(err).Msg("Resolution Path is not valid.")
39+
log.Err(err).Msg("Resolution Path is not valid or could not create .checkmarx folder.")
3740
return err
3841
}
3942

@@ -59,35 +62,48 @@ func (cr *ContainersResolver) Resolve(scanPath string, resolutionFolderPath stri
5962
return err
6063
}
6164

62-
//5. save to resolution file path
63-
err = cr.SaveObjectToFile(resolutionFolderPath, resolutionResult)
65+
//5. save to resolution file path (now using .checkmarx folder)
66+
err = cr.SaveObjectToFile(checkmarxPath, resolutionResult)
6467
if err != nil {
6568
log.Err(err).Msg("Could not save resolution result.")
6669
return err
6770
}
6871
//6. cleanup files generated folder
69-
err = cleanup(resolutionFolderPath, outputPath)
72+
err = cleanup(resolutionFolderPath, outputPath, checkmarxPath)
7073
if err != nil {
7174
log.Err(err).Msg("Could not cleanup resources.")
7275
return err
7376
}
7477
return nil
7578
}
7679

77-
func validate(resolutionFolderPath string) error {
80+
func validate(resolutionFolderPath string) (string, error) {
7881
isValidFolderPath, err := imagesExtractor.IsValidFolderPath(resolutionFolderPath)
7982
if err != nil || isValidFolderPath == false {
80-
return err
83+
return "", err
8184
}
82-
return nil
85+
86+
checkmarxPath := filepath.Join(resolutionFolderPath, ".checkmarx", "containers")
87+
88+
err = os.MkdirAll(checkmarxPath, 0755)
89+
if err != nil {
90+
return "", err
91+
}
92+
93+
return checkmarxPath, nil
8394
}
8495

85-
func cleanup(originalPath string, outputPath string) error {
86-
if outputPath != "" && outputPath != originalPath {
96+
func cleanup(originalPath string, outputPath string, checkmarxPath string) error {
97+
if outputPath != "" && outputPath != originalPath && checkmarxPath != "" {
8798
err := imagesExtractor.DeleteDirectory(outputPath)
99+
cxErr := imagesExtractor.DeleteDirectory(checkmarxPath)
100+
88101
if err != nil {
89102
return err
90103
}
104+
if cxErr != nil {
105+
return cxErr
106+
}
91107
}
92108
return nil
93109
}

pkg/containerResolver/containerScanner_test.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ package containersResolver_test
44

55
import (
66
"errors"
7-
"github.com/Checkmarx/containers-resolver/pkg/containerResolver"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
containersResolver "github.com/Checkmarx/containers-resolver/pkg/containerResolver"
812
"github.com/Checkmarx/containers-syft-packages-extractor/pkg/syftPackagesExtractor"
913
"github.com/Checkmarx/containers-types/types"
1014
"github.com/rs/zerolog/log"
1115
"github.com/stretchr/testify/assert"
1216
"github.com/stretchr/testify/mock"
13-
"os"
14-
"testing"
1517
)
1618

1719
// Mock for ImagesExtractorInterface
@@ -92,7 +94,7 @@ func TestResolve(t *testing.T) {
9294
expectedResolution := []*syftPackagesExtractor.ContainerResolution{
9395
{
9496
ContainerImage: syftPackagesExtractor.ContainerImage{
95-
ImageName: "image1",
97+
ImageName: "image1:blabla",
9698
ImageTag: "latest",
9799
Distribution: "debian",
98100
ImageHash: "sha256:123abc",
@@ -118,6 +120,8 @@ func TestResolve(t *testing.T) {
118120
}
119121

120122
t.Run("Success scenario", func(t *testing.T) {
123+
checkmarxPath := filepath.Join(resolutionFolderPath, ".checkmarx", "containers")
124+
createTestFolder(checkmarxPath)
121125

122126
mockImagesExtractor.On("ExtractFiles", scanPath).
123127
Return(sampleFileImages, map[string]map[string]string{"settings.json": {"key": "value"}}, "/output/path", nil)
@@ -127,15 +131,15 @@ func TestResolve(t *testing.T) {
127131
map[string]map[string]string{"settings.json": {"key": "value"}}).
128132
Return([]types.ImageModel{{Name: "image1"}}, nil)
129133
mockSyftPackagesExtractor.On("AnalyzeImages", mock.Anything).Return(expectedResolution, nil)
130-
mockImagesExtractor.On("SaveObjectToFile", resolutionFolderPath, expectedResolution).Return(nil)
134+
mockImagesExtractor.On("SaveObjectToFile", checkmarxPath, expectedResolution).Return(nil)
131135

132136
err := resolver.Resolve(scanPath, resolutionFolderPath, images, true)
133137
assert.NoError(t, err)
134138

135139
mockImagesExtractor.AssertCalled(t, "ExtractFiles", scanPath)
136140
mockImagesExtractor.AssertCalled(t, "ExtractAndMergeImagesFromFiles", sampleFileImages, mock.Anything, mock.Anything)
137141
mockSyftPackagesExtractor.AssertCalled(t, "AnalyzeImages", mock.Anything)
138-
mockImagesExtractor.AssertCalled(t, "SaveObjectToFile", resolutionFolderPath, expectedResolution)
142+
mockImagesExtractor.AssertCalled(t, "SaveObjectToFile", checkmarxPath, expectedResolution)
139143
})
140144

141145
t.Run("ScanPath Validation failure", func(t *testing.T) {
@@ -151,6 +155,9 @@ func TestResolve(t *testing.T) {
151155
mockImagesExtractor.ExpectedCalls = nil
152156
mockImagesExtractor.Calls = nil
153157

158+
checkmarxPath := filepath.Join(resolutionFolderPath, ".checkmarx", "containers")
159+
createTestFolder(checkmarxPath)
160+
154161
mockImagesExtractor.On("ExtractFiles", scanPath).
155162
Return(sampleFileImages, map[string]map[string]string{"settings.json": {"key": "value"}}, "/output/path",
156163
errors.New("invalid path"))
@@ -167,6 +174,9 @@ func TestResolve(t *testing.T) {
167174
mockSyftPackagesExtractor.ExpectedCalls = nil
168175
mockSyftPackagesExtractor.Calls = nil
169176

177+
checkmarxPath := filepath.Join(resolutionFolderPath, ".checkmarx", "containers")
178+
createTestFolder(checkmarxPath)
179+
170180
mockImagesExtractor.On("ExtractFiles", scanPath).
171181
Return(sampleFileImages, map[string]map[string]string{"settings.json": {"key": "value"}}, "/output/path", nil)
172182

0 commit comments

Comments
 (0)