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)))
 | 
			
		||||
	h.PathPrefix("/{id}/kubernetes").Handler(
 | 
			
		||||
		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(
 | 
			
		||||
		bouncer.AuthenticatedAccess(httperror.LoggerHandler(h.proxyRequestsToStoridgeAPI)))
 | 
			
		||||
	return h
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@ package endpointproxy
 | 
			
		|||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	httperror "github.com/portainer/libhttp/error"
 | 
			
		||||
| 
						 | 
				
			
			@ -65,6 +66,12 @@ func (handler *Handler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http.
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -65,17 +65,18 @@ func (handler *Handler) proxyRequestsToKubernetesAPI(w http.ResponseWriter, r *h
 | 
			
		|||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	//  For KubernetesLocalEnvironment
 | 
			
		||||
	requestPrefix := fmt.Sprintf("/%d/kubernetes", endpointID)
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
	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)
 | 
			
		||||
		case strings.Contains(r.URL.Path, "/azure/"):
 | 
			
		||||
			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/"):
 | 
			
		||||
			http.StripPrefix("/api/endpoints", h.EndpointEdgeHandler).ServeHTTP(w, r)
 | 
			
		||||
		default:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@ angular.module('portainer.agent').factory('AgentDockerhub', AgentDockerhub);
 | 
			
		|||
 | 
			
		||||
function AgentDockerhub($resource, API_ENDPOINT_ENDPOINTS) {
 | 
			
		||||
  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' } },
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue