1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
23 "git.arvados.org/arvados.git/lib/config"
24 "git.arvados.org/arvados.git/sdk/go/arvados"
25 "git.arvados.org/arvados.git/sdk/go/arvadosclient"
26 "git.arvados.org/arvados.git/sdk/go/arvadostest"
27 "git.arvados.org/arvados.git/sdk/go/auth"
28 "git.arvados.org/arvados.git/sdk/go/ctxlog"
29 "git.arvados.org/arvados.git/sdk/go/keepclient"
30 "github.com/prometheus/client_golang/prometheus"
31 "github.com/sirupsen/logrus"
32 "golang.org/x/net/html"
33 check "gopkg.in/check.v1"
36 var _ = check.Suite(&UnitSuite{})
39 arvados.DebugLocksPanicMode = true
42 type UnitSuite struct {
43 cluster *arvados.Cluster
47 func (s *UnitSuite) SetUpTest(c *check.C) {
48 logger := ctxlog.TestLogger(c)
49 ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), logger)
51 cfg, err := ldr.Load()
52 c.Assert(err, check.IsNil)
53 cc, err := cfg.GetCluster("")
54 c.Assert(err, check.IsNil)
61 registry: prometheus.NewRegistry(),
63 metrics: newMetrics(prometheus.NewRegistry()),
67 func newCollection(collID string) *arvados.Collection {
68 coll := &arvados.Collection{UUID: collID}
69 if pdh, ok := arvadostest.TestCollectionUUIDToPDH[collID]; ok {
70 coll.PortableDataHash = pdh
75 func newRequest(method, urlStr string) *http.Request {
76 u := mustParseURL(urlStr)
81 RequestURI: u.RequestURI(),
82 RemoteAddr: "10.20.30.40:56789",
83 Header: http.Header{},
87 func (s *UnitSuite) TestLogEventTypes(c *check.C) {
88 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
89 for method, expected := range map[string]string{
90 "GET": "file_download",
91 "POST": "file_upload",
94 filePath := "/" + method
95 req := newRequest(method, collURL+filePath)
96 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
97 if !c.Check(actual, check.NotNil) {
100 c.Check(actual.eventType, check.Equals, expected)
104 func (s *UnitSuite) TestUnloggedEventTypes(c *check.C) {
105 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
106 for _, method := range []string{"DELETE", "HEAD", "OPTIONS", "PATCH"} {
107 filePath := "/" + method
108 req := newRequest(method, collURL+filePath)
109 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
110 c.Check(actual, check.IsNil,
111 check.Commentf("%s request made a log event", method))
115 func (s *UnitSuite) TestLogFilePath(c *check.C) {
116 coll := newCollection(arvadostest.FooCollection)
117 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
118 for _, filePath := range []string{"/foo", "/Foo", "/foo/bar"} {
119 req := newRequest("GET", collURL+filePath)
120 actual := newFileEventLog(s.handler, req, filePath, coll, nil)
121 if !c.Check(actual, check.NotNil) {
124 c.Check(actual.collFilePath, check.Equals, filePath)
128 func (s *UnitSuite) TestLogRemoteAddr(c *check.C) {
129 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
131 req := newRequest("GET", collURL+filePath)
133 for _, addr := range []string{"10.20.30.55", "192.168.144.120", "192.0.2.4"} {
134 req.RemoteAddr = addr + ":57914"
135 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
136 if !c.Check(actual, check.NotNil) {
139 c.Check(actual.clientAddr, check.Equals, addr)
142 for _, addr := range []string{"100::20:30:40", "2001:db8::90:100", "3fff::30"} {
143 req.RemoteAddr = fmt.Sprintf("[%s]:57916", addr)
144 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
145 if !c.Check(actual, check.NotNil) {
148 c.Check(actual.clientAddr, check.Equals, addr)
152 func (s *UnitSuite) TestLogXForwardedFor(c *check.C) {
153 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
155 req := newRequest("GET", collURL+filePath)
156 for xff, expected := range map[string]string{
157 "10.20.30.55": "10.20.30.55",
158 "192.168.144.120, 10.20.30.120": "192.168.144.120",
159 "192.0.2.4, 192.0.2.6, 192.0.2.8": "192.0.2.4",
160 "192.0.2.4,192.168.2.4": "192.0.2.4",
161 "10.20.30.60,192.168.144.40,192.0.2.4": "10.20.30.60",
162 "100::20:30:50": "100::20:30:50",
163 "2001:db8::80:90, 100::100": "2001:db8::80:90",
164 "3fff::ff, 3fff::ee, 3fff::fe": "3fff::ff",
165 "3fff::3f,100::1000": "3fff::3f",
166 "2001:db8::88,100::88,3fff::88": "2001:db8::88",
167 "10.20.30.60, 2001:db8::60": "10.20.30.60",
168 "2001:db8::20,10.20.30.20": "2001:db8::20",
169 ", 10.20.30.123, 100::123": "10.20.30.123",
170 ",100::321,10.30.20.10": "100::321",
172 req.Header.Set("X-Forwarded-For", xff)
173 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
174 if !c.Check(actual, check.NotNil) {
177 c.Check(actual.clientAddr, check.Equals, expected)
181 func (s *UnitSuite) TestLogXForwardedForMalformed(c *check.C) {
182 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
184 req := newRequest("GET", collURL+filePath)
185 for _, xff := range []string{"", ",", "10.20,30.40", "foo, bar"} {
186 req.Header.Set("X-Forwarded-For", xff)
187 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
188 if !c.Check(actual, check.NotNil) {
191 c.Check(actual.clientAddr, check.Equals, "10.20.30.40")
195 func (s *UnitSuite) TestLogXForwardedForMultivalue(c *check.C) {
196 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
198 req := newRequest("GET", collURL+filePath)
199 req.Header.Set("X-Forwarded-For", ", ")
200 req.Header.Add("X-Forwarded-For", "2001:db8::db9:dbd")
201 req.Header.Add("X-Forwarded-For", "10.20.30.90")
202 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
203 c.Assert(actual, check.NotNil)
204 c.Check(actual.clientAddr, check.Equals, "2001:db8::db9:dbd")
207 func (s *UnitSuite) TestLogClientAddressCanonicalization(c *check.C) {
208 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
210 req := newRequest("GET", collURL+filePath)
211 expected := "2001:db8::12:0"
213 req.RemoteAddr = "[2001:db8::012:0000]:57918"
214 a := newFileEventLog(s.handler, req, filePath, nil, nil)
215 c.Assert(a, check.NotNil)
216 c.Check(a.clientAddr, check.Equals, expected)
218 req.RemoteAddr = "10.20.30.40:57919"
219 req.Header.Set("X-Forwarded-For", "2001:db8:0::0:12:00")
220 b := newFileEventLog(s.handler, req, filePath, nil, nil)
221 c.Assert(b, check.NotNil)
222 c.Check(b.clientAddr, check.Equals, expected)
225 func (s *UnitSuite) TestLogAnonymousUser(c *check.C) {
226 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
228 req := newRequest("GET", collURL+filePath)
229 actual := newFileEventLog(s.handler, req, filePath, nil, nil)
230 c.Assert(actual, check.NotNil)
231 c.Check(actual.userUUID, check.Equals, s.handler.Cluster.ClusterID+"-tpzed-anonymouspublic")
232 c.Check(actual.userFullName, check.Equals, "")
235 func (s *UnitSuite) TestLogUser(c *check.C) {
236 collURL := "http://keep-web.example/c=" + arvadostest.FooCollection
237 for userUUID, userFullName := range map[string]string{
238 arvadostest.ActiveUserUUID: "Active User",
239 arvadostest.SpectatorUserUUID: "Spectator User",
241 filePath := "/" + userUUID
242 req := newRequest("GET", collURL+filePath)
243 user := &arvados.User{
245 FullName: userFullName,
247 actual := newFileEventLog(s.handler, req, filePath, nil, user)
248 if !c.Check(actual, check.NotNil) {
251 c.Check(actual.userUUID, check.Equals, userUUID)
252 c.Check(actual.userFullName, check.Equals, userFullName)
256 func (s *UnitSuite) TestLogCollectionByUUID(c *check.C) {
257 for collUUID, collPDH := range arvadostest.TestCollectionUUIDToPDH {
258 collURL := "http://keep-web.example/c=" + collUUID
259 filePath := "/" + collUUID
260 req := newRequest("GET", collURL+filePath)
261 coll := newCollection(collUUID)
262 actual := newFileEventLog(s.handler, req, filePath, coll, nil)
263 if !c.Check(actual, check.NotNil) {
266 c.Check(actual.collUUID, check.Equals, collUUID)
267 c.Check(actual.collPDH, check.Equals, collPDH)
271 func (s *UnitSuite) TestLogCollectionByPDH(c *check.C) {
272 for _, collPDH := range arvadostest.TestCollectionUUIDToPDH {
273 collURL := "http://keep-web.example/c=" + collPDH
274 filePath := "/PDHFile"
275 req := newRequest("GET", collURL+filePath)
276 coll := newCollection(collPDH)
277 actual := newFileEventLog(s.handler, req, filePath, coll, nil)
278 if !c.Check(actual, check.NotNil) {
281 c.Check(actual.collPDH, check.Equals, collPDH)
282 c.Check(actual.collUUID, check.Equals, "")
286 func (s *UnitSuite) TestLogGETUUIDAsDict(c *check.C) {
288 reqPath := "/c=" + arvadostest.FooCollection + filePath
289 req := newRequest("GET", "http://keep-web.example"+reqPath)
290 coll := newCollection(arvadostest.FooCollection)
291 logEvent := newFileEventLog(s.handler, req, filePath, coll, nil)
292 c.Assert(logEvent, check.NotNil)
293 c.Check(logEvent.asDict(), check.DeepEquals, arvadosclient.Dict{
294 "event_type": "file_download",
295 "object_uuid": s.handler.Cluster.ClusterID + "-tpzed-anonymouspublic",
296 "properties": arvadosclient.Dict{
298 "collection_uuid": arvadostest.FooCollection,
299 "collection_file_path": filePath,
300 "portable_data_hash": arvadostest.FooCollectionPDH,
305 func (s *UnitSuite) TestLogGETPDHAsDict(c *check.C) {
307 reqPath := "/c=" + arvadostest.FooCollectionPDH + filePath
308 req := newRequest("GET", "http://keep-web.example"+reqPath)
309 coll := newCollection(arvadostest.FooCollectionPDH)
310 user := &arvados.User{
311 UUID: arvadostest.ActiveUserUUID,
312 FullName: "Active User",
314 logEvent := newFileEventLog(s.handler, req, filePath, coll, user)
315 c.Assert(logEvent, check.NotNil)
316 c.Check(logEvent.asDict(), check.DeepEquals, arvadosclient.Dict{
317 "event_type": "file_download",
318 "object_uuid": arvadostest.ActiveUserUUID,
319 "properties": arvadosclient.Dict{
321 "portable_data_hash": arvadostest.FooCollectionPDH,
322 "collection_uuid": "",
323 "collection_file_path": filePath,
328 func (s *UnitSuite) TestLogUploadAsDict(c *check.C) {
329 coll := newCollection(arvadostest.FooCollection)
330 user := &arvados.User{
331 UUID: arvadostest.ActiveUserUUID,
332 FullName: "Active User",
334 for _, method := range []string{"POST", "PUT"} {
335 filePath := "/" + method + "File"
336 reqPath := "/c=" + arvadostest.FooCollection + filePath
337 req := newRequest(method, "http://keep-web.example"+reqPath)
338 logEvent := newFileEventLog(s.handler, req, filePath, coll, user)
339 if !c.Check(logEvent, check.NotNil) {
342 c.Check(logEvent.asDict(), check.DeepEquals, arvadosclient.Dict{
343 "event_type": "file_upload",
344 "object_uuid": arvadostest.ActiveUserUUID,
345 "properties": arvadosclient.Dict{
347 "collection_uuid": arvadostest.FooCollection,
348 "collection_file_path": filePath,
354 func (s *UnitSuite) TestLogGETUUIDAsFields(c *check.C) {
356 reqPath := "/c=" + arvadostest.FooCollection + filePath
357 req := newRequest("GET", "http://keep-web.example"+reqPath)
358 coll := newCollection(arvadostest.FooCollection)
359 logEvent := newFileEventLog(s.handler, req, filePath, coll, nil)
360 c.Assert(logEvent, check.NotNil)
361 c.Check(logEvent.asFields(), check.DeepEquals, logrus.Fields{
362 "user_uuid": s.handler.Cluster.ClusterID + "-tpzed-anonymouspublic",
363 "collection_uuid": arvadostest.FooCollection,
364 "collection_file_path": filePath,
365 "portable_data_hash": arvadostest.FooCollectionPDH,
369 func (s *UnitSuite) TestLogGETPDHAsFields(c *check.C) {
371 reqPath := "/c=" + arvadostest.FooCollectionPDH + filePath
372 req := newRequest("GET", "http://keep-web.example"+reqPath)
373 coll := newCollection(arvadostest.FooCollectionPDH)
374 user := &arvados.User{
375 UUID: arvadostest.ActiveUserUUID,
376 FullName: "Active User",
378 logEvent := newFileEventLog(s.handler, req, filePath, coll, user)
379 c.Assert(logEvent, check.NotNil)
380 c.Check(logEvent.asFields(), check.DeepEquals, logrus.Fields{
381 "user_uuid": arvadostest.ActiveUserUUID,
382 "user_full_name": "Active User",
383 "collection_uuid": "",
384 "collection_file_path": filePath,
385 "portable_data_hash": arvadostest.FooCollectionPDH,
389 func (s *UnitSuite) TestLogUploadAsFields(c *check.C) {
390 coll := newCollection(arvadostest.FooCollection)
391 user := &arvados.User{
392 UUID: arvadostest.ActiveUserUUID,
393 FullName: "Active User",
395 for _, method := range []string{"POST", "PUT"} {
396 filePath := "/" + method + "File"
397 reqPath := "/c=" + arvadostest.FooCollection + filePath
398 req := newRequest(method, "http://keep-web.example"+reqPath)
399 logEvent := newFileEventLog(s.handler, req, filePath, coll, user)
400 if !c.Check(logEvent, check.NotNil) {
403 c.Check(logEvent.asFields(), check.DeepEquals, logrus.Fields{
404 "user_uuid": arvadostest.ActiveUserUUID,
405 "user_full_name": "Active User",
406 "collection_uuid": arvadostest.FooCollection,
407 "collection_file_path": filePath,
412 func (s *UnitSuite) TestCORSPreflight(c *check.C) {
414 u := mustParseURL("http://keep-web.example/c=" + arvadostest.FooCollection + "/foo")
415 req := &http.Request{
419 RequestURI: u.RequestURI(),
421 "Origin": {"https://workbench.example"},
422 "Access-Control-Request-Method": {"POST"},
426 // Check preflight for an allowed request
427 resp := httptest.NewRecorder()
428 h.ServeHTTP(resp, req)
429 c.Check(resp.Code, check.Equals, http.StatusOK)
430 c.Check(resp.Body.String(), check.Equals, "")
431 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
432 c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Equals, "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
433 c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Equals, "Authorization, Content-Type, Range, Depth, Destination, If, Lock-Token, Overwrite, Timeout, Cache-Control")
435 // Check preflight for a disallowed request
436 resp = httptest.NewRecorder()
437 req.Header.Set("Access-Control-Request-Method", "MAKE-COFFEE")
438 h.ServeHTTP(resp, req)
439 c.Check(resp.Body.String(), check.Equals, "")
440 c.Check(resp.Code, check.Equals, http.StatusMethodNotAllowed)
443 func (s *UnitSuite) TestWebdavPrefixAndSource(c *check.C) {
444 for _, trial := range []struct {
472 path: "/prefix/dir1/foo",
478 path: "/prefix/dir1/foo",
484 path: "/prefix/dir1/foo",
527 c.Logf("trial %+v", trial)
528 u := mustParseURL("http://" + arvadostest.FooBarDirCollection + ".keep-web.example" + trial.path)
529 req := &http.Request{
530 Method: trial.method,
533 RequestURI: u.RequestURI(),
535 "Authorization": {"Bearer " + arvadostest.ActiveTokenV2},
536 "X-Webdav-Prefix": {trial.prefix},
537 "X-Webdav-Source": {trial.source},
539 Body: ioutil.NopCloser(bytes.NewReader(nil)),
542 resp := httptest.NewRecorder()
543 s.handler.ServeHTTP(resp, req)
545 c.Check(resp.Code, check.Equals, http.StatusNotFound)
546 } else if trial.method == "PROPFIND" {
547 c.Check(resp.Code, check.Equals, http.StatusMultiStatus)
548 c.Check(resp.Body.String(), check.Matches, `(?ms).*>\n?$`)
549 } else if trial.seeOther {
550 c.Check(resp.Code, check.Equals, http.StatusSeeOther)
552 c.Check(resp.Code, check.Equals, http.StatusOK)
557 func (s *UnitSuite) TestEmptyResponse(c *check.C) {
558 // Ensure we start with an empty cache
559 defer os.Setenv("HOME", os.Getenv("HOME"))
560 os.Setenv("HOME", c.MkDir())
562 for _, trial := range []struct {
568 // If we return no content due to a Keep read error,
569 // we should emit a log message.
570 {false, false, http.StatusOK, `(?ms).*only wrote 0 bytes.*`},
572 // If we return no content because the client sent an
573 // If-Modified-Since header, our response should be
574 // 304. We still expect a "File download" log since it
575 // counts as a file access for auditing.
576 {true, true, http.StatusNotModified, `(?ms).*msg="File download".*`},
578 c.Logf("trial: %+v", trial)
579 arvadostest.StartKeep(2, true)
580 if trial.dataExists {
581 arv, err := arvadosclient.MakeArvadosClient()
582 c.Assert(err, check.IsNil)
583 arv.ApiToken = arvadostest.ActiveToken
584 kc, err := keepclient.MakeKeepClient(arv)
585 c.Assert(err, check.IsNil)
586 _, _, err = kc.PutB([]byte("foo"))
587 c.Assert(err, check.IsNil)
590 u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
591 req := &http.Request{
595 RequestURI: u.RequestURI(),
597 "Authorization": {"Bearer " + arvadostest.ActiveToken},
600 if trial.sendIMSHeader {
601 req.Header.Set("If-Modified-Since", strings.Replace(time.Now().UTC().Format(time.RFC1123), "UTC", "GMT", -1))
604 var logbuf bytes.Buffer
605 logger := logrus.New()
607 req = req.WithContext(ctxlog.Context(context.Background(), logger))
609 resp := httptest.NewRecorder()
610 s.handler.ServeHTTP(resp, req)
611 c.Check(resp.Code, check.Equals, trial.expectStatus)
612 c.Check(resp.Body.String(), check.Equals, "")
614 c.Log(logbuf.String())
615 c.Check(logbuf.String(), check.Matches, trial.logRegexp)
619 func (s *UnitSuite) TestInvalidUUID(c *check.C) {
620 bogusID := strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "-"
621 token := arvadostest.ActiveToken
622 for _, trial := range []string{
623 "http://keep-web/c=" + bogusID + "/foo",
624 "http://keep-web/c=" + bogusID + "/t=" + token + "/foo",
625 "http://keep-web/collections/download/" + bogusID + "/" + token + "/foo",
626 "http://keep-web/collections/" + bogusID + "/foo",
627 "http://" + bogusID + ".keep-web/" + bogusID + "/foo",
628 "http://" + bogusID + ".keep-web/t=" + token + "/" + bogusID + "/foo",
631 u := mustParseURL(trial)
632 req := &http.Request{
636 RequestURI: u.RequestURI(),
638 resp := httptest.NewRecorder()
639 s.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
640 s.handler.ServeHTTP(resp, req)
641 c.Check(resp.Code, check.Equals, http.StatusNotFound)
645 func mustParseURL(s string) *url.URL {
646 r, err := url.Parse(s)
648 panic("parse URL: " + s)
653 func (s *IntegrationSuite) TestVhost404(c *check.C) {
654 for _, testURL := range []string{
655 arvadostest.NonexistentCollection + ".example.com/theperthcountyconspiracy",
656 arvadostest.NonexistentCollection + ".example.com/t=" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
658 resp := httptest.NewRecorder()
659 u := mustParseURL(testURL)
660 req := &http.Request{
663 RequestURI: u.RequestURI(),
665 s.handler.ServeHTTP(resp, req)
666 c.Check(resp.Code, check.Equals, http.StatusNotFound)
667 c.Check(resp.Body.String(), check.Equals, notFoundMessage+"\n")
671 // An authorizer modifies an HTTP request to make use of the given
672 // token -- by adding it to a header, cookie, query param, or whatever
673 // -- and returns the HTTP status code we should expect from keep-web if
674 // the token is invalid.
675 type authorizer func(*http.Request, string) int
677 func (s *IntegrationSuite) TestVhostViaAuthzHeaderOAuth2(c *check.C) {
678 s.doVhostRequests(c, authzViaAuthzHeaderOAuth2)
680 func authzViaAuthzHeaderOAuth2(r *http.Request, tok string) int {
681 r.Header.Add("Authorization", "OAuth2 "+tok)
682 return http.StatusUnauthorized
685 func (s *IntegrationSuite) TestVhostViaAuthzHeaderBearer(c *check.C) {
686 s.doVhostRequests(c, authzViaAuthzHeaderBearer)
688 func authzViaAuthzHeaderBearer(r *http.Request, tok string) int {
689 r.Header.Add("Authorization", "Bearer "+tok)
690 return http.StatusUnauthorized
693 func (s *IntegrationSuite) TestVhostViaCookieValue(c *check.C) {
694 s.doVhostRequests(c, authzViaCookieValue)
696 func authzViaCookieValue(r *http.Request, tok string) int {
697 r.AddCookie(&http.Cookie{
698 Name: "arvados_api_token",
699 Value: auth.EncodeTokenCookie([]byte(tok)),
701 return http.StatusUnauthorized
704 func (s *IntegrationSuite) TestVhostViaHTTPBasicAuth(c *check.C) {
705 s.doVhostRequests(c, authzViaHTTPBasicAuth)
707 func authzViaHTTPBasicAuth(r *http.Request, tok string) int {
708 r.AddCookie(&http.Cookie{
709 Name: "arvados_api_token",
710 Value: auth.EncodeTokenCookie([]byte(tok)),
712 return http.StatusUnauthorized
715 func (s *IntegrationSuite) TestVhostViaHTTPBasicAuthWithExtraSpaceChars(c *check.C) {
716 s.doVhostRequests(c, func(r *http.Request, tok string) int {
717 r.AddCookie(&http.Cookie{
718 Name: "arvados_api_token",
719 Value: auth.EncodeTokenCookie([]byte(" " + tok + "\n")),
721 return http.StatusUnauthorized
725 func (s *IntegrationSuite) TestVhostViaPath(c *check.C) {
726 s.doVhostRequests(c, authzViaPath)
728 func authzViaPath(r *http.Request, tok string) int {
729 r.URL.Path = "/t=" + tok + r.URL.Path
730 return http.StatusNotFound
733 func (s *IntegrationSuite) TestVhostViaQueryString(c *check.C) {
734 s.doVhostRequests(c, authzViaQueryString)
736 func authzViaQueryString(r *http.Request, tok string) int {
737 r.URL.RawQuery = "api_token=" + tok
738 return http.StatusUnauthorized
741 func (s *IntegrationSuite) TestVhostViaPOST(c *check.C) {
742 s.doVhostRequests(c, authzViaPOST)
744 func authzViaPOST(r *http.Request, tok string) int {
746 r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
747 r.Body = ioutil.NopCloser(strings.NewReader(
748 url.Values{"api_token": {tok}}.Encode()))
749 return http.StatusUnauthorized
752 func (s *IntegrationSuite) TestVhostViaXHRPOST(c *check.C) {
753 s.doVhostRequests(c, authzViaPOST)
755 func authzViaXHRPOST(r *http.Request, tok string) int {
757 r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
758 r.Header.Add("Origin", "https://origin.example")
759 r.Body = ioutil.NopCloser(strings.NewReader(
762 "disposition": {"attachment"},
764 return http.StatusUnauthorized
767 // Try some combinations of {url, token} using the given authorization
768 // mechanism, and verify the result is correct.
769 func (s *IntegrationSuite) doVhostRequests(c *check.C, authz authorizer) {
770 for _, hostPath := range []string{
771 arvadostest.FooCollection + ".example.com/foo",
772 arvadostest.FooCollection + "--collections.example.com/foo",
773 arvadostest.FooCollection + "--collections.example.com/_/foo",
774 arvadostest.FooCollectionPDH + ".example.com/foo",
775 strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + "--collections.example.com/foo",
776 arvadostest.FooBarDirCollection + ".example.com/dir1/foo",
778 c.Log("doRequests: ", hostPath)
779 s.doVhostRequestsWithHostPath(c, authz, hostPath)
783 func (s *IntegrationSuite) doVhostRequestsWithHostPath(c *check.C, authz authorizer, hostPath string) {
784 for _, tok := range []string{
785 arvadostest.ActiveToken,
786 arvadostest.ActiveToken[:15],
787 arvadostest.SpectatorToken,
791 u := mustParseURL("http://" + hostPath)
792 req := &http.Request{
796 RequestURI: u.RequestURI(),
797 Header: http.Header{},
799 failCode := authz(req, tok)
800 req, resp := s.doReq(req)
801 code, body := resp.Code, resp.Body.String()
803 // If the initial request had a (non-empty) token
804 // showing in the query string, we should have been
805 // redirected in order to hide it in a cookie.
806 c.Check(req.URL.String(), check.Not(check.Matches), `.*api_token=.+`)
808 if tok == arvadostest.ActiveToken {
809 c.Check(code, check.Equals, http.StatusOK)
810 c.Check(body, check.Equals, "foo")
812 c.Check(code >= 400, check.Equals, true)
813 c.Check(code < 500, check.Equals, true)
814 if tok == arvadostest.SpectatorToken {
815 // Valid token never offers to retry
816 // with different credentials.
817 c.Check(code, check.Equals, http.StatusNotFound)
819 // Invalid token can ask to retry
820 // depending on the authz method.
821 c.Check(code, check.Equals, failCode)
824 c.Check(body, check.Equals, notFoundMessage+"\n")
826 c.Check(body, check.Equals, unauthorizedMessage+"\n")
832 func (s *IntegrationSuite) TestVhostPortMatch(c *check.C) {
833 for _, host := range []string{"download.example.com", "DOWNLOAD.EXAMPLE.COM"} {
834 for _, port := range []string{"80", "443", "8000"} {
835 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = fmt.Sprintf("download.example.com:%v", port)
836 u := mustParseURL(fmt.Sprintf("http://%v/by_id/%v/foo", host, arvadostest.FooCollection))
837 req := &http.Request{
841 RequestURI: u.RequestURI(),
842 Header: http.Header{"Authorization": []string{"Bearer " + arvadostest.ActiveToken}},
844 req, resp := s.doReq(req)
845 code, _ := resp.Code, resp.Body.String()
848 c.Check(code, check.Equals, 401)
850 c.Check(code, check.Equals, 200)
856 func (s *IntegrationSuite) do(method string, urlstring string, token string, hdr http.Header) (*http.Request, *httptest.ResponseRecorder) {
857 u := mustParseURL(urlstring)
858 if hdr == nil && token != "" {
859 hdr = http.Header{"Authorization": {"Bearer " + token}}
860 } else if hdr == nil {
862 } else if token != "" {
863 panic("must not pass both token and hdr")
865 return s.doReq(&http.Request{
869 RequestURI: u.RequestURI(),
874 func (s *IntegrationSuite) doReq(req *http.Request) (*http.Request, *httptest.ResponseRecorder) {
875 resp := httptest.NewRecorder()
876 s.handler.ServeHTTP(resp, req)
877 if resp.Code != http.StatusSeeOther {
880 cookies := (&http.Response{Header: resp.Header()}).Cookies()
881 u, _ := req.URL.Parse(resp.Header().Get("Location"))
886 RequestURI: u.RequestURI(),
887 Header: http.Header{},
889 for _, c := range cookies {
895 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToCookie(c *check.C) {
896 s.testVhostRedirectTokenToCookie(c, "GET",
897 arvadostest.FooCollection+".example.com/foo",
898 "?api_token="+arvadostest.ActiveToken,
906 func (s *IntegrationSuite) TestSingleOriginSecretLink(c *check.C) {
907 s.testVhostRedirectTokenToCookie(c, "GET",
908 "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.ActiveToken+"/foo",
917 func (s *IntegrationSuite) TestCollectionSharingToken(c *check.C) {
918 s.testVhostRedirectTokenToCookie(c, "GET",
919 "example.com/c="+arvadostest.FooFileCollectionUUID+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
926 // Same valid sharing token, but requesting a different collection
927 s.testVhostRedirectTokenToCookie(c, "GET",
928 "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
933 regexp.QuoteMeta(notFoundMessage+"\n"),
937 // Bad token in URL is 404 Not Found because it doesn't make sense to
938 // retry the same URL with different authorization.
939 func (s *IntegrationSuite) TestSingleOriginSecretLinkBadToken(c *check.C) {
940 s.testVhostRedirectTokenToCookie(c, "GET",
941 "example.com/c="+arvadostest.FooCollection+"/t=bogus/foo",
946 regexp.QuoteMeta(notFoundMessage+"\n"),
950 // Bad token in a cookie (even if it got there via our own
951 // query-string-to-cookie redirect) is, in principle, retryable via
952 // wb2-login-and-redirect flow.
953 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToBogusCookie(c *check.C) {
955 resp := s.testVhostRedirectTokenToCookie(c, "GET",
956 arvadostest.FooCollection+".example.com/foo",
957 "?api_token=thisisabogustoken",
958 http.Header{"Sec-Fetch-Mode": {"navigate"}},
963 u, err := url.Parse(resp.Header().Get("Location"))
964 c.Assert(err, check.IsNil)
965 c.Logf("redirected to %s", u)
966 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
967 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
968 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
970 // Download/attachment indicated by ?disposition=attachment
971 resp = s.testVhostRedirectTokenToCookie(c, "GET",
972 arvadostest.FooCollection+".example.com/foo",
973 "?api_token=thisisabogustoken&disposition=attachment",
974 http.Header{"Sec-Fetch-Mode": {"navigate"}},
979 u, err = url.Parse(resp.Header().Get("Location"))
980 c.Assert(err, check.IsNil)
981 c.Logf("redirected to %s", u)
982 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
983 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "")
984 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
986 // Download/attachment indicated by vhost
987 resp = s.testVhostRedirectTokenToCookie(c, "GET",
988 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
989 "?api_token=thisisabogustoken",
990 http.Header{"Sec-Fetch-Mode": {"navigate"}},
995 u, err = url.Parse(resp.Header().Get("Location"))
996 c.Assert(err, check.IsNil)
997 c.Logf("redirected to %s", u)
998 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
999 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "")
1000 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
1002 // Without "Sec-Fetch-Mode: navigate" header, just 401.
1003 s.testVhostRedirectTokenToCookie(c, "GET",
1004 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
1005 "?api_token=thisisabogustoken",
1006 http.Header{"Sec-Fetch-Mode": {"cors"}},
1008 http.StatusUnauthorized,
1009 regexp.QuoteMeta(unauthorizedMessage+"\n"),
1011 s.testVhostRedirectTokenToCookie(c, "GET",
1012 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
1013 "?api_token=thisisabogustoken",
1016 http.StatusUnauthorized,
1017 regexp.QuoteMeta(unauthorizedMessage+"\n"),
1021 func (s *IntegrationSuite) TestVhostRedirectWithNoCache(c *check.C) {
1022 resp := s.testVhostRedirectTokenToCookie(c, "GET",
1023 arvadostest.FooCollection+".example.com/foo",
1024 "?api_token=thisisabogustoken",
1026 "Sec-Fetch-Mode": {"navigate"},
1027 "Cache-Control": {"no-cache"},
1030 http.StatusSeeOther,
1033 u, err := url.Parse(resp.Header().Get("Location"))
1034 c.Assert(err, check.IsNil)
1035 c.Logf("redirected to %s", u)
1036 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
1037 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
1038 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
1041 func (s *IntegrationSuite) TestNoTokenWorkbench2LoginFlow(c *check.C) {
1042 for _, trial := range []struct {
1047 {cacheControl: "no-cache"},
1049 {anonToken: true, cacheControl: "no-cache"},
1051 c.Logf("trial: %+v", trial)
1053 if trial.anonToken {
1054 s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
1056 s.handler.Cluster.Users.AnonymousUserToken = ""
1058 req, err := http.NewRequest("GET", "http://"+arvadostest.FooCollection+".example.com/foo", nil)
1059 c.Assert(err, check.IsNil)
1060 req.Header.Set("Sec-Fetch-Mode", "navigate")
1061 if trial.cacheControl != "" {
1062 req.Header.Set("Cache-Control", trial.cacheControl)
1064 resp := httptest.NewRecorder()
1065 s.handler.ServeHTTP(resp, req)
1066 c.Check(resp.Code, check.Equals, http.StatusSeeOther)
1067 u, err := url.Parse(resp.Header().Get("Location"))
1068 c.Assert(err, check.IsNil)
1069 c.Logf("redirected to %q", u)
1070 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
1071 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
1072 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
1076 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSingleOriginError(c *check.C) {
1077 s.testVhostRedirectTokenToCookie(c, "GET",
1078 "example.com/c="+arvadostest.FooCollection+"/foo",
1079 "?api_token="+arvadostest.ActiveToken,
1082 http.StatusBadRequest,
1083 regexp.QuoteMeta("cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n"),
1087 // If client requests an attachment by putting ?disposition=attachment
1088 // in the query string, and gets redirected, the redirect target
1089 // should respond with an attachment.
1090 func (s *IntegrationSuite) TestVhostRedirectQueryTokenRequestAttachment(c *check.C) {
1091 resp := s.testVhostRedirectTokenToCookie(c, "GET",
1092 arvadostest.FooCollection+".example.com/foo",
1093 "?disposition=attachment&api_token="+arvadostest.ActiveToken,
1099 c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
1102 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSiteFS(c *check.C) {
1103 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1104 resp := s.testVhostRedirectTokenToCookie(c, "GET",
1105 "download.example.com/by_id/"+arvadostest.FooCollection+"/foo",
1106 "?api_token="+arvadostest.ActiveToken,
1112 c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
1115 func (s *IntegrationSuite) TestPastCollectionVersionFileAccess(c *check.C) {
1116 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1117 resp := s.testVhostRedirectTokenToCookie(c, "GET",
1118 "download.example.com/c="+arvadostest.WazVersion1Collection+"/waz",
1119 "?api_token="+arvadostest.ActiveToken,
1125 c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
1126 resp = s.testVhostRedirectTokenToCookie(c, "GET",
1127 "download.example.com/by_id/"+arvadostest.WazVersion1Collection+"/waz",
1128 "?api_token="+arvadostest.ActiveToken,
1134 c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
1137 func (s *IntegrationSuite) TestVhostRedirectQueryTokenTrustAllContent(c *check.C) {
1138 s.handler.Cluster.Collections.TrustAllContent = true
1139 s.testVhostRedirectTokenToCookie(c, "GET",
1140 "example.com/c="+arvadostest.FooCollection+"/foo",
1141 "?api_token="+arvadostest.ActiveToken,
1149 func (s *IntegrationSuite) TestVhostRedirectQueryTokenAttachmentOnlyHost(c *check.C) {
1150 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com:1234"
1152 s.testVhostRedirectTokenToCookie(c, "GET",
1153 "example.com/c="+arvadostest.FooCollection+"/foo",
1154 "?api_token="+arvadostest.ActiveToken,
1157 http.StatusBadRequest,
1158 regexp.QuoteMeta("cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n"),
1161 resp := s.testVhostRedirectTokenToCookie(c, "GET",
1162 "example.com:1234/c="+arvadostest.FooCollection+"/foo",
1163 "?api_token="+arvadostest.ActiveToken,
1169 c.Check(resp.Header().Get("Content-Disposition"), check.Equals, "attachment")
1172 func (s *IntegrationSuite) TestVhostRedirectMultipleTokens(c *check.C) {
1173 baseUrl := arvadostest.FooCollection + ".example.com/foo"
1174 query := url.Values{}
1176 // The intent of these tests is to check that requests are redirected
1177 // correctly in the presence of multiple API tokens. The exact response
1178 // codes and content are not closely considered: they're just how
1179 // keep-web responded when we made the smallest possible fix. Changing
1180 // those responses may be okay, but you should still test all these
1181 // different cases and the associated redirect logic.
1182 query["api_token"] = []string{arvadostest.ActiveToken, arvadostest.AnonymousToken}
1183 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
1184 query["api_token"] = []string{arvadostest.ActiveToken, arvadostest.AnonymousToken, ""}
1185 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
1186 query["api_token"] = []string{arvadostest.ActiveToken, "", arvadostest.AnonymousToken}
1187 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
1188 query["api_token"] = []string{"", arvadostest.ActiveToken}
1189 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
1191 expectContent := regexp.QuoteMeta(unauthorizedMessage + "\n")
1192 query["api_token"] = []string{arvadostest.AnonymousToken, "invalidtoo"}
1193 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
1194 query["api_token"] = []string{arvadostest.AnonymousToken, ""}
1195 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
1196 query["api_token"] = []string{"", arvadostest.AnonymousToken}
1197 s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
1200 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie(c *check.C) {
1201 s.testVhostRedirectTokenToCookie(c, "POST",
1202 arvadostest.FooCollection+".example.com/foo",
1204 http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
1205 url.Values{"api_token": {arvadostest.ActiveToken}}.Encode(),
1211 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie404(c *check.C) {
1212 s.testVhostRedirectTokenToCookie(c, "POST",
1213 arvadostest.FooCollection+".example.com/foo",
1215 http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
1216 url.Values{"api_token": {arvadostest.SpectatorToken}}.Encode(),
1217 http.StatusNotFound,
1218 regexp.QuoteMeta(notFoundMessage+"\n"),
1222 func (s *IntegrationSuite) TestAnonymousTokenOK(c *check.C) {
1223 s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
1224 s.testVhostRedirectTokenToCookie(c, "GET",
1225 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
1234 func (s *IntegrationSuite) TestAnonymousTokenError(c *check.C) {
1235 s.handler.Cluster.Users.AnonymousUserToken = "anonymousTokenConfiguredButInvalid"
1236 s.testVhostRedirectTokenToCookie(c, "GET",
1237 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
1241 http.StatusUnauthorized,
1242 "Authorization tokens are not accepted here: .*\n",
1246 func (s *IntegrationSuite) TestSpecialCharsInPath(c *check.C) {
1247 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1249 client := arvados.NewClientFromEnv()
1250 client.AuthToken = arvadostest.ActiveToken
1251 fs, err := (&arvados.Collection{}).FileSystem(client, nil)
1252 c.Assert(err, check.IsNil)
1253 path := `https:\\"odd' path chars`
1254 f, err := fs.OpenFile(path, os.O_CREATE, 0777)
1255 c.Assert(err, check.IsNil)
1257 mtxt, err := fs.MarshalManifest(".")
1258 c.Assert(err, check.IsNil)
1259 var coll arvados.Collection
1260 err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1261 "collection": map[string]string{
1262 "manifest_text": mtxt,
1265 c.Assert(err, check.IsNil)
1267 u, _ := url.Parse("http://download.example.com/c=" + coll.UUID + "/")
1268 req := &http.Request{
1272 RequestURI: u.RequestURI(),
1273 Header: http.Header{
1274 "Authorization": {"Bearer " + client.AuthToken},
1277 resp := httptest.NewRecorder()
1278 s.handler.ServeHTTP(resp, req)
1279 c.Check(resp.Code, check.Equals, http.StatusOK)
1280 doc, err := html.Parse(resp.Body)
1281 c.Assert(err, check.IsNil)
1282 pathHrefMap := getPathHrefMap(doc)
1283 c.Check(pathHrefMap, check.HasLen, 1) // the one leaf added to collection
1284 href, hasPath := pathHrefMap[path]
1285 c.Assert(hasPath, check.Equals, true) // the path is listed
1286 relUrl := mustParseURL(href)
1287 c.Check(relUrl.Path, check.Equals, "./"+path) // href can be decoded back to path
1290 func (s *IntegrationSuite) TestForwardSlashSubstitution(c *check.C) {
1291 arv := arvados.NewClientFromEnv()
1292 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1293 s.handler.Cluster.Collections.ForwardSlashNameSubstitution = "{SOLIDUS}"
1294 name := "foo/bar/baz"
1295 nameShown := strings.Replace(name, "/", "{SOLIDUS}", -1)
1297 client := arvados.NewClientFromEnv()
1298 client.AuthToken = arvadostest.ActiveToken
1299 fs, err := (&arvados.Collection{}).FileSystem(client, nil)
1300 c.Assert(err, check.IsNil)
1301 f, err := fs.OpenFile("filename", os.O_CREATE, 0777)
1302 c.Assert(err, check.IsNil)
1304 mtxt, err := fs.MarshalManifest(".")
1305 c.Assert(err, check.IsNil)
1306 var coll arvados.Collection
1307 err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1308 "collection": map[string]string{
1309 "manifest_text": mtxt,
1311 "owner_uuid": arvadostest.AProjectUUID,
1314 c.Assert(err, check.IsNil)
1315 defer arv.RequestAndDecode(&coll, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
1317 base := "http://download.example.com/by_id/" + coll.OwnerUUID + "/"
1318 for tryURL, expectedAnchorText := range map[string]string{
1319 base: nameShown + "/",
1320 base + nameShown + "/": "filename",
1322 u, _ := url.Parse(tryURL)
1323 req := &http.Request{
1327 RequestURI: u.RequestURI(),
1328 Header: http.Header{
1329 "Authorization": {"Bearer " + client.AuthToken},
1332 resp := httptest.NewRecorder()
1333 s.handler.ServeHTTP(resp, req)
1334 c.Check(resp.Code, check.Equals, http.StatusOK)
1335 doc, err := html.Parse(resp.Body)
1336 c.Assert(err, check.IsNil) // valid HTML
1337 pathHrefMap := getPathHrefMap(doc)
1338 href, hasExpected := pathHrefMap[expectedAnchorText]
1339 c.Assert(hasExpected, check.Equals, true) // has expected anchor text
1340 c.Assert(href, check.Not(check.Equals), "")
1341 relUrl := mustParseURL(href)
1342 c.Check(relUrl.Path, check.Equals, "./"+expectedAnchorText) // decoded href maps back to the anchor text
1346 // XHRs can't follow redirect-with-cookie so they rely on method=POST
1347 // and disposition=attachment (telling us it's acceptable to respond
1348 // with content instead of a redirect) and an Origin header that gets
1349 // added automatically by the browser (telling us it's desirable to do
1351 func (s *IntegrationSuite) TestXHRNoRedirect(c *check.C) {
1352 u, _ := url.Parse("http://example.com/c=" + arvadostest.FooCollection + "/foo")
1353 req := &http.Request{
1357 RequestURI: u.RequestURI(),
1358 Header: http.Header{
1359 "Origin": {"https://origin.example"},
1360 "Content-Type": {"application/x-www-form-urlencoded"},
1362 Body: ioutil.NopCloser(strings.NewReader(url.Values{
1363 "api_token": {arvadostest.ActiveToken},
1364 "disposition": {"attachment"},
1367 resp := httptest.NewRecorder()
1368 s.handler.ServeHTTP(resp, req)
1369 c.Check(resp.Code, check.Equals, http.StatusOK)
1370 c.Check(resp.Body.String(), check.Equals, "foo")
1371 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
1373 // GET + Origin header is representative of both AJAX GET
1374 // requests and inline images via <IMG crossorigin="anonymous"
1376 u.RawQuery = "api_token=" + url.QueryEscape(arvadostest.ActiveTokenV2)
1377 req = &http.Request{
1381 RequestURI: u.RequestURI(),
1382 Header: http.Header{
1383 "Origin": {"https://origin.example"},
1386 resp = httptest.NewRecorder()
1387 s.handler.ServeHTTP(resp, req)
1388 c.Check(resp.Code, check.Equals, http.StatusOK)
1389 c.Check(resp.Body.String(), check.Equals, "foo")
1390 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
1393 func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, hostPath, queryString string, reqHeader http.Header, reqBody string, expectStatus int, matchRespBody string) *httptest.ResponseRecorder {
1394 if reqHeader == nil {
1395 reqHeader = http.Header{}
1397 u, _ := url.Parse(`http://` + hostPath + queryString)
1398 c.Logf("requesting %s", u)
1399 req := &http.Request{
1403 RequestURI: u.RequestURI(),
1405 Body: ioutil.NopCloser(strings.NewReader(reqBody)),
1408 resp := httptest.NewRecorder()
1410 c.Check(resp.Code, check.Equals, expectStatus)
1411 c.Check(resp.Body.String(), check.Matches, matchRespBody)
1414 s.handler.ServeHTTP(resp, req)
1415 if resp.Code != http.StatusSeeOther {
1416 attachment, _ := regexp.MatchString(`^attachment(;|$)`, resp.Header().Get("Content-Disposition"))
1417 // Since we're not redirecting, check that any api_token in the URL is
1419 // If there is no token in the URL, then we're good.
1420 // Otherwise, if the response code is an error, the body is expected to
1421 // be static content, and nothing that might maliciously introspect the
1422 // URL. It's considered safe and allowed.
1423 // Otherwise, if the response content has attachment disposition,
1424 // that's considered safe for all the reasons explained in the
1425 // safeAttachment comment in handler.go.
1426 c.Check(!u.Query().Has("api_token") || resp.Code >= 400 || attachment, check.Equals, true)
1430 loc, err := url.Parse(resp.Header().Get("Location"))
1431 c.Assert(err, check.IsNil)
1432 c.Check(loc.Scheme, check.Equals, u.Scheme)
1433 c.Check(loc.Host, check.Equals, u.Host)
1434 c.Check(loc.RawPath, check.Equals, u.RawPath)
1435 // If the response was a redirect, it should never include an API token.
1436 c.Check(loc.Query().Has("api_token"), check.Equals, false)
1437 c.Check(resp.Body.String(), check.Matches, `.*href="http://`+regexp.QuoteMeta(html.EscapeString(hostPath))+`(\?[^"]*)?".*`)
1438 cookies := (&http.Response{Header: resp.Header()}).Cookies()
1440 c.Logf("following redirect to %s", u)
1441 req = &http.Request{
1445 RequestURI: loc.RequestURI(),
1448 for _, c := range cookies {
1452 resp = httptest.NewRecorder()
1453 s.handler.ServeHTTP(resp, req)
1455 if resp.Code != http.StatusSeeOther {
1456 c.Check(resp.Header().Get("Location"), check.Equals, "")
1461 func (s *IntegrationSuite) TestDirectoryListingWithAnonymousToken(c *check.C) {
1462 s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
1463 s.testDirectoryListing(c)
1466 func (s *IntegrationSuite) TestDirectoryListingWithNoAnonymousToken(c *check.C) {
1467 s.handler.Cluster.Users.AnonymousUserToken = ""
1468 s.testDirectoryListing(c)
1471 func (s *IntegrationSuite) testDirectoryListing(c *check.C) {
1472 // The "ownership cycle" test fixtures are reachable from the
1473 // "filter group without filters" group, causing webdav's
1474 // walkfs to recurse indefinitely. Avoid that by deleting one
1475 // of the bogus fixtures.
1476 arv := arvados.NewClientFromEnv()
1477 err := arv.RequestAndDecode(nil, "DELETE", "arvados/v1/groups/zzzzz-j7d0g-cx2al9cqkmsf1hs", nil, nil)
1479 c.Assert(err, check.FitsTypeOf, &arvados.TransactionError{})
1480 c.Check(err.(*arvados.TransactionError).StatusCode, check.Equals, 404)
1483 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1484 authHeader := http.Header{
1485 "Authorization": {"OAuth2 " + arvadostest.ActiveToken},
1487 for _, trial := range []struct {
1495 uri: strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/",
1497 expect: []string{"dir1/foo", "dir1/bar"},
1501 uri: strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/dir1/",
1503 expect: []string{"foo", "bar"},
1507 // URLs of this form ignore authHeader, and
1508 // FooAndBarFilesInDirUUID isn't public, so
1509 // this returns 401.
1510 uri: "download.example.com/collections/" + arvadostest.FooAndBarFilesInDirUUID + "/",
1515 uri: "download.example.com/users/active/foo_file_in_dir/",
1517 expect: []string{"dir1/"},
1521 uri: "download.example.com/users/active/foo_file_in_dir/dir1/",
1523 expect: []string{"bar"},
1527 uri: "download.example.com/",
1529 expect: []string{"users/"},
1533 uri: "download.example.com/users",
1535 redirect: "/users/",
1536 expect: []string{"active/"},
1540 uri: "download.example.com/users/",
1542 expect: []string{"active/"},
1546 uri: "download.example.com/users/active",
1548 redirect: "/users/active/",
1549 expect: []string{"foo_file_in_dir/"},
1553 uri: "download.example.com/users/active/",
1555 expect: []string{"foo_file_in_dir/"},
1559 uri: "collections.example.com/collections/download/" + arvadostest.FooAndBarFilesInDirUUID + "/" + arvadostest.ActiveToken + "/",
1561 expect: []string{"dir1/foo", "dir1/bar"},
1565 uri: "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/",
1567 expect: []string{"dir1/foo", "dir1/bar"},
1571 uri: "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken,
1573 expect: []string{"dir1/foo", "dir1/bar"},
1577 uri: "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID,
1579 expect: []string{"dir1/foo", "dir1/bar"},
1583 uri: "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1",
1585 redirect: "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1/",
1586 expect: []string{"foo", "bar"},
1590 uri: "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/_/dir1/",
1592 expect: []string{"foo", "bar"},
1596 uri: arvadostest.FooAndBarFilesInDirUUID + ".example.com/dir1?api_token=" + arvadostest.ActiveToken,
1599 expect: []string{"foo", "bar"},
1603 uri: "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/theperthcountyconspiracydoesnotexist/",
1608 uri: "download.example.com/c=" + arvadostest.WazVersion1Collection,
1610 expect: []string{"waz"},
1614 uri: "download.example.com/by_id/" + arvadostest.WazVersion1Collection,
1616 expect: []string{"waz"},
1620 uri: "download.example.com/users/active/This filter group/",
1622 expect: []string{"A Subproject/"},
1626 uri: "download.example.com/users/active/This filter group/A Subproject",
1628 expect: []string{"baz_file/"},
1632 uri: "download.example.com/by_id/" + arvadostest.AFilterGroupUUID,
1634 expect: []string{"A Subproject/"},
1638 uri: "download.example.com/by_id/" + arvadostest.AFilterGroupUUID + "/A Subproject",
1640 expect: []string{"baz_file/"},
1644 comment := check.Commentf("HTML: %q redir %q => %q", trial.uri, trial.redirect, trial.expect)
1645 resp := httptest.NewRecorder()
1646 u := mustParseURL("//" + trial.uri)
1647 req := &http.Request{
1651 RequestURI: u.RequestURI(),
1652 Header: copyHeader(trial.header),
1654 s.handler.ServeHTTP(resp, req)
1655 var cookies []*http.Cookie
1656 for resp.Code == http.StatusSeeOther {
1657 u, _ := req.URL.Parse(resp.Header().Get("Location"))
1658 req = &http.Request{
1662 RequestURI: u.RequestURI(),
1663 Header: copyHeader(trial.header),
1665 cookies = append(cookies, (&http.Response{Header: resp.Header()}).Cookies()...)
1666 for _, c := range cookies {
1669 resp = httptest.NewRecorder()
1670 s.handler.ServeHTTP(resp, req)
1672 if trial.redirect != "" {
1673 c.Check(req.URL.Path, check.Equals, trial.redirect, comment)
1675 if trial.expect == nil {
1676 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1678 c.Check(resp.Code, check.Equals, http.StatusOK, comment)
1679 listingPageDoc, err := html.Parse(resp.Body)
1680 c.Check(err, check.IsNil, comment) // valid HTML document
1681 pathHrefMap := getPathHrefMap(listingPageDoc)
1682 c.Assert(pathHrefMap, check.Not(check.HasLen), 0, comment)
1683 for _, e := range trial.expect {
1684 href, hasE := pathHrefMap[e]
1685 c.Check(hasE, check.Equals, true, comment) // expected path is listed
1686 relUrl := mustParseURL(href)
1687 c.Check(relUrl.Path, check.Equals, "./"+e, comment) // href can be decoded back to path
1689 wgetCommand := getWgetExamplePre(listingPageDoc)
1690 wgetExpected := regexp.MustCompile(`^\$ wget .*--cut-dirs=(\d+) .*'(https?://[^']+)'$`)
1691 wgetMatchGroups := wgetExpected.FindStringSubmatch(wgetCommand)
1692 c.Assert(wgetMatchGroups, check.NotNil) // wget command matches
1693 c.Check(wgetMatchGroups[1], check.Equals, fmt.Sprintf("%d", trial.cutDirs)) // correct level of cut dirs in wget command
1694 printedUrl := mustParseURL(wgetMatchGroups[2])
1695 c.Check(printedUrl.Host, check.Equals, req.URL.Host)
1696 c.Check(printedUrl.Path, check.Equals, req.URL.Path) // URL arg in wget command can be decoded to the right path
1699 comment = check.Commentf("WebDAV: %q => %q", trial.uri, trial.expect)
1700 req = &http.Request{
1704 RequestURI: u.RequestURI(),
1705 Header: copyHeader(trial.header),
1706 Body: ioutil.NopCloser(&bytes.Buffer{}),
1708 resp = httptest.NewRecorder()
1709 s.handler.ServeHTTP(resp, req)
1710 if trial.expect == nil {
1711 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1713 c.Check(resp.Code, check.Equals, http.StatusOK, comment)
1716 req = &http.Request{
1720 RequestURI: u.RequestURI(),
1721 Header: copyHeader(trial.header),
1722 Body: ioutil.NopCloser(&bytes.Buffer{}),
1724 resp = httptest.NewRecorder()
1725 s.handler.ServeHTTP(resp, req)
1726 // This check avoids logging a big XML document in the
1727 // event webdav throws a 500 error after sending
1728 // headers for a 207.
1729 if !c.Check(strings.HasSuffix(resp.Body.String(), "Internal Server Error"), check.Equals, false) {
1732 if trial.expect == nil {
1733 c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1735 c.Check(resp.Code, check.Equals, http.StatusMultiStatus, comment)
1736 for _, e := range trial.expect {
1737 if strings.HasSuffix(e, "/") {
1738 e = filepath.Join(u.Path, e) + "/"
1740 e = filepath.Join(u.Path, e)
1742 e = strings.Replace(e, " ", "%20", -1)
1743 c.Check(resp.Body.String(), check.Matches, `(?ms).*<D:href>`+e+`</D:href>.*`, comment)
1749 // Shallow-traverse the HTML document, gathering the nodes satisfying the
1750 // predicate function in the output slice. If a node matches the predicate,
1751 // none of its children will be visited.
1752 func getNodes(document *html.Node, predicate func(*html.Node) bool) []*html.Node {
1753 var acc []*html.Node
1754 var traverse func(*html.Node, []*html.Node) []*html.Node
1755 traverse = func(root *html.Node, sofar []*html.Node) []*html.Node {
1759 if predicate(root) {
1760 return append(sofar, root)
1762 for cur := root.FirstChild; cur != nil; cur = cur.NextSibling {
1763 sofar = traverse(cur, sofar)
1767 return traverse(document, acc)
1770 // Returns true if a node has the attribute targetAttr with the given value
1771 func matchesAttributeValue(node *html.Node, targetAttr string, value string) bool {
1772 for _, attr := range node.Attr {
1773 if attr.Key == targetAttr && attr.Val == value {
1780 // Concatenate the content of text-node children of node; only direct
1781 // children are visited, and any non-text children are skipped.
1782 func getNodeText(node *html.Node) string {
1783 var recv strings.Builder
1784 for c := node.FirstChild; c != nil; c = c.NextSibling {
1785 if c.Type == html.TextNode {
1786 recv.WriteString(c.Data)
1789 return recv.String()
1792 // Returns a map from the directory listing item string (a path) to the href
1793 // value of its <a> tag (an encoded relative URL)
1794 func getPathHrefMap(document *html.Node) map[string]string {
1795 isItemATag := func(node *html.Node) bool {
1796 return node.Type == html.ElementNode && node.Data == "a" && matchesAttributeValue(node, "class", "item")
1798 aTags := getNodes(document, isItemATag)
1799 output := make(map[string]string)
1800 for _, elem := range aTags {
1801 textContent := getNodeText(elem)
1802 for _, attr := range elem.Attr {
1803 if attr.Key == "href" {
1804 output[textContent] = attr.Val
1812 func getWgetExamplePre(document *html.Node) string {
1813 isWgetPre := func(node *html.Node) bool {
1814 return node.Type == html.ElementNode && matchesAttributeValue(node, "id", "wget-example")
1816 elements := getNodes(document, isWgetPre)
1817 if len(elements) != 1 {
1820 return getNodeText(elements[0])
1823 func (s *IntegrationSuite) TestDeleteLastFile(c *check.C) {
1824 arv := arvados.NewClientFromEnv()
1825 var newCollection arvados.Collection
1826 err := arv.RequestAndDecode(&newCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1827 "collection": map[string]string{
1828 "owner_uuid": arvadostest.ActiveUserUUID,
1829 "manifest_text": ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt 0:3:bar.txt\n",
1830 "name": "keep-web test collection",
1832 "ensure_unique_name": true,
1834 c.Assert(err, check.IsNil)
1835 defer arv.RequestAndDecode(&newCollection, "DELETE", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
1837 var updated arvados.Collection
1838 for _, fnm := range []string{"foo.txt", "bar.txt"} {
1839 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com"
1840 u, _ := url.Parse("http://example.com/c=" + newCollection.UUID + "/" + fnm)
1841 req := &http.Request{
1845 RequestURI: u.RequestURI(),
1846 Header: http.Header{
1847 "Authorization": {"Bearer " + arvadostest.ActiveToken},
1850 resp := httptest.NewRecorder()
1851 s.handler.ServeHTTP(resp, req)
1852 c.Check(resp.Code, check.Equals, http.StatusNoContent)
1854 updated = arvados.Collection{}
1855 err = arv.RequestAndDecode(&updated, "GET", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
1856 c.Check(err, check.IsNil)
1857 c.Check(updated.ManifestText, check.Not(check.Matches), `(?ms).*\Q`+fnm+`\E.*`)
1858 c.Logf("updated manifest_text %q", updated.ManifestText)
1860 c.Check(updated.ManifestText, check.Equals, "")
1863 func (s *IntegrationSuite) TestFileContentType(c *check.C) {
1864 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1866 client := arvados.NewClientFromEnv()
1867 client.AuthToken = arvadostest.ActiveToken
1868 arv, err := arvadosclient.New(client)
1869 c.Assert(err, check.Equals, nil)
1870 kc, err := keepclient.MakeKeepClient(arv)
1871 c.Assert(err, check.Equals, nil)
1873 fs, err := (&arvados.Collection{}).FileSystem(client, kc)
1874 c.Assert(err, check.IsNil)
1876 trials := []struct {
1881 {"picture.txt", "BMX bikes are small this year\n", "text/plain; charset=utf-8"},
1882 {"picture.bmp", "BMX bikes are small this year\n", "image/(x-ms-)?bmp"},
1883 {"picture.jpg", "BMX bikes are small this year\n", "image/jpeg"},
1884 {"picture1", "BMX bikes are small this year\n", "image/bmp"}, // content sniff; "BM" is the magic signature for .bmp
1885 {"picture2", "Cars are small this year\n", "text/plain; charset=utf-8"}, // content sniff
1887 for _, trial := range trials {
1888 f, err := fs.OpenFile(trial.filename, os.O_CREATE|os.O_WRONLY, 0777)
1889 c.Assert(err, check.IsNil)
1890 _, err = f.Write([]byte(trial.content))
1891 c.Assert(err, check.IsNil)
1892 c.Assert(f.Close(), check.IsNil)
1894 mtxt, err := fs.MarshalManifest(".")
1895 c.Assert(err, check.IsNil)
1896 var coll arvados.Collection
1897 err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1898 "collection": map[string]string{
1899 "manifest_text": mtxt,
1902 c.Assert(err, check.IsNil)
1904 for _, trial := range trials {
1905 u, _ := url.Parse("http://download.example.com/by_id/" + coll.UUID + "/" + trial.filename)
1906 req := &http.Request{
1910 RequestURI: u.RequestURI(),
1911 Header: http.Header{
1912 "Authorization": {"Bearer " + client.AuthToken},
1915 resp := httptest.NewRecorder()
1916 s.handler.ServeHTTP(resp, req)
1917 c.Check(resp.Code, check.Equals, http.StatusOK)
1918 c.Check(resp.Header().Get("Content-Type"), check.Matches, trial.contentType)
1919 c.Check(resp.Body.String(), check.Equals, trial.content)
1923 func (s *IntegrationSuite) TestCacheSize(c *check.C) {
1924 req, err := http.NewRequest("GET", "http://"+arvadostest.FooCollection+".example.com/foo", nil)
1925 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveTokenV2)
1926 c.Assert(err, check.IsNil)
1927 resp := httptest.NewRecorder()
1928 s.handler.ServeHTTP(resp, req)
1929 c.Assert(resp.Code, check.Equals, http.StatusOK)
1930 c.Check(s.handler.Cache.sessions[arvadostest.ActiveTokenV2].client.DiskCacheSize.Percent(), check.Equals, int64(10))
1933 // Writing to a collection shouldn't affect its entry in the
1934 // PDH-to-manifest cache.
1935 func (s *IntegrationSuite) TestCacheWriteCollectionSamePDH(c *check.C) {
1936 arv, err := arvadosclient.MakeArvadosClient()
1937 c.Assert(err, check.Equals, nil)
1938 arv.ApiToken = arvadostest.ActiveToken
1940 u := mustParseURL("http://x.example/testfile")
1941 req := &http.Request{
1945 RequestURI: u.RequestURI(),
1946 Header: http.Header{"Authorization": {"Bearer " + arv.ApiToken}},
1949 checkWithID := func(id string, status int) {
1950 req.URL.Host = strings.Replace(id, "+", "-", -1) + ".example"
1951 req.Host = req.URL.Host
1952 resp := httptest.NewRecorder()
1953 s.handler.ServeHTTP(resp, req)
1954 c.Check(resp.Code, check.Equals, status)
1957 var colls [2]arvados.Collection
1958 for i := range colls {
1959 err := arv.Create("collections",
1960 map[string]interface{}{
1961 "ensure_unique_name": true,
1962 "collection": map[string]interface{}{
1963 "name": "test collection",
1966 c.Assert(err, check.Equals, nil)
1969 // Populate cache with empty collection
1970 checkWithID(colls[0].PortableDataHash, http.StatusNotFound)
1972 // write a file to colls[0]
1974 reqPut.Method = "PUT"
1975 reqPut.URL.Host = colls[0].UUID + ".example"
1976 reqPut.Host = req.URL.Host
1977 reqPut.Body = ioutil.NopCloser(bytes.NewBufferString("testdata"))
1978 resp := httptest.NewRecorder()
1979 s.handler.ServeHTTP(resp, &reqPut)
1980 c.Check(resp.Code, check.Equals, http.StatusCreated)
1982 // new file should not appear in colls[1]
1983 checkWithID(colls[1].PortableDataHash, http.StatusNotFound)
1984 checkWithID(colls[1].UUID, http.StatusNotFound)
1986 checkWithID(colls[0].UUID, http.StatusOK)
1989 func copyHeader(h http.Header) http.Header {
1991 for k, v := range h {
1992 hc[k] = append([]string(nil), v...)
1997 func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Request,
1998 successCode int, direction string, perm bool, userUuid, collectionUuid, collectionPDH, filepath string) {
2000 client := arvados.NewClientFromEnv()
2001 client.AuthToken = arvadostest.AdminToken
2002 var logentries arvados.LogList
2004 err := client.RequestAndDecode(&logentries, "GET", "arvados/v1/logs", nil,
2005 arvados.ResourceListParams{
2007 Order: "created_at desc"})
2008 c.Check(err, check.IsNil)
2009 c.Check(logentries.Items, check.HasLen, 1)
2010 lastLogId := logentries.Items[0].ID
2011 c.Logf("lastLogId: %d", lastLogId)
2013 var logbuf bytes.Buffer
2014 logger := logrus.New()
2015 logger.Out = &logbuf
2016 resp := httptest.NewRecorder()
2017 req = req.WithContext(ctxlog.Context(context.Background(), logger))
2018 s.handler.ServeHTTP(resp, req)
2021 c.Check(resp.Result().StatusCode, check.Equals, successCode)
2022 c.Check(logbuf.String(), check.Matches, `(?ms).*msg="File `+direction+`".*`)
2023 c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*level=error.*`)
2025 deadline := time.Now().Add(time.Second)
2027 c.Assert(time.Now().After(deadline), check.Equals, false, check.Commentf("timed out waiting for log entry"))
2028 logentries = arvados.LogList{}
2029 err = client.RequestAndDecode(&logentries, "GET", "arvados/v1/logs", nil,
2030 arvados.ResourceListParams{
2031 Filters: []arvados.Filter{
2032 {Attr: "event_type", Operator: "=", Operand: "file_" + direction},
2033 {Attr: "object_uuid", Operator: "=", Operand: userUuid},
2036 Order: "created_at desc",
2038 c.Assert(err, check.IsNil)
2039 if len(logentries.Items) > 0 &&
2040 logentries.Items[0].ID > lastLogId &&
2041 logentries.Items[0].ObjectUUID == userUuid &&
2042 logentries.Items[0].Properties["collection_uuid"] == collectionUuid &&
2043 (collectionPDH == "" || logentries.Items[0].Properties["portable_data_hash"] == collectionPDH) &&
2044 logentries.Items[0].Properties["collection_file_path"] == filepath {
2047 c.Logf("logentries.Items: %+v", logentries.Items)
2048 time.Sleep(50 * time.Millisecond)
2051 c.Check(resp.Result().StatusCode, check.Equals, http.StatusForbidden)
2052 c.Check(logbuf.String(), check.Equals, "")
2056 func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
2057 u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
2059 s.handler.Cluster.Collections.TrustAllContent = true
2061 for _, adminperm := range []bool{true, false} {
2062 for _, userperm := range []bool{true, false} {
2063 s.handler.Cluster.Collections.WebDAVPermission.Admin.Download = adminperm
2064 s.handler.Cluster.Collections.WebDAVPermission.User.Download = userperm
2066 // Test admin permission
2067 req := &http.Request{
2071 RequestURI: u.RequestURI(),
2072 Header: http.Header{
2073 "Authorization": {"Bearer " + arvadostest.AdminToken},
2076 s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", adminperm,
2077 arvadostest.AdminUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
2079 // Test user permission
2080 req = &http.Request{
2084 RequestURI: u.RequestURI(),
2085 Header: http.Header{
2086 "Authorization": {"Bearer " + arvadostest.ActiveToken},
2089 s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", userperm,
2090 arvadostest.ActiveUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
2094 s.handler.Cluster.Collections.WebDAVPermission.User.Download = true
2096 for _, tryurl := range []string{"http://" + arvadostest.MultilevelCollection1 + ".keep-web.example/dir1/subdir/file1",
2097 "http://keep-web/users/active/multilevel_collection_1/dir1/subdir/file1"} {
2099 u = mustParseURL(tryurl)
2100 req := &http.Request{
2104 RequestURI: u.RequestURI(),
2105 Header: http.Header{
2106 "Authorization": {"Bearer " + arvadostest.ActiveToken},
2109 s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
2110 arvadostest.ActiveUserUUID, arvadostest.MultilevelCollection1, arvadostest.MultilevelCollection1PDH, "dir1/subdir/file1")
2113 u = mustParseURL("http://" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".keep-web.example/foo")
2114 req := &http.Request{
2118 RequestURI: u.RequestURI(),
2119 Header: http.Header{
2120 "Authorization": {"Bearer " + arvadostest.ActiveToken},
2123 s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
2124 arvadostest.ActiveUserUUID, "", arvadostest.FooCollectionPDH, "foo")
2127 func (s *IntegrationSuite) TestUploadLoggingPermission(c *check.C) {
2128 for _, adminperm := range []bool{true, false} {
2129 for _, userperm := range []bool{true, false} {
2131 arv := arvados.NewClientFromEnv()
2132 arv.AuthToken = arvadostest.ActiveToken
2134 var coll arvados.Collection
2135 err := arv.RequestAndDecode(&coll,
2137 "/arvados/v1/collections",
2139 map[string]interface{}{
2140 "ensure_unique_name": true,
2141 "collection": map[string]interface{}{
2142 "name": "test collection",
2145 c.Assert(err, check.Equals, nil)
2147 u := mustParseURL("http://" + coll.UUID + ".keep-web.example/bar")
2149 s.handler.Cluster.Collections.WebDAVPermission.Admin.Upload = adminperm
2150 s.handler.Cluster.Collections.WebDAVPermission.User.Upload = userperm
2152 // Test admin permission
2153 req := &http.Request{
2157 RequestURI: u.RequestURI(),
2158 Header: http.Header{
2159 "Authorization": {"Bearer " + arvadostest.AdminToken},
2161 Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
2163 s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", adminperm,
2164 arvadostest.AdminUserUUID, coll.UUID, "", "bar")
2166 // Test user permission
2167 req = &http.Request{
2171 RequestURI: u.RequestURI(),
2172 Header: http.Header{
2173 "Authorization": {"Bearer " + arvadostest.ActiveToken},
2175 Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
2177 s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", userperm,
2178 arvadostest.ActiveUserUUID, coll.UUID, "", "bar")
2183 func (s *IntegrationSuite) TestConcurrentWrites(c *check.C) {
2184 s.handler.Cluster.Collections.WebDAVCache.TTL = arvados.Duration(time.Second * 2)
2185 lockTidyInterval = time.Second
2186 client := arvados.NewClientFromEnv()
2187 client.AuthToken = arvadostest.ActiveTokenV2
2188 // Start small, and increase concurrency (2^2, 4^2, ...)
2189 // only until hitting failure. Avoids unnecessarily long
2191 for n := 2; n < 16 && !c.Failed(); n = n * 2 {
2192 c.Logf("%s: n=%d", c.TestName(), n)
2194 var coll arvados.Collection
2195 err := client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, nil)
2196 c.Assert(err, check.IsNil)
2197 defer client.RequestAndDecode(&coll, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
2199 var wg sync.WaitGroup
2200 for i := 0; i < n && !c.Failed(); i++ {
2205 u := mustParseURL(fmt.Sprintf("http://%s.collections.example.com/i=%d", coll.UUID, i))
2206 resp := httptest.NewRecorder()
2207 req, err := http.NewRequest("MKCOL", u.String(), nil)
2208 c.Assert(err, check.IsNil)
2209 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
2210 s.handler.ServeHTTP(resp, req)
2211 c.Assert(resp.Code, check.Equals, http.StatusCreated)
2212 for j := 0; j < n && !c.Failed(); j++ {
2217 content := fmt.Sprintf("i=%d/j=%d", i, j)
2218 u := mustParseURL("http://" + coll.UUID + ".collections.example.com/" + content)
2220 resp := httptest.NewRecorder()
2221 req, err := http.NewRequest("PUT", u.String(), strings.NewReader(content))
2222 c.Assert(err, check.IsNil)
2223 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
2224 s.handler.ServeHTTP(resp, req)
2225 c.Check(resp.Code, check.Equals, http.StatusCreated)
2227 time.Sleep(time.Second)
2228 resp = httptest.NewRecorder()
2229 req, err = http.NewRequest("GET", u.String(), nil)
2230 c.Assert(err, check.IsNil)
2231 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
2232 s.handler.ServeHTTP(resp, req)
2233 c.Check(resp.Code, check.Equals, http.StatusOK)
2234 c.Check(resp.Body.String(), check.Equals, content)
2240 for i := 0; i < n; i++ {
2241 u := mustParseURL(fmt.Sprintf("http://%s.collections.example.com/i=%d", coll.UUID, i))
2242 resp := httptest.NewRecorder()
2243 req, err := http.NewRequest("PROPFIND", u.String(), &bytes.Buffer{})
2244 c.Assert(err, check.IsNil)
2245 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
2246 s.handler.ServeHTTP(resp, req)
2247 c.Assert(resp.Code, check.Equals, http.StatusMultiStatus)