2017-05-23 18:56:10 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2019-07-25 22:38:07 +00:00
|
|
|
"fmt"
|
2017-05-23 18:56:10 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2018-12-09 03:49:27 +00:00
|
|
|
"strconv"
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
"github.com/orcaman/concurrent-map"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// TODO: contain code related to legacy extension management
|
|
|
|
|
|
|
|
var extensionPorts = map[portainer.ExtensionID]string{
|
2019-02-18 01:46:34 +00:00
|
|
|
portainer.RegistryManagementExtension: "7001",
|
|
|
|
portainer.OAuthAuthenticationExtension: "7002",
|
2019-05-24 06:04:58 +00:00
|
|
|
portainer.RBACExtension: "7003",
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
type (
|
|
|
|
// Manager represents a service used to manage Docker proxies.
|
|
|
|
Manager struct {
|
2018-12-09 03:49:27 +00:00
|
|
|
proxyFactory *proxyFactory
|
2019-07-25 22:38:07 +00:00
|
|
|
reverseTunnelService portainer.ReverseTunnelService
|
2018-12-09 03:49:27 +00:00
|
|
|
proxies cmap.ConcurrentMap
|
|
|
|
extensionProxies cmap.ConcurrentMap
|
|
|
|
legacyExtensionProxies cmap.ConcurrentMap
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ManagerParams represents the required parameters to create a new Manager instance.
|
|
|
|
ManagerParams struct {
|
|
|
|
ResourceControlService portainer.ResourceControlService
|
2019-09-09 22:58:26 +00:00
|
|
|
UserService portainer.UserService
|
2018-05-06 07:15:57 +00:00
|
|
|
TeamMembershipService portainer.TeamMembershipService
|
|
|
|
SettingsService portainer.SettingsService
|
|
|
|
RegistryService portainer.RegistryService
|
|
|
|
DockerHubService portainer.DockerHubService
|
|
|
|
SignatureService portainer.DigitalSignatureService
|
2019-07-25 22:38:07 +00:00
|
|
|
ReverseTunnelService portainer.ReverseTunnelService
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
|
|
|
)
|
2017-05-23 18:56:10 +00:00
|
|
|
|
|
|
|
// NewManager initializes a new proxy Service
|
2018-05-06 07:15:57 +00:00
|
|
|
func NewManager(parameters *ManagerParams) *Manager {
|
2017-05-23 18:56:10 +00:00
|
|
|
return &Manager{
|
2018-12-09 03:49:27 +00:00
|
|
|
proxies: cmap.New(),
|
|
|
|
extensionProxies: cmap.New(),
|
|
|
|
legacyExtensionProxies: cmap.New(),
|
2017-05-23 18:56:10 +00:00
|
|
|
proxyFactory: &proxyFactory{
|
2018-05-06 07:15:57 +00:00
|
|
|
ResourceControlService: parameters.ResourceControlService,
|
2019-09-09 22:58:26 +00:00
|
|
|
UserService: parameters.UserService,
|
2018-05-06 07:15:57 +00:00
|
|
|
TeamMembershipService: parameters.TeamMembershipService,
|
|
|
|
SettingsService: parameters.SettingsService,
|
|
|
|
RegistryService: parameters.RegistryService,
|
|
|
|
DockerHubService: parameters.DockerHubService,
|
|
|
|
SignatureService: parameters.SignatureService,
|
2019-07-25 22:38:07 +00:00
|
|
|
ReverseTunnelService: parameters.ReverseTunnelService,
|
2017-05-23 18:56:10 +00:00
|
|
|
},
|
2019-07-25 22:38:07 +00:00
|
|
|
reverseTunnelService: parameters.ReverseTunnelService,
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// GetProxy returns the proxy associated to a key
|
2019-07-25 22:38:07 +00:00
|
|
|
func (manager *Manager) GetProxy(endpoint *portainer.Endpoint) http.Handler {
|
|
|
|
proxy, ok := manager.proxies.Get(string(endpoint.ID))
|
2018-12-09 03:49:27 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
2018-05-06 07:15:57 +00:00
|
|
|
}
|
2018-12-09 03:49:27 +00:00
|
|
|
return proxy.(http.Handler)
|
2018-05-28 14:40:33 +00:00
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
2018-05-28 14:40:33 +00:00
|
|
|
// CreateAndRegisterProxy creates a new HTTP reverse proxy based on endpoint properties and and adds it to the registered proxies.
|
|
|
|
// It can also be used to create a new HTTP reverse proxy and replace an already registered proxy.
|
|
|
|
func (manager *Manager) CreateAndRegisterProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
|
|
|
|
proxy, err := manager.createProxy(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
manager.proxies.Set(string(endpoint.ID), proxy)
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// DeleteProxy deletes the proxy associated to a key
|
2019-07-25 22:38:07 +00:00
|
|
|
func (manager *Manager) DeleteProxy(endpoint *portainer.Endpoint) {
|
|
|
|
manager.proxies.Remove(string(endpoint.ID))
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetExtensionProxy returns an extension proxy associated to an extension identifier
|
|
|
|
func (manager *Manager) GetExtensionProxy(extensionID portainer.ExtensionID) http.Handler {
|
|
|
|
proxy, ok := manager.extensionProxies.Get(strconv.Itoa(int(extensionID)))
|
2017-05-23 18:56:10 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return proxy.(http.Handler)
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// CreateExtensionProxy creates a new HTTP reverse proxy for an extension and
|
|
|
|
// registers it in the extension map associated to the specified extension identifier
|
|
|
|
func (manager *Manager) CreateExtensionProxy(extensionID portainer.ExtensionID) (http.Handler, error) {
|
2019-06-22 23:36:45 +00:00
|
|
|
address := "http://" + portainer.ExtensionServer + ":" + extensionPorts[extensionID]
|
2018-02-23 02:10:26 +00:00
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
extensionURL, err := url.Parse(address)
|
2018-02-23 02:10:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-28 14:40:33 +00:00
|
|
|
proxy := manager.proxyFactory.newHTTPProxy(extensionURL)
|
2018-12-09 03:49:27 +00:00
|
|
|
manager.extensionProxies.Set(strconv.Itoa(int(extensionID)), proxy)
|
|
|
|
|
2018-02-23 02:10:26 +00:00
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2019-03-10 23:50:10 +00:00
|
|
|
// GetExtensionURL retrieves the URL of an extension running locally based on the extension port table
|
2019-02-18 01:46:34 +00:00
|
|
|
func (manager *Manager) GetExtensionURL(extensionID portainer.ExtensionID) string {
|
|
|
|
return "http://localhost:" + extensionPorts[extensionID]
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// DeleteExtensionProxy deletes the extension proxy associated to an extension identifier
|
|
|
|
func (manager *Manager) DeleteExtensionProxy(extensionID portainer.ExtensionID) {
|
|
|
|
manager.extensionProxies.Remove(strconv.Itoa(int(extensionID)))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLegacyExtensionProxy returns a legacy extension proxy associated to a key
|
|
|
|
func (manager *Manager) GetLegacyExtensionProxy(key string) http.Handler {
|
|
|
|
proxy, ok := manager.legacyExtensionProxies.Get(key)
|
2018-02-23 02:10:26 +00:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return proxy.(http.Handler)
|
|
|
|
}
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// CreateLegacyExtensionProxy creates a new HTTP reverse proxy for a legacy extension and adds it to the registered proxies.
|
|
|
|
func (manager *Manager) CreateLegacyExtensionProxy(key, extensionAPIURL string) (http.Handler, error) {
|
|
|
|
extensionURL, err := url.Parse(extensionAPIURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := manager.proxyFactory.newHTTPProxy(extensionURL)
|
2019-05-24 21:53:10 +00:00
|
|
|
manager.legacyExtensionProxies.Set(key, proxy)
|
2018-12-09 03:49:27 +00:00
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
func (manager *Manager) createDockerProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
|
|
|
|
baseURL := endpoint.URL
|
|
|
|
if endpoint.Type == portainer.EdgeAgentEnvironment {
|
|
|
|
tunnel := manager.reverseTunnelService.GetTunnelDetails(endpoint.ID)
|
|
|
|
baseURL = fmt.Sprintf("http://localhost:%d", tunnel.Port)
|
2018-12-09 03:49:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 22:38:07 +00:00
|
|
|
endpointURL, err := url.Parse(baseURL)
|
2018-12-09 03:49:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch endpoint.Type {
|
|
|
|
case portainer.AgentOnDockerEnvironment:
|
2019-07-25 22:38:07 +00:00
|
|
|
return manager.proxyFactory.newDockerHTTPSProxy(endpointURL, &endpoint.TLSConfig, endpoint)
|
|
|
|
case portainer.EdgeAgentEnvironment:
|
|
|
|
return manager.proxyFactory.newDockerHTTPProxy(endpointURL, endpoint), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpointURL.Scheme == "tcp" {
|
|
|
|
if endpoint.TLSConfig.TLS || endpoint.TLSConfig.TLSSkipVerify {
|
|
|
|
return manager.proxyFactory.newDockerHTTPSProxy(endpointURL, &endpoint.TLSConfig, endpoint)
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager.proxyFactory.newDockerHTTPProxy(endpointURL, endpoint), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return manager.proxyFactory.newLocalProxy(endpointURL.Path, endpoint), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *Manager) createProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
|
|
|
|
if endpoint.Type == portainer.AzureEnvironment {
|
2018-12-09 03:49:27 +00:00
|
|
|
return newAzureProxy(&endpoint.AzureCredentials)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
2019-07-25 22:38:07 +00:00
|
|
|
|
|
|
|
return manager.createDockerProxy(endpoint)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|