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

Project Application association Issue - Workaround - AST-40286 #714

Merged
merged 13 commits into from
Apr 30, 2024
Merged
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ golang.org/x/crypto v0.22.0 h1:g1v0xeRhjcugydODzvb3mEM9SQ0HGp9s/nh3COQ/C30=
golang.org/x/crypto v0.22.0/go.mod h1:vr6Su+7cTlO45qkww3VDJlzDn0ctJvRgYbC2NvXHt+M=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
OrShamirCM marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/commands/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func getGroupIds(groups []*wrappers.Group) []string {
return groupIds
}

func assignGroupsToProject(projectID string, projectName string, groups []*wrappers.Group,
func assignGroupsToProjectNewAccessManagement(projectID string, projectName string, groups []*wrappers.Group,
accessManagement wrappers.AccessManagementWrapper) error {
if !wrappers.FeatureFlags[accessManagementEnabled] {
return nil
Expand Down
26 changes: 13 additions & 13 deletions internal/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
)

const (
failedCreatingProj = "Failed creating a project"
failedUpdatingProj = "Failed updating a project"

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"
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"
)

var (
Expand Down Expand Up @@ -291,7 +291,7 @@ func runCreateProjectCommand(
return errors.Wrapf(err, "%s", failedCreatingProj)
}
}
err = assignGroupsToProject(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
err = assignGroupsToProjectNewAccessManagement(projResponseModel.ID, projResponseModel.Name, groups, accessManagementWrapper)
if err != nil {
return err
}
Expand Down
107 changes: 87 additions & 20 deletions internal/commands/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path"
"path/filepath"
"reflect"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -603,6 +604,7 @@ func findProject(
projectsWrapper wrappers.ProjectsWrapper,
groupsWrapper wrappers.GroupsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper,
applicationWrapper wrappers.ApplicationsWrapper,
) (string, error) {
params := make(map[string]string)
params["names"] = projectName
Expand All @@ -613,10 +615,10 @@ func findProject(

for i := 0; i < len(resp.Projects); i++ {
if resp.Projects[i].Name == projectName {
return updateProject(resp, cmd, projectsWrapper, groupsWrapper, accessManagementWrapper, projectName, applicationID)
return updateProject(resp, cmd, projectsWrapper, groupsWrapper, accessManagementWrapper, applicationWrapper, projectName, applicationID)
}
}
projectID, err := createProject(projectName, cmd, projectsWrapper, groupsWrapper, accessManagementWrapper, applicationID)
projectID, err := createProject(projectName, cmd, projectsWrapper, groupsWrapper, accessManagementWrapper, applicationWrapper, applicationID)
if err != nil {
return "", err
}
Expand All @@ -629,18 +631,15 @@ func createProject(
projectsWrapper wrappers.ProjectsWrapper,
groupsWrapper wrappers.GroupsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper,
applicationsWrapper wrappers.ApplicationsWrapper,
applicationID []string,
) (string, error) {
projectGroups, _ := cmd.Flags().GetString(commonParams.ProjectGroupList)
projectTags, _ := cmd.Flags().GetString(commonParams.ProjectTagList)
projectPrivatePackage, _ := cmd.Flags().GetString(commonParams.ProjecPrivatePackageFlag)
groupsMap, err := createGroupsMap(projectGroups, groupsWrapper)
if err != nil {
return "", err
}

var projModel = wrappers.Project{}
projModel.Name = projectName
projModel.Groups = getGroupsForRequest(groupsMap)
projModel.ApplicationIds = applicationID

if projectPrivatePackage != "" {
Expand All @@ -654,17 +653,54 @@ func createProject(
}
if err == nil {
projectID = resp.ID
err = assignGroupsToProject(projectID, projectName, groupsMap, accessManagementWrapper)

if len(applicationID) > 0 {
err = verifyApplicationAssociationDone(applicationID, projectID, applicationsWrapper)
if err != nil {
return projectID, err
}
}

if projectGroups != "" {
err = UpsertProjectGroups(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, nil, projectGroups, projectID, projectName)
if err != nil {
return projectID, err
}
}
}
return projectID, err
}

func verifyApplicationAssociationDone(applicationID []string, projectID string, applicationsWrapper wrappers.ApplicationsWrapper) error {
var applicationRes *wrappers.ApplicationsResponseModel
var err error
params := make(map[string]string)
params["id"] = applicationID[0]

logger.PrintIfVerbose("polling application until project association done or timeout of 2 min")
start := time.Now()
timeout := 2 * time.Minute
for applicationRes != nil && len(applicationRes.Applications) > 0 &&
!slices.Contains(applicationRes.Applications[0].ProjectIds, projectID) {
applicationRes, err = applicationsWrapper.Get(params)
if err != nil {
return err
} else if time.Since(start) < timeout {
return errors.Errorf("%s: %v", failedProjectApplicationAssociation, "timeout of 2 min for association")
}
}

logger.PrintIfVerbose("application association done successfully")
return nil
}

func updateProject(
resp *wrappers.ProjectsCollectionResponseModel,
cmd *cobra.Command,
projectsWrapper wrappers.ProjectsWrapper,
groupsWrapper wrappers.GroupsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper,
applicationsWrapper wrappers.ApplicationsWrapper,
projectName string,
applicationID []string,

Expand Down Expand Up @@ -703,18 +739,7 @@ func updateProject(
projModel.Groups = projModelResp.Groups
projModel.Tags = projModelResp.Tags
projModel.ApplicationIds = projModelResp.ApplicationIds
if projectGroups != "" {
groupsMap, groupErr := createGroupsMap(projectGroups, groupsWrapper)
if groupErr != nil {
return "", errors.Errorf("%s: %v", failedUpdatingProj, groupErr)
}
logger.PrintIfVerbose("Updating project groups")
projModel.Groups = getGroupsForRequest(groupsMap)
err = assignGroupsToProject(projectID, projectName, groupsMap, accessManagementWrapper)
if err != nil {
return "", err
}
}

if projectTags != "" {
logger.PrintIfVerbose("Updating project tags")
projModel.Tags = createTagMap(projectTags)
Expand All @@ -727,9 +752,50 @@ func updateProject(
if err != nil {
return "", errors.Errorf("%s: %v", failedUpdatingProj, err)
}

if len(applicationID) > 0 {
err = verifyApplicationAssociationDone(applicationID, projectID, applicationsWrapper)
if err != nil {
return projectID, err
}
}

if projectGroups != "" {
err = UpsertProjectGroups(groupsWrapper, &projModel, projectsWrapper, accessManagementWrapper, projModelResp, projectGroups, projectID, projectName)
if err != nil {
return projectID, err
}
}
return projectID, nil
}

func UpsertProjectGroups(groupsWrapper wrappers.GroupsWrapper, projModel *wrappers.Project, projectsWrapper wrappers.ProjectsWrapper,
accessManagementWrapper wrappers.AccessManagementWrapper, projModelResp *wrappers.ProjectResponseModel,
projectGroups string, projectID string, projectName string) error {
groupsMap, groupErr := createGroupsMap(projectGroups, groupsWrapper)
if groupErr != nil {
return errors.Errorf("%s: %v", failedUpdatingProj, groupErr)
}

projModel.Groups = getGroupsForRequest(groupsMap)
if projModelResp != nil {
groups := append(getGroupsForRequest(groupsMap), projModelResp.Groups...)
projModel.Groups = groups
}

err := assignGroupsToProjectNewAccessManagement(projectID, projectName, groupsMap, accessManagementWrapper)
if err != nil {
return err
}

logger.PrintIfVerbose("Updating project groups")
err = projectsWrapper.Update(projectID, projModel)
if err != nil {
return errors.Errorf("%s: %v", failedUpdatingProj, err)
}
return nil
}

func createApplicationIds(applicationID, existingApplicationIds []string) []string {
for _, id := range applicationID {
if !util.Contains(existingApplicationIds, id) {
Expand Down Expand Up @@ -828,6 +894,7 @@ func setupScanTypeProjectAndConfig(
projectsWrapper,
groupsWrapper,
accessManagementWrapper,
applicationsWrapper,
)
if findProjectErr != nil {
return findProjectErr
Expand Down
4 changes: 2 additions & 2 deletions internal/commands/scan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ func TestCreateScanBranches(t *testing.T) {
func TestCreateScanWithProjectGroup(t *testing.T) {
err := execCmdNotNilAssertion(
t,
"scan", "create", "--project-name", "invalidGroup", "-s", ".", "--project-groups", "invalidGroup",
"scan", "create", "--project-name", "invalidGroup", "-s", ".", "--branch", "main", "--project-groups", "invalidGroup",
)
assert.Assert(t, err.Error() == "Failed finding groups: [invalidGroup]", "\n the received error is:", err.Error())
assert.Assert(t, err.Error() == "Failed updating a project: Failed finding groups: [invalidGroup]", "\n the received error is:", err.Error())
}

func TestScanWorkflowMissingID(t *testing.T) {
Expand Down
Loading