Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI | Support Container Engine Scan (AST-36237, AST-36236, AST-36238) #672

Closed
wants to merge 99 commits into from

Conversation

AlvoBen
Copy link
Collaborator

@AlvoBen AlvoBen commented Mar 6, 2024

By submitting a PR to this repository, you agree to the terms within the Checkmarx Code of Conduct. Please see the contributing guidelines for how to create and submit a high-quality PR for this repo.

Description

Start Container Scan - Trigger Container Resolver For Containers and Scans.
Support Containers as New Scan Type With Realted Flags.

References

https://checkmarx.atlassian.net/jira/software/c/projects/AST/boards/471?assignee=712020%3A8b115cb9-1cab-4fd5-9878-130ce7842e3f&selectedIssue=AST-36237

https://checkmarx.atlassian.net/jira/software/c/projects/AST/boards/471?assignee=712020%3A8b115cb9-1cab-4fd5-9878-130ce7842e3f&selectedIssue=AST-36236

Testing

Added unit and integration tests.

Checklist

  • I have added documentation for new/changed functionality in this PR (if applicable).
  • I have updated the CLI help for new/changed functionality in this PR (if applicable).
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used

@pedrompflopes pedrompflopes requested review from a team, margaritalm and pedrompflopes and removed request for a team March 6, 2024 10:32
@AlvoBen AlvoBen requested review from tiagobcx and OrShamirCM and removed request for margaritalm and pedrompflopes March 6, 2024 10:32
@AlvoBen AlvoBen marked this pull request as draft March 6, 2024 10:32
Copy link

github-actions bot commented Mar 6, 2024

Logo
Checkmarx One – Scan Summary & Details66f21fde-5ec5-4f66-9529-290bed36607a

Policy Management Violations

Policy Name Rule(s) Break Build
[SAST-ML0] Not allowed NEW Sast vulnerabilities true

New Issues

Severity Issue Source File / Package Checkmarx Insight
LOW IAM Access Analyzer Not Enabled /positive1.tf: 1 IAM Access Analyzer should be enabled and configured to continuously monitor resource permissions

Copy link
Contributor

