Merge branch '21223-arv-mount-nofile' refs #21223
[arvados.git] / services / keep-web / handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package keepweb
6
7 import (
8         "bytes"
9         "context"
10         "fmt"
11         "html"
12         "io"
13         "io/ioutil"
14         "net/http"
15         "net/http/httptest"
16         "net/url"
17         "os"
18         "path/filepath"
19         "regexp"
20         "strings"
21         "sync"
22         "time"
23
24         "git.arvados.org/arvados.git/lib/config"
25         "git.arvados.org/arvados.git/sdk/go/arvados"
26         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
27         "git.arvados.org/arvados.git/sdk/go/arvadostest"
28         "git.arvados.org/arvados.git/sdk/go/auth"
29         "git.arvados.org/arvados.git/sdk/go/ctxlog"
30         "git.arvados.org/arvados.git/sdk/go/keepclient"
31         "github.com/prometheus/client_golang/prometheus"
32         "github.com/sirupsen/logrus"
33         check "gopkg.in/check.v1"
34 )
35
36 var _ = check.Suite(&UnitSuite{})
37
38 func init() {
39         arvados.DebugLocksPanicMode = true
40 }
41
42 type UnitSuite struct {
43         cluster *arvados.Cluster
44         handler *handler
45 }
46
47 func (s *UnitSuite) SetUpTest(c *check.C) {
48         logger := ctxlog.TestLogger(c)
49         ldr := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), logger)
50         ldr.Path = "-"
51         cfg, err := ldr.Load()
52         c.Assert(err, check.IsNil)
53         cc, err := cfg.GetCluster("")
54         c.Assert(err, check.IsNil)
55         s.cluster = cc
56         s.handler = &handler{
57                 Cluster: cc,
58                 Cache: cache{
59                         cluster:  cc,
60                         logger:   logger,
61                         registry: prometheus.NewRegistry(),
62                 },
63         }
64 }
65
66 func (s *UnitSuite) TestCORSPreflight(c *check.C) {
67         h := s.handler
68         u := mustParseURL("http://keep-web.example/c=" + arvadostest.FooCollection + "/foo")
69         req := &http.Request{
70                 Method:     "OPTIONS",
71                 Host:       u.Host,
72                 URL:        u,
73                 RequestURI: u.RequestURI(),
74                 Header: http.Header{
75                         "Origin":                        {"https://workbench.example"},
76                         "Access-Control-Request-Method": {"POST"},
77                 },
78         }
79
80         // Check preflight for an allowed request
81         resp := httptest.NewRecorder()
82         h.ServeHTTP(resp, req)
83         c.Check(resp.Code, check.Equals, http.StatusOK)
84         c.Check(resp.Body.String(), check.Equals, "")
85         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
86         c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Equals, "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
87         c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Equals, "Authorization, Content-Type, Range, Depth, Destination, If, Lock-Token, Overwrite, Timeout, Cache-Control")
88
89         // Check preflight for a disallowed request
90         resp = httptest.NewRecorder()
91         req.Header.Set("Access-Control-Request-Method", "MAKE-COFFEE")
92         h.ServeHTTP(resp, req)
93         c.Check(resp.Body.String(), check.Equals, "")
94         c.Check(resp.Code, check.Equals, http.StatusMethodNotAllowed)
95 }
96
97 func (s *UnitSuite) TestWebdavPrefixAndSource(c *check.C) {
98         for _, trial := range []struct {
99                 method   string
100                 path     string
101                 prefix   string
102                 source   string
103                 notFound bool
104                 seeOther bool
105         }{
106                 {
107                         method: "PROPFIND",
108                         path:   "/",
109                 },
110                 {
111                         method: "PROPFIND",
112                         path:   "/dir1",
113                 },
114                 {
115                         method: "PROPFIND",
116                         path:   "/dir1/",
117                 },
118                 {
119                         method: "PROPFIND",
120                         path:   "/dir1/foo",
121                         prefix: "/dir1",
122                         source: "/dir1",
123                 },
124                 {
125                         method: "PROPFIND",
126                         path:   "/prefix/dir1/foo",
127                         prefix: "/prefix/",
128                         source: "",
129                 },
130                 {
131                         method: "PROPFIND",
132                         path:   "/prefix/dir1/foo",
133                         prefix: "/prefix",
134                         source: "",
135                 },
136                 {
137                         method: "PROPFIND",
138                         path:   "/prefix/dir1/foo",
139                         prefix: "/prefix/",
140                         source: "/",
141                 },
142                 {
143                         method: "PROPFIND",
144                         path:   "/prefix/foo",
145                         prefix: "/prefix/",
146                         source: "/dir1/",
147                 },
148                 {
149                         method: "GET",
150                         path:   "/prefix/foo",
151                         prefix: "/prefix/",
152                         source: "/dir1/",
153                 },
154                 {
155                         method: "PROPFIND",
156                         path:   "/prefix/",
157                         prefix: "/prefix",
158                         source: "/dir1",
159                 },
160                 {
161                         method: "PROPFIND",
162                         path:   "/prefix",
163                         prefix: "/prefix",
164                         source: "/dir1/",
165                 },
166                 {
167                         method:   "GET",
168                         path:     "/prefix",
169                         prefix:   "/prefix",
170                         source:   "/dir1",
171                         seeOther: true,
172                 },
173                 {
174                         method:   "PROPFIND",
175                         path:     "/dir1/foo",
176                         prefix:   "",
177                         source:   "/dir1",
178                         notFound: true,
179                 },
180         } {
181                 c.Logf("trial %+v", trial)
182                 u := mustParseURL("http://" + arvadostest.FooBarDirCollection + ".keep-web.example" + trial.path)
183                 req := &http.Request{
184                         Method:     trial.method,
185                         Host:       u.Host,
186                         URL:        u,
187                         RequestURI: u.RequestURI(),
188                         Header: http.Header{
189                                 "Authorization":   {"Bearer " + arvadostest.ActiveTokenV2},
190                                 "X-Webdav-Prefix": {trial.prefix},
191                                 "X-Webdav-Source": {trial.source},
192                         },
193                         Body: ioutil.NopCloser(bytes.NewReader(nil)),
194                 }
195
196                 resp := httptest.NewRecorder()
197                 s.handler.ServeHTTP(resp, req)
198                 if trial.notFound {
199                         c.Check(resp.Code, check.Equals, http.StatusNotFound)
200                 } else if trial.method == "PROPFIND" {
201                         c.Check(resp.Code, check.Equals, http.StatusMultiStatus)
202                         c.Check(resp.Body.String(), check.Matches, `(?ms).*>\n?$`)
203                 } else if trial.seeOther {
204                         c.Check(resp.Code, check.Equals, http.StatusSeeOther)
205                 } else {
206                         c.Check(resp.Code, check.Equals, http.StatusOK)
207                 }
208         }
209 }
210
211 func (s *UnitSuite) TestEmptyResponse(c *check.C) {
212         for _, trial := range []struct {
213                 dataExists    bool
214                 sendIMSHeader bool
215                 expectStatus  int
216                 logRegexp     string
217         }{
218                 // If we return no content due to a Keep read error,
219                 // we should emit a log message.
220                 {false, false, http.StatusOK, `(?ms).*only wrote 0 bytes.*`},
221
222                 // If we return no content because the client sent an
223                 // If-Modified-Since header, our response should be
224                 // 304.  We still expect a "File download" log since it
225                 // counts as a file access for auditing.
226                 {true, true, http.StatusNotModified, `(?ms).*msg="File download".*`},
227         } {
228                 c.Logf("trial: %+v", trial)
229                 arvadostest.StartKeep(2, true)
230                 if trial.dataExists {
231                         arv, err := arvadosclient.MakeArvadosClient()
232                         c.Assert(err, check.IsNil)
233                         arv.ApiToken = arvadostest.ActiveToken
234                         kc, err := keepclient.MakeKeepClient(arv)
235                         c.Assert(err, check.IsNil)
236                         _, _, err = kc.PutB([]byte("foo"))
237                         c.Assert(err, check.IsNil)
238                 }
239
240                 u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
241                 req := &http.Request{
242                         Method:     "GET",
243                         Host:       u.Host,
244                         URL:        u,
245                         RequestURI: u.RequestURI(),
246                         Header: http.Header{
247                                 "Authorization": {"Bearer " + arvadostest.ActiveToken},
248                         },
249                 }
250                 if trial.sendIMSHeader {
251                         req.Header.Set("If-Modified-Since", strings.Replace(time.Now().UTC().Format(time.RFC1123), "UTC", "GMT", -1))
252                 }
253
254                 var logbuf bytes.Buffer
255                 logger := logrus.New()
256                 logger.Out = &logbuf
257                 req = req.WithContext(ctxlog.Context(context.Background(), logger))
258
259                 resp := httptest.NewRecorder()
260                 s.handler.ServeHTTP(resp, req)
261                 c.Check(resp.Code, check.Equals, trial.expectStatus)
262                 c.Check(resp.Body.String(), check.Equals, "")
263
264                 c.Log(logbuf.String())
265                 c.Check(logbuf.String(), check.Matches, trial.logRegexp)
266         }
267 }
268
269 func (s *UnitSuite) TestInvalidUUID(c *check.C) {
270         bogusID := strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + "-"
271         token := arvadostest.ActiveToken
272         for _, trial := range []string{
273                 "http://keep-web/c=" + bogusID + "/foo",
274                 "http://keep-web/c=" + bogusID + "/t=" + token + "/foo",
275                 "http://keep-web/collections/download/" + bogusID + "/" + token + "/foo",
276                 "http://keep-web/collections/" + bogusID + "/foo",
277                 "http://" + bogusID + ".keep-web/" + bogusID + "/foo",
278                 "http://" + bogusID + ".keep-web/t=" + token + "/" + bogusID + "/foo",
279         } {
280                 c.Log(trial)
281                 u := mustParseURL(trial)
282                 req := &http.Request{
283                         Method:     "GET",
284                         Host:       u.Host,
285                         URL:        u,
286                         RequestURI: u.RequestURI(),
287                 }
288                 resp := httptest.NewRecorder()
289                 s.cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
290                 s.handler.ServeHTTP(resp, req)
291                 c.Check(resp.Code, check.Equals, http.StatusNotFound)
292         }
293 }
294
295 func mustParseURL(s string) *url.URL {
296         r, err := url.Parse(s)
297         if err != nil {
298                 panic("parse URL: " + s)
299         }
300         return r
301 }
302
303 func (s *IntegrationSuite) TestVhost404(c *check.C) {
304         for _, testURL := range []string{
305                 arvadostest.NonexistentCollection + ".example.com/theperthcountyconspiracy",
306                 arvadostest.NonexistentCollection + ".example.com/t=" + arvadostest.ActiveToken + "/theperthcountyconspiracy",
307         } {
308                 resp := httptest.NewRecorder()
309                 u := mustParseURL(testURL)
310                 req := &http.Request{
311                         Method:     "GET",
312                         URL:        u,
313                         RequestURI: u.RequestURI(),
314                 }
315                 s.handler.ServeHTTP(resp, req)
316                 c.Check(resp.Code, check.Equals, http.StatusNotFound)
317                 c.Check(resp.Body.String(), check.Equals, notFoundMessage+"\n")
318         }
319 }
320
321 // An authorizer modifies an HTTP request to make use of the given
322 // token -- by adding it to a header, cookie, query param, or whatever
323 // -- and returns the HTTP status code we should expect from keep-web if
324 // the token is invalid.
325 type authorizer func(*http.Request, string) int
326
327 func (s *IntegrationSuite) TestVhostViaAuthzHeaderOAuth2(c *check.C) {
328         s.doVhostRequests(c, authzViaAuthzHeaderOAuth2)
329 }
330 func authzViaAuthzHeaderOAuth2(r *http.Request, tok string) int {
331         r.Header.Add("Authorization", "OAuth2 "+tok)
332         return http.StatusUnauthorized
333 }
334
335 func (s *IntegrationSuite) TestVhostViaAuthzHeaderBearer(c *check.C) {
336         s.doVhostRequests(c, authzViaAuthzHeaderBearer)
337 }
338 func authzViaAuthzHeaderBearer(r *http.Request, tok string) int {
339         r.Header.Add("Authorization", "Bearer "+tok)
340         return http.StatusUnauthorized
341 }
342
343 func (s *IntegrationSuite) TestVhostViaCookieValue(c *check.C) {
344         s.doVhostRequests(c, authzViaCookieValue)
345 }
346 func authzViaCookieValue(r *http.Request, tok string) int {
347         r.AddCookie(&http.Cookie{
348                 Name:  "arvados_api_token",
349                 Value: auth.EncodeTokenCookie([]byte(tok)),
350         })
351         return http.StatusUnauthorized
352 }
353
354 func (s *IntegrationSuite) TestVhostViaHTTPBasicAuth(c *check.C) {
355         s.doVhostRequests(c, authzViaHTTPBasicAuth)
356 }
357 func authzViaHTTPBasicAuth(r *http.Request, tok string) int {
358         r.AddCookie(&http.Cookie{
359                 Name:  "arvados_api_token",
360                 Value: auth.EncodeTokenCookie([]byte(tok)),
361         })
362         return http.StatusUnauthorized
363 }
364
365 func (s *IntegrationSuite) TestVhostViaHTTPBasicAuthWithExtraSpaceChars(c *check.C) {
366         s.doVhostRequests(c, func(r *http.Request, tok string) int {
367                 r.AddCookie(&http.Cookie{
368                         Name:  "arvados_api_token",
369                         Value: auth.EncodeTokenCookie([]byte(" " + tok + "\n")),
370                 })
371                 return http.StatusUnauthorized
372         })
373 }
374
375 func (s *IntegrationSuite) TestVhostViaPath(c *check.C) {
376         s.doVhostRequests(c, authzViaPath)
377 }
378 func authzViaPath(r *http.Request, tok string) int {
379         r.URL.Path = "/t=" + tok + r.URL.Path
380         return http.StatusNotFound
381 }
382
383 func (s *IntegrationSuite) TestVhostViaQueryString(c *check.C) {
384         s.doVhostRequests(c, authzViaQueryString)
385 }
386 func authzViaQueryString(r *http.Request, tok string) int {
387         r.URL.RawQuery = "api_token=" + tok
388         return http.StatusUnauthorized
389 }
390
391 func (s *IntegrationSuite) TestVhostViaPOST(c *check.C) {
392         s.doVhostRequests(c, authzViaPOST)
393 }
394 func authzViaPOST(r *http.Request, tok string) int {
395         r.Method = "POST"
396         r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
397         r.Body = ioutil.NopCloser(strings.NewReader(
398                 url.Values{"api_token": {tok}}.Encode()))
399         return http.StatusUnauthorized
400 }
401
402 func (s *IntegrationSuite) TestVhostViaXHRPOST(c *check.C) {
403         s.doVhostRequests(c, authzViaPOST)
404 }
405 func authzViaXHRPOST(r *http.Request, tok string) int {
406         r.Method = "POST"
407         r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
408         r.Header.Add("Origin", "https://origin.example")
409         r.Body = ioutil.NopCloser(strings.NewReader(
410                 url.Values{
411                         "api_token":   {tok},
412                         "disposition": {"attachment"},
413                 }.Encode()))
414         return http.StatusUnauthorized
415 }
416
417 // Try some combinations of {url, token} using the given authorization
418 // mechanism, and verify the result is correct.
419 func (s *IntegrationSuite) doVhostRequests(c *check.C, authz authorizer) {
420         for _, hostPath := range []string{
421                 arvadostest.FooCollection + ".example.com/foo",
422                 arvadostest.FooCollection + "--collections.example.com/foo",
423                 arvadostest.FooCollection + "--collections.example.com/_/foo",
424                 arvadostest.FooCollectionPDH + ".example.com/foo",
425                 strings.Replace(arvadostest.FooCollectionPDH, "+", "-", -1) + "--collections.example.com/foo",
426                 arvadostest.FooBarDirCollection + ".example.com/dir1/foo",
427         } {
428                 c.Log("doRequests: ", hostPath)
429                 s.doVhostRequestsWithHostPath(c, authz, hostPath)
430         }
431 }
432
433 func (s *IntegrationSuite) doVhostRequestsWithHostPath(c *check.C, authz authorizer, hostPath string) {
434         for _, tok := range []string{
435                 arvadostest.ActiveToken,
436                 arvadostest.ActiveToken[:15],
437                 arvadostest.SpectatorToken,
438                 "bogus",
439                 "",
440         } {
441                 u := mustParseURL("http://" + hostPath)
442                 req := &http.Request{
443                         Method:     "GET",
444                         Host:       u.Host,
445                         URL:        u,
446                         RequestURI: u.RequestURI(),
447                         Header:     http.Header{},
448                 }
449                 failCode := authz(req, tok)
450                 req, resp := s.doReq(req)
451                 code, body := resp.Code, resp.Body.String()
452
453                 // If the initial request had a (non-empty) token
454                 // showing in the query string, we should have been
455                 // redirected in order to hide it in a cookie.
456                 c.Check(req.URL.String(), check.Not(check.Matches), `.*api_token=.+`)
457
458                 if tok == arvadostest.ActiveToken {
459                         c.Check(code, check.Equals, http.StatusOK)
460                         c.Check(body, check.Equals, "foo")
461                 } else {
462                         c.Check(code >= 400, check.Equals, true)
463                         c.Check(code < 500, check.Equals, true)
464                         if tok == arvadostest.SpectatorToken {
465                                 // Valid token never offers to retry
466                                 // with different credentials.
467                                 c.Check(code, check.Equals, http.StatusNotFound)
468                         } else {
469                                 // Invalid token can ask to retry
470                                 // depending on the authz method.
471                                 c.Check(code, check.Equals, failCode)
472                         }
473                         if code == 404 {
474                                 c.Check(body, check.Equals, notFoundMessage+"\n")
475                         } else {
476                                 c.Check(body, check.Equals, unauthorizedMessage+"\n")
477                         }
478                 }
479         }
480 }
481
482 func (s *IntegrationSuite) TestVhostPortMatch(c *check.C) {
483         for _, host := range []string{"download.example.com", "DOWNLOAD.EXAMPLE.COM"} {
484                 for _, port := range []string{"80", "443", "8000"} {
485                         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = fmt.Sprintf("download.example.com:%v", port)
486                         u := mustParseURL(fmt.Sprintf("http://%v/by_id/%v/foo", host, arvadostest.FooCollection))
487                         req := &http.Request{
488                                 Method:     "GET",
489                                 Host:       u.Host,
490                                 URL:        u,
491                                 RequestURI: u.RequestURI(),
492                                 Header:     http.Header{"Authorization": []string{"Bearer " + arvadostest.ActiveToken}},
493                         }
494                         req, resp := s.doReq(req)
495                         code, _ := resp.Code, resp.Body.String()
496
497                         if port == "8000" {
498                                 c.Check(code, check.Equals, 401)
499                         } else {
500                                 c.Check(code, check.Equals, 200)
501                         }
502                 }
503         }
504 }
505
506 func (s *IntegrationSuite) do(method string, urlstring string, token string, hdr http.Header) (*http.Request, *httptest.ResponseRecorder) {
507         u := mustParseURL(urlstring)
508         if hdr == nil && token != "" {
509                 hdr = http.Header{"Authorization": {"Bearer " + token}}
510         } else if hdr == nil {
511                 hdr = http.Header{}
512         } else if token != "" {
513                 panic("must not pass both token and hdr")
514         }
515         return s.doReq(&http.Request{
516                 Method:     method,
517                 Host:       u.Host,
518                 URL:        u,
519                 RequestURI: u.RequestURI(),
520                 Header:     hdr,
521         })
522 }
523
524 func (s *IntegrationSuite) doReq(req *http.Request) (*http.Request, *httptest.ResponseRecorder) {
525         resp := httptest.NewRecorder()
526         s.handler.ServeHTTP(resp, req)
527         if resp.Code != http.StatusSeeOther {
528                 return req, resp
529         }
530         cookies := (&http.Response{Header: resp.Header()}).Cookies()
531         u, _ := req.URL.Parse(resp.Header().Get("Location"))
532         req = &http.Request{
533                 Method:     "GET",
534                 Host:       u.Host,
535                 URL:        u,
536                 RequestURI: u.RequestURI(),
537                 Header:     http.Header{},
538         }
539         for _, c := range cookies {
540                 req.AddCookie(c)
541         }
542         return s.doReq(req)
543 }
544
545 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToCookie(c *check.C) {
546         s.testVhostRedirectTokenToCookie(c, "GET",
547                 arvadostest.FooCollection+".example.com/foo",
548                 "?api_token="+arvadostest.ActiveToken,
549                 nil,
550                 "",
551                 http.StatusOK,
552                 "foo",
553         )
554 }
555
556 func (s *IntegrationSuite) TestSingleOriginSecretLink(c *check.C) {
557         s.testVhostRedirectTokenToCookie(c, "GET",
558                 "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.ActiveToken+"/foo",
559                 "",
560                 nil,
561                 "",
562                 http.StatusOK,
563                 "foo",
564         )
565 }
566
567 func (s *IntegrationSuite) TestCollectionSharingToken(c *check.C) {
568         s.testVhostRedirectTokenToCookie(c, "GET",
569                 "example.com/c="+arvadostest.FooFileCollectionUUID+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
570                 "",
571                 nil,
572                 "",
573                 http.StatusOK,
574                 "foo",
575         )
576         // Same valid sharing token, but requesting a different collection
577         s.testVhostRedirectTokenToCookie(c, "GET",
578                 "example.com/c="+arvadostest.FooCollection+"/t="+arvadostest.FooFileCollectionSharingToken+"/foo",
579                 "",
580                 nil,
581                 "",
582                 http.StatusNotFound,
583                 regexp.QuoteMeta(notFoundMessage+"\n"),
584         )
585 }
586
587 // Bad token in URL is 404 Not Found because it doesn't make sense to
588 // retry the same URL with different authorization.
589 func (s *IntegrationSuite) TestSingleOriginSecretLinkBadToken(c *check.C) {
590         s.testVhostRedirectTokenToCookie(c, "GET",
591                 "example.com/c="+arvadostest.FooCollection+"/t=bogus/foo",
592                 "",
593                 nil,
594                 "",
595                 http.StatusNotFound,
596                 regexp.QuoteMeta(notFoundMessage+"\n"),
597         )
598 }
599
600 // Bad token in a cookie (even if it got there via our own
601 // query-string-to-cookie redirect) is, in principle, retryable via
602 // wb2-login-and-redirect flow.
603 func (s *IntegrationSuite) TestVhostRedirectQueryTokenToBogusCookie(c *check.C) {
604         // Inline
605         resp := s.testVhostRedirectTokenToCookie(c, "GET",
606                 arvadostest.FooCollection+".example.com/foo",
607                 "?api_token=thisisabogustoken",
608                 http.Header{"Sec-Fetch-Mode": {"navigate"}},
609                 "",
610                 http.StatusSeeOther,
611                 "",
612         )
613         u, err := url.Parse(resp.Header().Get("Location"))
614         c.Assert(err, check.IsNil)
615         c.Logf("redirected to %s", u)
616         c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
617         c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
618         c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
619
620         // Download/attachment indicated by ?disposition=attachment
621         resp = s.testVhostRedirectTokenToCookie(c, "GET",
622                 arvadostest.FooCollection+".example.com/foo",
623                 "?api_token=thisisabogustoken&disposition=attachment",
624                 http.Header{"Sec-Fetch-Mode": {"navigate"}},
625                 "",
626                 http.StatusSeeOther,
627                 "",
628         )
629         u, err = url.Parse(resp.Header().Get("Location"))
630         c.Assert(err, check.IsNil)
631         c.Logf("redirected to %s", u)
632         c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
633         c.Check(u.Query().Get("redirectToPreview"), check.Equals, "")
634         c.Check(u.Query().Get("redirectToDownload"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
635
636         // Download/attachment indicated by vhost
637         resp = s.testVhostRedirectTokenToCookie(c, "GET",
638                 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
639                 "?api_token=thisisabogustoken",
640                 http.Header{"Sec-Fetch-Mode": {"navigate"}},
641                 "",
642                 http.StatusSeeOther,
643                 "",
644         )
645         u, err = url.Parse(resp.Header().Get("Location"))
646         c.Assert(err, check.IsNil)
647         c.Logf("redirected to %s", u)
648         c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
649         c.Check(u.Query().Get("redirectToPreview"), check.Equals, "")
650         c.Check(u.Query().Get("redirectToDownload"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
651
652         // Without "Sec-Fetch-Mode: navigate" header, just 401.
653         s.testVhostRedirectTokenToCookie(c, "GET",
654                 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
655                 "?api_token=thisisabogustoken",
656                 http.Header{"Sec-Fetch-Mode": {"cors"}},
657                 "",
658                 http.StatusUnauthorized,
659                 regexp.QuoteMeta(unauthorizedMessage+"\n"),
660         )
661         s.testVhostRedirectTokenToCookie(c, "GET",
662                 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host+"/c="+arvadostest.FooCollection+"/foo",
663                 "?api_token=thisisabogustoken",
664                 nil,
665                 "",
666                 http.StatusUnauthorized,
667                 regexp.QuoteMeta(unauthorizedMessage+"\n"),
668         )
669 }
670
671 func (s *IntegrationSuite) TestVhostRedirectWithNoCache(c *check.C) {
672         resp := s.testVhostRedirectTokenToCookie(c, "GET",
673                 arvadostest.FooCollection+".example.com/foo",
674                 "?api_token=thisisabogustoken",
675                 http.Header{
676                         "Sec-Fetch-Mode": {"navigate"},
677                         "Cache-Control":  {"no-cache"},
678                 },
679                 "",
680                 http.StatusSeeOther,
681                 "",
682         )
683         u, err := url.Parse(resp.Header().Get("Location"))
684         c.Assert(err, check.IsNil)
685         c.Logf("redirected to %s", u)
686         c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
687         c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
688         c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
689 }
690
691 func (s *IntegrationSuite) TestNoTokenWorkbench2LoginFlow(c *check.C) {
692         for _, trial := range []struct {
693                 anonToken    bool
694                 cacheControl string
695         }{
696                 {},
697                 {cacheControl: "no-cache"},
698                 {anonToken: true},
699                 {anonToken: true, cacheControl: "no-cache"},
700         } {
701                 c.Logf("trial: %+v", trial)
702
703                 if trial.anonToken {
704                         s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
705                 } else {
706                         s.handler.Cluster.Users.AnonymousUserToken = ""
707                 }
708                 req, err := http.NewRequest("GET", "http://"+arvadostest.FooCollection+".example.com/foo", nil)
709                 c.Assert(err, check.IsNil)
710                 req.Header.Set("Sec-Fetch-Mode", "navigate")
711                 if trial.cacheControl != "" {
712                         req.Header.Set("Cache-Control", trial.cacheControl)
713                 }
714                 resp := httptest.NewRecorder()
715                 s.handler.ServeHTTP(resp, req)
716                 c.Check(resp.Code, check.Equals, http.StatusSeeOther)
717                 u, err := url.Parse(resp.Header().Get("Location"))
718                 c.Assert(err, check.IsNil)
719                 c.Logf("redirected to %q", u)
720                 c.Check(u.Host, check.Equals, s.handler.Cluster.Services.Workbench2.ExternalURL.Host)
721                 c.Check(u.Query().Get("redirectToPreview"), check.Equals, "/c="+arvadostest.FooCollection+"/foo")
722                 c.Check(u.Query().Get("redirectToDownload"), check.Equals, "")
723         }
724 }
725
726 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSingleOriginError(c *check.C) {
727         s.testVhostRedirectTokenToCookie(c, "GET",
728                 "example.com/c="+arvadostest.FooCollection+"/foo",
729                 "?api_token="+arvadostest.ActiveToken,
730                 nil,
731                 "",
732                 http.StatusBadRequest,
733                 regexp.QuoteMeta("cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n"),
734         )
735 }
736
737 // If client requests an attachment by putting ?disposition=attachment
738 // in the query string, and gets redirected, the redirect target
739 // should respond with an attachment.
740 func (s *IntegrationSuite) TestVhostRedirectQueryTokenRequestAttachment(c *check.C) {
741         resp := s.testVhostRedirectTokenToCookie(c, "GET",
742                 arvadostest.FooCollection+".example.com/foo",
743                 "?disposition=attachment&api_token="+arvadostest.ActiveToken,
744                 nil,
745                 "",
746                 http.StatusOK,
747                 "foo",
748         )
749         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
750 }
751
752 func (s *IntegrationSuite) TestVhostRedirectQueryTokenSiteFS(c *check.C) {
753         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
754         resp := s.testVhostRedirectTokenToCookie(c, "GET",
755                 "download.example.com/by_id/"+arvadostest.FooCollection+"/foo",
756                 "?api_token="+arvadostest.ActiveToken,
757                 nil,
758                 "",
759                 http.StatusOK,
760                 "foo",
761         )
762         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
763 }
764
765 func (s *IntegrationSuite) TestPastCollectionVersionFileAccess(c *check.C) {
766         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
767         resp := s.testVhostRedirectTokenToCookie(c, "GET",
768                 "download.example.com/c="+arvadostest.WazVersion1Collection+"/waz",
769                 "?api_token="+arvadostest.ActiveToken,
770                 nil,
771                 "",
772                 http.StatusOK,
773                 "waz",
774         )
775         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
776         resp = s.testVhostRedirectTokenToCookie(c, "GET",
777                 "download.example.com/by_id/"+arvadostest.WazVersion1Collection+"/waz",
778                 "?api_token="+arvadostest.ActiveToken,
779                 nil,
780                 "",
781                 http.StatusOK,
782                 "waz",
783         )
784         c.Check(resp.Header().Get("Content-Disposition"), check.Matches, "attachment(;.*)?")
785 }
786
787 func (s *IntegrationSuite) TestVhostRedirectQueryTokenTrustAllContent(c *check.C) {
788         s.handler.Cluster.Collections.TrustAllContent = true
789         s.testVhostRedirectTokenToCookie(c, "GET",
790                 "example.com/c="+arvadostest.FooCollection+"/foo",
791                 "?api_token="+arvadostest.ActiveToken,
792                 nil,
793                 "",
794                 http.StatusOK,
795                 "foo",
796         )
797 }
798
799 func (s *IntegrationSuite) TestVhostRedirectQueryTokenAttachmentOnlyHost(c *check.C) {
800         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com:1234"
801
802         s.testVhostRedirectTokenToCookie(c, "GET",
803                 "example.com/c="+arvadostest.FooCollection+"/foo",
804                 "?api_token="+arvadostest.ActiveToken,
805                 nil,
806                 "",
807                 http.StatusBadRequest,
808                 regexp.QuoteMeta("cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)\n"),
809         )
810
811         resp := s.testVhostRedirectTokenToCookie(c, "GET",
812                 "example.com:1234/c="+arvadostest.FooCollection+"/foo",
813                 "?api_token="+arvadostest.ActiveToken,
814                 nil,
815                 "",
816                 http.StatusOK,
817                 "foo",
818         )
819         c.Check(resp.Header().Get("Content-Disposition"), check.Equals, "attachment")
820 }
821
822 func (s *IntegrationSuite) TestVhostRedirectMultipleTokens(c *check.C) {
823         baseUrl := arvadostest.FooCollection + ".example.com/foo"
824         query := url.Values{}
825
826         // The intent of these tests is to check that requests are redirected
827         // correctly in the presence of multiple API tokens. The exact response
828         // codes and content are not closely considered: they're just how
829         // keep-web responded when we made the smallest possible fix. Changing
830         // those responses may be okay, but you should still test all these
831         // different cases and the associated redirect logic.
832         query["api_token"] = []string{arvadostest.ActiveToken, arvadostest.AnonymousToken}
833         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
834         query["api_token"] = []string{arvadostest.ActiveToken, arvadostest.AnonymousToken, ""}
835         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
836         query["api_token"] = []string{arvadostest.ActiveToken, "", arvadostest.AnonymousToken}
837         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
838         query["api_token"] = []string{"", arvadostest.ActiveToken}
839         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusOK, "foo")
840
841         expectContent := regexp.QuoteMeta(unauthorizedMessage + "\n")
842         query["api_token"] = []string{arvadostest.AnonymousToken, "invalidtoo"}
843         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
844         query["api_token"] = []string{arvadostest.AnonymousToken, ""}
845         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
846         query["api_token"] = []string{"", arvadostest.AnonymousToken}
847         s.testVhostRedirectTokenToCookie(c, "GET", baseUrl, "?"+query.Encode(), nil, "", http.StatusUnauthorized, expectContent)
848 }
849
850 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie(c *check.C) {
851         s.testVhostRedirectTokenToCookie(c, "POST",
852                 arvadostest.FooCollection+".example.com/foo",
853                 "",
854                 http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
855                 url.Values{"api_token": {arvadostest.ActiveToken}}.Encode(),
856                 http.StatusOK,
857                 "foo",
858         )
859 }
860
861 func (s *IntegrationSuite) TestVhostRedirectPOSTFormTokenToCookie404(c *check.C) {
862         s.testVhostRedirectTokenToCookie(c, "POST",
863                 arvadostest.FooCollection+".example.com/foo",
864                 "",
865                 http.Header{"Content-Type": {"application/x-www-form-urlencoded"}},
866                 url.Values{"api_token": {arvadostest.SpectatorToken}}.Encode(),
867                 http.StatusNotFound,
868                 regexp.QuoteMeta(notFoundMessage+"\n"),
869         )
870 }
871
872 func (s *IntegrationSuite) TestAnonymousTokenOK(c *check.C) {
873         s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
874         s.testVhostRedirectTokenToCookie(c, "GET",
875                 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
876                 "",
877                 nil,
878                 "",
879                 http.StatusOK,
880                 "Hello world\n",
881         )
882 }
883
884 func (s *IntegrationSuite) TestAnonymousTokenError(c *check.C) {
885         s.handler.Cluster.Users.AnonymousUserToken = "anonymousTokenConfiguredButInvalid"
886         s.testVhostRedirectTokenToCookie(c, "GET",
887                 "example.com/c="+arvadostest.HelloWorldCollection+"/Hello%20world.txt",
888                 "",
889                 nil,
890                 "",
891                 http.StatusUnauthorized,
892                 "Authorization tokens are not accepted here: .*\n",
893         )
894 }
895
896 func (s *IntegrationSuite) TestSpecialCharsInPath(c *check.C) {
897         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
898
899         client := arvados.NewClientFromEnv()
900         client.AuthToken = arvadostest.ActiveToken
901         fs, err := (&arvados.Collection{}).FileSystem(client, nil)
902         c.Assert(err, check.IsNil)
903         f, err := fs.OpenFile("https:\\\"odd' path chars", os.O_CREATE, 0777)
904         c.Assert(err, check.IsNil)
905         f.Close()
906         mtxt, err := fs.MarshalManifest(".")
907         c.Assert(err, check.IsNil)
908         var coll arvados.Collection
909         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
910                 "collection": map[string]string{
911                         "manifest_text": mtxt,
912                 },
913         })
914         c.Assert(err, check.IsNil)
915
916         u, _ := url.Parse("http://download.example.com/c=" + coll.UUID + "/")
917         req := &http.Request{
918                 Method:     "GET",
919                 Host:       u.Host,
920                 URL:        u,
921                 RequestURI: u.RequestURI(),
922                 Header: http.Header{
923                         "Authorization": {"Bearer " + client.AuthToken},
924                 },
925         }
926         resp := httptest.NewRecorder()
927         s.handler.ServeHTTP(resp, req)
928         c.Check(resp.Code, check.Equals, http.StatusOK)
929         c.Check(resp.Body.String(), check.Matches, `(?ms).*href="./https:%5c%22odd%27%20path%20chars"\S+https:\\&#34;odd&#39; path chars.*`)
930 }
931
932 func (s *IntegrationSuite) TestForwardSlashSubstitution(c *check.C) {
933         arv := arvados.NewClientFromEnv()
934         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
935         s.handler.Cluster.Collections.ForwardSlashNameSubstitution = "{SOLIDUS}"
936         name := "foo/bar/baz"
937         nameShown := strings.Replace(name, "/", "{SOLIDUS}", -1)
938         nameShownEscaped := strings.Replace(name, "/", "%7bSOLIDUS%7d", -1)
939
940         client := arvados.NewClientFromEnv()
941         client.AuthToken = arvadostest.ActiveToken
942         fs, err := (&arvados.Collection{}).FileSystem(client, nil)
943         c.Assert(err, check.IsNil)
944         f, err := fs.OpenFile("filename", os.O_CREATE, 0777)
945         c.Assert(err, check.IsNil)
946         f.Close()
947         mtxt, err := fs.MarshalManifest(".")
948         c.Assert(err, check.IsNil)
949         var coll arvados.Collection
950         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
951                 "collection": map[string]string{
952                         "manifest_text": mtxt,
953                         "name":          name,
954                         "owner_uuid":    arvadostest.AProjectUUID,
955                 },
956         })
957         c.Assert(err, check.IsNil)
958         defer arv.RequestAndDecode(&coll, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
959
960         base := "http://download.example.com/by_id/" + coll.OwnerUUID + "/"
961         for tryURL, expectRegexp := range map[string]string{
962                 base:                          `(?ms).*href="./` + nameShownEscaped + `/"\S+` + nameShown + `.*`,
963                 base + nameShownEscaped + "/": `(?ms).*href="./filename"\S+filename.*`,
964         } {
965                 u, _ := url.Parse(tryURL)
966                 req := &http.Request{
967                         Method:     "GET",
968                         Host:       u.Host,
969                         URL:        u,
970                         RequestURI: u.RequestURI(),
971                         Header: http.Header{
972                                 "Authorization": {"Bearer " + client.AuthToken},
973                         },
974                 }
975                 resp := httptest.NewRecorder()
976                 s.handler.ServeHTTP(resp, req)
977                 c.Check(resp.Code, check.Equals, http.StatusOK)
978                 c.Check(resp.Body.String(), check.Matches, expectRegexp)
979         }
980 }
981
982 // XHRs can't follow redirect-with-cookie so they rely on method=POST
983 // and disposition=attachment (telling us it's acceptable to respond
984 // with content instead of a redirect) and an Origin header that gets
985 // added automatically by the browser (telling us it's desirable to do
986 // so).
987 func (s *IntegrationSuite) TestXHRNoRedirect(c *check.C) {
988         u, _ := url.Parse("http://example.com/c=" + arvadostest.FooCollection + "/foo")
989         req := &http.Request{
990                 Method:     "POST",
991                 Host:       u.Host,
992                 URL:        u,
993                 RequestURI: u.RequestURI(),
994                 Header: http.Header{
995                         "Origin":       {"https://origin.example"},
996                         "Content-Type": {"application/x-www-form-urlencoded"},
997                 },
998                 Body: ioutil.NopCloser(strings.NewReader(url.Values{
999                         "api_token":   {arvadostest.ActiveToken},
1000                         "disposition": {"attachment"},
1001                 }.Encode())),
1002         }
1003         resp := httptest.NewRecorder()
1004         s.handler.ServeHTTP(resp, req)
1005         c.Check(resp.Code, check.Equals, http.StatusOK)
1006         c.Check(resp.Body.String(), check.Equals, "foo")
1007         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
1008
1009         // GET + Origin header is representative of both AJAX GET
1010         // requests and inline images via <IMG crossorigin="anonymous"
1011         // src="...">.
1012         u.RawQuery = "api_token=" + url.QueryEscape(arvadostest.ActiveTokenV2)
1013         req = &http.Request{
1014                 Method:     "GET",
1015                 Host:       u.Host,
1016                 URL:        u,
1017                 RequestURI: u.RequestURI(),
1018                 Header: http.Header{
1019                         "Origin": {"https://origin.example"},
1020                 },
1021         }
1022         resp = httptest.NewRecorder()
1023         s.handler.ServeHTTP(resp, req)
1024         c.Check(resp.Code, check.Equals, http.StatusOK)
1025         c.Check(resp.Body.String(), check.Equals, "foo")
1026         c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, "*")
1027 }
1028
1029 func (s *IntegrationSuite) testVhostRedirectTokenToCookie(c *check.C, method, hostPath, queryString string, reqHeader http.Header, reqBody string, expectStatus int, matchRespBody string) *httptest.ResponseRecorder {
1030         if reqHeader == nil {
1031                 reqHeader = http.Header{}
1032         }
1033         u, _ := url.Parse(`http://` + hostPath + queryString)
1034         c.Logf("requesting %s", u)
1035         req := &http.Request{
1036                 Method:     method,
1037                 Host:       u.Host,
1038                 URL:        u,
1039                 RequestURI: u.RequestURI(),
1040                 Header:     reqHeader,
1041                 Body:       ioutil.NopCloser(strings.NewReader(reqBody)),
1042         }
1043
1044         resp := httptest.NewRecorder()
1045         defer func() {
1046                 c.Check(resp.Code, check.Equals, expectStatus)
1047                 c.Check(resp.Body.String(), check.Matches, matchRespBody)
1048         }()
1049
1050         s.handler.ServeHTTP(resp, req)
1051         if resp.Code != http.StatusSeeOther {
1052                 attachment, _ := regexp.MatchString(`^attachment(;|$)`, resp.Header().Get("Content-Disposition"))
1053                 // Since we're not redirecting, check that any api_token in the URL is
1054                 // handled safely.
1055                 // If there is no token in the URL, then we're good.
1056                 // Otherwise, if the response code is an error, the body is expected to
1057                 // be static content, and nothing that might maliciously introspect the
1058                 // URL. It's considered safe and allowed.
1059                 // Otherwise, if the response content has attachment disposition,
1060                 // that's considered safe for all the reasons explained in the
1061                 // safeAttachment comment in handler.go.
1062                 c.Check(!u.Query().Has("api_token") || resp.Code >= 400 || attachment, check.Equals, true)
1063                 return resp
1064         }
1065
1066         loc, err := url.Parse(resp.Header().Get("Location"))
1067         c.Assert(err, check.IsNil)
1068         c.Check(loc.Scheme, check.Equals, u.Scheme)
1069         c.Check(loc.Host, check.Equals, u.Host)
1070         c.Check(loc.RawPath, check.Equals, u.RawPath)
1071         // If the response was a redirect, it should never include an API token.
1072         c.Check(loc.Query().Has("api_token"), check.Equals, false)
1073         c.Check(resp.Body.String(), check.Matches, `.*href="http://`+regexp.QuoteMeta(html.EscapeString(hostPath))+`(\?[^"]*)?".*`)
1074         cookies := (&http.Response{Header: resp.Header()}).Cookies()
1075
1076         c.Logf("following redirect to %s", u)
1077         req = &http.Request{
1078                 Method:     "GET",
1079                 Host:       loc.Host,
1080                 URL:        loc,
1081                 RequestURI: loc.RequestURI(),
1082                 Header:     reqHeader,
1083         }
1084         for _, c := range cookies {
1085                 req.AddCookie(c)
1086         }
1087
1088         resp = httptest.NewRecorder()
1089         s.handler.ServeHTTP(resp, req)
1090
1091         if resp.Code != http.StatusSeeOther {
1092                 c.Check(resp.Header().Get("Location"), check.Equals, "")
1093         }
1094         return resp
1095 }
1096
1097 func (s *IntegrationSuite) TestDirectoryListingWithAnonymousToken(c *check.C) {
1098         s.handler.Cluster.Users.AnonymousUserToken = arvadostest.AnonymousToken
1099         s.testDirectoryListing(c)
1100 }
1101
1102 func (s *IntegrationSuite) TestDirectoryListingWithNoAnonymousToken(c *check.C) {
1103         s.handler.Cluster.Users.AnonymousUserToken = ""
1104         s.testDirectoryListing(c)
1105 }
1106
1107 func (s *IntegrationSuite) testDirectoryListing(c *check.C) {
1108         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1109         authHeader := http.Header{
1110                 "Authorization": {"OAuth2 " + arvadostest.ActiveToken},
1111         }
1112         for _, trial := range []struct {
1113                 uri      string
1114                 header   http.Header
1115                 expect   []string
1116                 redirect string
1117                 cutDirs  int
1118         }{
1119                 {
1120                         uri:     strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/",
1121                         header:  authHeader,
1122                         expect:  []string{"dir1/foo", "dir1/bar"},
1123                         cutDirs: 0,
1124                 },
1125                 {
1126                         uri:     strings.Replace(arvadostest.FooAndBarFilesInDirPDH, "+", "-", -1) + ".example.com/dir1/",
1127                         header:  authHeader,
1128                         expect:  []string{"foo", "bar"},
1129                         cutDirs: 1,
1130                 },
1131                 {
1132                         // URLs of this form ignore authHeader, and
1133                         // FooAndBarFilesInDirUUID isn't public, so
1134                         // this returns 401.
1135                         uri:    "download.example.com/collections/" + arvadostest.FooAndBarFilesInDirUUID + "/",
1136                         header: authHeader,
1137                         expect: nil,
1138                 },
1139                 {
1140                         uri:     "download.example.com/users/active/foo_file_in_dir/",
1141                         header:  authHeader,
1142                         expect:  []string{"dir1/"},
1143                         cutDirs: 3,
1144                 },
1145                 {
1146                         uri:     "download.example.com/users/active/foo_file_in_dir/dir1/",
1147                         header:  authHeader,
1148                         expect:  []string{"bar"},
1149                         cutDirs: 4,
1150                 },
1151                 {
1152                         uri:     "download.example.com/",
1153                         header:  authHeader,
1154                         expect:  []string{"users/"},
1155                         cutDirs: 0,
1156                 },
1157                 {
1158                         uri:      "download.example.com/users",
1159                         header:   authHeader,
1160                         redirect: "/users/",
1161                         expect:   []string{"active/"},
1162                         cutDirs:  1,
1163                 },
1164                 {
1165                         uri:     "download.example.com/users/",
1166                         header:  authHeader,
1167                         expect:  []string{"active/"},
1168                         cutDirs: 1,
1169                 },
1170                 {
1171                         uri:      "download.example.com/users/active",
1172                         header:   authHeader,
1173                         redirect: "/users/active/",
1174                         expect:   []string{"foo_file_in_dir/"},
1175                         cutDirs:  2,
1176                 },
1177                 {
1178                         uri:     "download.example.com/users/active/",
1179                         header:  authHeader,
1180                         expect:  []string{"foo_file_in_dir/"},
1181                         cutDirs: 2,
1182                 },
1183                 {
1184                         uri:     "collections.example.com/collections/download/" + arvadostest.FooAndBarFilesInDirUUID + "/" + arvadostest.ActiveToken + "/",
1185                         header:  nil,
1186                         expect:  []string{"dir1/foo", "dir1/bar"},
1187                         cutDirs: 4,
1188                 },
1189                 {
1190                         uri:     "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken + "/",
1191                         header:  nil,
1192                         expect:  []string{"dir1/foo", "dir1/bar"},
1193                         cutDirs: 2,
1194                 },
1195                 {
1196                         uri:     "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/t=" + arvadostest.ActiveToken,
1197                         header:  nil,
1198                         expect:  []string{"dir1/foo", "dir1/bar"},
1199                         cutDirs: 2,
1200                 },
1201                 {
1202                         uri:     "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID,
1203                         header:  authHeader,
1204                         expect:  []string{"dir1/foo", "dir1/bar"},
1205                         cutDirs: 1,
1206                 },
1207                 {
1208                         uri:      "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1",
1209                         header:   authHeader,
1210                         redirect: "/c=" + arvadostest.FooAndBarFilesInDirUUID + "/dir1/",
1211                         expect:   []string{"foo", "bar"},
1212                         cutDirs:  2,
1213                 },
1214                 {
1215                         uri:     "download.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/_/dir1/",
1216                         header:  authHeader,
1217                         expect:  []string{"foo", "bar"},
1218                         cutDirs: 3,
1219                 },
1220                 {
1221                         uri:      arvadostest.FooAndBarFilesInDirUUID + ".example.com/dir1?api_token=" + arvadostest.ActiveToken,
1222                         header:   authHeader,
1223                         redirect: "/dir1/",
1224                         expect:   []string{"foo", "bar"},
1225                         cutDirs:  1,
1226                 },
1227                 {
1228                         uri:    "collections.example.com/c=" + arvadostest.FooAndBarFilesInDirUUID + "/theperthcountyconspiracydoesnotexist/",
1229                         header: authHeader,
1230                         expect: nil,
1231                 },
1232                 {
1233                         uri:     "download.example.com/c=" + arvadostest.WazVersion1Collection,
1234                         header:  authHeader,
1235                         expect:  []string{"waz"},
1236                         cutDirs: 1,
1237                 },
1238                 {
1239                         uri:     "download.example.com/by_id/" + arvadostest.WazVersion1Collection,
1240                         header:  authHeader,
1241                         expect:  []string{"waz"},
1242                         cutDirs: 2,
1243                 },
1244         } {
1245                 comment := check.Commentf("HTML: %q => %q", trial.uri, trial.expect)
1246                 resp := httptest.NewRecorder()
1247                 u := mustParseURL("//" + trial.uri)
1248                 req := &http.Request{
1249                         Method:     "GET",
1250                         Host:       u.Host,
1251                         URL:        u,
1252                         RequestURI: u.RequestURI(),
1253                         Header:     copyHeader(trial.header),
1254                 }
1255                 s.handler.ServeHTTP(resp, req)
1256                 var cookies []*http.Cookie
1257                 for resp.Code == http.StatusSeeOther {
1258                         u, _ := req.URL.Parse(resp.Header().Get("Location"))
1259                         req = &http.Request{
1260                                 Method:     "GET",
1261                                 Host:       u.Host,
1262                                 URL:        u,
1263                                 RequestURI: u.RequestURI(),
1264                                 Header:     copyHeader(trial.header),
1265                         }
1266                         cookies = append(cookies, (&http.Response{Header: resp.Header()}).Cookies()...)
1267                         for _, c := range cookies {
1268                                 req.AddCookie(c)
1269                         }
1270                         resp = httptest.NewRecorder()
1271                         s.handler.ServeHTTP(resp, req)
1272                 }
1273                 if trial.redirect != "" {
1274                         c.Check(req.URL.Path, check.Equals, trial.redirect, comment)
1275                 }
1276                 if trial.expect == nil {
1277                         c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1278                 } else {
1279                         c.Check(resp.Code, check.Equals, http.StatusOK, comment)
1280                         for _, e := range trial.expect {
1281                                 c.Check(resp.Body.String(), check.Matches, `(?ms).*href="./`+e+`".*`, comment)
1282                         }
1283                         c.Check(resp.Body.String(), check.Matches, `(?ms).*--cut-dirs=`+fmt.Sprintf("%d", trial.cutDirs)+` .*`, comment)
1284                 }
1285
1286                 comment = check.Commentf("WebDAV: %q => %q", trial.uri, trial.expect)
1287                 req = &http.Request{
1288                         Method:     "OPTIONS",
1289                         Host:       u.Host,
1290                         URL:        u,
1291                         RequestURI: u.RequestURI(),
1292                         Header:     copyHeader(trial.header),
1293                         Body:       ioutil.NopCloser(&bytes.Buffer{}),
1294                 }
1295                 resp = httptest.NewRecorder()
1296                 s.handler.ServeHTTP(resp, req)
1297                 if trial.expect == nil {
1298                         c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1299                 } else {
1300                         c.Check(resp.Code, check.Equals, http.StatusOK, comment)
1301                 }
1302
1303                 req = &http.Request{
1304                         Method:     "PROPFIND",
1305                         Host:       u.Host,
1306                         URL:        u,
1307                         RequestURI: u.RequestURI(),
1308                         Header:     copyHeader(trial.header),
1309                         Body:       ioutil.NopCloser(&bytes.Buffer{}),
1310                 }
1311                 resp = httptest.NewRecorder()
1312                 s.handler.ServeHTTP(resp, req)
1313                 if trial.expect == nil {
1314                         c.Check(resp.Code, check.Equals, http.StatusUnauthorized, comment)
1315                 } else {
1316                         c.Check(resp.Code, check.Equals, http.StatusMultiStatus, comment)
1317                         for _, e := range trial.expect {
1318                                 if strings.HasSuffix(e, "/") {
1319                                         e = filepath.Join(u.Path, e) + "/"
1320                                 } else {
1321                                         e = filepath.Join(u.Path, e)
1322                                 }
1323                                 c.Check(resp.Body.String(), check.Matches, `(?ms).*<D:href>`+e+`</D:href>.*`, comment)
1324                         }
1325                 }
1326         }
1327 }
1328
1329 func (s *IntegrationSuite) TestDeleteLastFile(c *check.C) {
1330         arv := arvados.NewClientFromEnv()
1331         var newCollection arvados.Collection
1332         err := arv.RequestAndDecode(&newCollection, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1333                 "collection": map[string]string{
1334                         "owner_uuid":    arvadostest.ActiveUserUUID,
1335                         "manifest_text": ". acbd18db4cc2f85cedef654fccc4a4d8+3 0:3:foo.txt 0:3:bar.txt\n",
1336                         "name":          "keep-web test collection",
1337                 },
1338                 "ensure_unique_name": true,
1339         })
1340         c.Assert(err, check.IsNil)
1341         defer arv.RequestAndDecode(&newCollection, "DELETE", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
1342
1343         var updated arvados.Collection
1344         for _, fnm := range []string{"foo.txt", "bar.txt"} {
1345                 s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "example.com"
1346                 u, _ := url.Parse("http://example.com/c=" + newCollection.UUID + "/" + fnm)
1347                 req := &http.Request{
1348                         Method:     "DELETE",
1349                         Host:       u.Host,
1350                         URL:        u,
1351                         RequestURI: u.RequestURI(),
1352                         Header: http.Header{
1353                                 "Authorization": {"Bearer " + arvadostest.ActiveToken},
1354                         },
1355                 }
1356                 resp := httptest.NewRecorder()
1357                 s.handler.ServeHTTP(resp, req)
1358                 c.Check(resp.Code, check.Equals, http.StatusNoContent)
1359
1360                 updated = arvados.Collection{}
1361                 err = arv.RequestAndDecode(&updated, "GET", "arvados/v1/collections/"+newCollection.UUID, nil, nil)
1362                 c.Check(err, check.IsNil)
1363                 c.Check(updated.ManifestText, check.Not(check.Matches), `(?ms).*\Q`+fnm+`\E.*`)
1364                 c.Logf("updated manifest_text %q", updated.ManifestText)
1365         }
1366         c.Check(updated.ManifestText, check.Equals, "")
1367 }
1368
1369 func (s *IntegrationSuite) TestFileContentType(c *check.C) {
1370         s.handler.Cluster.Services.WebDAVDownload.ExternalURL.Host = "download.example.com"
1371
1372         client := arvados.NewClientFromEnv()
1373         client.AuthToken = arvadostest.ActiveToken
1374         arv, err := arvadosclient.New(client)
1375         c.Assert(err, check.Equals, nil)
1376         kc, err := keepclient.MakeKeepClient(arv)
1377         c.Assert(err, check.Equals, nil)
1378
1379         fs, err := (&arvados.Collection{}).FileSystem(client, kc)
1380         c.Assert(err, check.IsNil)
1381
1382         trials := []struct {
1383                 filename    string
1384                 content     string
1385                 contentType string
1386         }{
1387                 {"picture.txt", "BMX bikes are small this year\n", "text/plain; charset=utf-8"},
1388                 {"picture.bmp", "BMX bikes are small this year\n", "image/(x-ms-)?bmp"},
1389                 {"picture.jpg", "BMX bikes are small this year\n", "image/jpeg"},
1390                 {"picture1", "BMX bikes are small this year\n", "image/bmp"},            // content sniff; "BM" is the magic signature for .bmp
1391                 {"picture2", "Cars are small this year\n", "text/plain; charset=utf-8"}, // content sniff
1392         }
1393         for _, trial := range trials {
1394                 f, err := fs.OpenFile(trial.filename, os.O_CREATE|os.O_WRONLY, 0777)
1395                 c.Assert(err, check.IsNil)
1396                 _, err = f.Write([]byte(trial.content))
1397                 c.Assert(err, check.IsNil)
1398                 c.Assert(f.Close(), check.IsNil)
1399         }
1400         mtxt, err := fs.MarshalManifest(".")
1401         c.Assert(err, check.IsNil)
1402         var coll arvados.Collection
1403         err = client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, map[string]interface{}{
1404                 "collection": map[string]string{
1405                         "manifest_text": mtxt,
1406                 },
1407         })
1408         c.Assert(err, check.IsNil)
1409
1410         for _, trial := range trials {
1411                 u, _ := url.Parse("http://download.example.com/by_id/" + coll.UUID + "/" + trial.filename)
1412                 req := &http.Request{
1413                         Method:     "GET",
1414                         Host:       u.Host,
1415                         URL:        u,
1416                         RequestURI: u.RequestURI(),
1417                         Header: http.Header{
1418                                 "Authorization": {"Bearer " + client.AuthToken},
1419                         },
1420                 }
1421                 resp := httptest.NewRecorder()
1422                 s.handler.ServeHTTP(resp, req)
1423                 c.Check(resp.Code, check.Equals, http.StatusOK)
1424                 c.Check(resp.Header().Get("Content-Type"), check.Matches, trial.contentType)
1425                 c.Check(resp.Body.String(), check.Equals, trial.content)
1426         }
1427 }
1428
1429 func (s *IntegrationSuite) TestKeepClientBlockCache(c *check.C) {
1430         s.handler.Cluster.Collections.WebDAVCache.MaxBlockEntries = 42
1431         c.Check(keepclient.DefaultBlockCache.MaxBlocks, check.Not(check.Equals), 42)
1432         u := mustParseURL("http://keep-web.example/c=" + arvadostest.FooCollection + "/t=" + arvadostest.ActiveToken + "/foo")
1433         req := &http.Request{
1434                 Method:     "GET",
1435                 Host:       u.Host,
1436                 URL:        u,
1437                 RequestURI: u.RequestURI(),
1438         }
1439         resp := httptest.NewRecorder()
1440         s.handler.ServeHTTP(resp, req)
1441         c.Check(resp.Code, check.Equals, http.StatusOK)
1442         c.Check(keepclient.DefaultBlockCache.MaxBlocks, check.Equals, 42)
1443 }
1444
1445 // Writing to a collection shouldn't affect its entry in the
1446 // PDH-to-manifest cache.
1447 func (s *IntegrationSuite) TestCacheWriteCollectionSamePDH(c *check.C) {
1448         arv, err := arvadosclient.MakeArvadosClient()
1449         c.Assert(err, check.Equals, nil)
1450         arv.ApiToken = arvadostest.ActiveToken
1451
1452         u := mustParseURL("http://x.example/testfile")
1453         req := &http.Request{
1454                 Method:     "GET",
1455                 Host:       u.Host,
1456                 URL:        u,
1457                 RequestURI: u.RequestURI(),
1458                 Header:     http.Header{"Authorization": {"Bearer " + arv.ApiToken}},
1459         }
1460
1461         checkWithID := func(id string, status int) {
1462                 req.URL.Host = strings.Replace(id, "+", "-", -1) + ".example"
1463                 req.Host = req.URL.Host
1464                 resp := httptest.NewRecorder()
1465                 s.handler.ServeHTTP(resp, req)
1466                 c.Check(resp.Code, check.Equals, status)
1467         }
1468
1469         var colls [2]arvados.Collection
1470         for i := range colls {
1471                 err := arv.Create("collections",
1472                         map[string]interface{}{
1473                                 "ensure_unique_name": true,
1474                                 "collection": map[string]interface{}{
1475                                         "name": "test collection",
1476                                 },
1477                         }, &colls[i])
1478                 c.Assert(err, check.Equals, nil)
1479         }
1480
1481         // Populate cache with empty collection
1482         checkWithID(colls[0].PortableDataHash, http.StatusNotFound)
1483
1484         // write a file to colls[0]
1485         reqPut := *req
1486         reqPut.Method = "PUT"
1487         reqPut.URL.Host = colls[0].UUID + ".example"
1488         reqPut.Host = req.URL.Host
1489         reqPut.Body = ioutil.NopCloser(bytes.NewBufferString("testdata"))
1490         resp := httptest.NewRecorder()
1491         s.handler.ServeHTTP(resp, &reqPut)
1492         c.Check(resp.Code, check.Equals, http.StatusCreated)
1493
1494         // new file should not appear in colls[1]
1495         checkWithID(colls[1].PortableDataHash, http.StatusNotFound)
1496         checkWithID(colls[1].UUID, http.StatusNotFound)
1497
1498         checkWithID(colls[0].UUID, http.StatusOK)
1499 }
1500
1501 func copyHeader(h http.Header) http.Header {
1502         hc := http.Header{}
1503         for k, v := range h {
1504                 hc[k] = append([]string(nil), v...)
1505         }
1506         return hc
1507 }
1508
1509 func (s *IntegrationSuite) checkUploadDownloadRequest(c *check.C, req *http.Request,
1510         successCode int, direction string, perm bool, userUuid, collectionUuid, collectionPDH, filepath string) {
1511
1512         client := arvados.NewClientFromEnv()
1513         client.AuthToken = arvadostest.AdminToken
1514         var logentries arvados.LogList
1515         limit1 := 1
1516         err := client.RequestAndDecode(&logentries, "GET", "arvados/v1/logs", nil,
1517                 arvados.ResourceListParams{
1518                         Limit: &limit1,
1519                         Order: "created_at desc"})
1520         c.Check(err, check.IsNil)
1521         c.Check(logentries.Items, check.HasLen, 1)
1522         lastLogId := logentries.Items[0].ID
1523         c.Logf("lastLogId: %d", lastLogId)
1524
1525         var logbuf bytes.Buffer
1526         logger := logrus.New()
1527         logger.Out = &logbuf
1528         resp := httptest.NewRecorder()
1529         req = req.WithContext(ctxlog.Context(context.Background(), logger))
1530         s.handler.ServeHTTP(resp, req)
1531
1532         if perm {
1533                 c.Check(resp.Result().StatusCode, check.Equals, successCode)
1534                 c.Check(logbuf.String(), check.Matches, `(?ms).*msg="File `+direction+`".*`)
1535                 c.Check(logbuf.String(), check.Not(check.Matches), `(?ms).*level=error.*`)
1536
1537                 deadline := time.Now().Add(time.Second)
1538                 for {
1539                         c.Assert(time.Now().After(deadline), check.Equals, false, check.Commentf("timed out waiting for log entry"))
1540                         logentries = arvados.LogList{}
1541                         err = client.RequestAndDecode(&logentries, "GET", "arvados/v1/logs", nil,
1542                                 arvados.ResourceListParams{
1543                                         Filters: []arvados.Filter{
1544                                                 {Attr: "event_type", Operator: "=", Operand: "file_" + direction},
1545                                                 {Attr: "object_uuid", Operator: "=", Operand: userUuid},
1546                                         },
1547                                         Limit: &limit1,
1548                                         Order: "created_at desc",
1549                                 })
1550                         c.Assert(err, check.IsNil)
1551                         if len(logentries.Items) > 0 &&
1552                                 logentries.Items[0].ID > lastLogId &&
1553                                 logentries.Items[0].ObjectUUID == userUuid &&
1554                                 logentries.Items[0].Properties["collection_uuid"] == collectionUuid &&
1555                                 (collectionPDH == "" || logentries.Items[0].Properties["portable_data_hash"] == collectionPDH) &&
1556                                 logentries.Items[0].Properties["collection_file_path"] == filepath {
1557                                 break
1558                         }
1559                         c.Logf("logentries.Items: %+v", logentries.Items)
1560                         time.Sleep(50 * time.Millisecond)
1561                 }
1562         } else {
1563                 c.Check(resp.Result().StatusCode, check.Equals, http.StatusForbidden)
1564                 c.Check(logbuf.String(), check.Equals, "")
1565         }
1566 }
1567
1568 func (s *IntegrationSuite) TestDownloadLoggingPermission(c *check.C) {
1569         u := mustParseURL("http://" + arvadostest.FooCollection + ".keep-web.example/foo")
1570
1571         s.handler.Cluster.Collections.TrustAllContent = true
1572
1573         for _, adminperm := range []bool{true, false} {
1574                 for _, userperm := range []bool{true, false} {
1575                         s.handler.Cluster.Collections.WebDAVPermission.Admin.Download = adminperm
1576                         s.handler.Cluster.Collections.WebDAVPermission.User.Download = userperm
1577
1578                         // Test admin permission
1579                         req := &http.Request{
1580                                 Method:     "GET",
1581                                 Host:       u.Host,
1582                                 URL:        u,
1583                                 RequestURI: u.RequestURI(),
1584                                 Header: http.Header{
1585                                         "Authorization": {"Bearer " + arvadostest.AdminToken},
1586                                 },
1587                         }
1588                         s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", adminperm,
1589                                 arvadostest.AdminUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
1590
1591                         // Test user permission
1592                         req = &http.Request{
1593                                 Method:     "GET",
1594                                 Host:       u.Host,
1595                                 URL:        u,
1596                                 RequestURI: u.RequestURI(),
1597                                 Header: http.Header{
1598                                         "Authorization": {"Bearer " + arvadostest.ActiveToken},
1599                                 },
1600                         }
1601                         s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", userperm,
1602                                 arvadostest.ActiveUserUUID, arvadostest.FooCollection, arvadostest.FooCollectionPDH, "foo")
1603                 }
1604         }
1605
1606         s.handler.Cluster.Collections.WebDAVPermission.User.Download = true
1607
1608         for _, tryurl := range []string{"http://" + arvadostest.MultilevelCollection1 + ".keep-web.example/dir1/subdir/file1",
1609                 "http://keep-web/users/active/multilevel_collection_1/dir1/subdir/file1"} {
1610
1611                 u = mustParseURL(tryurl)
1612                 req := &http.Request{
1613                         Method:     "GET",
1614                         Host:       u.Host,
1615                         URL:        u,
1616                         RequestURI: u.RequestURI(),
1617                         Header: http.Header{
1618                                 "Authorization": {"Bearer " + arvadostest.ActiveToken},
1619                         },
1620                 }
1621                 s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
1622                         arvadostest.ActiveUserUUID, arvadostest.MultilevelCollection1, arvadostest.MultilevelCollection1PDH, "dir1/subdir/file1")
1623         }
1624
1625         u = mustParseURL("http://" + strings.Replace(arvadostest.FooCollectionPDH, "+", "-", 1) + ".keep-web.example/foo")
1626         req := &http.Request{
1627                 Method:     "GET",
1628                 Host:       u.Host,
1629                 URL:        u,
1630                 RequestURI: u.RequestURI(),
1631                 Header: http.Header{
1632                         "Authorization": {"Bearer " + arvadostest.ActiveToken},
1633                 },
1634         }
1635         s.checkUploadDownloadRequest(c, req, http.StatusOK, "download", true,
1636                 arvadostest.ActiveUserUUID, "", arvadostest.FooCollectionPDH, "foo")
1637 }
1638
1639 func (s *IntegrationSuite) TestUploadLoggingPermission(c *check.C) {
1640         for _, adminperm := range []bool{true, false} {
1641                 for _, userperm := range []bool{true, false} {
1642
1643                         arv := arvados.NewClientFromEnv()
1644                         arv.AuthToken = arvadostest.ActiveToken
1645
1646                         var coll arvados.Collection
1647                         err := arv.RequestAndDecode(&coll,
1648                                 "POST",
1649                                 "/arvados/v1/collections",
1650                                 nil,
1651                                 map[string]interface{}{
1652                                         "ensure_unique_name": true,
1653                                         "collection": map[string]interface{}{
1654                                                 "name": "test collection",
1655                                         },
1656                                 })
1657                         c.Assert(err, check.Equals, nil)
1658
1659                         u := mustParseURL("http://" + coll.UUID + ".keep-web.example/bar")
1660
1661                         s.handler.Cluster.Collections.WebDAVPermission.Admin.Upload = adminperm
1662                         s.handler.Cluster.Collections.WebDAVPermission.User.Upload = userperm
1663
1664                         // Test admin permission
1665                         req := &http.Request{
1666                                 Method:     "PUT",
1667                                 Host:       u.Host,
1668                                 URL:        u,
1669                                 RequestURI: u.RequestURI(),
1670                                 Header: http.Header{
1671                                         "Authorization": {"Bearer " + arvadostest.AdminToken},
1672                                 },
1673                                 Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
1674                         }
1675                         s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", adminperm,
1676                                 arvadostest.AdminUserUUID, coll.UUID, "", "bar")
1677
1678                         // Test user permission
1679                         req = &http.Request{
1680                                 Method:     "PUT",
1681                                 Host:       u.Host,
1682                                 URL:        u,
1683                                 RequestURI: u.RequestURI(),
1684                                 Header: http.Header{
1685                                         "Authorization": {"Bearer " + arvadostest.ActiveToken},
1686                                 },
1687                                 Body: io.NopCloser(bytes.NewReader([]byte("bar"))),
1688                         }
1689                         s.checkUploadDownloadRequest(c, req, http.StatusCreated, "upload", userperm,
1690                                 arvadostest.ActiveUserUUID, coll.UUID, "", "bar")
1691                 }
1692         }
1693 }
1694
1695 func (s *IntegrationSuite) TestConcurrentWrites(c *check.C) {
1696         s.handler.Cluster.Collections.WebDAVCache.TTL = arvados.Duration(time.Second * 2)
1697         lockTidyInterval = time.Second
1698         client := arvados.NewClientFromEnv()
1699         client.AuthToken = arvadostest.ActiveTokenV2
1700         // Start small, and increase concurrency (2^2, 4^2, ...)
1701         // only until hitting failure. Avoids unnecessarily long
1702         // failure reports.
1703         for n := 2; n < 16 && !c.Failed(); n = n * 2 {
1704                 c.Logf("%s: n=%d", c.TestName(), n)
1705
1706                 var coll arvados.Collection
1707                 err := client.RequestAndDecode(&coll, "POST", "arvados/v1/collections", nil, nil)
1708                 c.Assert(err, check.IsNil)
1709                 defer client.RequestAndDecode(&coll, "DELETE", "arvados/v1/collections/"+coll.UUID, nil, nil)
1710
1711                 var wg sync.WaitGroup
1712                 for i := 0; i < n && !c.Failed(); i++ {
1713                         i := i
1714                         wg.Add(1)
1715                         go func() {
1716                                 defer wg.Done()
1717                                 u := mustParseURL(fmt.Sprintf("http://%s.collections.example.com/i=%d", coll.UUID, i))
1718                                 resp := httptest.NewRecorder()
1719                                 req, err := http.NewRequest("MKCOL", u.String(), nil)
1720                                 c.Assert(err, check.IsNil)
1721                                 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
1722                                 s.handler.ServeHTTP(resp, req)
1723                                 c.Assert(resp.Code, check.Equals, http.StatusCreated)
1724                                 for j := 0; j < n && !c.Failed(); j++ {
1725                                         j := j
1726                                         wg.Add(1)
1727                                         go func() {
1728                                                 defer wg.Done()
1729                                                 content := fmt.Sprintf("i=%d/j=%d", i, j)
1730                                                 u := mustParseURL("http://" + coll.UUID + ".collections.example.com/" + content)
1731
1732                                                 resp := httptest.NewRecorder()
1733                                                 req, err := http.NewRequest("PUT", u.String(), strings.NewReader(content))
1734                                                 c.Assert(err, check.IsNil)
1735                                                 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
1736                                                 s.handler.ServeHTTP(resp, req)
1737                                                 c.Check(resp.Code, check.Equals, http.StatusCreated)
1738
1739                                                 time.Sleep(time.Second)
1740                                                 resp = httptest.NewRecorder()
1741                                                 req, err = http.NewRequest("GET", u.String(), nil)
1742                                                 c.Assert(err, check.IsNil)
1743                                                 req.Header.Set("Authorization", "Bearer "+client.AuthToken)
1744                                                 s.handler.ServeHTTP(resp, req)
1745                                                 c.Check(resp.Code, check.Equals, http.StatusOK)
1746                                                 c.Check(resp.Body.String(), check.Equals, content)
1747                                         }()
1748                                 }
1749                         }()
1750                 }
1751                 wg.Wait()
1752                 for i := 0; i < n; i++ {
1753                         u := mustParseURL(fmt.Sprintf("http://%s.collections.example.com/i=%d", coll.UUID, i))
1754                         resp := httptest.NewRecorder()
1755                         req, err := http.NewRequest("PROPFIND", u.String(), &bytes.Buffer{})
1756                         c.Assert(err, check.IsNil)
1757                         req.Header.Set("Authorization", "Bearer "+client.AuthToken)
1758                         s.handler.ServeHTTP(resp, req)
1759                         c.Assert(resp.Code, check.Equals, http.StatusMultiStatus)
1760                 }
1761         }
1762 }