Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
a908502
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
6908ff8
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
b28d35f
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
905aa57
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
457f57e
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
4be4f14
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 12, 2022
32d3d5f
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
dae1c5d
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
e8d8f14
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
ad45dbc
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
461e2d0
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
3608abe
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
19b1a4e
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 13, 2022
a80ad7a
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 14, 2022
e1efb2e
Merge branch 'main' into feature/AST-11603_CLI_add_ssh_key_project_cr…
cx-hugo-machado Apr 18, 2022
fbf8f82
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 18, 2022
d24b651
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 18, 2022
0602dea
AST-11621 - CLI | Handle file filters when source is zip file
cx-hugo-machado Apr 18, 2022
8372e4f
AST-11621 - CLI | Handle file filters when source is zip file
cx-hugo-machado Apr 19, 2022
3ad74f8
Revert "AST-11621 - CLI | Handle file filters when source is zip file"
cx-hugo-machado Apr 19, 2022
9ed0629
AST-11621 - CLI | Handle file filters when source is zip file
cx-hugo-machado Apr 19, 2022
7800854
AST-11621 - CLI | Handle file filters when source is zip file
cx-hugo-machado Apr 19, 2022
c1ba9d2
AST-11602 - CLI | Add SSH key to scan create command
cx-hugo-machado Apr 19, 2022
f8d480c
Merge branch 'main' into feature/AST-11602_CLI_add_ssh_key_scan_create
cx-hugo-machado Apr 19, 2022
fec2c87
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
5811714
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
ded3512
Merge branch 'feature/AST-11602_CLI_add_ssh_key_scan_create' into fea…
cx-hugo-machado Apr 20, 2022
bff543c
Merge branch 'feature/AST-11603_CLI_add_ssh_key_project_create' into …
cx-hugo-machado Apr 20, 2022
90877cb
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
2e55b2a
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
30eb515
Merge branch 'feature/AST-11603_CLI_add_ssh_key_project_create' into …
cx-hugo-machado Apr 20, 2022
774fdd4
Merge branch 'feature/AST-11602_CLI_add_ssh_key_scan_create' into fea…
cx-hugo-machado Apr 20, 2022
acafc8f
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
24e8ad5
Merge branch 'feature/AST-11603_CLI_add_ssh_key_project_create' into …
cx-hugo-machado Apr 20, 2022
57cc94e
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 20, 2022
4f5cb1f
Merge branch 'main' into feature/CLI_handle_file_filters_zip_file
cx-hugo-machado Apr 28, 2022
6be9fee
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 29, 2022
3b63993
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 29, 2022
282f849
AST-11603 - CLI | Add SSH key to project create command
cx-hugo-machado Apr 29, 2022
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
101 changes: 88 additions & 13 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"encoding/json"
"fmt"
"io"
"io/fs"
"io/ioutil"
"log"
Expand All @@ -18,6 +19,7 @@ import (
"github.com/checkmarx/ast-cli/internal/commands/util"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
"github.com/google/shlex"
"github.com/google/uuid"
"github.com/pkg/errors"

"github.com/MakeNowJust/heredoc"
Expand All @@ -29,19 +31,20 @@ import (
)

const (
failedCreating = "Failed creating a scan"
failedGetting = "Failed showing a scan"
failedGettingTags = "Failed getting tags"
failedDeleting = "Failed deleting a scan"
failedCanceling = "Failed canceling a scan"
failedGettingAll = "Failed listing"
thresholdLog = "%s: Limit = %d, Current = %v"
thresholdMsgLog = "Threshold check finished with status %s : %s"
mbBytes = 1024.0 * 1024.0
scaType = "sca"
notExploitable = "NOT_EXPLOITABLE"
git = "git"
invalidSSHSource = "provided source does not need a key. Make sure you are defining the right source or remove the flag --ssh-key"
failedCreating = "Failed creating a scan"
failedGetting = "Failed showing a scan"
failedGettingTags = "Failed getting tags"
failedDeleting = "Failed deleting a scan"
failedCanceling = "Failed canceling a scan"
failedGettingAll = "Failed listing"
thresholdLog = "%s: Limit = %d, Current = %v"
thresholdMsgLog = "Threshold check finished with status %s : %s"
mbBytes = 1024.0 * 1024.0
scaType = "sca"
notExploitable = "NOT_EXPLOITABLE"
git = "git"
invalidSSHSource = "provided source does not need a key. Make sure you are defining the right source or remove the flag --ssh-key"
errorUnzippingFile = "an error occurred while unzipping file. Reason: "
)

var (
Expand Down Expand Up @@ -863,6 +866,15 @@ func getUploadURLFromSource(
return "", errors.Wrapf(err, "%s: Input in bad format", failedCreating)
}

var errorUnzippingFile error
// apply file filters to zip file
if (len(sourceDirFilter) > 0 || len(userIncludeFilter) > 0) && len(zipFilePath) > 0 {
directoryPath, errorUnzippingFile = UnzipFile(zipFilePath)
if errorUnzippingFile != nil {
return "", errorUnzippingFile
}
}

if directoryPath != "" {
var dirPathErr error
// Get sca resolver flags
Expand Down Expand Up @@ -902,6 +914,69 @@ func getUploadURLFromSource(
return preSignedURL, nil
}

func UnzipFile(f string) (string, error) {
tempDir := os.TempDir() + string(os.PathSeparator) + "cx-unzipped-temp-dir-" + uuid.New().String() + string(os.PathSeparator)

err := os.Mkdir(tempDir, directoryPermission)
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}

archive, err := zip.OpenReader(f)
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}
defer func() {
_ = archive.Close()
}()

for _, f := range archive.File {
filePath := filepath.Join(tempDir, f.Name)
PrintIfVerbose("unzipping file " + filePath + "...")

if !strings.HasPrefix(filePath, filepath.Clean(tempDir)+string(os.PathSeparator)) {
return "", errors.New("invalid file path " + filePath)
}
if f.FileInfo().IsDir() {
PrintIfVerbose("creating directory...")
err = os.MkdirAll(filePath, os.ModePerm)
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}
continue
}

if err = os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}

dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}

