From b9731bccf2b2cc8c06ef8bdb41f72ca4791e63db Mon Sep 17 00:00:00 2001 From: Victor Vieux Date: Thu, 10 Apr 2014 22:29:03 +0000 Subject: [PATCH] refactor build job to know only configFile Docker-DCO-1.1-Signed-off-by: Victor Vieux (github: vieux) --- api/server/server.go | 11 ++++++++--- integration/buildfile_test.go | 6 +++--- server/buildfile.go | 18 +++--------------- server/server.go | 19 ++++++++++++------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/api/server/server.go b/api/server/server.go index de75ad7c16..a734490c01 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -829,8 +829,6 @@ func postBuild(eng *engine.Engine, version version.Version, w http.ResponseWrite return fmt.Errorf("Multipart upload for build is no longer supported. Please upgrade your docker client.") } var ( - authEncoded = r.Header.Get("X-Registry-Auth") - authConfig = ®istry.AuthConfig{} configFileEncoded = r.Header.Get("X-Registry-Config") configFile = ®istry.ConfigFile{} job = eng.Job("build") @@ -840,14 +838,22 @@ func postBuild(eng *engine.Engine, version version.Version, w http.ResponseWrite // Both headers will be parsed and sent along to the daemon, but if a non-empty // ConfigFile is present, any value provided as an AuthConfig directly will // be overridden. See BuildFile::CmdFrom for details. + // /* + var ( + authEncoded = r.Header.Get("X-Registry-Auth") + authConfig = ®istry.AuthConfig{} + ) if version.LessThan("1.9") && authEncoded != "" { authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded)) if err := json.NewDecoder(authJson).Decode(authConfig); 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 authConfig = ®istry.AuthConfig{} + } else { + configFile.Configs[authConfig.ServerAddress] = *authConfig } } + // */ if configFileEncoded != "" { configFileJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(configFileEncoded)) @@ -870,7 +876,6 @@ func postBuild(eng *engine.Engine, version version.Version, w http.ResponseWrite job.Setenv("q", r.FormValue("q")) job.Setenv("nocache", r.FormValue("nocache")) job.Setenv("rm", r.FormValue("rm")) - job.SetenvJson("authConfig", authConfig) job.SetenvJson("configFile", configFile) if err := job.Run(); err != nil { diff --git a/integration/buildfile_test.go b/integration/buildfile_test.go index bb864a5a12..0c5335426c 100644 --- a/integration/buildfile_test.go +++ b/integration/buildfile_test.go @@ -394,7 +394,7 @@ func buildImage(context testContextTemplate, t *testing.T, eng *engine.Engine, u } dockerfile := constructDockerfile(context.dockerfile, ip, port) - buildfile := server.NewBuildFile(srv, ioutil.Discard, ioutil.Discard, false, useCache, false, ioutil.Discard, utils.NewStreamFormatter(false), nil, nil) + buildfile := server.NewBuildFile(srv, ioutil.Discard, ioutil.Discard, false, useCache, false, ioutil.Discard, utils.NewStreamFormatter(false), nil) id, err := buildfile.Build(context.Archive(dockerfile, t)) if err != nil { return nil, err @@ -828,7 +828,7 @@ func TestForbiddenContextPath(t *testing.T) { } dockerfile := constructDockerfile(context.dockerfile, ip, port) - buildfile := server.NewBuildFile(srv, ioutil.Discard, ioutil.Discard, false, true, false, ioutil.Discard, utils.NewStreamFormatter(false), nil, nil) + buildfile := server.NewBuildFile(srv, ioutil.Discard, ioutil.Discard, false, true, false, ioutil.Discard, utils.NewStreamFormatter(false), nil) _, err = buildfile.Build(context.Archive(dockerfile, t)) if err == nil { @@ -874,7 +874,7 @@ func TestBuildADDFileNotFound(t *testing.T) { } dockerfile := constructDockerfile(context.dockerfile, ip, port) - buildfile := server.NewBuildFile(mkServerFromEngine(eng, t), ioutil.Discard, ioutil.Discard, false, true, false, ioutil.Discard, utils.NewStreamFormatter(false), nil, nil) + buildfile := server.NewBuildFile(mkServerFromEngine(eng, t), ioutil.Discard, ioutil.Discard, false, true, false, ioutil.Discard, utils.NewStreamFormatter(false), nil) _, err = buildfile.Build(context.Archive(dockerfile, t)) if err == nil { diff --git a/server/buildfile.go b/server/buildfile.go index bf7631abfb..7cd1e9971b 100644 --- a/server/buildfile.go +++ b/server/buildfile.go @@ -49,7 +49,6 @@ type buildFile struct { utilizeCache bool rm bool - authConfig *registry.AuthConfig configFile *registry.ConfigFile tmpContainers map[string]struct{} @@ -80,20 +79,10 @@ func (b *buildFile) CmdFrom(name string) error { if err != nil { if b.runtime.Graph().IsNotExist(err) { remote, tag := utils.ParseRepositoryTag(name) - pullRegistryAuth := b.authConfig - if len(b.configFile.Configs) > 0 { - // The request came with a full auth config file, we prefer to use that - endpoint, _, err := registry.ResolveRepositoryName(remote) - if err != nil { - return err - } - resolvedAuth := b.configFile.ResolveAuthConfig(endpoint) - pullRegistryAuth = &resolvedAuth - } job := b.srv.Eng.Job("pull", remote, tag) job.SetenvBool("json", b.sf.Json()) job.SetenvBool("parallel", true) - job.SetenvJson("authConfig", pullRegistryAuth) + job.SetenvJson("configFile", b.configFile) job.Stdout.Add(b.outOld) if err := job.Run(); err != nil { return err @@ -832,7 +821,7 @@ func stripComments(raw []byte) string { return strings.Join(out, "\n") } -func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, outOld io.Writer, sf *utils.StreamFormatter, auth *registry.AuthConfig, authConfigFile *registry.ConfigFile) BuildFile { +func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeCache, rm bool, outOld io.Writer, sf *utils.StreamFormatter, configFile *registry.ConfigFile) BuildFile { return &buildFile{ runtime: srv.runtime, srv: srv, @@ -845,8 +834,7 @@ func NewBuildFile(srv *Server, outStream, errStream io.Writer, verbose, utilizeC utilizeCache: utilizeCache, rm: rm, sf: sf, - authConfig: auth, - configFile: authConfigFile, + configFile: configFile, outOld: outOld, } } diff --git a/server/server.go b/server/server.go index 85806d68f4..6d7295efbd 100644 --- a/server/server.go +++ b/server/server.go @@ -448,12 +448,10 @@ func (srv *Server) Build(job *engine.Job) engine.Status { suppressOutput = job.GetenvBool("q") noCache = job.GetenvBool("nocache") rm = job.GetenvBool("rm") - authConfig = ®istry.AuthConfig{} configFile = ®istry.ConfigFile{} tag string context io.ReadCloser ) - job.GetenvJson("authConfig", authConfig) job.GetenvJson("configFile", configFile) repoName, tag = utils.ParseRepositoryTag(repoName) @@ -506,7 +504,7 @@ func (srv *Server) Build(job *engine.Job) engine.Status { Writer: job.Stdout, StreamFormatter: sf, }, - !suppressOutput, !noCache, rm, job.Stdout, sf, authConfig, configFile) + !suppressOutput, !noCache, rm, job.Stdout, sf, configFile) id, err := b.Build(context) if err != nil { return job.Error(err) @@ -1386,16 +1384,23 @@ func (srv *Server) ImagePull(job *engine.Job) engine.Status { localName = job.Args[0] tag string sf = utils.NewStreamFormatter(job.GetenvBool("json")) - authConfig = ®istry.AuthConfig{} + authConfig registry.AuthConfig + configFile = ®istry.ConfigFile{} metaHeaders map[string][]string ) if len(job.Args) > 1 { tag = job.Args[1] } - job.GetenvJson("authConfig", authConfig) + job.GetenvJson("configFile", configFile) job.GetenvJson("metaHeaders", metaHeaders) + endpoint, _, err := registry.ResolveRepositoryName(localName) + if err != nil { + return job.Error(err) + } + authConfig = configFile.ResolveAuthConfig(endpoint) + c, err := srv.poolAdd("pull", localName+":"+tag) if err != nil { if c != nil { @@ -1414,12 +1419,12 @@ func (srv *Server) ImagePull(job *engine.Job) engine.Status { return job.Error(err) } - endpoint, err := registry.ExpandAndVerifyRegistryUrl(hostname) + endpoint, err = registry.ExpandAndVerifyRegistryUrl(hostname) if err != nil { return job.Error(err) } - r, err := registry.NewRegistry(authConfig, srv.HTTPRequestFactory(metaHeaders), endpoint) + r, err := registry.NewRegistry(&authConfig, srv.HTTPRequestFactory(metaHeaders), endpoint) if err != nil { return job.Error(err) }