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

Add the ability to import a SARIF file (AST-36884, AST-36890) #674

Merged
merged 164 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 145 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
af62d5d
implemented import command with tests + rename applicationerrors to c…
checkmarx-kobi-hagmi Mar 7, 2024
eccc8fe
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Mar 7, 2024
8710c82
import with BYOR
tamarleviCm Mar 10, 2024
06662a7
further implementation of import command and renaming applicationerro…
checkmarx-kobi-hagmi Mar 10, 2024
64deb01
import with BYOR
tamarleviCm Mar 10, 2024
5214f62
import with BYOR
tamarleviCm Mar 10, 2024
365263a
import with BYOR
tamarleviCm Mar 10, 2024
f608ef7
Added scan create implementation of import
checkmarx-kobi-hagmi Mar 10, 2024
02f4b86
import with BYOR
tamarleviCm Mar 10, 2024
30b705b
Added test
checkmarx-kobi-hagmi Mar 10, 2024
6f528ea
Fixed lint issues
checkmarx-kobi-hagmi Mar 10, 2024
8f79dcd
fixed lint error
checkmarx-kobi-hagmi Mar 10, 2024
5e0812d
integration tests
tamarleviCm Mar 11, 2024
b42126c
integration tests
tamarleviCm Mar 11, 2024
19b8d27
Remove scan create with import flags and tests
checkmarx-kobi-hagmi Mar 11, 2024
f9ea864
Removed import file type
checkmarx-kobi-hagmi Mar 11, 2024
28255f3
Small refactor get project by name
checkmarx-kobi-hagmi Mar 11, 2024
7a17fec
fix lint issues
checkmarx-kobi-hagmi Mar 11, 2024
fe9dec7
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Mar 12, 2024
4a09710
Removed flag import file type. uncommented args
checkmarx-kobi-hagmi Mar 12, 2024
103eb65
Updated --help text
checkmarx-kobi-hagmi Mar 12, 2024
8259b3c
project will return only exact match
checkmarx-kobi-hagmi Mar 13, 2024
e8a1173
Added result limit
checkmarx-kobi-hagmi Mar 13, 2024
a2abdba
added setLimit and setDefaultLimit
checkmarx-kobi-hagmi Mar 13, 2024
4b773d7
removed new line
checkmarx-kobi-hagmi Mar 13, 2024
366ce1a
Merge branch 'main' into feature/import-sarif-file
tamarleviCm Mar 17, 2024
443c357
Support in importing a file to a not-existing project
checkmarx-kobi-hagmi Mar 17, 2024
251cc43
Merge branch 'feature/import-sarif-file' into feature/kobih/import-sa…
checkmarx-kobi-hagmi Mar 17, 2024
42b129d
Resolved PR comments
checkmarx-kobi-hagmi Mar 17, 2024
37f3484
Fixed linter issues
checkmarx-kobi-hagmi Mar 17, 2024
d4cd978
Fix linter issues
checkmarx-kobi-hagmi Mar 17, 2024
1e63a2a
Renamed const
checkmarx-kobi-hagmi Mar 17, 2024
de5c2d1
Added validation to sarif file
checkmarx-kobi-hagmi Mar 17, 2024
2d2f2ba
linter fix + change constants strcuture
checkmarx-kobi-hagmi Mar 17, 2024
f3e9ae2
small syntax improvement + extracting to constant
checkmarx-kobi-hagmi Mar 18, 2024
6686143
Update import_test.go
checkmarx-kobi-hagmi Mar 18, 2024
4008259
Update import_test.go
checkmarx-kobi-hagmi Mar 18, 2024
ff1905f
fix linter
checkmarx-kobi-hagmi Mar 18, 2024
3a56112
Update import.go
checkmarx-kobi-hagmi Mar 18, 2024
3228aa6
Update import.go
checkmarx-kobi-hagmi Mar 18, 2024
22cdca5
Merge branch 'feature/import-sarif-file' into tamar/import
checkmarx-kobi-hagmi Mar 18, 2024
51f763d
Merge branch 'tamar/import' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Mar 18, 2024
bf00d19
Added tests for importFile function
checkmarx-kobi-hagmi Mar 18, 2024
8bf6f15
created module for error constants
checkmarx-kobi-hagmi Mar 19, 2024
6e3e59e
const reference fixes
checkmarx-kobi-hagmi Mar 19, 2024
ae6c5f6
possible linter fix
checkmarx-kobi-hagmi Mar 19, 2024
893e882
fix linter issues
checkmarx-kobi-hagmi Mar 19, 2024
e96ffe0
Linter issue fix
checkmarx-kobi-hagmi Mar 19, 2024
00de95d
Renamed integration tests
checkmarx-kobi-hagmi Mar 19, 2024
a179f35
removed function call
checkmarx-kobi-hagmi Mar 19, 2024
c89b87f
go mod tidy
checkmarx-kobi-hagmi Mar 20, 2024
a58db7f
Revert "go mod tidy"
checkmarx-kobi-hagmi Mar 20, 2024
7a513c9
Revert "Revert "go mod tidy""
checkmarx-kobi-hagmi Mar 20, 2024
e5e8684
Revert "go mod tidy"
checkmarx-kobi-hagmi Mar 20, 2024
34f0cfd
Revert "removed function call"
checkmarx-kobi-hagmi Mar 20, 2024
47dee4f
Revert "Renamed integration tests"
checkmarx-kobi-hagmi Mar 20, 2024
ac89c10
Revert "Linter issue fix"
checkmarx-kobi-hagmi Mar 20, 2024
897aafe
Revert "fix linter issues"
checkmarx-kobi-hagmi Mar 20, 2024
65fa218
Revert "possible linter fix"
checkmarx-kobi-hagmi Mar 20, 2024
c40356c
Revert "const reference fixes"
checkmarx-kobi-hagmi Mar 20, 2024
d6f8a49
Hopefully fixed linter issues
checkmarx-kobi-hagmi Mar 20, 2024
92b7e2e
fixed mixedCap linter issue
checkmarx-kobi-hagmi Mar 20, 2024
8383f3e
fixed undefined
checkmarx-kobi-hagmi Mar 20, 2024
e0dc199
fixed constants reference
checkmarx-kobi-hagmi Mar 20, 2024
9a9bc3b
Fixed vulcheck
checkmarx-kobi-hagmi Mar 20, 2024
b886f10
fixed goimports
checkmarx-kobi-hagmi Mar 20, 2024
63cd01f
ignore heredoc
checkmarx-kobi-hagmi Mar 20, 2024
4e463ef
renamed aliases
checkmarx-kobi-hagmi Mar 20, 2024
c3428d9
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM Mar 24, 2024
24fceed
PR comments fixes
checkmarx-kobi-hagmi Mar 24, 2024
bfce5f6
lint fix
checkmarx-kobi-hagmi Mar 24, 2024
391fd1d
attempt renaming
checkmarx-kobi-hagmi Mar 24, 2024
c05f04e
Changed camel casing errorConstants
checkmarx-kobi-hagmi Mar 24, 2024
3af803c
add project cleanup by name and more integration tests
checkmarx-kobi-hagmi Mar 26, 2024
0efe4c3
Added import to ff configuration
checkmarx-kobi-hagmi Mar 26, 2024
1f577ff
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM Mar 26, 2024
3d2d026
apply import command only if BYOR FF is enabled
checkmarx-kobi-hagmi Mar 27, 2024
f8af9ee
remove duplicated call to delete project
checkmarx-kobi-hagmi Mar 27, 2024
555deba
fix
checkmarx-kobi-hagmi Mar 27, 2024
a93d3c2
added default value for BOYR not enabled
checkmarx-kobi-hagmi Mar 27, 2024
a475fc2
Fix lint issues
checkmarx-kobi-hagmi Mar 28, 2024
1bf6d00
improvements
checkmarx-kobi-hagmi Mar 28, 2024
71ed509
Tests to support BYOR FF
checkmarx-kobi-hagmi Mar 28, 2024
9488418
Added two tests and validation to deleteProjectByName
checkmarx-kobi-hagmi Mar 31, 2024
846e245
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM Mar 31, 2024
ebed14c
Added getProjectName test
checkmarx-kobi-hagmi Mar 31, 2024
8e759f5
1. loading feature flags before calling a command. 2. Added Skip test…
checkmarx-kobi-hagmi Mar 31, 2024
06e4311
Merge branch 'feature/kobih/import-sarif-file' of https://github.com/…
checkmarx-kobi-hagmi Mar 31, 2024
dbf6c39
Removed unnecessary code
checkmarx-kobi-hagmi Mar 31, 2024
d910606
Rename test
checkmarx-kobi-hagmi Mar 31, 2024
b9381e8
Added integration test - project name empty
checkmarx-kobi-hagmi Mar 31, 2024
f9f5da5
Removed Skips temporarily
checkmarx-kobi-hagmi Apr 1, 2024
4c3ee75
commented out tests to check coverage
checkmarx-kobi-hagmi Apr 1, 2024
4a9f0ff
attempt to increase coverage
checkmarx-kobi-hagmi Apr 2, 2024
fd409f4
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM Apr 4, 2024
c07fc3c
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM Apr 4, 2024
95850bd
import file returning importId as well
checkmarx-kobi-hagmi Apr 11, 2024
9cae1cc
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Apr 11, 2024
4954b35
go fmt
checkmarx-kobi-hagmi Apr 11, 2024
f3fd241
removed getProjectName
checkmarx-kobi-hagmi Apr 14, 2024
97f37d2
restructured import.go and everything related to it. import_test is l…
checkmarx-kobi-hagmi Apr 15, 2024
c212531
restructuring completed
checkmarx-kobi-hagmi Apr 16, 2024
b3602ac
Update util_command.go
checkmarx-kobi-hagmi Apr 16, 2024
e5149e1
renaming shared logic to projects under services folder
checkmarx-kobi-hagmi Apr 18, 2024
2f4f633
temporary suppressing warning for cyclomatic lint error in updateProject
checkmarx-kobi-hagmi Apr 18, 2024
e062767
removed linter issue
checkmarx-kobi-hagmi Apr 18, 2024
19fecfa
fixed compilation issues
checkmarx-kobi-hagmi Apr 18, 2024
15731b5
compilation fix
checkmarx-kobi-hagmi Apr 18, 2024
8277157
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Apr 22, 2024
742e743
resolved conflicts from main
checkmarx-kobi-hagmi Apr 22, 2024
79fa5ab
Added additional services
checkmarx-kobi-hagmi Apr 24, 2024
f330b01
Added tests to services
checkmarx-kobi-hagmi Apr 25, 2024
5994cb8
fixed linter errors
checkmarx-kobi-hagmi Apr 25, 2024
b37eead
linter fix
checkmarx-kobi-hagmi Apr 25, 2024
35454d7
Added more test
checkmarx-kobi-hagmi Apr 25, 2024
0d9ad2f
fixed lint errors
checkmarx-kobi-hagmi Apr 25, 2024
2352bbe
Added unit tests
checkmarx-kobi-hagmi Apr 28, 2024
2aad531
small improvement
checkmarx-kobi-hagmi Apr 28, 2024
e0721e1
added additional test
checkmarx-kobi-hagmi Apr 28, 2024
906dc2c
removed two error checks
checkmarx-kobi-hagmi Apr 30, 2024
3dfab1a
removed more error checks for flags
checkmarx-kobi-hagmi Apr 30, 2024
ed897a3
fix compilation error
checkmarx-kobi-hagmi Apr 30, 2024
ad07129
1. added example to documentation 2. added ignore from coverage build…
checkmarx-kobi-hagmi Apr 30, 2024
721fae6
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Apr 30, 2024
dff29e6
fixed merge conflicts with main
checkmarx-kobi-hagmi Apr 30, 2024
8aded13
Revert "fixed merge conflicts with main"
checkmarx-kobi-hagmi Apr 30, 2024
be59e96
Revert "Merge branch 'main' into feature/kobih/import-sarif-file"
checkmarx-kobi-hagmi Apr 30, 2024
8cfbfba
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi Apr 30, 2024
1d87fc2
Fixed access management PR
checkmarx-kobi-hagmi Apr 30, 2024
f0f8d2e
Fixed threshold PR
checkmarx-kobi-hagmi Apr 30, 2024
f316a40
fixed compilation issue
checkmarx-kobi-hagmi Apr 30, 2024
3fe9d65
linter fix
checkmarx-kobi-hagmi Apr 30, 2024
17e5824
lint issue fix
checkmarx-kobi-hagmi Apr 30, 2024
6651795
Added more unit tests
checkmarx-kobi-hagmi Apr 30, 2024
1822364
tests fix
checkmarx-kobi-hagmi May 1, 2024
66d62eb
trial to exclude function from integration tests
checkmarx-kobi-hagmi May 1, 2024
c064149
test fix
checkmarx-kobi-hagmi May 1, 2024
7e022fd
remove build tags
checkmarx-kobi-hagmi May 1, 2024
9af6d2c
Added additional integration test
checkmarx-kobi-hagmi May 1, 2024
c75e5c3
Include services module in integration tests
checkmarx-kobi-hagmi May 1, 2024
f8335a9
removed defer
checkmarx-kobi-hagmi May 1, 2024
a8af9c3
removed added test
checkmarx-kobi-hagmi May 1, 2024
2b637a5
route fixes
checkmarx-kobi-hagmi May 1, 2024
6fce281
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM May 5, 2024
4c26b4a
fix CR comments
checkmarx-kobi-hagmi May 5, 2024
84f0ece
changed to details
checkmarx-kobi-hagmi May 5, 2024
9abf731
CR fixes
checkmarx-kobi-hagmi May 5, 2024
ef0164d
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi May 5, 2024
c33eaa8
Fix merge conflicts
checkmarx-kobi-hagmi May 5, 2024
9ca55e3
fixed test call
checkmarx-kobi-hagmi May 5, 2024
5f696e1
added missing wrapper to the call
checkmarx-kobi-hagmi May 6, 2024
a418236
Merge branch 'main' into feature/kobih/import-sarif-file
checkmarx-kobi-hagmi May 6, 2024
87dd92b
resolved conflicts
checkmarx-kobi-hagmi May 6, 2024
b7663cb
compilation fix
checkmarx-kobi-hagmi May 6, 2024
370e06e
test logic fix
checkmarx-kobi-hagmi May 6, 2024
e1391d2
fix unit test + modified exit codes constants location
checkmarx-kobi-hagmi May 6, 2024
e282b9b
mod tidy
checkmarx-kobi-hagmi May 6, 2024
00b648a
Removed test that is not relevant anymore
checkmarx-kobi-hagmi May 6, 2024
c0d5a98
Removed test that is not relevant anymore
checkmarx-kobi-hagmi May 6, 2024
97273fb
indicative error message instead of parsing
checkmarx-kobi-hagmi May 6, 2024
a8ce0fd
added status code to error message
checkmarx-kobi-hagmi May 6, 2024
6892b84
changed error message
checkmarx-kobi-hagmi May 6, 2024
61e5932
Merge branch 'main' into feature/kobih/import-sarif-file
OrShamirCM May 6, 2024
7653b34
test fixes
checkmarx-kobi-hagmi May 7, 2024
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 .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ linters-settings:
- github.com/spf13/cobra
- github.com/pkg/errors
- github.com/google
- github.com/MakeNowJust/heredoc
checkmarx-kobi-hagmi marked this conversation as resolved.
Show resolved Hide resolved
dupl:
threshold: 500
funlen:
Expand Down
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
policyEvaluationPath := viper.GetString(params.PolicyEvaluationPathKey)
sastMetadataPath := viper.GetString(params.SastMetadataPathKey)
accessManagementPath := viper.GetString(params.AccessManagementPathKey)
byorPath := viper.GetString(params.ByorPathKey)

