From 54145ce949162ec0cb4a2c48c16f4f29785299c4 Mon Sep 17 00:00:00 2001 From: Prabhat Khera <91852476+prabhat-org@users.noreply.github.com> Date: Thu, 21 Jul 2022 16:59:40 +1200 Subject: [PATCH] fix(kubeconfig): fix kubeconfig url EE-3455 (#7282) --- api/http/handler/helm/handler.go | 3 +-- api/http/handler/kubernetes/kubernetes_config.go | 4 +--- api/kubernetes/kubeclusteraccess_service.go | 12 +++++++++++- api/kubernetes/kubeclusteraccess_service_test.go | 2 +- app/kubernetes/helm/rest.js | 2 +- 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/api/http/handler/helm/handler.go b/api/http/handler/helm/handler.go index cb32be80e..bf03642a2 100644 --- a/api/http/handler/helm/handler.go +++ b/api/http/handler/helm/handler.go @@ -2,7 +2,6 @@ package helm import ( "net/http" - "strings" "github.com/gorilla/mux" "github.com/portainer/libhelm" @@ -108,7 +107,7 @@ func (handler *Handler) getHelmClusterAccess(r *http.Request) (*options.Kubernet hostURL := "localhost" if !sslSettings.SelfSigned { - hostURL = strings.Split(r.Host, ":")[0] + hostURL = r.Host } kubeConfigInternal := handler.kubeClusterAccessService.GetData(hostURL, endpoint.ID) diff --git a/api/http/handler/kubernetes/kubernetes_config.go b/api/http/handler/kubernetes/kubernetes_config.go index fcb8ce0d8..a5dc95b3d 100644 --- a/api/http/handler/kubernetes/kubernetes_config.go +++ b/api/http/handler/kubernetes/kubernetes_config.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/http" - "strings" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" @@ -145,8 +144,7 @@ func (handler *Handler) buildConfig(r *http.Request, tokenData *portainer.TokenD } func (handler *Handler) buildCluster(r *http.Request, endpoint portainer.Endpoint) clientV1.NamedCluster { - hostURL := strings.Split(r.Host, ":")[0] - kubeConfigInternal := handler.kubeClusterAccessService.GetData(hostURL, endpoint.ID) + kubeConfigInternal := handler.kubeClusterAccessService.GetData(r.Host, endpoint.ID) return clientV1.NamedCluster{ Name: buildClusterName(endpoint.Name), Cluster: clientV1.Cluster{ diff --git a/api/kubernetes/kubeclusteraccess_service.go b/api/kubernetes/kubeclusteraccess_service.go index dcbe204f7..f2aa6f3d9 100644 --- a/api/kubernetes/kubeclusteraccess_service.go +++ b/api/kubernetes/kubeclusteraccess_service.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" portainer "github.com/portainer/portainer/api" + "github.com/sirupsen/logrus" ) // KubeClusterAccessService represents a service that is responsible for centralizing kube cluster access data @@ -94,11 +95,20 @@ func (service *kubeClusterAccessService) IsSecure() bool { // - pass down params to binaries func (service *kubeClusterAccessService) GetData(hostURL string, endpointID portainer.EndpointID) kubernetesClusterAccessData { baseURL := service.baseURL + + // When the api call is internal, the baseURL should not be used. + if hostURL == "localhost" { + hostURL = hostURL + service.httpsBindAddr + baseURL = "/" + } + if baseURL != "/" { baseURL = fmt.Sprintf("/%s/", strings.Trim(baseURL, "/")) } - clusterURL := hostURL + service.httpsBindAddr + baseURL + logrus.Infof("[kubeconfig] [hostURL: %s, httpsBindAddr: %s, baseURL: %s]", hostURL, service.httpsBindAddr, baseURL) + + clusterURL := hostURL + baseURL clusterServerURL := fmt.Sprintf("https://%sapi/endpoints/%d/kubernetes", clusterURL, endpointID) diff --git a/api/kubernetes/kubeclusteraccess_service_test.go b/api/kubernetes/kubeclusteraccess_service_test.go index d415d86e5..3905f23cb 100644 --- a/api/kubernetes/kubeclusteraccess_service_test.go +++ b/api/kubernetes/kubeclusteraccess_service_test.go @@ -115,7 +115,7 @@ func TestKubeClusterAccessService_GetKubeConfigInternal(t *testing.T) { clusterAccessDetails := kcs.GetData("mysite.com", 1) wantClusterAccessDetails := kubernetesClusterAccessData{ - ClusterServerURL: "https://mysite.com:9443/api/endpoints/1/kubernetes", + ClusterServerURL: "https://mysite.com/api/endpoints/1/kubernetes", CertificateAuthorityFile: "", CertificateAuthorityData: "", } diff --git a/app/kubernetes/helm/rest.js b/app/kubernetes/helm/rest.js index 60dac07e5..838ef3dd2 100644 --- a/app/kubernetes/helm/rest.js +++ b/app/kubernetes/helm/rest.js @@ -5,7 +5,7 @@ angular.module('portainer.kubernetes').factory('HelmFactory', HelmFactory); /* @ngInject */ function HelmFactory($resource, API_ENDPOINT_ENDPOINTS) { const helmUrl = API_ENDPOINT_ENDPOINTS + '/:endpointId/kubernetes/helm'; - const templatesUrl = '/api/templates/helm'; + const templatesUrl = 'api/templates/helm'; return $resource( helmUrl,