Skip to content

Commit

Permalink
fix: correctly handle project field during partial cluster update (#7994
Browse files Browse the repository at this point in the history
)

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
  • Loading branch information
Alexander Matyushentsev committed Dec 20, 2021
1 parent fcaa8ab commit 487db97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
16 changes: 11 additions & 5 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ package cluster
import (
"time"

"github.com/argoproj/argo-cd/v2/util/argo"

"github.com/argoproj/gitops-engine/pkg/utils/kube"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"

"github.com/argoproj/argo-cd/v2/pkg/apiclient/cluster"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
servercache "github.com/argoproj/argo-cd/v2/server/cache"
"github.com/argoproj/argo-cd/v2/server/rbacpolicy"
"github.com/argoproj/argo-cd/v2/util/argo"
"github.com/argoproj/argo-cd/v2/util/clusterauth"
"github.com/argoproj/argo-cd/v2/util/db"
"github.com/argoproj/argo-cd/v2/util/rbac"
Expand Down Expand Up @@ -187,6 +187,9 @@ var clusterFieldsByPath = map[string]func(updated *appv1.Cluster, existing *appv
"annotations": func(updated *appv1.Cluster, existing *appv1.Cluster) {
updated.Annotations = existing.Annotations
},
"project": func(updated *appv1.Cluster, existing *appv1.Cluster) {
updated.Project = existing.Project
},
}

// Update updates a cluster
Expand All @@ -203,9 +206,12 @@ func (s *Server) Update(ctx context.Context, q *cluster.ClusterUpdateRequest) (*
if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceClusters, rbacpolicy.ActionUpdate, createRBACObject(c.Project, q.Cluster.Server)); err != nil {
return nil, err
}
// verify that user can do update inside project where cluster will be located
if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceClusters, rbacpolicy.ActionUpdate, createRBACObject(q.Cluster.Project, q.Cluster.Server)); err != nil {
return nil, err

if len(q.UpdatedFields) == 0 || sets.NewString(q.UpdatedFields...).Has("project") {
// verify that user can do update inside project where cluster will be located
if err := s.enf.EnforceErr(ctx.Value("claims"), rbacpolicy.ResourceClusters, rbacpolicy.ActionUpdate, createRBACObject(q.Cluster.Project, q.Cluster.Server)); err != nil {
return nil, err
}
}

if len(q.UpdatedFields) != 0 {
Expand Down
14 changes: 14 additions & 0 deletions server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,18 @@ func TestUpdateCluster_FieldsPathSet(t *testing.T) {
assert.Equal(t, updated.Name, "minikube")
assert.Equal(t, updated.Namespaces, []string{"default", "kube-system"})
assert.Equal(t, updated.Annotations, annotationEnv)

_, err = server.Update(context.Background(), &clusterapi.ClusterUpdateRequest{
Cluster: &v1alpha1.Cluster{
Server: "https://127.0.0.1",
Project: "new-project",
},
UpdatedFields: []string{"project"},
})

require.NoError(t, err)

assert.Equal(t, updated.Name, "minikube")
assert.Equal(t, updated.Namespaces, []string{"default", "kube-system"})
assert.Equal(t, updated.Project, "new-project")
}

0 comments on commit 487db97

Please sign in to comment.