scansWrapper := wrappers.NewHTTPScansWrapper(scans)
resultsPdfReportsWrapper := wrappers.NewResultsPdfReportsHTTPWrapper(resultsPdfPath)
Expand Down Expand Up @@ -81,6 +82,7 @@ func main() {
policyWrapper := wrappers.NewHTTPPolicyWrapper(policyEvaluationPath)
sastMetadataWrapper := wrappers.NewSastIncrementalHTTPWrapper(sastMetadataPath)
accessManagementWrapper := wrappers.NewAccessManagementHTTPWrapper(accessManagementPath)
byorWrapper := wrappers.NewByorHTTPWrapper(byorPath)

astCli := commands.NewAstCLI(
applicationsWrapper,
Expand Down Expand Up @@ -112,6 +114,7 @@ func main() {
policyWrapper,
sastMetadataWrapper,
accessManagementWrapper,
byorWrapper,
)
exitListener()
err = astCli.Execute()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
golang.org/x/net v0.23.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80
golang.org/x/crypto v0.22.0
golang.org/x/text v0.14.0
Expand All @@ -34,7 +36,6 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand All @@ -43,5 +44,4 @@ require (
golang.org/x/sys v0.19.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

)
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpE
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 h1:nrZ3ySNYwJbSpD6ce9duiP+QkD3JuLCcWkdaehUS/3Y=
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/.scripts/integration_up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ go test \
-tags integration \
-v \
-timeout 90m \
-coverpkg github.com/checkmarx/ast-cli/internal/commands,github.com/checkmarx/ast-cli/internal/wrappers \
-coverpkg github.com/checkmarx/ast-cli/internal/commands,github.com/checkmarx/ast-cli/internal/services,github.com/checkmarx/ast-cli/internal/wrappers \
-coverprofile cover.out \
github.com/checkmarx/ast-cli/test/integration

Expand Down
95 changes: 5 additions & 90 deletions internal/commands/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,25 @@ package commands

import (
"encoding/json"
"strings"

featureFlagsConstants "github.com/checkmarx/ast-cli/internal/constants/feature-flags"
commonParams "github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/services"
"github.com/checkmarx/ast-cli/internal/wrappers"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

const accessManagementEnabled = "ACCESS_MANAGEMENT_ENABLED" // feature flag

func createGroupsMap(groupsStr string, groupsWrapper wrappers.GroupsWrapper) ([]*wrappers.Group, error) {
OrShamirCM marked this conversation as resolved.
Show resolved Hide resolved
groups := strings.Split(groupsStr, ",")
var groupsMap []*wrappers.Group
var groupsNotFound []string
for _, group := range groups {
if len(group) > 0 {
groupsFromEnv, err := groupsWrapper.Get(group)
if err != nil {
groupsNotFound = append(groupsNotFound, group)
} else {
findGroup := findGroupByName(groupsFromEnv, group)
if findGroup != nil && findGroup.Name != "" {
groupsMap = append(groupsMap, findGroup)
} else {
groupsNotFound = append(groupsNotFound, group)
}
}
}
}
if len(groupsNotFound) > 0 {
return nil, errors.Errorf("%s: %v", failedFindingGroup, groupsNotFound)
}
return groupsMap, nil
}

func findGroupByName(groups []wrappers.Group, name string) *wrappers.Group {
for i := 0; i < len(groups); i++ {
if groups[i].Name == name {
return &groups[i]
}
}
return nil
}

func updateGroupValues(input *[]byte, cmd *cobra.Command, groupsWrapper wrappers.GroupsWrapper) ([]*wrappers.Group, error) {
groupListStr, _ := cmd.Flags().GetString(commonParams.GroupList)
groups, err := createGroupsMap(groupListStr, groupsWrapper)
groups, err := services.CreateGroupsMap(groupListStr, groupsWrapper)
if err != nil {
return groups, err
}
if !wrappers.FeatureFlags[accessManagementEnabled] {
if !wrappers.FeatureFlags[featureFlagsConstants.AccessManagementEnabled] {
var info map[string]interface{}
_ = json.Unmarshal(*input, &info)
info["groups"] = getGroupIds(groups)
info["groups"] = services.GetGroupIds(groups)
*input, _ = json.Marshal(info)
}
return groups, nil
}
func getGroupsForRequest(groups []*wrappers.Group) []string {
if !wrappers.FeatureFlags[accessManagementEnabled] {
return getGroupIds(groups)
}
return nil
}
func getGroupIds(groups []*wrappers.Group) []string {
var groupIds []string
for _, group := range groups {
groupIds = append(groupIds, group.ID)
}
return groupIds
}

func assignGroupsToProjectNewAccessManagement(projectID string, projectName string, groups []*wrappers.Group,
accessManagement wrappers.AccessManagementWrapper) error {
if !wrappers.FeatureFlags[accessManagementEnabled] {
return nil
}
groupsAssignedToTheProject, err := accessManagement.GetGroups(projectID)
if err != nil {
return err
}
groupsToAssign := getGroupsToAssign(groups, groupsAssignedToTheProject)
if len(groupsToAssign) == 0 {
return nil
}

err = accessManagement.CreateGroupsAssignment(projectID, projectName, groupsToAssign)
if err != nil {
return err
}
return nil
}

func getGroupsToAssign(receivedGroups, existingGroups []*wrappers.Group) []*wrappers.Group {
var groupsToAssign []*wrappers.Group
var groupsMap = make(map[string]bool)
for _, existingGroup := range existingGroups {
groupsMap[existingGroup.ID] = true
}
for _, receivedGroup := range receivedGroups {
find := groupsMap[receivedGroup.ID]
if !find {
groupsToAssign = append(groupsToAssign, receivedGroup)
}
}
return groupsToAssign
}
63 changes: 27 additions & 36 deletions internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"strings"
"time"

applicationErrors "github.com/checkmarx/ast-cli/internal/errors"

"github.com/MakeNowJust/heredoc"
"github.com/checkmarx/ast-cli/internal/commands/util"
"github.com/checkmarx/ast-cli/internal/commands/util/printer"
errorConstants "github.com/checkmarx/ast-cli/internal/constants/errors"
"github.com/checkmarx/ast-cli/internal/logger"
commonParams "github.com/checkmarx/ast-cli/internal/params"
"github.com/checkmarx/ast-cli/internal/services"
"github.com/spf13/viper"

"github.com/pkg/errors"
Expand All @@ -22,18 +22,13 @@ import (
)

const (
failedCreatingProj = "Failed creating a project"
failedUpdatingProj = "Failed updating a project"
failedProjectApplicationAssociation = "Failed association project to application"
failedGettingProj = "Failed getting a project"
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"
failedDeletingProj = "Failed deleting a project"
failedGettingBranches = "Failed getting branches for project"
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 @@ -214,11 +209,12 @@ func updateProjectRequestValues(input *[]byte, cmd *cobra.Command) error {
projectName, _ := cmd.Flags().GetString(commonParams.ProjectName)
mainBranch, _ := cmd.Flags().GetString(commonParams.MainBranchFlag)
_ = json.Unmarshal(*input, &info)
if projectName != "" {
info["name"] = projectName
} else {
return errors.Errorf("Project name is required")
if projectName == "" {
return errors.Errorf(errorConstants.ProjectNameIsRequired)
}

info["name"] = projectName

if mainBranch != "" {
info["mainBranch"] = mainBranch
}
Expand All @@ -233,26 +229,21 @@ func runCreateProjectCommand(
accessManagementWrapper wrappers.AccessManagementWrapper,
) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
applicationName, err := cmd.Flags().GetString(commonParams.ApplicationName)
if err != nil {
return err
}

applicationName, _ := cmd.Flags().GetString(commonParams.ApplicationName)
var applicationID []string

if applicationName != "" {
application, getAppErr := getApplication(applicationName, applicationsWrapper)
if getAppErr != nil {
return getAppErr
}
if application == nil {
return errors.Errorf(applicationErrors.ApplicationDoesntExistOrNoPermission)
return errors.Errorf(errorConstants.ApplicationDoesntExistOrNoPermission)
}
applicationID = []string{application.ID}
}

var input = []byte("{}")
err = updateProjectRequestValues(&input, cmd)
err := updateProjectRequestValues(&input, cmd)
if err != nil {
return err
}
Expand All @@ -272,26 +263,26 @@ func runCreateProjectCommand(
// Try to parse to a project model in order to manipulate the request payload
err = json.Unmarshal(input, &projModel)
if err != nil {
return errors.Wrapf(err, "%s: Input in bad format", failedCreatingProj)
return errors.Wrapf(err, "%s: Input in bad format", services.FailedCreatingProj)
}
var payload []byte
payload, _ = json.Marshal(projModel)
logger.PrintIfVerbose(fmt.Sprintf("Payload to projects service: %s\n", string(payload)))
projResponseModel, errorModel, err = projectsWrapper.Create(&projModel)
if err != nil {
return errors.Wrapf(err, "%s", failedCreatingProj)
return errors.Wrapf(err, "%s", services.FailedCreatingProj)
}

// Checking the response
if errorModel != nil {
return errors.Errorf(ErrorCodeFormat, failedCreatingProj, errorModel.Code, errorModel.Message)
return errors.Errorf(services.ErrorCodeFormat, services.FailedCreatingProj, errorModel.Code, errorModel.Message)
} else if projResponseModel != nil {
err = printByFormat(cmd, toProjectView(*projResponseModel))
if err != nil {
return errors.Wrapf(err, "%s", failedCreatingProj)
return errors.Wrapf(err, "%s", services.FailedCreatingProj)
}
}
err = assignGroupsToProjectNewAccessManagement(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
err = services.AssignGroupsToProjectNewAccessManagement(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
if err != nil {
return err
}
Expand Down Expand Up @@ -413,7 +404,7 @@ func runListProjectsCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd *

// Checking the response
if errorModel != nil {
return errors.Errorf(ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message)
return errors.Errorf(services.ErrorCodeFormat, failedGettingAll, errorModel.Code, errorModel.Message)
} else if allProjectsModel != nil && allProjectsModel.Projects != nil {
err = printByFormat(cmd, toProjectViews(allProjectsModel.Projects))
if err != nil {
Expand All @@ -431,15 +422,15 @@ func runGetProjectByIDCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd
var err error
projectID, _ := cmd.Flags().GetString(commonParams.ProjectIDFlag)
if projectID == "" {
return errors.Errorf("%s: Please provide a project ID", failedGettingProj)
return errors.Errorf("%s: Please provide a project ID", services.FailedGettingProj)
}
projectResponseModel, errorModel, err = projectsWrapper.GetByID(projectID)
if err != nil {
return errors.Wrapf(err, "%s", failedGettingProj)
return errors.Wrapf(err, "%s", services.FailedGettingProj)
}
// Checking the response
if errorModel != nil {
return errors.Errorf("%s: CODE: %d, %s", failedGettingProj, errorModel.Code, errorModel.Message)
return errors.Errorf("%s: CODE: %d, %s", services.FailedGettingProj, errorModel.Code, errorModel.Message)
} else if projectResponseModel != nil {
err = printByFormat(cmd, toProjectView(*projectResponseModel))
if err != nil {
Expand Down Expand Up @@ -504,7 +495,7 @@ func runDeleteProjectCommand(projectsWrapper wrappers.ProjectsWrapper) func(cmd
}
// Checking the response
if errorModel != nil {
return errors.Errorf(ErrorCodeFormat, failedDeletingProj, errorModel.Code, errorModel.Message)
return errors.Errorf(services.ErrorCodeFormat, failedDeletingProj, errorModel.Code, errorModel.Message)
}
return nil
}
Expand Down
Loading