2014-03-11 00:16:58 +00:00
|
|
|
package registry
|
2013-03-15 00:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2013-07-25 03:25:16 +00:00
|
|
|
"io/ioutil"
|
2013-05-10 13:34:19 +00:00
|
|
|
"os"
|
2015-04-01 22:39:37 +00:00
|
|
|
"path/filepath"
|
2013-03-15 00:43:59 +00:00
|
|
|
"testing"
|
2015-04-22 12:06:58 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/cliconfig"
|
2013-03-15 00:43:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEncodeAuth(t *testing.T) {
|
2015-04-22 12:06:58 +00:00
|
|
|
newAuthConfig := &cliconfig.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
|
|
|
|
authStr := cliconfig.EncodeAuth(newAuthConfig)
|
|
|
|
decAuthConfig := &cliconfig.AuthConfig{}
|
2013-07-24 12:28:22 +00:00
|
|
|
var err error
|
2015-04-22 12:06:58 +00:00
|
|
|
decAuthConfig.Username, decAuthConfig.Password, err = cliconfig.DecodeAuth(authStr)
|
2013-03-15 00:43:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if newAuthConfig.Username != decAuthConfig.Username {
|
|
|
|
t.Fatal("Encode Username doesn't match decoded Username")
|
|
|
|
}
|
|
|
|
if newAuthConfig.Password != decAuthConfig.Password {
|
|
|
|
t.Fatal("Encode Password doesn't match decoded Password")
|
|
|
|
}
|
|
|
|
if authStr != "a2VuOnRlc3Q=" {
|
|
|
|
t.Fatal("AuthString encoding isn't correct.")
|
|
|
|
}
|
|
|
|
}
|
2013-05-10 13:34:19 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
|
2013-10-16 20:44:15 +00:00
|
|
|
root, err := ioutil.TempDir("", "docker-test-auth")
|
2013-07-25 03:25:16 +00:00
|
|
|
if err != nil {
|
2013-09-24 16:26:17 +00:00
|
|
|
return nil, err
|
2013-07-25 03:25:16 +00:00
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
root = filepath.Join(root, cliconfig.CONFIGFILE)
|
|
|
|
configFile := cliconfig.NewConfigFile(root)
|
2013-07-25 03:25:16 +00:00
|
|
|
|
2015-02-12 18:23:22 +00:00
|
|
|
for _, registry := range []string{"testIndex", INDEXSERVER} {
|
2015-04-22 12:06:58 +00:00
|
|
|
configFile.AuthConfigs[registry] = cliconfig.AuthConfig{
|
2013-09-24 16:26:17 +00:00
|
|
|
Username: "docker-user",
|
|
|
|
Password: "docker-pass",
|
|
|
|
Email: "docker@docker.io",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return configFile, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSameAuthDataPostSave(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2013-07-25 03:25:16 +00:00
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
defer os.RemoveAll(configFile.Filename())
|
2013-07-25 03:25:16 +00:00
|
|
|
|
2015-04-01 22:39:37 +00:00
|
|
|
err = configFile.Save()
|
2013-07-25 03:25:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-04-01 22:39:37 +00:00
|
|
|
authConfig := configFile.AuthConfigs["testIndex"]
|
2013-07-25 03:25:16 +00:00
|
|
|
if authConfig.Username != "docker-user" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Password != "docker-pass" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Email != "docker@docker.io" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
if authConfig.Auth != "" {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
2013-09-24 16:26:17 +00:00
|
|
|
|
|
|
|
func TestResolveAuthConfigIndexServer(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
defer os.RemoveAll(configFile.Filename())
|
2013-09-24 16:26:17 +00:00
|
|
|
|
2015-02-12 18:23:22 +00:00
|
|
|
indexConfig := configFile.AuthConfigs[INDEXSERVER]
|
2014-10-07 01:54:52 +00:00
|
|
|
|
|
|
|
officialIndex := &IndexInfo{
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
privateIndex := &IndexInfo{
|
|
|
|
Official: false,
|
2013-09-24 16:26:17 +00:00
|
|
|
}
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
resolved := ResolveAuthConfig(configFile, officialIndex)
|
2015-02-12 18:23:22 +00:00
|
|
|
assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return INDEXSERVER")
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
resolved = ResolveAuthConfig(configFile, privateIndex)
|
2015-02-12 18:23:22 +00:00
|
|
|
assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return INDEXSERVER")
|
2013-09-24 16:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestResolveAuthConfigFullURL(t *testing.T) {
|
|
|
|
configFile, err := setupTempConfigFile()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
defer os.RemoveAll(configFile.Filename())
|
2013-09-24 16:26:17 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
registryAuth := cliconfig.AuthConfig{
|
2013-09-24 16:26:17 +00:00
|
|
|
Username: "foo-user",
|
|
|
|
Password: "foo-pass",
|
|
|
|
Email: "foo@example.com",
|
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
localAuth := cliconfig.AuthConfig{
|
2013-09-24 16:26:17 +00:00
|
|
|
Username: "bar-user",
|
|
|
|
Password: "bar-pass",
|
|
|
|
Email: "bar@example.com",
|
|
|
|
}
|
2015-04-22 12:06:58 +00:00
|
|
|
officialAuth := cliconfig.AuthConfig{
|
2014-10-07 01:54:52 +00:00
|
|
|
Username: "baz-user",
|
|
|
|
Password: "baz-pass",
|
|
|
|
Email: "baz@example.com",
|
|
|
|
}
|
2015-02-12 18:23:22 +00:00
|
|
|
configFile.AuthConfigs[INDEXSERVER] = officialAuth
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-04-22 12:06:58 +00:00
|
|
|
expectedAuths := map[string]cliconfig.AuthConfig{
|
2014-10-07 01:54:52 +00:00
|
|
|
"registry.example.com": registryAuth,
|
|
|
|
"localhost:8000": localAuth,
|
|
|
|
"registry.com": localAuth,
|
|
|
|
}
|
2013-09-24 16:26:17 +00:00
|
|
|
|
|
|
|
validRegistries := map[string][]string{
|
2014-10-07 01:54:52 +00:00
|
|
|
"registry.example.com": {
|
2013-09-24 16:26:17 +00:00
|
|
|
"https://registry.example.com/v1/",
|
|
|
|
"http://registry.example.com/v1/",
|
|
|
|
"registry.example.com",
|
|
|
|
"registry.example.com/v1/",
|
|
|
|
},
|
2014-10-07 01:54:52 +00:00
|
|
|
"localhost:8000": {
|
2013-09-24 16:26:17 +00:00
|
|
|
"https://localhost:8000/v1/",
|
|
|
|
"http://localhost:8000/v1/",
|
|
|
|
"localhost:8000",
|
|
|
|
"localhost:8000/v1/",
|
|
|
|
},
|
2014-02-20 22:57:58 +00:00
|
|
|
"registry.com": {
|
|
|
|
"https://registry.com/v1/",
|
|
|
|
"http://registry.com/v1/",
|
|
|
|
"registry.com",
|
|
|
|
"registry.com/v1/",
|
|
|
|
},
|
2013-09-24 16:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for configKey, registries := range validRegistries {
|
2014-10-07 01:54:52 +00:00
|
|
|
configured, ok := expectedAuths[configKey]
|
|
|
|
if !ok || configured.Email == "" {
|
2015-01-14 22:12:03 +00:00
|
|
|
t.Fail()
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
index := &IndexInfo{
|
|
|
|
Name: configKey,
|
|
|
|
}
|
2013-09-24 16:26:17 +00:00
|
|
|
for _, registry := range registries {
|
2015-04-01 22:39:37 +00:00
|
|
|
configFile.AuthConfigs[registry] = configured
|
2015-04-22 12:06:58 +00:00
|
|
|
resolved := ResolveAuthConfig(configFile, index)
|
2013-09-24 16:26:17 +00:00
|
|
|
if resolved.Email != configured.Email {
|
|
|
|
t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2015-04-01 22:39:37 +00:00
|
|
|
delete(configFile.AuthConfigs, registry)
|
2015-04-22 12:06:58 +00:00
|
|
|
resolved = ResolveAuthConfig(configFile, index)
|
2014-10-07 01:54:52 +00:00
|
|
|
if resolved.Email == configured.Email {
|
|
|
|
t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
|
|
|
|
}
|
2013-09-24 16:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|