Skip to content

Commit

Permalink
fix: #9812 UI can now get clusters with slashes in name
Browse files Browse the repository at this point in the history
Fixes #9812

If a cluster name has a slash in it, the API would not be able
to fetch that cluster and would display "in-cluster (undefined)"
for that application. This fixes that issue by URI-encoding
the cluster name on the UI side and URI-decoding the cluster name
on the API side.

Signed-off-by: Edmund Rhudy <erhudy@users.noreply.github.com>
  • Loading branch information
erhudy committed Nov 4, 2022
1 parent e21a82f commit 60bdccf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
7 changes: 7 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cluster

import (
"net/url"
"time"

"context"
Expand Down Expand Up @@ -145,6 +146,12 @@ func (s *Server) getCluster(ctx context.Context, q *cluster.ClusterQuery) (*appv
q.Name = ""
if q.Id.Type == "name" {
q.Name = q.Id.Value
} else if q.Id.Type == "name_escaped" {
nameUnescaped, err := url.QueryUnescape(q.Id.Value)
if err != nil {
return nil, err
}
q.Name = nameUnescaped
} else {
q.Server = q.Id.Value
}
Expand Down
60 changes: 60 additions & 0 deletions server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,66 @@ func newNoopEnforcer() *rbac.Enforcer {
return enf
}

func TestGetCluster_UrlEncodedName(t *testing.T) {
db := &dbmocks.ArgoDB{}

mockCluster := v1alpha1.Cluster{
Name: "test/ing",
Server: "https://127.0.0.1",
Namespaces: []string{"default", "kube-system"},
}
mockClusterList := v1alpha1.ClusterList{
ListMeta: v1.ListMeta{},
Items: []v1alpha1.Cluster{
mockCluster,
},
}

db.On("ListClusters", mock.Anything).Return(&mockClusterList, nil)

server := NewServer(db, newNoopEnforcer(), newServerInMemoryCache(), &kubetest.MockKubectlCmd{})

cluster, err := server.Get(context.Background(), &clusterapi.ClusterQuery{
Id: &clusterapi.ClusterID{
Type: "name_escaped",
Value: "test%2fing",
},
})
require.NoError(t, err)

assert.Equal(t, cluster.Name, "test/ing")
}

func TestGetCluster_NameWithUrlEncodingButShouldNotBeUnescaped(t *testing.T) {
db := &dbmocks.ArgoDB{}

mockCluster := v1alpha1.Cluster{
Name: "test%2fing",
Server: "https://127.0.0.1",
Namespaces: []string{"default", "kube-system"},
}
mockClusterList := v1alpha1.ClusterList{
ListMeta: v1.ListMeta{},
Items: []v1alpha1.Cluster{
mockCluster,
},
}

db.On("ListClusters", mock.Anything).Return(&mockClusterList, nil)

server := NewServer(db, newNoopEnforcer(), newServerInMemoryCache(), &kubetest.MockKubectlCmd{})

cluster, err := server.Get(context.Background(), &clusterapi.ClusterQuery{
Id: &clusterapi.ClusterID{
Type: "name",
Value: "test%2fing",
},
})
require.NoError(t, err)

assert.Equal(t, cluster.Name, "test%2fing")
}

func TestUpdateCluster_NoFieldsPaths(t *testing.T) {
db := &dbmocks.ArgoDB{}
var updated *v1alpha1.Cluster
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/shared/services/clusters-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class ClustersService {
}

public get(url: string, name: string): Promise<models.Cluster> {
const requestUrl = `/clusters/${url ? encodeURIComponent(url) : name}?id.type=${url ? 'url' : 'name'}`;
const requestUrl = `/clusters/${url ? encodeURIComponent(url) : encodeURIComponent(name)}?id.type=${url ? 'url' : 'name_escaped'}`;
return requests.get(requestUrl).then(res => res.body as models.Cluster);
}

Expand Down

0 comments on commit 60bdccf

Please sign in to comment.