auth_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package registry // import "github.com/docker/docker/registry"
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types/registry"
  5. "gotest.tools/v3/assert"
  6. )
  7. func buildAuthConfigs() map[string]registry.AuthConfig {
  8. authConfigs := map[string]registry.AuthConfig{}
  9. for _, reg := range []string{"testIndex", IndexServer} {
  10. authConfigs[reg] = registry.AuthConfig{
  11. Username: "docker-user",
  12. Password: "docker-pass",
  13. }
  14. }
  15. return authConfigs
  16. }
  17. func TestResolveAuthConfigIndexServer(t *testing.T) {
  18. authConfigs := buildAuthConfigs()
  19. indexConfig := authConfigs[IndexServer]
  20. officialIndex := &registry.IndexInfo{
  21. Official: true,
  22. }
  23. privateIndex := &registry.IndexInfo{
  24. Official: false,
  25. }
  26. resolved := ResolveAuthConfig(authConfigs, officialIndex)
  27. assert.Equal(t, resolved, indexConfig, "Expected ResolveAuthConfig to return IndexServer")
  28. resolved = ResolveAuthConfig(authConfigs, privateIndex)
  29. assert.Check(t, resolved != indexConfig, "Expected ResolveAuthConfig to not return IndexServer")
  30. }
  31. func TestResolveAuthConfigFullURL(t *testing.T) {
  32. authConfigs := buildAuthConfigs()
  33. registryAuth := registry.AuthConfig{
  34. Username: "foo-user",
  35. Password: "foo-pass",
  36. }
  37. localAuth := registry.AuthConfig{
  38. Username: "bar-user",
  39. Password: "bar-pass",
  40. }
  41. officialAuth := registry.AuthConfig{
  42. Username: "baz-user",
  43. Password: "baz-pass",
  44. }
  45. authConfigs[IndexServer] = officialAuth
  46. expectedAuths := map[string]registry.AuthConfig{
  47. "registry.example.com": registryAuth,
  48. "localhost:8000": localAuth,
  49. "example.com": localAuth,
  50. }
  51. validRegistries := map[string][]string{
  52. "registry.example.com": {
  53. "https://registry.example.com/v1/",
  54. "http://registry.example.com/v1/",
  55. "registry.example.com",
  56. "registry.example.com/v1/",
  57. },
  58. "localhost:8000": {
  59. "https://localhost:8000/v1/",
  60. "http://localhost:8000/v1/",
  61. "localhost:8000",
  62. "localhost:8000/v1/",
  63. },
  64. "example.com": {
  65. "https://example.com/v1/",
  66. "http://example.com/v1/",
  67. "example.com",
  68. "example.com/v1/",
  69. },
  70. }
  71. for configKey, registries := range validRegistries {
  72. configured, ok := expectedAuths[configKey]
  73. if !ok {
  74. t.Fail()
  75. }
  76. index := &registry.IndexInfo{
  77. Name: configKey,
  78. }
  79. for _, reg := range registries {
  80. authConfigs[reg] = configured
  81. resolved := ResolveAuthConfig(authConfigs, index)
  82. if resolved.Username != configured.Username || resolved.Password != configured.Password {
  83. t.Errorf("%s -> %v != %v\n", reg, resolved, configured)
  84. }
  85. delete(authConfigs, reg)
  86. resolved = ResolveAuthConfig(authConfigs, index)
  87. if resolved.Username == configured.Username || resolved.Password == configured.Password {
  88. t.Errorf("%s -> %v == %v\n", reg, resolved, configured)
  89. }
  90. }
  91. }
  92. }