mirror of https://github.com/portainer/portainer
feat(dockerhub): EE-1384 new endpoint prefix for proxying requests to agent (#5428)
Co-authored-by: Simon Meng <simon.meng@portainer.io>pull/5456/head
parent
3b5e15aa42
commit
5fe7526de7
|
@ -29,6 +29,10 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler {
|
||||||
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToDockerAPI)))
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToDockerAPI)))
|
||||||
h.PathPrefix("/{id}/kubernetes").Handler(
|
h.PathPrefix("/{id}/kubernetes").Handler(
|
||||||
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToKubernetesAPI)))
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToKubernetesAPI)))
|
||||||
|
h.PathPrefix("/{id}/agent/docker").Handler(
|
||||||
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToDockerAPI)))
|
||||||
|
h.PathPrefix("/{id}/agent/kubernetes").Handler(
|
||||||
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToKubernetesAPI)))
|
||||||
h.PathPrefix("/{id}/storidge").Handler(
|
h.PathPrefix("/{id}/storidge").Handler(
|
||||||
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToStoridgeAPI)))
|
bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToStoridgeAPI)))
|
||||||
return h
|
return h
|
||||||
|
|
|
@ -3,6 +3,7 @@ package endpointproxy
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
httperror "github.com/portainer/libhttp/error"
|
httperror "github.com/portainer/libhttp/error"
|
||||||
|
@ -65,6 +66,12 @@ func (handler *Handler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http.
|
||||||
}
|
}
|
||||||
|
|
||||||
id := strconv.Itoa(endpointID)
|
id := strconv.Itoa(endpointID)
|
||||||
http.StripPrefix("/"+id+"/docker", proxy).ServeHTTP(w, r)
|
|
||||||
|
prefix := "/" + id + "/agent/docker";
|
||||||
|
if !strings.HasPrefix(r.URL.Path, prefix) {
|
||||||
|
prefix = "/" + id + "/docker";
|
||||||
|
}
|
||||||
|
|
||||||
|
http.StripPrefix(prefix, proxy).ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,17 +65,18 @@ func (handler *Handler) proxyRequestsToKubernetesAPI(w http.ResponseWriter, r *h
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For KubernetesLocalEnvironment
|
||||||
requestPrefix := fmt.Sprintf("/%d/kubernetes", endpointID)
|
requestPrefix := fmt.Sprintf("/%d/kubernetes", endpointID)
|
||||||
|
|
||||||
if endpoint.Type == portainer.AgentOnKubernetesEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment {
|
if endpoint.Type == portainer.AgentOnKubernetesEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment {
|
||||||
if isKubernetesRequest(strings.TrimPrefix(r.URL.String(), requestPrefix)) {
|
requestPrefix = fmt.Sprintf("/%d", endpointID)
|
||||||
requestPrefix = fmt.Sprintf("/%d", endpointID)
|
|
||||||
|
agentPrefix := fmt.Sprintf("/%d/agent/kubernetes", endpointID)
|
||||||
|
if strings.HasPrefix(r.URL.Path, agentPrefix) {
|
||||||
|
requestPrefix = agentPrefix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
http.StripPrefix(requestPrefix, proxy).ServeHTTP(w, r)
|
http.StripPrefix(requestPrefix, proxy).ServeHTTP(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isKubernetesRequest(requestURL string) bool {
|
|
||||||
return strings.HasPrefix(requestURL, "/api") || strings.HasPrefix(requestURL, "/healthz")
|
|
||||||
}
|
|
|
@ -176,6 +176,8 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
http.StripPrefix("/api/endpoints", h.EndpointProxyHandler).ServeHTTP(w, r)
|
http.StripPrefix("/api/endpoints", h.EndpointProxyHandler).ServeHTTP(w, r)
|
||||||
case strings.Contains(r.URL.Path, "/azure/"):
|
case strings.Contains(r.URL.Path, "/azure/"):
|
||||||
http.StripPrefix("/api/endpoints", h.EndpointProxyHandler).ServeHTTP(w, r)
|
http.StripPrefix("/api/endpoints", h.EndpointProxyHandler).ServeHTTP(w, r)
|
||||||
|
case strings.Contains(r.URL.Path, "/agent/"):
|
||||||
|
http.StripPrefix("/api/endpoints", h.EndpointProxyHandler).ServeHTTP(w, r)
|
||||||
case strings.Contains(r.URL.Path, "/edge/"):
|
case strings.Contains(r.URL.Path, "/edge/"):
|
||||||
http.StripPrefix("/api/endpoints", h.EndpointEdgeHandler).ServeHTTP(w, r)
|
http.StripPrefix("/api/endpoints", h.EndpointEdgeHandler).ServeHTTP(w, r)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -4,7 +4,7 @@ angular.module('portainer.agent').factory('AgentDockerhub', AgentDockerhub);
|
||||||
|
|
||||||
function AgentDockerhub($resource, API_ENDPOINT_ENDPOINTS) {
|
function AgentDockerhub($resource, API_ENDPOINT_ENDPOINTS) {
|
||||||
return $resource(
|
return $resource(
|
||||||
`${API_ENDPOINT_ENDPOINTS}/:endpointId/:endpointType/v2/dockerhub/:registryId`,
|
`${API_ENDPOINT_ENDPOINTS}/:endpointId/agent/:endpointType/v2/dockerhub/:registryId`,
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
limits: { method: 'GET', params: { registryId: '@registryId' } },
|
limits: { method: 'GET', params: { registryId: '@registryId' } },
|
||||||
|
|
Loading…
Reference in New Issue