2022-01-11 11:01:15 +02:00
|
|
|
package cnbutils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2022-03-31 15:02:06 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2022-01-11 11:01:15 +02:00
|
|
|
"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 {
|
2022-03-31 15:02:06 +02:00
|
|
|
log.Entry().Debugf("Adding credentials for: registry %q", registry)
|
|
|
|
|
2022-01-11 11:01:15 +02:00
|
|
|
auth[registry] = fmt.Sprintf("Basic %s", value.Auth)
|
|
|
|
}
|
|
|
|
|
|
|
|
cnbRegistryAuth, err := json.Marshal(auth)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(cnbRegistryAuth), nil
|
|
|
|
}
|