registry_unit_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/containerd/log"
  10. "github.com/docker/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. Version: 2,
  40. Official: false,
  41. TrimHostname: false,
  42. TLSConfig: nil,
  43. }
  44. n, _ := reference.ParseNormalizedNamed("testremotename")
  45. repoInfo := &registrypkg.RepositoryInfo{
  46. Name: n,
  47. Index: &registry.IndexInfo{
  48. Name: "testrepo",
  49. Mirrors: nil,
  50. Secure: false,
  51. Official: false,
  52. },
  53. Official: false,
  54. }
  55. imagePullConfig := &ImagePullConfig{
  56. Config: Config{
  57. MetaHeaders: http.Header{},
  58. AuthConfig: &registry.AuthConfig{
  59. RegistryToken: secretRegistryToken,
  60. },
  61. },
  62. }
  63. p := newPuller(endpoint, repoInfo, imagePullConfig, nil)
  64. ctx := context.Background()
  65. p.repo, err = newRepository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. log.G(ctx).Debug("About to pull")
  70. // We expect it to fail, since we haven't mock'd the full registry exchange in our handler above
  71. tag, _ := reference.WithTag(n, "tag_goes_here")
  72. _ = p.pullRepository(ctx, tag)
  73. }
  74. func TestTokenPassThru(t *testing.T) {
  75. handler := &tokenPassThruHandler{shouldSend401: func(url string) bool { return url == "/v2/" }}
  76. ts := httptest.NewServer(handler)
  77. defer ts.Close()
  78. testTokenPassThru(t, ts)
  79. if !handler.reached {
  80. t.Fatal("Handler not reached")
  81. }
  82. if !handler.gotToken {
  83. t.Fatal("Failed to receive registry token")
  84. }
  85. }
  86. func TestTokenPassThruDifferentHost(t *testing.T) {
  87. handler := new(tokenPassThruHandler)
  88. ts := httptest.NewServer(handler)
  89. defer ts.Close()
  90. tsredirect := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  91. if r.RequestURI == "/v2/" {
  92. w.Header().Set("WWW-Authenticate", `Bearer realm="foorealm"`)
  93. w.WriteHeader(401)
  94. return
  95. }
  96. http.Redirect(w, r, ts.URL+r.URL.Path, http.StatusMovedPermanently)
  97. }))
  98. defer tsredirect.Close()
  99. testTokenPassThru(t, tsredirect)
  100. if !handler.reached {
  101. t.Fatal("Handler not reached")
  102. }
  103. if handler.gotToken {
  104. t.Fatal("Redirect should not forward Authorization header to another host")
  105. }
  106. }