registry_unit_test.go 3.1 KB

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