From 02c7bbefb8841e5e86a4b9d467defa668e3ea613 Mon Sep 17 00:00:00 2001 From: Josh Hawn Date: Tue, 23 Jun 2015 12:58:17 -0700 Subject: [PATCH] [api, builder] Fix build auth config With the 1.7 release, we introduced a change to how we store registry credentials, but the build API endpoint did not expect a change in the format of that file. This patch fixes this problem so that you can again pull private images during `docker build`. Docker-DCO-1.1-Signed-off-by: Josh Hawn (github: jlhawn) --- api/server/server.go | 20 +++++++++----------- builder/evaluator.go | 4 ++-- builder/internals.go | 13 +++++++++---- builder/job.go | 11 ++++------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/api/server/server.go b/api/server/server.go index 036f1dc34b..ea93916c83 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -1221,18 +1221,17 @@ func (s *Server) getImagesByName(version version.Version, w http.ResponseWriter, func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error { var ( - authConfig = &cliconfig.AuthConfig{} - configFileEncoded = r.Header.Get("X-Registry-Config") - configFile = &cliconfig.ConfigFile{} - buildConfig = builder.NewBuildConfig() + authConfigs = map[string]cliconfig.AuthConfig{} + authConfigsEncoded = r.Header.Get("X-Registry-Config") + buildConfig = builder.NewBuildConfig() ) - if configFileEncoded != "" { - configFileJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(configFileEncoded)) - if err := json.NewDecoder(configFileJson).Decode(configFile); err != nil { + if authConfigsEncoded != "" { + authConfigsJSON := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authConfigsEncoded)) + if err := json.NewDecoder(authConfigsJSON).Decode(&authConfigs); err != nil { // for a pull it is not an error if no auth was given - // to increase compatibility with the existing api it is defaulting to be empty - configFile = &cliconfig.ConfigFile{} + // to increase compatibility with the existing api it is defaulting + // to be empty. } } @@ -1259,8 +1258,7 @@ func (s *Server) postBuild(version version.Version, w http.ResponseWriter, r *ht buildConfig.SuppressOutput = boolValue(r, "q") buildConfig.NoCache = boolValue(r, "nocache") buildConfig.ForceRemove = boolValue(r, "forcerm") - buildConfig.AuthConfig = authConfig - buildConfig.ConfigFile = configFile + buildConfig.AuthConfigs = authConfigs buildConfig.MemorySwap = int64ValueOrZero(r, "memswap") buildConfig.Memory = int64ValueOrZero(r, "memory") buildConfig.CpuShares = int64ValueOrZero(r, "cpushares") diff --git a/builder/evaluator.go b/builder/evaluator.go index 62f86bb889..24f15c77ec 100644 --- a/builder/evaluator.go +++ b/builder/evaluator.go @@ -98,8 +98,8 @@ type Builder struct { // the final configs of the Dockerfile but dont want the layers disableCommit bool - AuthConfig *cliconfig.AuthConfig - ConfigFile *cliconfig.ConfigFile + // Registry server auth configs used to pull images when handling `FROM`. + AuthConfigs map[string]cliconfig.AuthConfig // Deprecated, original writer used for ImagePull. To be removed. OutOld io.Writer diff --git a/builder/internals.go b/builder/internals.go index f471382ae4..63cbaff497 100644 --- a/builder/internals.go +++ b/builder/internals.go @@ -21,6 +21,7 @@ import ( "github.com/Sirupsen/logrus" "github.com/docker/docker/builder/parser" + "github.com/docker/docker/cliconfig" "github.com/docker/docker/daemon" "github.com/docker/docker/graph" imagepkg "github.com/docker/docker/image" @@ -454,15 +455,19 @@ func (b *Builder) pullImage(name string) (*imagepkg.Image, error) { tag = "latest" } - pullRegistryAuth := b.AuthConfig - if len(b.ConfigFile.AuthConfigs) > 0 { + pullRegistryAuth := &cliconfig.AuthConfig{} + if len(b.AuthConfigs) > 0 { // The request came with a full auth config file, we prefer to use that repoInfo, err := b.Daemon.RegistryService.ResolveRepository(remote) if err != nil { return nil, err } - resolvedAuth := registry.ResolveAuthConfig(b.ConfigFile, repoInfo.Index) - pullRegistryAuth = &resolvedAuth + + resolvedConfig := registry.ResolveAuthConfig( + &cliconfig.ConfigFile{AuthConfigs: b.AuthConfigs}, + repoInfo.Index, + ) + pullRegistryAuth = &resolvedConfig } imagePullConfig := &graph.ImagePullConfig{ diff --git a/builder/job.go b/builder/job.go index 6668fa947a..26b37b6ccc 100644 --- a/builder/job.go +++ b/builder/job.go @@ -59,8 +59,7 @@ type Config struct { CpuSetCpus string CpuSetMems string CgroupParent string - AuthConfig *cliconfig.AuthConfig - ConfigFile *cliconfig.ConfigFile + AuthConfigs map[string]cliconfig.AuthConfig Stdout io.Writer Context io.ReadCloser @@ -85,9 +84,8 @@ func (b *Config) WaitCancelled() <-chan struct{} { func NewBuildConfig() *Config { return &Config{ - AuthConfig: &cliconfig.AuthConfig{}, - ConfigFile: &cliconfig.ConfigFile{}, - cancelled: make(chan struct{}), + AuthConfigs: map[string]cliconfig.AuthConfig{}, + cancelled: make(chan struct{}), } } @@ -190,8 +188,7 @@ func Build(d *daemon.Daemon, buildConfig *Config) error { Pull: buildConfig.Pull, OutOld: buildConfig.Stdout, StreamFormatter: sf, - AuthConfig: buildConfig.AuthConfig, - ConfigFile: buildConfig.ConfigFile, + AuthConfigs: buildConfig.AuthConfigs, dockerfileName: buildConfig.DockerfileName, cpuShares: buildConfig.CpuShares, cpuPeriod: buildConfig.CpuPeriod,