mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-12 10:55:20 +02:00
5b42b6af70
Co-authored-by: Ralf Pannemans <ralf.pannemans@sap.com>
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package cnbutils
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
"github.com/docker/cli/cli/config/configfile"
|
|
)
|
|
|
|
func GenerateCnbAuth(config string, utils BuildUtils) (string, error) {
|
|
var err error
|
|
dockerConfig := &configfile.ConfigFile{}
|
|
|
|
if config != "" {
|
|
dockerConfigJSON, err := utils.FileRead(config)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
err = json.Unmarshal(dockerConfigJSON, dockerConfig)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
auth := map[string]string{}
|
|
for registry, value := range dockerConfig.AuthConfigs {
|
|
if value.Auth == "" && value.Username == "" && value.Password == "" {
|
|
log.Entry().Warnf("docker config.json contains empty credentials for registry %q. Either 'auth' or 'username' and 'password' have to be provided.", registry)
|
|
continue
|
|
}
|
|
|
|
if value.Auth == "" {
|
|
value.Auth = encodeAuth(value.Username, value.Password)
|
|
}
|
|
|
|
log.Entry().Debugf("Adding credentials for: registry %q", registry)
|
|
|
|
auth[registry] = fmt.Sprintf("Basic %s", value.Auth)
|
|
}
|
|
|
|
cnbRegistryAuth, err := json.Marshal(auth)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(cnbRegistryAuth), nil
|
|
}
|
|
|
|
func encodeAuth(username, password string) string {
|
|
auth := username + ":" + password
|
|
return base64.StdEncoding.EncodeToString([]byte(auth))
|
|
}
|