fileInArchive, err := f.Open()
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}

if _, err = io.Copy(dstFile, fileInArchive); err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}

err = dstFile.Close()
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}
err = fileInArchive.Close()
if err != nil {
return "", errors.Errorf("%s %s", errorUnzippingFile, err.Error())
}
}

return tempDir, nil
}

func definePathForZipFileOrDirectory(cmd *cobra.Command) (zipFile, sourceDir string, err error) {
source, _ := cmd.Flags().GetString(commonParams.SourcesFlag)
sourceTrimmed := strings.TrimSpace(source)
Expand Down
6 changes: 6 additions & 0 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,9 @@ func TestScanWorkFlowWithScaFilter(t *testing.T) {
err := executeTestCommand(cmd, baseArgs...)
assert.NilError(t, err)
}

func TestCreateScanFilterZipFile(t *testing.T) {
baseArgs := []string{"scan", "create", "--project-name", "MOCK", "-b", "dummy_branch"}

execCmdNilAssertion(t, append(baseArgs, "-s", "data/sources.zip", "--file-filter", "!.java")...)
}
15 changes: 14 additions & 1 deletion test/integration/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ func TestScanCreateWithSSHKey(t *testing.T) {
sshKey := viper.GetString("CX_SCAN_SSH_KEY")

_ = ioutil.WriteFile(SSHKeyFilePath, []byte(sshKey), 0644)
defer func() { _ = os.Remove(SSHKeyFilePath) }()

_, projectName := getRootProject(t)

Expand All @@ -562,6 +563,18 @@ func TestScanCreateWithSSHKey(t *testing.T) {
}

executeCmdWithTimeOutNilAssertion(t, "Create a scan with ssh-key should pass", 4*time.Minute, args...)
}

func TestCreateScanFilterZipFile(t *testing.T) {
_, projectName := getRootProject(t)

args := []string{
"scan", "create",
flag(params.ProjectName), projectName,
flag(params.BranchFlag), "main",
flag(params.SourcesFlag), Zip,
flag(params.SourceDirFilterFlag), "!*.html",
}

_ = os.Remove(SSHKeyFilePath)
executeCmdWithTimeOutNilAssertion(t, "Scan must complete successfully", 4*time.Minute, args...)
}