92872435c4
* refactor(registries): move to portainer * feat(registries): show browse link * feat(registry): move registry extension code * fix(registry): revert files * refactor(registry): use component * refactor(registry): replace $scope with this * refactor(registry): use async await * refactor(registry): rename and extract * refactor(registry): rename progression-modal files * refactor(registry): replace view with component * refactor(registry): replace with component * style(regirstries): sort handler keys * feat(registry): force the recreation of a proxy client * fix(registry): ignore 404 tags
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package registryproxy
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
)
|
|
|
|
type gitlabTransport struct {
|
|
config *portainer.RegistryManagementConfiguration
|
|
httpTransport *http.Transport
|
|
}
|
|
|
|
func newGitlabRegistryProxy(uri string, config *portainer.RegistryManagementConfiguration) (http.Handler, error) {
|
|
url, err := url.Parse(uri)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
proxy := newSingleHostReverseProxyWithHostHeader(url)
|
|
proxy.Transport = &gitlabTransport{
|
|
config: config,
|
|
httpTransport: &http.Transport{},
|
|
}
|
|
|
|
return proxy, nil
|
|
}
|
|
|
|
// RoundTrip will simply check if the configuration associated to the
|
|
// custom registry has a token saved in it and add it in the request
|
|
// to authenticate on the gitlab API.
|
|
// As Gitlab API also supports Bearer token a new request is created to avoid transmitting
|
|
// Portainer Bearer Token, that will conflict with the Private-Token
|
|
func (transport *gitlabTransport) RoundTrip(request *http.Request) (*http.Response, error) {
|
|
token := transport.config.Password
|
|
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)
|
|
}
|