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

Feature/ast 11602 cli add ssh key scan create #387

Merged
merged 30 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a908502
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
6908ff8
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
b28d35f
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
905aa57
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
457f57e
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
4be4f14
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 12, 2022
32d3d5f
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
dae1c5d
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
e8d8f14
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
ad45dbc
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
461e2d0
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
3608abe
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
19b1a4e
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 13, 2022
a80ad7a
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 14, 2022
e1efb2e
Merge branch 'main' into feature/AST-11603_CLI_add_ssh_key_project_cr…
hmmachadocx Apr 18, 2022
fbf8f82
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 18, 2022
d24b651
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 18, 2022
9ed0629
AST-11621 - CLI | Handle file filters when source is zip file
hmmachadocx Apr 19, 2022
7800854
AST-11621 - CLI | Handle file filters when source is zip file
hmmachadocx Apr 19, 2022
c1ba9d2
AST-11602 - CLI | Add SSH key to scan create command
hmmachadocx Apr 19, 2022
f8d480c
Merge branch 'main' into feature/AST-11602_CLI_add_ssh_key_scan_create
hmmachadocx Apr 19, 2022
fec2c87
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 20, 2022
5811714
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 20, 2022
90877cb
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 20, 2022
2e55b2a
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 20, 2022
774fdd4
Merge branch 'feature/AST-11602_CLI_add_ssh_key_scan_create' into fea…
hmmachadocx Apr 20, 2022
acafc8f
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 20, 2022
b46aa95
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 27, 2022
fa1f7be
Merge branch 'main' into feature/AST-11602_CLI_add_ssh_key_scan_create
hmmachadocx Apr 28, 2022
a2a3e6e
AST-11603 - CLI | Add SSH key to project create command
hmmachadocx Apr 28, 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
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
CX_AST_PASSWORD: ${{ secrets.CX_AST_PASSWORD }}
CX_APIKEY: ${{ secrets.CX_APIKEY }}
CX_TENANT: ${{ secrets.CX_TENANT }}
CX_SCAN_SSH_KEY: ${{ secrets.CX_SCAN_SSH_KEY }}
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
PROXY_HOST: localhost
PROXY_PORT: 3128
Expand Down
111 changes: 110 additions & 1 deletion internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/MakeNowJust/heredoc"
"github.com/checkmarx/ast-cli/internal/commands/util"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"

commonParams "github.com/checkmarx/ast-cli/internal/params"
Expand All @@ -23,6 +24,11 @@ const (
failedDeletingProj = "Failed deleting a project"
failedGettingBranches = "Failed getting branches for project"
failedFindingGroup = "Failed finding groups"
projOriginLevel = "Project"
repoConfKey = "scan.handler.git.repository"
sshConfKey = "scan.handler.git.sshKey"
mandatoryRepoURLError = "flag --repo-url is mandatory when --ssh-key is provided"
invalidRepoURL = "provided repository url doesn't need a key. Make sure you are defining the right repository or remove the flag --ssh-key"
)

