Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,10 @@
containerConfig := wrappers.ContainerConfig{}

containerResolveLocally, _ := cmd.Flags().GetBool(commonParams.ContainerResolveLocallyFlag)
initializeContainersConfigWithResubmitValues(resubmitConfig, &containerConfig, containerResolveLocally)
source, _ := cmd.Flags().GetString(commonParams.SourcesFlag)
sourceTrimmed := strings.TrimSpace(source)
isGitScan := util.IsGitURL(sourceTrimmed)
initializeContainersConfigWithResubmitValues(resubmitConfig, &containerConfig, containerResolveLocally, isGitScan)

fileFolderFilter, _ := cmd.PersistentFlags().GetString(commonParams.ContainersFileFolderFilterFlag)
if fileFolderFilter != "" {
Expand All @@ -1201,7 +1204,7 @@
containerConfig.ImagesFilter = imageTagFilter
}
userCustomImages, _ := cmd.Flags().GetString(commonParams.ContainerImagesFlag)
if userCustomImages != "" && !containerResolveLocally {
if userCustomImages != "" && (!containerResolveLocally || isGitScan) {
containerImagesList := strings.Split(strings.TrimSpace(userCustomImages), ",")
for _, containerImageName := range containerImagesList {
if containerImagesErr := validateContainerImageFormat(containerImageName); containerImagesErr != nil {
Expand All @@ -1216,7 +1219,7 @@
return containerMapConfig, nil
}

func initializeContainersConfigWithResubmitValues(resubmitConfig []wrappers.Config, containerConfig *wrappers.ContainerConfig, containerResolveLocally bool) {
func initializeContainersConfigWithResubmitValues(resubmitConfig []wrappers.Config, containerConfig *wrappers.ContainerConfig, containerResolveLocally bool, isGitScan bool) {

Check failure on line 1222 in internal/commands/scan.go

View workflow job for this annotation

GitHub Actions / lint

paramTypeCombine: func(resubmitConfig []wrappers.Config, containerConfig *wrappers.ContainerConfig, containerResolveLocally bool, isGitScan bool) could be replaced with func(resubmitConfig []wrappers.Config, containerConfig *wrappers.ContainerConfig, containerResolveLocally, isGitScan bool) (gocritic)
for _, config := range resubmitConfig {
if config.Type != commonParams.ContainersType {
continue
Expand All @@ -1238,7 +1241,7 @@
containerConfig.ImagesFilter = resubmitImagesFilter.(string)
}
resubmitUserCustomImages := config.Value[ConfigUserCustomImagesKey]
if resubmitUserCustomImages != nil && resubmitUserCustomImages != "" && !containerResolveLocally {
if resubmitUserCustomImages != nil && resubmitUserCustomImages != "" && (!containerResolveLocally || isGitScan) {
containerConfig.UserCustomImages = resubmitUserCustomImages.(string)
}
}
Expand Down
106 changes: 104 additions & 2 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,12 +1852,95 @@ func TestAddContainersScan_WithCustomImages_ShouldSetUserCustomImages(t *testing
expectedImages, containerMapConfig.UserCustomImages)
}

func TestAddContainersScan_GitScanWithResolveLocallyAndCustomImages_ShouldSetUserCustomImages(t *testing.T) {
// Setup
var resubmitConfig []wrappers.Config

// Create command with container flags
cmdCommand := &cobra.Command{}
cmdCommand.Flags().String(commonParams.ContainerImagesFlag, "", "Container images")
cmdCommand.Flags().Bool(commonParams.ContainerResolveLocallyFlag, false, "Resolve containers locally")
cmdCommand.Flags().String(commonParams.SourcesFlag, "", "Source")

// Set test values for git scan with resolve locally and custom images
expectedImages := "artifactory.company.com/repo/image1:latest,artifactory.company.com/repo/image2:1.0.3"
gitURL := "https://github.com/user/repo.git"
_ = cmdCommand.Flags().Set(commonParams.ContainerImagesFlag, expectedImages)
_ = cmdCommand.Flags().Set(commonParams.ContainerResolveLocallyFlag, "true")
_ = cmdCommand.Flags().Set(commonParams.SourcesFlag, gitURL)

// Enable container scan type
originalScanTypes := actualScanTypes
actualScanTypes = commonParams.ContainersType
defer func() {
actualScanTypes = originalScanTypes
}()

// Execute
result, err := addContainersScan(cmdCommand, resubmitConfig)

// Verify no error occurred
assert.NilError(t, err)
assert.Assert(t, result != nil, "Expected result to not be nil")

// Verify
containerMapConfig, ok := result[resultsMapValue].(*wrappers.ContainerConfig)
assert.Assert(t, ok, "Expected result to contain a ContainerConfig")

// Check that the UserCustomImages field was correctly set even with resolve locally true (because it's a git scan)
assert.Equal(t, containerMapConfig.UserCustomImages, expectedImages,
"Expected UserCustomImages to be set to '%s' for git scan even with resolve locally, but got '%s'",
expectedImages, containerMapConfig.UserCustomImages)
}

func TestAddContainersScan_UploadScanWithResolveLocallyAndCustomImages_ShouldNotSetUserCustomImages(t *testing.T) {
// Setup
var resubmitConfig []wrappers.Config

// Create command with container flags
cmdCommand := &cobra.Command{}
cmdCommand.Flags().String(commonParams.ContainerImagesFlag, "", "Container images")
cmdCommand.Flags().Bool(commonParams.ContainerResolveLocallyFlag, false, "Resolve containers locally")
cmdCommand.Flags().String(commonParams.SourcesFlag, "", "Source")

// Set test values for upload scan (local path) with resolve locally and custom images
customImages := "artifactory.company.com/repo/image1:latest,artifactory.company.com/repo/image2:1.0.3"
localPath := "/path/to/local/directory"
_ = cmdCommand.Flags().Set(commonParams.ContainerImagesFlag, customImages)
_ = cmdCommand.Flags().Set(commonParams.ContainerResolveLocallyFlag, "true")
_ = cmdCommand.Flags().Set(commonParams.SourcesFlag, localPath)

// Enable container scan type
originalScanTypes := actualScanTypes
actualScanTypes = commonParams.ContainersType
defer func() {
actualScanTypes = originalScanTypes
}()

// Execute
result, err := addContainersScan(cmdCommand, resubmitConfig)

// Verify no error occurred
assert.NilError(t, err)
assert.Assert(t, result != nil, "Expected result to not be nil")

// Verify
containerMapConfig, ok := result[resultsMapValue].(*wrappers.ContainerConfig)
assert.Assert(t, ok, "Expected result to contain a ContainerConfig")

// Check that the UserCustomImages field was NOT set for upload scan with resolve locally
assert.Equal(t, containerMapConfig.UserCustomImages, "",
"Expected UserCustomImages to be empty for upload scan with resolve locally, but got '%s'",
containerMapConfig.UserCustomImages)
}

func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testing.T) {
// Define test cases
testCases := []struct {
name string
resubmitConfig []wrappers.Config
containerResolveLocally bool
isGitScan bool
expectedCustomImages string
}{
{
Expand All @@ -1871,10 +1954,11 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
},
},
containerResolveLocally: false,
isGitScan: false,
expectedCustomImages: "image1:tag1,image2:tag2",
},
{
name: "When UserCustomImages is valid string and ContainerResolveLocally is true, it should not be set in containerConfig",
name: "When UserCustomImages is valid string and ContainerResolveLocally is true (upload scan), it should not be set in containerConfig",
resubmitConfig: []wrappers.Config{
{
Type: commonParams.ContainersType,
Expand All @@ -1884,8 +1968,23 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
},
},
containerResolveLocally: true,
isGitScan: false,
expectedCustomImages: "",
},
{
name: "When UserCustomImages is valid string and ContainerResolveLocally is true but is git scan, it should be set in containerConfig",
resubmitConfig: []wrappers.Config{
{
Type: commonParams.ContainersType,
Value: map[string]interface{}{
ConfigUserCustomImagesKey: "image1:tag1,image2:tag2",
},
},
},
containerResolveLocally: true,
isGitScan: true,
expectedCustomImages: "image1:tag1,image2:tag2",
},
{
name: "When UserCustomImages is empty string, containerConfig should not be updated",
resubmitConfig: []wrappers.Config{
Expand All @@ -1897,6 +1996,7 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
},
},
containerResolveLocally: false,
isGitScan: false,
expectedCustomImages: "",
},
{
Expand All @@ -1910,6 +2010,7 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
},
},
containerResolveLocally: false,
isGitScan: false,
expectedCustomImages: "",
},
{
Expand All @@ -1921,6 +2022,7 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
},
},
containerResolveLocally: false,
isGitScan: false,
expectedCustomImages: "",
},
}
Expand All @@ -1932,7 +2034,7 @@ func TestInitializeContainersConfigWithResubmitValues_UserCustomImages(t *testin
containerConfig := &wrappers.ContainerConfig{}

// Call the function under test
initializeContainersConfigWithResubmitValues(tc.resubmitConfig, containerConfig, tc.containerResolveLocally)
initializeContainersConfigWithResubmitValues(tc.resubmitConfig, containerConfig, tc.containerResolveLocally, tc.isGitScan)

// Assert the result
assert.Equal(t, tc.expectedCustomImages, containerConfig.UserCustomImages,
Expand Down
Loading