Skip to content

Commit

Permalink
chore(server): simplify project validation logic (#21191)
Browse files Browse the repository at this point in the history
* chore(server): simplify project validation logic

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

* improve tests

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>

---------

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
crenshaw-dev authored Dec 17, 2024
1 parent 0de5f60 commit 9203dd1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 24 deletions.
36 changes: 12 additions & 24 deletions server/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,43 +398,31 @@ func (s *Server) Update(ctx context.Context, q *project.ProjectUpdateRequest) (*
return nil, err
}

var srcValidatedApps []v1alpha1.Application
var dstValidatedApps []v1alpha1.Application
getProjectClusters := func(project string) ([]*v1alpha1.Cluster, error) {
return s.db.GetProjectClusters(ctx, project)
}

for _, a := range argo.FilterByProjects(appsList.Items, []string{q.Project.Name}) {
if oldProj.IsSourcePermitted(a.Spec.GetSource()) {
srcValidatedApps = append(srcValidatedApps, a)
}

dstPermitted, err := oldProj.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
if err != nil {
return nil, err
}

if dstPermitted {
dstValidatedApps = append(dstValidatedApps, a)
}
}

invalidSrcCount := 0
invalidDstCount := 0

for _, a := range srcValidatedApps {
if !q.Project.IsSourcePermitted(a.Spec.GetSource()) {
for _, a := range argo.FilterByProjects(appsList.Items, []string{q.Project.Name}) {
if oldProj.IsSourcePermitted(a.Spec.GetSource()) && !q.Project.IsSourcePermitted(a.Spec.GetSource()) {
invalidSrcCount++
}
}
for _, a := range dstValidatedApps {
dstPermitted, err := q.Project.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)

dstPermitted, err := oldProj.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
if err != nil {
return nil, err
}

if !dstPermitted {
invalidDstCount++
if dstPermitted {
dstPermitted, err := q.Project.IsDestinationPermitted(a.Spec.Destination, getProjectClusters)
if err != nil {
return nil, err
}
if !dstPermitted {
invalidDstCount++
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions server/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ func TestProjectServer(t *testing.T) {
require.Error(t, err)
statusCode, _ := status.FromError(err)
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
assert.Equal(t, "as a result of project update 1 applications destination became invalid", statusCode.Message())
})

t.Run("TestRemoveSourceSuccessful", func(t *testing.T) {
Expand Down Expand Up @@ -232,6 +233,7 @@ func TestProjectServer(t *testing.T) {
require.Error(t, err)
statusCode, _ := status.FromError(err)
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
assert.Equal(t, "as a result of project update 1 applications source became invalid", statusCode.Message())
})

t.Run("TestRemoveSourceUsedByAppSuccessfulIfPermittedByAnotherSrc", func(t *testing.T) {
Expand Down Expand Up @@ -318,6 +320,7 @@ func TestProjectServer(t *testing.T) {
require.Error(t, err)
statusCode, _ := status.FromError(err)
assert.Equal(t, codes.InvalidArgument, statusCode.Code())
assert.Equal(t, "project is referenced by 1 applications", statusCode.Message())
})

// configure a user named "admin" which is denied by default
Expand Down

0 comments on commit 9203dd1

Please sign in to comment.