Skip to content

Commit d04beb7

Browse files
shin-mhennings
authored andcommitted
Pass auth config through headers rather than as URL param
1 parent a260347 commit d04beb7

2 files changed

Lines changed: 34 additions & 24 deletions

File tree

api.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package docker
22

33
import (
44
"code.google.com/p/go.net/websocket"
5-
"encoding/base64"
65
"encoding/json"
76
"fmt"
87
"github.com/dotcloud/docker/auth"
@@ -395,13 +394,12 @@ func postImagesCreate(srv *Server, version float64, w http.ResponseWriter, r *ht
395394
tag := r.Form.Get("tag")
396395
repo := r.Form.Get("repo")
397396

398-
authEncoded := r.Form.Get("authConfig")
397+
authJson := r.Header.Get("X-Registry-Auth")
399398
authConfig := &auth.AuthConfig{}
400-
if authEncoded != "" {
401-
authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
402-
if err := json.NewDecoder(authJson).Decode(authConfig); err != nil {
399+
if authJson != "" {
400+
if err := json.NewDecoder(strings.NewReader(authJson)).Decode(authConfig); err != nil {
403401
// for a pull it is not an error if no auth was given
404-
// to increase compatibilit to existing api it is defaulting to be empty
402+
// to increase compatibility with the existing api it is defaulting to be empty
405403
authConfig = &auth.AuthConfig{}
406404
}
407405
}
@@ -495,17 +493,14 @@ func postImagesPush(srv *Server, version float64, w http.ResponseWriter, r *http
495493
}
496494
authConfig := &auth.AuthConfig{}
497495

498-
authEncoded := r.Form.Get("authConfig")
499-
if authEncoded != "" {
500-
// the new format is to handle the authConfg as a parameter
501-
authJson := base64.NewDecoder(base64.URLEncoding, strings.NewReader(authEncoded))
502-
if err := json.NewDecoder(authJson).Decode(authConfig); err != nil {
503-
// for a pull it is not an error if no auth was given
504-
// to increase compatibilit to existing api it is defaulting to be empty
496+
authJson := r.Header.Get("X-Registry-Auth")
497+
if authJson != "" {
498+
if err := json.NewDecoder(strings.NewReader(authJson)).Decode(authConfig); err != nil {
499+
// to increase compatibility with the existing api it is defaulting to be empty
505500
authConfig = &auth.AuthConfig{}
506501
}
507502
} else {
508-
// the old format is supported for compatibility if there was no authConfig parameter
503+
// the old format is supported for compatibility if there was no authConfig header
509504
if err := json.NewDecoder(r.Body).Decode(authConfig); err != nil {
510505
return err
511506
}

commands.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (cli *DockerCli) CmdInsert(args ...string) error {
128128
v.Set("url", cmd.Arg(1))
129129
v.Set("path", cmd.Arg(2))
130130

131-
if err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, cli.out); err != nil {
131+
if err := cli.stream("POST", "/images/"+cmd.Arg(0)+"/insert?"+v.Encode(), nil, cli.out, nil); err != nil {
132132
return err
133133
}
134134
return nil
@@ -795,7 +795,7 @@ func (cli *DockerCli) CmdImport(args ...string) error {
795795
v.Set("tag", tag)
796796
v.Set("fromSrc", src)
797797

798-
err := cli.stream("POST", "/images/create?"+v.Encode(), cli.in, cli.out)
798+
err := cli.stream("POST", "/images/create?"+v.Encode(), cli.in, cli.out, nil)
799799
if err != nil {
800800
return err
801801
}
@@ -841,9 +841,13 @@ func (cli *DockerCli) CmdPush(args ...string) error {
841841
if err != nil {
842842
return err
843843
}
844-
v.Set("authConfig", base64.URLEncoding.EncodeToString(buf))
844+
registryAuthHeader := []string{
845+
string(buf),
846+
}
845847

846-
return cli.stream("POST", "/images/"+name+"/push?"+v.Encode(), nil, cli.out)
848+
return cli.stream("POST", "/images/"+name+"/push?"+v.Encode(), nil, cli.out, map[string][]string{
849+
"X-Registry-Auth": registryAuthHeader,
850+
})
847851
}
848852

849853
if err := push(authConfig); err != nil {
@@ -896,9 +900,13 @@ func (cli *DockerCli) CmdPull(args ...string) error {
896900
if err != nil {
897901
return err
898902
}
899-
v.Set("authConfig", base64.URLEncoding.EncodeToString(buf))
903+
registryAuthHeader := []string{
904+
string(buf),
905+
}
900906

901-
return cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.out)
907+
return cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.out, map[string][]string{
908+
"X-Registry-Auth": registryAuthHeader,
909+
})
902910
}
903911

904912
if err := pull(authConfig); err != nil {
@@ -1143,7 +1151,7 @@ func (cli *DockerCli) CmdEvents(args ...string) error {
11431151
v.Set("since", *since)
11441152
}
11451153

1146-
if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out); err != nil {
1154+
if err := cli.stream("GET", "/events?"+v.Encode(), nil, cli.out, nil); err != nil {
11471155
return err
11481156
}
11491157
return nil
@@ -1160,7 +1168,7 @@ func (cli *DockerCli) CmdExport(args ...string) error {
11601168
return nil
11611169
}
11621170

1163-
if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, cli.out); err != nil {
1171+
if err := cli.stream("GET", "/containers/"+cmd.Arg(0)+"/export", nil, cli.out, nil); err != nil {
11641172
return err
11651173
}
11661174
return nil
@@ -1451,7 +1459,7 @@ func (cli *DockerCli) CmdRun(args ...string) error {
14511459
}
14521460
v.Set("authConfig", base64.URLEncoding.EncodeToString(buf))
14531461

1454-
err = cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.err)
1462+
err = cli.stream("POST", "/images/create?"+v.Encode(), nil, cli.err, nil)
14551463
if err != nil {
14561464
return err
14571465
}
@@ -1628,7 +1636,7 @@ func (cli *DockerCli) call(method, path string, data interface{}) ([]byte, int,
16281636
return body, resp.StatusCode, nil
16291637
}
16301638

1631-
func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) error {
1639+
func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer, headers map[string][]string) error {
16321640
if (method == "POST" || method == "PUT") && in == nil {
16331641
in = bytes.NewReader([]byte{})
16341642
}
@@ -1641,6 +1649,13 @@ func (cli *DockerCli) stream(method, path string, in io.Reader, out io.Writer) e
16411649
if method == "POST" {
16421650
req.Header.Set("Content-Type", "plain/text")
16431651
}
1652+
1653+
if headers != nil {
1654+
for k, v := range headers {
1655+
req.Header[k] = v
1656+
}
1657+
}
1658+
16441659
dial, err := net.Dial(cli.proto, cli.addr)
16451660
if err != nil {
16461661
if strings.Contains(err.Error(), "connection refused") {

0 commit comments

Comments
 (0)