registry_unit_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package distribution
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "net/url"
  6. "os"
  7. "strings"
  8. "testing"
  9. "github.com/Sirupsen/logrus"
  10. "github.com/docker/docker/reference"
  11. "github.com/docker/docker/registry"
  12. "github.com/docker/docker/utils"
  13. "github.com/docker/engine-api/types"
  14. registrytypes "github.com/docker/engine-api/types/registry"
  15. "golang.org/x/net/context"
  16. )
  17. const secretRegistryToken = "mysecrettoken"
  18. type tokenPassThruHandler struct {
  19. reached bool
  20. gotToken bool
  21. shouldSend401 func(url string) bool
  22. }
  23. func (h *tokenPassThruHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  24. h.reached = true
  25. if strings.Contains(r.Header.Get("Authorization"), secretRegistryToken) {
  26. logrus.Debug("Detected registry token in auth header")
  27. h.gotToken = true
  28. }
  29. if h.shouldSend401 == nil || h.shouldSend401(r.RequestURI) {
  30. w.Header().Set("WWW-Authenticate", `Bearer realm="foorealm"`)
  31. w.WriteHeader(401)
  32. }
  33. }
  34. func testTokenPassThru(t *testing.T, ts *httptest.Server) {
  35. tmp, err := utils.TestDirectory("")
  36. if err != nil {
  37. t.Fatal(err)
  38. }
  39. defer os.RemoveAll(tmp)
  40. uri, err := url.Parse(ts.URL)
  41. if err != nil {
  42. t.Fatalf("could not parse url from test server: %v", err)
  43. }
  44. endpoint := registry.APIEndpoint{
  45. Mirror: false,
  46. URL: uri,
  47. Version: 2,
  48. Official: false,
  49. TrimHostname: false,
  50. TLSConfig: nil,
  51. //VersionHeader: "verheader",
  52. }
  53. n, _ := reference.ParseNamed("testremotename")
  54. repoInfo := &registry.RepositoryInfo{
  55. Named: n,
  56. Index: &registrytypes.IndexInfo{
  57. Name: "testrepo",
  58. Mirrors: nil,
  59. Secure: false,
  60. Official: false,
  61. },
  62. Official: false,
  63. }
  64. imagePullConfig := &ImagePullConfig{
  65. MetaHeaders: http.Header{},
  66. AuthConfig: &types.AuthConfig{
  67. RegistryToken: secretRegistryToken,
  68. },
  69. }
  70. puller, err := newPuller(endpoint, repoInfo, imagePullConfig)
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. p := puller.(*v2Puller)
  75. ctx := context.Background()
  76. p.repo, _, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. logrus.Debug("About to pull")
  81. // We expect it to fail, since we haven't mock'd the full registry exchange in our handler above
  82. tag, _ := reference.WithTag(n, "tag_goes_here")
  83. _ = p.pullV2Repository(ctx, tag)
  84. }
  85. func TestTokenPassThru(t *testing.T) {
  86. handler := &tokenPassThruHandler{shouldSend401: func(url string) bool { return url == "/v2/" }}
  87. ts := httptest.NewServer(handler)
  88. defer ts.Close()
  89. testTokenPassThru(t, ts)
  90. if !handler.reached {
  91. t.Fatal("Handler not reached")
  92. }
  93. if !handler.gotToken {
  94. t.Fatal("Failed to receive registry token")
  95. }
  96. }
  97. func TestTokenPassThruDifferentHost(t *testing.T) {
  98. handler := new(tokenPassThruHandler)
  99. ts := httptest.NewServer(handler)
  100. defer ts.Close()
  101. tsredirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  102. if r.RequestURI == "/v2/" {
  103. w.Header().Set("WWW-Authenticate", `Bearer realm="foorealm"`)
  104. w.WriteHeader(401)
  105. return
  106. }
  107. http.Redirect(w, r, ts.URL+r.URL.Path, http.StatusMovedPermanently)
  108. }))
  109. defer tsredirect.Close()
  110. testTokenPassThru(t, tsredirect)
  111. if !handler.reached {
  112. t.Fatal("Handler not reached")
  113. }
  114. if handler.gotToken {
  115. t.Fatal("Redirect should not forward Authorization header to another host")
  116. }
  117. }