2021-12-06 20:11:44 +00:00
|
|
|
package registryutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
2022-09-28 17:56:32 +00:00
|
|
|
|
2021-12-06 20:11:44 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
|
2023-10-24 16:55:11 +00:00
|
|
|
"github.com/segmentio/encoding/json"
|
2021-12-06 20:11:44 +00:00
|
|
|
)
|
|
|
|
|
2023-10-24 16:55:11 +00:00
|
|
|
type authHeader struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
ServerAddress string `json:"serveraddress"`
|
|
|
|
}
|
|
|
|
|
2021-12-06 20:11:44 +00:00
|
|
|
// GetRegistryAuthHeader generate the X-Registry-Auth header from registry
|
|
|
|
func GetRegistryAuthHeader(registry *portainer.Registry) (header string, err error) {
|
|
|
|
authHeader := authHeader{
|
|
|
|
ServerAddress: registry.URL,
|
|
|
|
}
|
|
|
|
|
|
|
|
authHeader.Username, authHeader.Password, err = GetRegEffectiveCredential(registry)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
headerData, err := json.Marshal(authHeader)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
header = base64.StdEncoding.EncodeToString(headerData)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|