auth_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package registry
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/docker/docker/cliconfig"
  8. )
  9. func TestEncodeAuth(t *testing.T) {
  10. newAuthConfig := &cliconfig.AuthConfig{Username: "ken", Password: "test", Email: "test@example.com"}
  11. authStr := cliconfig.EncodeAuth(newAuthConfig)
  12. decAuthConfig := &cliconfig.AuthConfig{}
  13. var err error
  14. decAuthConfig.Username, decAuthConfig.Password, err = cliconfig.DecodeAuth(authStr)
  15. if err != nil {
  16. t.Fatal(err)
  17. }
  18. if newAuthConfig.Username != decAuthConfig.Username {
  19. t.Fatal("Encode Username doesn't match decoded Username")
  20. }
  21. if newAuthConfig.Password != decAuthConfig.Password {
  22. t.Fatal("Encode Password doesn't match decoded Password")
  23. }
  24. if authStr != "a2VuOnRlc3Q=" {
  25. t.Fatal("AuthString encoding isn't correct.")
  26. }
  27. }
  28. func setupTempConfigFile() (*cliconfig.ConfigFile, error) {
  29. root, err := ioutil.TempDir("", "docker-test-auth")
  30. if err != nil {
  31. return nil, err
  32. }
  33. root = filepath.Join(root, cliconfig.CONFIGFILE)
  34. configFile := cliconfig.NewConfigFile(root)
  35. for _, registry := range []string{"testIndex", IndexServerAddress()} {
  36. configFile.AuthConfigs[registry] = cliconfig.AuthConfig{
  37. Username: "docker-user",
  38. Password: "docker-pass",
  39. Email: "docker@docker.io",
  40. }
  41. }
  42. return configFile, nil
  43. }
  44. func TestSameAuthDataPostSave(t *testing.T) {
  45. configFile, err := setupTempConfigFile()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. defer os.RemoveAll(configFile.Filename())
  50. err = configFile.Save()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. authConfig := configFile.AuthConfigs["testIndex"]
  55. if authConfig.Username != "docker-user" {
  56. t.Fail()
  57. }
  58. if authConfig.Password != "docker-pass" {
  59. t.Fail()
  60. }
  61. if authConfig.Email != "docker@docker.io" {
  62. t.Fail()
  63. }
  64. if authConfig.Auth != "" {
  65. t.Fail()
  66. }
  67. }
  68. func TestResolveAuthConfigIndexServer(t *testing.T) {
  69. configFile, err := setupTempConfigFile()
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. defer os.RemoveAll(configFile.Filename())
  74. indexConfig := configFile.AuthConfigs[IndexServerAddress()]
  75. officialIndex := &IndexInfo{
  76. Official: true,
  77. }
  78. privateIndex := &IndexInfo{
  79. Official: false,
  80. }
  81. resolved := ResolveAuthConfig(configFile, officialIndex)
  82. assertEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServerAddress()")
  83. resolved = ResolveAuthConfig(configFile, privateIndex)
  84. assertNotEqual(t, resolved, indexConfig, "Expected ResolveAuthConfig to not return IndexServerAddress()")
  85. }
  86. func TestResolveAuthConfigFullURL(t *testing.T) {
  87. configFile, err := setupTempConfigFile()
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. defer os.RemoveAll(configFile.Filename())
  92. registryAuth := cliconfig.AuthConfig{
  93. Username: "foo-user",
  94. Password: "foo-pass",
  95. Email: "foo@example.com",
  96. }
  97. localAuth := cliconfig.AuthConfig{
  98. Username: "bar-user",
  99. Password: "bar-pass",
  100. Email: "bar@example.com",
  101. }
  102. officialAuth := cliconfig.AuthConfig{
  103. Username: "baz-user",
  104. Password: "baz-pass",
  105. Email: "baz@example.com",
  106. }
  107. configFile.AuthConfigs[IndexServerAddress()] = officialAuth
  108. expectedAuths := map[string]cliconfig.AuthConfig{
  109. "registry.example.com": registryAuth,
  110. "localhost:8000": localAuth,
  111. "registry.com": localAuth,
  112. }
  113. validRegistries := map[string][]string{
  114. "registry.example.com": {
  115. "https://registry.example.com/v1/",
  116. "http://registry.example.com/v1/",
  117. "registry.example.com",
  118. "registry.example.com/v1/",
  119. },
  120. "localhost:8000": {
  121. "https://localhost:8000/v1/",
  122. "http://localhost:8000/v1/",
  123. "localhost:8000",
  124. "localhost:8000/v1/",
  125. },
  126. "registry.com": {
  127. "https://registry.com/v1/",
  128. "http://registry.com/v1/",
  129. "registry.com",
  130. "registry.com/v1/",
  131. },
  132. }
  133. for configKey, registries := range validRegistries {
  134. configured, ok := expectedAuths[configKey]
  135. if !ok || configured.Email == "" {
  136. t.Fail()
  137. }
  138. index := &IndexInfo{
  139. Name: configKey,
  140. }
  141. for _, registry := range registries {
  142. configFile.AuthConfigs[registry] = configured
  143. resolved := ResolveAuthConfig(configFile, index)
  144. if resolved.Email != configured.Email {
  145. t.Errorf("%s -> %q != %q\n", registry, resolved.Email, configured.Email)
  146. }
  147. delete(configFile.AuthConfigs, registry)
  148. resolved = ResolveAuthConfig(configFile, index)
  149. if resolved.Email == configured.Email {
  150. t.Errorf("%s -> %q == %q\n", registry, resolved.Email, configured.Email)
  151. }
  152. }
  153. }
  154. }