@OrShamirCM OrShamirCM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@@ -185,6 +189,7 @@ func NewScanCommand(
kicsRealtimeCmd := scanRealtimeSubCommand()

scaRealtimeCmd := scarealtime.NewScaRealtimeCommand(scaRealTimeWrapper)
containerResolver = containerResolverWrapper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move it to the start of the method

@@ -502,7 +509,8 @@ func scanCreateSubCommand(
"",
fmt.Sprintf("Parameters to use in SCA resolver (requires --%s).", commonParams.ScaResolverFlag),
)
createScanCmd.PersistentFlags().String(commonParams.ScanTypes, "", "Scan types, ex: (sast,iac-security,sca,api-security)")
createScanCmd.PersistentFlags().String(commonParams.ContainerImagesFlag, "", "List of container images to scan, ex: manuelbcd/vulnapp:latest,debian:10")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the flag suppose to be under FF (in help too)

scanTypes, _ := cmd.Flags().GetString(commonParams.ScanTypes)
var scanTypeList []string
if len(scanTypes) > 0 {
scanTypeList = strings.Split(scanTypes, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trim

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if possible to add space *

return true
}
//check if user license supports only container scanning
actualScanTypeList := strings.Split(actualScanTypes, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change the name form actual to permissions / licenses (if it's not too complex)

internal/commands/scan.go Show resolved Hide resolved
containerImagesList = strings.Split(strings.TrimSpace(containerImages), ",")
for _, containerImageName := range containerImagesList {
if containerImagesErr := validateContainerImageFormat(containerImageName); containerImagesErr != nil {
return containerImagesErr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add log error in validate when error

}
containerResolverERR := containerResolver.Resolve(directoryPath, directoryPath, containerImagesList, debug)
if containerResolverERR != nil {
return containerResolverERR
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add log error for container resolver

@@ -2474,6 +2554,14 @@ func validateCreateScanFlags(cmd *cobra.Command) error {
return nil
}

func validateContainerImageFormat(containerImage string) error {
imageParts := strings.Split(containerImage, ":")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add trim

execCmdNilAssertion(t, append(baseArgs, "-s", blankSpace+"."+blankSpace)...)
}

func TestCreateScan_ContainerImagesFlagWithoutValue_FailCreatingScan(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is single container type with license

err := execCmdNotNilAssertion(t, "scan", "create", "--project-name", "MOCK", "-s", dummyRepo, "-b", "dummy_branch", "--container-images")
assert.Assert(t, err.Error() == "flag needs an argument: --container-images")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add zip case for unit without flags / filters + flags/ filters

Copy link
Contributor

@tamarleviCm tamarleviCm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed up to utils_test.go

@@ -502,7 +509,18 @@ func scanCreateSubCommand(
"",
fmt.Sprintf("Parameters to use in SCA resolver (requires --%s).", commonParams.ScaResolverFlag),
)
createScanCmd.PersistentFlags().String(commonParams.ScanTypes, "", "Scan types, ex: (sast,iac-security,sca,api-security)")
if len(wrappers.FeatureFlags) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need this condition?
if FF is not received you have to know it before (when you call the FF service) and fail the command if FF is necessary
but maybe you have to ignore containers when FF was not received

@@ -1000,6 +1022,7 @@ func addScaScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) map[string
scaConfig.Filter, _ = cmd.Flags().GetString(commonParams.ScaFilterFlag)
scaConfig.LastSastScanTime, _ = cmd.Flags().GetString(commonParams.LastSastScanTime)
scaConfig.PrivatePackageVersion, _ = cmd.Flags().GetString(commonParams.ScaPrivatePackageVersionFlag)
scaConfig.EnableContainersScan = !(wrappers.FeatureFlags[wrappers.ContainerEngineCLIEnabled] && userAllowedEngines[commonParams.ContainersType])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
scaConfig.EnableContainersScan = !(wrappers.FeatureFlags[wrappers.ContainerEngineCLIEnabled] && userAllowedEngines[commonParams.ContainersType])
scaConfig.EnableContainersScan = (wrappers.FeatureFlags[wrappers.ContainerEngineCLIEnabled] && userAllowedEngines[commonParams.ContainersType])

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be - not the condition

@@ -1106,6 +1143,11 @@ func compressFolder(sourceDir, filter, userIncludeFilter, scaResolver string) (s
return outputFile.Name(), err
}

func isSingleContainerScanTriggered() bool {
scanTypeList := strings.Split(actualScanTypes, ",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actualScanTypes could include spaces?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No :)

@@ -21,6 +21,10 @@ var FeatureFlagsBaseMap = []CommandFlags{
Name: MinioEnabled,
Default: true,
},
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove !!

Copy link
Contributor

@OrShamirCM OrShamirCM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be closed? all merged to other branch?
If so please close and delete the branch


var defaultEngines = map[string]bool{
"sast": true,
"sca": true,
"api-security": true,
"iac-security": true,
"containers": true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the bool here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this engines is allowed, I deleted the containers engine if the FF is off

@@ -138,6 +138,9 @@ type ScaConfig struct {
ExploitablePath string `json:"ExploitablePath,omitempty"`
LastSastScanTime string `json:"LastSastScanTime,omitempty"`
PrivatePackageVersion string `json:"privatePackageVersion,omitempty"`
EnableContainersScan bool `json:"enableContainersScan,omitempty"`
}
type ContainerConfig struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need it if it empty

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future if we will required to pass some information there.
This is the same configuration handling we do in all of our engines, I thought it will be a good idea to do with container engine the same.

@AlvoBen AlvoBen changed the title Feature/benalvo/add container engine logic (AST-36237, AST-36236) CLI | Support Container Engine Scan (AST-36237, AST-36236) Mar 26, 2024
@AlvoBen AlvoBen marked this pull request as ready for review March 26, 2024 17:27
@pedrompflopes pedrompflopes requested review from a team and hmmachadocx and removed request for a team March 26, 2024 17:28
@AlvoBen AlvoBen changed the title CLI | Support Container Engine Scan (AST-36237, AST-36236) CLI | Support Container Engine Scan (AST-36237, AST-36236, AST-36238) Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants