You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
feat(cnbBuild): remove docker config after parsing (#3417)
Co-authored-by: Philipp Stehle <philipp.stehle@sap.com>
This commit is contained in:
@@ -2,7 +2,6 @@ package cmd
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
@@ -21,7 +20,6 @@ import (
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
"github.com/SAP/jenkins-library/pkg/telemetry"
|
||||
"github.com/docker/cli/cli/config/configfile"
|
||||
"github.com/pkg/errors"
|
||||
ignore "github.com/sabhiram/go-gitignore"
|
||||
)
|
||||
@@ -295,36 +293,12 @@ func runCnbBuild(config *cnbBuildOptions, telemetryData *telemetry.CustomData, u
|
||||
}
|
||||
|
||||
dockerConfigFile := ""
|
||||
dockerConfig := &configfile.ConfigFile{}
|
||||
dockerConfigJSON := []byte(`{"auths":{}}`)
|
||||
if len(config.DockerConfigJSON) > 0 {
|
||||
dockerConfigFile, err = prepareDockerConfig(config.DockerConfigJSON, utils)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.Wrapf(err, "failed to rename DockerConfigJSON file '%v'", config.DockerConfigJSON)
|
||||
}
|
||||
dockerConfigJSON, err = utils.FileRead(dockerConfigFile)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.Wrapf(err, "failed to read DockerConfigJSON file '%v'", config.DockerConfigJSON)
|
||||
}
|
||||
}
|
||||
|
||||
err = json.Unmarshal(dockerConfigJSON, dockerConfig)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.Wrapf(err, "failed to parse DockerConfigJSON file '%v'", config.DockerConfigJSON)
|
||||
}
|
||||
|
||||
auth := map[string]string{}
|
||||
for registry, value := range dockerConfig.AuthConfigs {
|
||||
auth[registry] = fmt.Sprintf("Basic %s", value.Auth)
|
||||
}
|
||||
|
||||
cnbRegistryAuth, err := json.Marshal(auth)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.Wrap(err, "failed to marshal DockerConfigJSON")
|
||||
}
|
||||
|
||||
target := "/workspace"
|
||||
@@ -374,6 +348,20 @@ func runCnbBuild(config *cnbBuildOptions, telemetryData *telemetry.CustomData, u
|
||||
}
|
||||
}
|
||||
|
||||
cnbRegistryAuth, err := cnbutils.GenerateCnbAuth(dockerConfigFile, utils)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.Wrap(err, "failed to generate CNB_REGISTRY_AUTH")
|
||||
}
|
||||
|
||||
if dockerConfigFile != "" {
|
||||
err = utils.FileRemove(dockerConfigFile)
|
||||
if err != nil {
|
||||
log.SetErrorCategory(log.ErrorBuild)
|
||||
return errors.Wrap(err, "failed to remove docker config.json file")
|
||||
}
|
||||
}
|
||||
|
||||
if len(config.ContainerRegistryURL) == 0 || len(config.ContainerImageName) == 0 || len(config.ContainerImageTag) == 0 {
|
||||
log.SetErrorCategory(log.ErrorConfiguration)
|
||||
return errors.New("containerRegistryUrl, containerImageName and containerImageTag must be present")
|
||||
@@ -411,7 +399,7 @@ func runCnbBuild(config *cnbBuildOptions, telemetryData *telemetry.CustomData, u
|
||||
log.Entry().Info("skipping certificates update")
|
||||
}
|
||||
|
||||
utils.AppendEnv([]string{fmt.Sprintf("CNB_REGISTRY_AUTH=%s", string(cnbRegistryAuth))})
|
||||
utils.AppendEnv([]string{fmt.Sprintf("CNB_REGISTRY_AUTH=%s", cnbRegistryAuth)})
|
||||
utils.AppendEnv([]string{"CNB_PLATFORM_API=0.8"})
|
||||
|
||||
creatorArgs := []string{
|
||||
|
||||
@@ -17,7 +17,6 @@ func newCnbBuildTestsUtils() cnbutils.MockUtils {
|
||||
utils := cnbutils.MockUtils{
|
||||
ExecMockRunner: &mock.ExecMockRunner{},
|
||||
FilesMock: &mock.FilesMock{},
|
||||
DockerMock: &cnbutils.DockerMock{},
|
||||
}
|
||||
return utils
|
||||
}
|
||||
@@ -118,6 +117,12 @@ func TestRunCnbBuild(t *testing.T) {
|
||||
assert.Contains(t, runner.Calls[0].Params, "/tmp/buildpacks/order.toml")
|
||||
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:%s", registry, config.ContainerImageName, config.ContainerImageTag))
|
||||
assert.Contains(t, runner.Calls[0].Params, fmt.Sprintf("%s/%s:latest", registry, config.ContainerImageName))
|
||||
|
||||
initialFileExists, _ := utils.FileExists("/path/to/test.json")
|
||||
renamedFileExists, _ := utils.FileExists("/path/to/config.json")
|
||||
|
||||
assert.False(t, initialFileExists)
|
||||
assert.False(t, renamedFileExists)
|
||||
})
|
||||
|
||||
t.Run("success case (customTlsCertificates)", func(t *testing.T) {
|
||||
@@ -200,7 +205,7 @@ func TestRunCnbBuild(t *testing.T) {
|
||||
addBuilderFiles(&utils)
|
||||
|
||||
err := runCnbBuild(&config, nil, &utils, &commonPipelineEnvironment, &piperhttp.Client{})
|
||||
assert.EqualError(t, err, "failed to parse DockerConfigJSON file '/path/to/config.json': json: cannot unmarshal string into Go struct field ConfigFile.auths of type types.AuthConfig")
|
||||
assert.EqualError(t, err, "failed to generate CNB_REGISTRY_AUTH: json: cannot unmarshal string into Go struct field ConfigFile.auths of type types.AuthConfig")
|
||||
})
|
||||
|
||||
t.Run("error case: DockerConfigJSON file not there (config.json)", func(t *testing.T) {
|
||||
@@ -215,7 +220,7 @@ func TestRunCnbBuild(t *testing.T) {
|
||||
addBuilderFiles(&utils)
|
||||
|
||||
err := runCnbBuild(&config, nil, &utils, &commonPipelineEnvironment, &piperhttp.Client{})
|
||||
assert.EqualError(t, err, "failed to read DockerConfigJSON file 'not-there/config.json': could not read 'not-there/config.json'")
|
||||
assert.EqualError(t, err, "failed to generate CNB_REGISTRY_AUTH: could not read 'not-there/config.json'")
|
||||
})
|
||||
|
||||
t.Run("error case: DockerConfigJSON file not there (not config.json)", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user