2017-05-23 18:56:10 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2017-10-15 17:24:40 +00:00
|
|
|
"net"
|
2017-05-23 18:56:10 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
"github.com/portainer/portainer/crypto"
|
|
|
|
)
|
|
|
|
|
2018-06-01 14:13:24 +00:00
|
|
|
// AzureAPIBaseURL is the URL where Azure API requests will be proxied.
|
|
|
|
const AzureAPIBaseURL = "https://management.azure.com"
|
2018-05-28 14:40:33 +00:00
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
// proxyFactory is a factory to create reverse proxies to Docker endpoints
|
|
|
|
type proxyFactory struct {
|
|
|
|
ResourceControlService portainer.ResourceControlService
|
|
|
|
TeamMembershipService portainer.TeamMembershipService
|
2017-06-01 08:14:55 +00:00
|
|
|
SettingsService portainer.SettingsService
|
2018-03-22 22:44:43 +00:00
|
|
|
RegistryService portainer.RegistryService
|
|
|
|
DockerHubService portainer.DockerHubService
|
2018-05-06 07:15:57 +00:00
|
|
|
SignatureService portainer.DigitalSignatureService
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 14:40:33 +00:00
|
|
|
func (factory *proxyFactory) newHTTPProxy(u *url.URL) http.Handler {
|
2017-05-23 18:56:10 +00:00
|
|
|
u.Scheme = "http"
|
2018-02-23 02:10:26 +00:00
|
|
|
return newSingleHostReverseProxyWithHostHeader(u)
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 14:40:33 +00:00
|
|
|
func newAzureProxy(credentials *portainer.AzureCredentials) (http.Handler, error) {
|
2018-06-01 14:13:24 +00:00
|
|
|
url, err := url.Parse(AzureAPIBaseURL)
|
2018-05-28 14:40:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := newSingleHostReverseProxyWithHostHeader(url)
|
|
|
|
proxy.Transport = NewAzureTransport(credentials)
|
|
|
|
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
func (factory *proxyFactory) newDockerHTTPSProxy(u *url.URL, tlsConfig *portainer.TLSConfiguration, enableSignature bool) (http.Handler, error) {
|
2017-05-23 18:56:10 +00:00
|
|
|
u.Scheme = "https"
|
2018-05-06 07:15:57 +00:00
|
|
|
|
|
|
|
proxy := factory.createDockerReverseProxy(u, enableSignature)
|
2018-05-19 14:25:11 +00:00
|
|
|
config, err := crypto.CreateTLSConfigurationFromDisk(tlsConfig.TLSCACertPath, tlsConfig.TLSCertPath, tlsConfig.TLSKeyPath, tlsConfig.TLSSkipVerify)
|
2017-05-23 18:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy.Transport.(*proxyTransport).dockerTransport.TLSClientConfig = config
|
|
|
|
return proxy, nil
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
func (factory *proxyFactory) newDockerHTTPProxy(u *url.URL, enableSignature bool) http.Handler {
|
2018-02-23 02:10:26 +00:00
|
|
|
u.Scheme = "http"
|
2018-05-06 07:15:57 +00:00
|
|
|
return factory.createDockerReverseProxy(u, enableSignature)
|
2018-02-23 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
func (factory *proxyFactory) createDockerReverseProxy(u *url.URL, enableSignature bool) *httputil.ReverseProxy {
|
2017-05-23 18:56:10 +00:00
|
|
|
proxy := newSingleHostReverseProxyWithHostHeader(u)
|
|
|
|
transport := &proxyTransport{
|
2018-05-06 07:15:57 +00:00
|
|
|
enableSignature: enableSignature,
|
2017-05-23 18:56:10 +00:00
|
|
|
ResourceControlService: factory.ResourceControlService,
|
|
|
|
TeamMembershipService: factory.TeamMembershipService,
|
2017-06-01 08:14:55 +00:00
|
|
|
SettingsService: factory.SettingsService,
|
2018-03-22 22:44:43 +00:00
|
|
|
RegistryService: factory.RegistryService,
|
|
|
|
DockerHubService: factory.DockerHubService,
|
2018-02-23 02:10:26 +00:00
|
|
|
dockerTransport: &http.Transport{},
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
|
|
|
|
if enableSignature {
|
|
|
|
transport.SignatureService = factory.SignatureService
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
proxy.Transport = transport
|
|
|
|
return proxy
|
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
func newSocketTransport(socketPath string) *http.Transport {
|
|
|
|
return &http.Transport{
|
|
|
|
Dial: func(proto, addr string) (conn net.Conn, err error) {
|
|
|
|
return net.Dial("unix", socketPath)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|