var (
Expand Down Expand Up @@ -88,6 +94,8 @@ func NewProjectCommand(projectsWrapper wrappers.ProjectsWrapper, groupsWrapper w
createProjCmd.PersistentFlags().String(commonParams.GroupList, "", "List of groups, ex: (PowerUsers,etc)")
createProjCmd.PersistentFlags().StringP(commonParams.ProjectName, "", "", "Name of project")
createProjCmd.PersistentFlags().StringP(commonParams.MainBranchFlag, "", "", "Main branch")
createProjCmd.PersistentFlags().String(commonParams.SSHKeyFlag, "", "Path to ssh key")
hmmachadocx marked this conversation as resolved.
Show resolved Hide resolved
createProjCmd.PersistentFlags().String(commonParams.RepoURLFlag, "", "Repository URL")

listProjectsCmd := &cobra.Command{
Use: "list",
Expand Down Expand Up @@ -283,7 +291,11 @@ func runCreateProjectCommand(
if err != nil {
return err
}
updateTagValues(&input, cmd)
setupScanTags(&input, cmd)
err = validateConfiguration(cmd)
if err != nil {
return err
}
var projModel = wrappers.Project{}
var projResponseModel *wrappers.ProjectResponseModel
var errorModel *wrappers.ErrorModel
Expand All @@ -299,6 +311,7 @@ func runCreateProjectCommand(
if err != nil {
return errors.Wrapf(err, "%s", failedCreatingProj)
}

// Checking the response
if errorModel != nil {
return errors.Errorf(ErrorCodeFormat, failedCreatingProj, errorModel.Code, errorModel.Message)
Expand All @@ -308,10 +321,106 @@ func runCreateProjectCommand(
return errors.Wrapf(err, "%s", failedCreatingProj)
}
}

err = updateProjectConfigurationIfNeeded(cmd, projectsWrapper, projResponseModel.ID)
if err != nil {
return err
}

return nil
}
}

func updateProjectConfigurationIfNeeded(cmd *cobra.Command, projectsWrapper wrappers.ProjectsWrapper, projectID string) error {
// Just update project configuration id a repository url is defined
if cmd.Flags().Changed(commonParams.RepoURLFlag) {
var projectConfigurations []wrappers.ProjectConfiguration

repoURL, _ := cmd.Flags().GetString(commonParams.RepoURLFlag)

urlConf := getProjectConfiguration(repoConfKey, "repository", git, projOriginLevel, repoURL, "String", true)

projectConfigurations = append(projectConfigurations, urlConf)

if cmd.Flags().Changed(commonParams.SSHKeyFlag) {
sshKeyPath, _ := cmd.Flags().GetString(commonParams.SSHKeyFlag)

sshKey, sshErr := util.ReadFileAsString(sshKeyPath)
if sshErr != nil {
return sshErr
}

sshKeyConf := getProjectConfiguration(sshConfKey, "sshKey", git, projOriginLevel, sshKey, "Secret", true)

projectConfigurations = append(projectConfigurations, sshKeyConf)
}

_, configErr := projectsWrapper.UpdateConfiguration(projectID, projectConfigurations)
if configErr != nil {
return configErr
}
}

return nil
}

func getProjectConfiguration(key, name, category, level, value, valueType string, allowOverride bool) wrappers.ProjectConfiguration {
config := wrappers.ProjectConfiguration{}
config.Key = key
config.Name = name
config.Category = category
config.OriginLevel = level
config.Value = value
config.ValueType = valueType
config.AllowOverride = allowOverride

return config
}

func validateConfiguration(cmd *cobra.Command) error {
var sshKeyDefined bool
var repoURLDefined bool

// Validate if ssh key is empty when provided
if cmd.Flags().Changed(commonParams.SSHKeyFlag) {
sshKey, _ := cmd.Flags().GetString(commonParams.SSHKeyFlag)

if strings.TrimSpace(sshKey) == "" {
return errors.New("flag needs an argument: --ssh-key")
}

sshKeyDefined = true
}

// Validate if repo url is empty when provided
if cmd.Flags().Changed(commonParams.RepoURLFlag) {
repoURL, _ := cmd.Flags().GetString(commonParams.RepoURLFlag)

if strings.TrimSpace(repoURL) == "" {
return errors.New("flag needs an argument: --repo-url")
}

repoURLDefined = true
}

// If ssh key is defined we have two checks to validate:
// 1. repo url needs to be provided
// 2. provided repo url needs to be a ssh url
if sshKeyDefined {
if !repoURLDefined {
return errors.New(mandatoryRepoURLError)
}

repoURL, _ := cmd.Flags().GetString(commonParams.RepoURLFlag)

if !util.IsSSHURL(repoURL) {
return errors.New(invalidRepoURL)
}
}

return nil
}

func runListProjectsCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
var allProjectsModel *wrappers.ProjectsCollectionResponseModel
Expand Down
63 changes: 63 additions & 0 deletions internal/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"testing"

"gotest.tools/assert"

"github.com/checkmarx/ast-cli/internal/commands/util"
)

func TestProjectHelp(t *testing.T) {
Expand Down Expand Up @@ -101,3 +103,64 @@ func TestRunProjectCreateInvalidGroup(t *testing.T) {
"project", "create", "--project-name", "invalidprj", "--groups", "invalidgroup")
assert.Assert(t, err.Error() == "Failed finding groups: [invalidgroup]")
}

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

err := execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key")...)
assert.Error(t, err, "flag needs an argument: --ssh-key", err.Error())

err = execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "")...)
assert.Error(t, err, "flag needs an argument: --ssh-key", err.Error())

err = execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", " ")...)
assert.Error(t, err, "flag needs an argument: --ssh-key", err.Error())
}

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

err := execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key", "--repo-url")...)
assert.Error(t, err, "flag needs an argument: --repo-url", err.Error())

err = execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key", "--repo-url", "")...)
assert.Error(t, err, "flag needs an argument: --repo-url", err.Error())

err = execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key", "--repo-url", " ")...)
assert.Error(t, err, "flag needs an argument: --repo-url", err.Error())
}

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

err := execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key")...)

assert.Error(t, err, mandatoryRepoURLError)
}

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

err := execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key", "--repo-url", "https://github.com/dummyuser/dummy_project.git")...)

assert.Error(t, err, invalidRepoURL)
}

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

err := execCmdNotNilAssertion(t, append(baseArgs, "--ssh-key", "dummy_key", "--repo-url", "git@github.com:dummyRepo/dummyProject.git")...)

expectedMessages := []string{
"open dummy_key: The system cannot find the file specified.",
"open dummy_key: no such file or directory",
}

assert.Assert(t, util.Contains(expectedMessages, err.Error()))
}

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

execCmdNilAssertion(t, append(baseArgs, "--ssh-key", "data/sources.zip", "--repo-url", "git@github.com:dummyRepo/dummyProject.git")...)
}
Loading