registry_unit_test.go 3.2 KB

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