diff --git a/api/http/proxy/factory/azure/transport.go b/api/http/proxy/factory/azure/transport.go index 8ea0e0760..0c8505c8b 100644 --- a/api/http/proxy/factory/azure/transport.go +++ b/api/http/proxy/factory/azure/transport.go @@ -33,7 +33,7 @@ func NewTransport(credentials *portainer.AzureCredentials) *Transport { } } -// RoundTrip is the implementation of the Transport interface. +// RoundTrip is the implementation of the the http.RoundTripper interface func (transport *Transport) RoundTrip(request *http.Request) (*http.Response, error) { err := transport.retrieveAuthenticationToken() if err != nil { diff --git a/api/http/proxy/factory/factory.go b/api/http/proxy/factory/factory.go index 2440928cb..207977f21 100644 --- a/api/http/proxy/factory/factory.go +++ b/api/http/proxy/factory/factory.go @@ -107,3 +107,8 @@ func (factory *ProxyFactory) NewEndpointProxy(endpoint *portainer.Endpoint) (htt return factory.newDockerProxy(endpoint) } + +// NewGitlabProxy returns a new HTTP proxy to a Gitlab API server +func (factory *ProxyFactory) NewGitlabProxy(gitlabAPIUri string) (http.Handler, error) { + return newGitlabProxy(gitlabAPIUri) +} diff --git a/api/http/proxy/factory/gitlab.go b/api/http/proxy/factory/gitlab.go new file mode 100644 index 000000000..cbe28411c --- /dev/null +++ b/api/http/proxy/factory/gitlab.go @@ -0,0 +1,19 @@ +package factory + +import ( + "net/http" + "net/url" + + "github.com/portainer/portainer/api/http/proxy/factory/gitlab" +) + +func newGitlabProxy(uri string) (http.Handler, error) { + url, err := url.Parse(uri) + if err != nil { + return nil, err + } + + proxy := newSingleHostReverseProxyWithHostHeader(url) + proxy.Transport = gitlab.NewTransport() + return proxy, nil +} diff --git a/api/http/proxy/factory/gitlab/transport.go b/api/http/proxy/factory/gitlab/transport.go new file mode 100644 index 000000000..4857ed710 --- /dev/null +++ b/api/http/proxy/factory/gitlab/transport.go @@ -0,0 +1,34 @@ +package gitlab + +import ( + "errors" + "net/http" +) + +type Transport struct { + httpTransport *http.Transport +} + +// NewTransport returns a pointer to a new instance of Transport that implements the HTTP Transport +// interface for proxying requests to the Gitlab API. +func NewTransport() *Transport { + return &Transport{ + httpTransport: &http.Transport{}, + } +} + +// RoundTrip is the implementation of the the http.RoundTripper interface +func (transport *Transport) RoundTrip(request *http.Request) (*http.Response, error) { + token := request.Header.Get("Private-Token") + if token == "" { + return nil, errors.New("no gitlab token provided") + } + + r, err := http.NewRequest(request.Method, request.URL.String(), nil) + if err != nil { + return nil, err + } + + r.Header.Set("Private-Token", token) + return transport.httpTransport.RoundTrip(r) +} diff --git a/api/http/proxy/gitlab.go b/api/http/proxy/gitlab.go deleted file mode 100644 index b809a09a3..000000000 --- a/api/http/proxy/gitlab.go +++ /dev/null @@ -1,38 +0,0 @@ -package proxy - -import ( - "errors" - "net/http" - "net/url" -) - -type gitlabTransport struct { - httpTransport *http.Transport -} - -func newGitlabProxy(uri string) (http.Handler, error) { - url, err := url.Parse(uri) - if err != nil { - return nil, err - } - - proxy := newSingleHostReverseProxyWithHostHeader(url) - proxy.Transport = &gitlabTransport{ - httpTransport: &http.Transport{}, - } - - return proxy, nil -} - -func (transport *gitlabTransport) RoundTrip(request *http.Request) (*http.Response, error) { - token := request.Header.Get("Private-Token") - if token == "" { - return nil, errors.New("No gitlab token provided") - } - r, err := http.NewRequest(request.Method, request.URL.String(), nil) - if err != nil { - return nil, err - } - r.Header.Set("Private-Token", token) - return transport.httpTransport.RoundTrip(r) -} diff --git a/api/http/proxy/manager.go b/api/http/proxy/manager.go index d692d4efa..fa27f6399 100644 --- a/api/http/proxy/manager.go +++ b/api/http/proxy/manager.go @@ -120,7 +120,7 @@ func (manager *Manager) DeleteExtensionProxy(extensionID portainer.ExtensionID) manager.extensionProxies.Remove(strconv.Itoa(int(extensionID))) } -// CreateLegacyExtensionProxy creates a new HTTP reverse proxy for a legacy extension and adds it to the registered proxies. +// 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) { proxy, err := manager.proxyFactory.NewLegacyExtensionProxy(extensionAPIURL) if err != nil { @@ -140,7 +140,7 @@ func (manager *Manager) GetLegacyExtensionProxy(key string) http.Handler { return proxy.(http.Handler) } -// CreateGitlabProxy creates a new HTTP reverse proxy that can be used to send requests to the Gitlab API.. +// CreateGitlabProxy creates a new HTTP reverse proxy that can be used to send requests to the Gitlab API func (manager *Manager) CreateGitlabProxy(url string) (http.Handler, error) { - return newGitlabProxy(url) + return manager.proxyFactory.NewGitlabProxy(url) }