* feat(api/bolt): backport bolt changes * feat(api/exec): backport exec changes * feat(api/http): backport http/handler/dockerhub changes * feat(api/http): backport http/handler/endpoints changes * feat(api/http): backport http/handler/registries changes * feat(api/http): backport http/handler/stacks changes * feat(api/http): backport http/handler changes * feat(api/http): backport http/proxy/factory/azure changes * feat(api/http): backport http/proxy/factory/docker changes * feat(api/http): backport http/proxy/factory/utils changes * feat(api/http): backport http/proxy/factory/kubernetes changes * feat(api/http): backport http/proxy/factory changes * feat(api/http): backport http/security changes * feat(api/http): backport http changes * feat(api/internal): backport internal changes * feat(api): backport api changes * feat(api/kubernetes): backport kubernetes changes * fix(api/http): changes on backend following backport
57 lines
1.6 KiB
Go
57 lines
1.6 KiB
Go
package docker
|
|
|
|
import (
|
|
portainer "github.com/portainer/portainer/api"
|
|
"github.com/portainer/portainer/api/http/security"
|
|
)
|
|
|
|
type (
|
|
registryAccessContext struct {
|
|
isAdmin bool
|
|
user *portainer.User
|
|
endpointID portainer.EndpointID
|
|
teamMemberships []portainer.TeamMembership
|
|
registries []portainer.Registry
|
|
}
|
|
|
|
registryAuthenticationHeader struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Serveraddress string `json:"serveraddress"`
|
|
}
|
|
|
|
portainerRegistryAuthenticationHeader struct {
|
|
RegistryId portainer.RegistryID `json:"registryId"`
|
|
}
|
|
)
|
|
|
|
func createRegistryAuthenticationHeader(registryId portainer.RegistryID, accessContext *registryAccessContext) *registryAuthenticationHeader {
|
|
var authenticationHeader *registryAuthenticationHeader
|
|
|
|
if registryId == 0 { // dockerhub (anonymous)
|
|
authenticationHeader = ®istryAuthenticationHeader{
|
|
Serveraddress: "docker.io",
|
|
}
|
|
} else { // any "custom" registry
|
|
var matchingRegistry *portainer.Registry
|
|
for _, registry := range accessContext.registries {
|
|
if registry.ID == registryId &&
|
|
(accessContext.isAdmin ||
|
|
security.AuthorizedRegistryAccess(®istry, accessContext.user, accessContext.teamMemberships, accessContext.endpointID)) {
|
|
matchingRegistry = ®istry
|
|
break
|
|
}
|
|
}
|
|
|
|
if matchingRegistry != nil {
|
|
authenticationHeader = ®istryAuthenticationHeader{
|
|
Username: matchingRegistry.Username,
|
|
Password: matchingRegistry.Password,
|
|
Serveraddress: matchingRegistry.URL,
|
|
}
|
|
}
|
|
}
|
|
|
|
return authenticationHeader
|
|
}
|