From b00e80c60dc65080f74a1589bf1958f1ec039790 Mon Sep 17 00:00:00 2001 From: Vijay Krishnan Date: Fri, 14 Sep 2018 16:35:20 -0700 Subject: [PATCH] Added registry token support for docker registry --- auth.go | 9 +++++++++ auth_test.go | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/auth.go b/auth.go index acb3a02b..4335d6e0 100644 --- a/auth.go +++ b/auth.go @@ -32,6 +32,9 @@ type AuthConfiguration struct { // see https://godoc.org/github.com/docker/docker/api/types#AuthConfig // It can be used in place of password not in conjunction with it IdentityToken string `json:"identitytoken,omitempty"` + + // RegistryToken can be supplied with the registrytoken + RegistryToken string `json:"registrytoken,omitempty"` } // AuthConfigurations represents authentication options to use for the @@ -50,6 +53,7 @@ type dockerConfig struct { Auth string `json:"auth"` Email string `json:"email"` IdentityToken string `json:"identitytoken"` + RegistryToken string `json:"registrytoken"` } // NewAuthConfigurationsFromFile returns AuthConfigurations from a path containing JSON @@ -162,6 +166,11 @@ func authConfigs(confs map[string]dockerConfig) (*AuthConfigurations, error) { authConfig.IdentityToken = conf.IdentityToken } + // if registrytoken provided then zero the password and set it + if conf.RegistryToken != "" { + authConfig.Password = "" + authConfig.RegistryToken = conf.RegistryToken + } c.Configs[reg] = authConfig } diff --git a/auth_test.go b/auth_test.go index 12a5db7f..5998ff57 100644 --- a/auth_test.go +++ b/auth_test.go @@ -196,6 +196,27 @@ func TestAuthConfigIdentityToken(t *testing.T) { } } +func TestAuthConfigRegistryToken(t *testing.T) { + t.Parallel() + auth := base64.StdEncoding.EncodeToString([]byte("someuser:")) + read := strings.NewReader(fmt.Sprintf(`{"auths":{"docker.io":{"auth":"%s","registrytoken":"sometoken"}}}`, auth)) + ac, err := NewAuthConfigurations(read) + if err != nil { + t.Fatal(err) + } + + c, ok := ac.Configs["docker.io"] + if !ok { + t.Error("NewAuthConfigurations: Expected Configs to contain docker.io") + } + if got, want := c.Username, "someuser"; got != want { + t.Errorf(`AuthConfigurations.Configs["docker.io"].Username: wrong result. Want %q. Got %q`, want, got) + } + if got, want := c.RegistryToken, "sometoken"; got != want { + t.Errorf(`AuthConfigurations.Configs["docker.io"].RegistryToken: wrong result. Want %q. Got %q`, want, got) + } +} + func TestAuthCheck(t *testing.T) { t.Parallel() fakeRT := &FakeRoundTripper{status: http.StatusOK}