1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
23 "git.arvados.org/arvados.git/lib/cmd"
24 "git.arvados.org/arvados.git/lib/webdavfs"
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/auth"
28 "git.arvados.org/arvados.git/sdk/go/ctxlog"
29 "git.arvados.org/arvados.git/sdk/go/httpserver"
30 "github.com/sirupsen/logrus"
31 "golang.org/x/net/webdav"
36 Cluster *arvados.Cluster
40 lock map[string]*sync.RWMutex
44 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
46 var notFoundMessage = "Not Found"
47 var unauthorizedMessage = "401 Unauthorized\n\nA valid Arvados token must be provided to access this resource."
49 // parseCollectionIDFromURL returns a UUID or PDH if s is a UUID or a
50 // PDH (even if it is a PDH with "+" replaced by " " or "-");
52 func parseCollectionIDFromURL(s string) string {
53 if arvadosclient.UUIDMatch(s) {
56 if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
62 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
63 json.NewEncoder(w).Encode(struct{ Version string }{cmd.Version.String()})
66 type errorWithHTTPStatus interface {
70 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
71 // sends an HTTP header indicating success, updateOnSuccess first
72 // calls the provided update func. If the update func fails, an error
73 // response is sent (using the error's HTTP status or 500 if none),
74 // and the status code and body sent by the handler are ignored (all
75 // response writes return the update error).
76 type updateOnSuccess struct {
77 httpserver.ResponseWriter
78 logger logrus.FieldLogger
84 func (uos *updateOnSuccess) Write(p []byte) (int, error) {
86 uos.WriteHeader(http.StatusOK)
91 return uos.ResponseWriter.Write(p)
94 func (uos *updateOnSuccess) WriteHeader(code int) {
97 if code >= 200 && code < 400 {
98 if uos.err = uos.update(); uos.err != nil {
99 code := http.StatusInternalServerError
100 if he := errorWithHTTPStatus(nil); errors.As(uos.err, &he) {
101 code = he.HTTPStatus()
103 uos.logger.WithError(uos.err).Errorf("update() returned %T error, changing response to HTTP %d", uos.err, code)
104 http.Error(uos.ResponseWriter, uos.err.Error(), code)
109 uos.ResponseWriter.WriteHeader(code)
113 corsAllowHeadersHeader = strings.Join([]string{
114 "Authorization", "Content-Type", "Range",
115 // WebDAV request headers:
116 "Depth", "Destination", "If", "Lock-Token", "Overwrite", "Timeout", "Cache-Control",
118 writeMethod = map[string]bool{
129 webdavMethod = map[string]bool{
142 browserMethod = map[string]bool{
147 // top-level dirs to serve with siteFS
148 siteFSDir = map[string]bool{
149 "": true, // root directory
155 func stripDefaultPort(host string) string {
156 // Will consider port 80 and port 443 to be the same vhost. I think that's fine.
157 u := &url.URL{Host: host}
158 if p := u.Port(); p == "80" || p == "443" {
159 return strings.ToLower(u.Hostname())
161 return strings.ToLower(host)
165 // CheckHealth implements service.Handler.
166 func (h *handler) CheckHealth() error {
170 // Done implements service.Handler.
171 func (h *handler) Done() <-chan struct{} {
175 // ServeHTTP implements http.Handler.
176 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
177 if xfp := r.Header.Get("X-Forwarded-Proto"); xfp != "" && xfp != "http" {
181 w := httpserver.WrapResponseWriter(wOrig)
183 if r.Method == "OPTIONS" && ServeCORSPreflight(w, r.Header) {
187 if !browserMethod[r.Method] && !webdavMethod[r.Method] {
188 w.WriteHeader(http.StatusMethodNotAllowed)
192 if r.Header.Get("Origin") != "" {
193 // Allow simple cross-origin requests without user
194 // credentials ("user credentials" as defined by CORS,
195 // i.e., cookies, HTTP authentication, and client-side
196 // SSL certificates. See
197 // http://www.w3.org/TR/cors/#user-credentials).
198 w.Header().Set("Access-Control-Allow-Origin", "*")
199 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
207 arvPath := r.URL.Path
208 if prefix := r.Header.Get("X-Webdav-Prefix"); prefix != "" {
209 // Enable a proxy (e.g., container log handler in
210 // controller) to satisfy a request for path
211 // "/foo/bar/baz.txt" using content from
212 // "//abc123-4.internal/bar/baz.txt", by adding a
213 // request header "X-Webdav-Prefix: /foo"
214 if !strings.HasPrefix(arvPath, prefix) {
215 http.Error(w, "X-Webdav-Prefix header is not a prefix of the requested path", http.StatusBadRequest)
218 arvPath = r.URL.Path[len(prefix):]
222 w.Header().Set("Vary", "X-Webdav-Prefix, "+w.Header().Get("Vary"))
223 webdavPrefix = prefix
225 pathParts := strings.Split(arvPath[1:], "/")
228 var collectionID string
230 var reqTokens []string
234 credentialsOK := h.Cluster.Collections.TrustAllContent
235 reasonNotAcceptingCredentials := ""
237 if r.Host != "" && stripDefaultPort(r.Host) == stripDefaultPort(h.Cluster.Services.WebDAVDownload.ExternalURL.Host) {
240 } else if r.FormValue("disposition") == "attachment" {
245 reasonNotAcceptingCredentials = fmt.Sprintf("vhost %q does not specify a single collection ID or match Services.WebDAVDownload.ExternalURL %q, and Collections.TrustAllContent is false",
246 r.Host, h.Cluster.Services.WebDAVDownload.ExternalURL)
249 if collectionID = arvados.CollectionIDFromDNSName(r.Host); collectionID != "" {
250 // http://ID.collections.example/PATH...
252 } else if r.URL.Path == "/status.json" {
255 } else if siteFSDir[pathParts[0]] {
257 } else if len(pathParts) >= 1 && strings.HasPrefix(pathParts[0], "c=") {
259 collectionID = parseCollectionIDFromURL(pathParts[0][2:])
261 } else if len(pathParts) >= 2 && pathParts[0] == "collections" {
262 if len(pathParts) >= 4 && pathParts[1] == "download" {
263 // /collections/download/ID/TOKEN/PATH...
264 collectionID = parseCollectionIDFromURL(pathParts[2])
265 tokens = []string{pathParts[3]}
269 // /collections/ID/PATH...
270 collectionID = parseCollectionIDFromURL(pathParts[1])
272 // This path is only meant to work for public
273 // data. Tokens provided with the request are
275 credentialsOK = false
276 reasonNotAcceptingCredentials = "the '/collections/UUID/PATH' form only works for public data"
281 if cc := r.Header.Get("Cache-Control"); strings.Contains(cc, "no-cache") || strings.Contains(cc, "must-revalidate") {
286 reqTokens = auth.CredentialsFromRequest(r).Tokens
290 origin := r.Header.Get("Origin")
291 cors := origin != "" && !strings.HasSuffix(origin, "://"+r.Host)
292 safeAjax := cors && (r.Method == http.MethodGet || r.Method == http.MethodHead)
293 // Important distinction: safeAttachment checks whether api_token exists
294 // as a query parameter. haveFormTokens checks whether api_token exists
295 // as request form data *or* a query parameter. Different checks are
296 // necessary because both the request disposition and the location of
297 // the API token affect whether or not the request needs to be
298 // redirected. The different branch comments below explain further.
299 safeAttachment := attachment && !r.URL.Query().Has("api_token")
300 if formTokens, haveFormTokens := r.Form["api_token"]; !haveFormTokens {
301 // No token to use or redact.
302 } else if safeAjax || safeAttachment {
303 // If this is a cross-origin request, the URL won't
304 // appear in the browser's address bar, so
305 // substituting a clipboard-safe URL is pointless.
306 // Redirect-with-cookie wouldn't work anyway, because
307 // it's not safe to allow third-party use of our
310 // If we're supplying an attachment, we don't need to
311 // convert POST to GET to avoid the "really resubmit
312 // form?" problem, so provided the token isn't
313 // embedded in the URL, there's no reason to do
314 // redirect-with-cookie in this case either.
315 for _, tok := range formTokens {
316 reqTokens = append(reqTokens, tok)
318 } else if browserMethod[r.Method] {
319 // If this is a page view, and the client provided a
320 // token via query string or POST body, we must put
321 // the token in an HttpOnly cookie, and redirect to an
322 // equivalent URL with the query param redacted and
324 h.seeOtherWithCookie(w, r, "", credentialsOK)
328 targetPath := pathParts[stripParts:]
329 if tokens == nil && len(targetPath) > 0 && strings.HasPrefix(targetPath[0], "t=") {
330 // http://ID.example/t=TOKEN/PATH...
331 // /c=ID/t=TOKEN/PATH...
333 // This form must only be used to pass scoped tokens
334 // that give permission for a single collection. See
335 // FormValue case above.
336 tokens = []string{targetPath[0][2:]}
338 targetPath = targetPath[1:]
344 if writeMethod[r.Method] {
345 http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
348 if len(reqTokens) == 0 {
349 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
350 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
354 } else if collectionID == "" {
355 http.Error(w, notFoundMessage, http.StatusNotFound)
358 fsprefix = "by_id/" + collectionID + "/"
361 if src := r.Header.Get("X-Webdav-Source"); strings.HasPrefix(src, "/") && !strings.Contains(src, "//") && !strings.Contains(src, "/../") {
367 if h.Cluster.Users.AnonymousUserToken != "" {
368 tokens = append(tokens, h.Cluster.Users.AnonymousUserToken)
372 if len(targetPath) > 0 && targetPath[0] == "_" {
373 // If a collection has a directory called "t=foo" or
374 // "_", it can be served at
375 // //collections.example/_/t=foo/ or
376 // //collections.example/_/_/ respectively:
377 // //collections.example/t=foo/ won't work because
378 // t=foo will be interpreted as a token "foo".
379 targetPath = targetPath[1:]
383 dirOpenMode := os.O_RDONLY
384 if writeMethod[r.Method] {
385 dirOpenMode = os.O_RDWR
389 var tokenScopeProblem bool
391 var tokenUser *arvados.User
392 var sessionFS arvados.CustomFileSystem
393 var session *cachedSession
394 var collectionDir arvados.File
395 for _, token = range tokens {
396 var statusErr errorWithHTTPStatus
397 fs, sess, user, err := h.Cache.GetSession(token)
398 if errors.As(err, &statusErr) && statusErr.HTTPStatus() == http.StatusUnauthorized {
401 } else if err != nil {
402 http.Error(w, "cache error: "+err.Error(), http.StatusInternalServerError)
405 if token != h.Cluster.Users.AnonymousUserToken {
408 f, err := fs.OpenFile(fsprefix, dirOpenMode, 0)
409 if errors.As(err, &statusErr) &&
410 statusErr.HTTPStatus() == http.StatusForbidden &&
411 token != h.Cluster.Users.AnonymousUserToken {
412 // collection id is outside scope of supplied
414 tokenScopeProblem = true
417 } else if os.IsNotExist(err) {
418 // collection does not exist or is not
419 // readable using this token
422 } else if err != nil {
423 http.Error(w, err.Error(), http.StatusInternalServerError)
430 collectionDir, sessionFS, session, tokenUser = f, fs, sess, user
433 if forceReload && collectionDir != nil {
434 err := collectionDir.Sync()
436 if he := errorWithHTTPStatus(nil); errors.As(err, &he) {
437 http.Error(w, err.Error(), he.HTTPStatus())
439 http.Error(w, err.Error(), http.StatusInternalServerError)
446 // The URL is a "secret sharing link" that
447 // didn't work out. Asking the client for
448 // additional credentials would just be
450 http.Error(w, notFoundMessage, http.StatusNotFound)
454 // The client provided valid token(s), but the
455 // collection was not found.
456 http.Error(w, notFoundMessage, http.StatusNotFound)
459 if tokenScopeProblem {
460 // The client provided a valid token but
461 // fetching a collection returned 401, which
462 // means the token scope doesn't permit
463 // fetching that collection.
464 http.Error(w, notFoundMessage, http.StatusForbidden)
467 // The client's token was invalid (e.g., expired), or
468 // the client didn't even provide one. Redirect to
469 // workbench2's login-and-redirect-to-download url if
470 // this is a browser navigation request. (The redirect
471 // flow can't preserve the original method if it's not
472 // GET, and doesn't make sense if the UA is a
473 // command-line tool, is trying to load an inline
474 // image, etc.; in these cases, there's nothing we can
475 // do, so return 401 unauthorized.)
477 // Note Sec-Fetch-Mode is sent by all non-EOL
478 // browsers, except Safari.
479 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-Fetch-Mode
481 // TODO(TC): This response would be confusing to
482 // someone trying (anonymously) to download public
483 // data that has been deleted. Allow a referrer to
484 // provide this context somehow?
485 if r.Method == http.MethodGet && r.Header.Get("Sec-Fetch-Mode") == "navigate" {
486 target := url.URL(h.Cluster.Services.Workbench2.ExternalURL)
487 redirkey := "redirectToPreview"
489 redirkey = "redirectToDownload"
491 callback := "/c=" + collectionID + "/" + strings.Join(targetPath, "/")
492 // target.RawQuery = url.Values{redirkey:
493 // {target}}.Encode() would be the obvious
494 // thing to do here, but wb2 doesn't decode
495 // this as a query param -- it takes
496 // everything after "${redirkey}=" as the
497 // target URL. If we encode "/" as "%2F" etc.,
498 // the redirect won't work.
499 target.RawQuery = redirkey + "=" + callback
500 w.Header().Add("Location", target.String())
501 w.WriteHeader(http.StatusSeeOther)
505 http.Error(w, fmt.Sprintf("Authorization tokens are not accepted here: %v, and no anonymous user token is configured.", reasonNotAcceptingCredentials), http.StatusUnauthorized)
508 // If none of the above cases apply, suggest the
509 // user-agent (which is either a non-browser agent
510 // like wget, or a browser that can't redirect through
511 // a login flow) prompt the user for credentials.
512 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
513 http.Error(w, unauthorizedMessage, http.StatusUnauthorized)
517 if r.Method == http.MethodGet || r.Method == http.MethodHead {
518 targetfnm := fsprefix + strings.Join(pathParts[stripParts:], "/")
519 if fi, err := sessionFS.Stat(targetfnm); err == nil && fi.IsDir() {
520 if !strings.HasSuffix(r.URL.Path, "/") {
521 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
523 h.serveDirectory(w, r, fi.Name(), sessionFS, targetfnm, !useSiteFS)
530 if len(targetPath) > 0 {
531 basename = targetPath[len(targetPath)-1]
533 if arvadosclient.PDHMatch(collectionID) && writeMethod[r.Method] {
534 http.Error(w, webdavfs.ErrReadOnly.Error(), http.StatusMethodNotAllowed)
537 if !h.userPermittedToUploadOrDownload(r.Method, tokenUser) {
538 http.Error(w, "Not permitted", http.StatusForbidden)
541 h.logUploadOrDownload(r, session.arvadosclient, sessionFS, fsprefix+strings.Join(targetPath, "/"), nil, tokenUser)
543 writing := writeMethod[r.Method]
544 locker := h.collectionLock(collectionID, writing)
545 defer locker.Unlock()
548 // Save the collection only if/when all
549 // webdav->filesystem operations succeed --
550 // and send a 500 error if the modified
551 // collection can't be saved.
553 // Perform the write in a separate sitefs, so
554 // concurrent read operations on the same
555 // collection see the previous saved
556 // state. After the write succeeds and the
557 // collection record is updated, we reset the
558 // session so the updates are visible in
559 // subsequent read requests.
560 client := session.client.WithRequestID(r.Header.Get("X-Request-Id"))
561 sessionFS = client.SiteFileSystem(session.keepclient)
562 writingDir, err := sessionFS.OpenFile(fsprefix, os.O_RDONLY, 0)
564 http.Error(w, err.Error(), http.StatusInternalServerError)
567 defer writingDir.Close()
568 w = &updateOnSuccess{
570 logger: ctxlog.FromContext(r.Context()),
571 update: func() error {
572 err := writingDir.Sync()
573 var te arvados.TransactionError
574 if errors.As(err, &te) {
580 // Sync the changes to the persistent
581 // sessionfs for this token.
582 snap, err := writingDir.Snapshot()
586 collectionDir.Splice(snap)
590 if r.Method == http.MethodGet {
591 applyContentDispositionHdr(w, r, basename, attachment)
593 if webdavPrefix == "" {
594 webdavPrefix = "/" + strings.Join(pathParts[:stripParts], "/")
596 wh := &webdav.Handler{
597 Prefix: webdavPrefix,
598 FileSystem: &webdavfs.FS{
599 FileSystem: sessionFS,
601 Writing: writeMethod[r.Method],
602 AlwaysReadEOF: r.Method == "PROPFIND",
604 LockSystem: webdavfs.NoLockSystem,
605 Logger: func(r *http.Request, err error) {
606 if err != nil && !os.IsNotExist(err) {
607 ctxlog.FromContext(r.Context()).WithError(err).Error("error reported by webdav handler")
611 h.metrics.track(wh, w, r)
612 if r.Method == http.MethodGet && w.WroteStatus() == http.StatusOK {
613 wrote := int64(w.WroteBodyBytes())
614 fnm := strings.Join(pathParts[stripParts:], "/")
615 fi, err := wh.FileSystem.Stat(r.Context(), fnm)
616 if err == nil && fi.Size() != wrote {
618 f, err := wh.FileSystem.OpenFile(r.Context(), fnm, os.O_RDONLY, 0)
620 n, err = f.Read(make([]byte, 1024))
623 ctxlog.FromContext(r.Context()).Errorf("stat.Size()==%d but only wrote %d bytes; read(1024) returns %d, %v", fi.Size(), wrote, n, err)
628 var dirListingTemplate = `<!DOCTYPE HTML>
630 <META name="robots" content="NOINDEX">
631 <TITLE>{{ .CollectionName }}</TITLE>
632 <STYLE type="text/css">
637 background-color: #D9EDF7;
638 border-radius: .25em;
649 font-family: monospace;
656 <H1>{{ .CollectionName }}</H1>
658 <P>This collection of data files is being shared with you through
659 Arvados. You can download individual files listed below. To download
660 the entire directory tree with wget, try:</P>
662 <PRE>$ wget --mirror --no-parent --no-host --cut-dirs={{ .StripParts }} https://{{ .Request.Host }}{{ .Request.URL.Path }}</PRE>
664 <H2>File Listing</H2>
670 <LI>{{" " | printf "%15s " | nbsp}}<A href="{{print "./" .Name}}/">{{.Name}}/</A></LI>
672 <LI>{{.Size | printf "%15d " | nbsp}}<A href="{{print "./" .Name}}">{{.Name}}</A></LI>
677 <P>(No files; this collection is empty.)</P>
684 Arvados is a free and open source software bioinformatics platform.
685 To learn more, visit arvados.org.
686 Arvados is not responsible for the files listed on this page.
693 type fileListEnt struct {
699 func (h *handler) serveDirectory(w http.ResponseWriter, r *http.Request, collectionName string, fs http.FileSystem, base string, recurse bool) {
700 var files []fileListEnt
701 var walk func(string) error
702 if !strings.HasSuffix(base, "/") {
705 walk = func(path string) error {
706 dirname := base + path
708 dirname = strings.TrimSuffix(dirname, "/")
710 d, err := fs.Open(dirname)
714 ents, err := d.Readdir(-1)
718 for _, ent := range ents {
719 if recurse && ent.IsDir() {
720 err = walk(path + ent.Name() + "/")
725 files = append(files, fileListEnt{
726 Name: path + ent.Name(),
734 if err := walk(""); err != nil {
735 http.Error(w, "error getting directory listing: "+err.Error(), http.StatusInternalServerError)
739 funcs := template.FuncMap{
740 "nbsp": func(s string) template.HTML {
741 return template.HTML(strings.Replace(s, " ", " ", -1))
744 tmpl, err := template.New("dir").Funcs(funcs).Parse(dirListingTemplate)
746 http.Error(w, "error parsing template: "+err.Error(), http.StatusInternalServerError)
749 sort.Slice(files, func(i, j int) bool {
750 return files[i].Name < files[j].Name
752 w.WriteHeader(http.StatusOK)
753 tmpl.Execute(w, map[string]interface{}{
754 "CollectionName": collectionName,
757 "StripParts": strings.Count(strings.TrimRight(r.URL.Path, "/"), "/"),
761 func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) {
762 disposition := "inline"
764 disposition = "attachment"
766 if strings.ContainsRune(r.RequestURI, '?') {
767 // Help the UA realize that the filename is just
768 // "filename.txt", not
769 // "filename.txt?disposition=attachment".
771 // TODO(TC): Follow advice at RFC 6266 appendix D
772 disposition += "; filename=" + strconv.QuoteToASCII(filename)
774 if disposition != "inline" {
775 w.Header().Set("Content-Disposition", disposition)
779 func (h *handler) seeOtherWithCookie(w http.ResponseWriter, r *http.Request, location string, credentialsOK bool) {
780 if formTokens, haveFormTokens := r.Form["api_token"]; haveFormTokens {
782 // It is not safe to copy the provided token
783 // into a cookie unless the current vhost
784 // (origin) serves only a single collection or
785 // we are in TrustAllContent mode.
786 http.Error(w, "cannot serve inline content at this URL (possible configuration error; see https://doc.arvados.org/install/install-keep-web.html#dns)", http.StatusBadRequest)
790 // The HttpOnly flag is necessary to prevent
791 // JavaScript code (included in, or loaded by, a page
792 // in the collection being served) from employing the
793 // user's token beyond reading other files in the same
794 // domain, i.e., same collection.
796 // The 303 redirect is necessary in the case of a GET
797 // request to avoid exposing the token in the Location
798 // bar, and in the case of a POST request to avoid
799 // raising warnings when the user refreshes the
801 for _, tok := range formTokens {
805 http.SetCookie(w, &http.Cookie{
806 Name: "arvados_api_token",
807 Value: auth.EncodeTokenCookie([]byte(tok)),
810 SameSite: http.SameSiteLaxMode,
816 // Propagate query parameters (except api_token) from
817 // the original request.
818 redirQuery := r.URL.Query()
819 redirQuery.Del("api_token")
823 newu, err := u.Parse(location)
825 http.Error(w, "error resolving redirect target: "+err.Error(), http.StatusInternalServerError)
831 Scheme: r.URL.Scheme,
834 RawQuery: redirQuery.Encode(),
837 w.Header().Add("Location", redir)
838 w.WriteHeader(http.StatusSeeOther)
839 io.WriteString(w, `<A href="`)
840 io.WriteString(w, html.EscapeString(redir))
841 io.WriteString(w, `">Continue</A>`)
844 func (h *handler) userPermittedToUploadOrDownload(method string, tokenUser *arvados.User) bool {
845 var permitDownload bool
846 var permitUpload bool
847 if tokenUser != nil && tokenUser.IsAdmin {
848 permitUpload = h.Cluster.Collections.WebDAVPermission.Admin.Upload
849 permitDownload = h.Cluster.Collections.WebDAVPermission.Admin.Download
851 permitUpload = h.Cluster.Collections.WebDAVPermission.User.Upload
852 permitDownload = h.Cluster.Collections.WebDAVPermission.User.Download
854 if (method == "PUT" || method == "POST") && !permitUpload {
855 // Disallow operations that upload new files.
856 // Permit webdav operations that move existing files around.
858 } else if method == "GET" && !permitDownload {
859 // Disallow downloading file contents.
860 // Permit webdav operations like PROPFIND that retrieve metadata
861 // but not file contents.
867 func (h *handler) logUploadOrDownload(
869 client *arvadosclient.ArvadosClient,
870 fs arvados.CustomFileSystem,
872 collection *arvados.Collection,
873 user *arvados.User) {
875 log := ctxlog.FromContext(r.Context())
876 props := make(map[string]string)
877 props["reqPath"] = r.URL.Path
880 log = log.WithField("user_uuid", user.UUID).
881 WithField("user_full_name", user.FullName)
884 useruuid = fmt.Sprintf("%s-tpzed-anonymouspublic", h.Cluster.ClusterID)
886 if collection == nil && fs != nil {
887 collection, filepath = h.determineCollection(fs, filepath)
889 if collection != nil {
890 log = log.WithField("collection_file_path", filepath)
891 props["collection_file_path"] = filepath
892 // h.determineCollection populates the collection_uuid
893 // prop with the PDH, if this collection is being
894 // accessed via PDH. For logging, we use a different
895 // field depending on whether it's a UUID or PDH.
896 if len(collection.UUID) > 32 {
897 log = log.WithField("portable_data_hash", collection.UUID)
898 props["portable_data_hash"] = collection.UUID
900 log = log.WithField("collection_uuid", collection.UUID)
901 props["collection_uuid"] = collection.UUID
904 if r.Method == "PUT" || r.Method == "POST" {
905 log.Info("File upload")
906 if h.Cluster.Collections.WebDAVLogEvents {
908 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
909 "object_uuid": useruuid,
910 "event_type": "file_upload",
911 "properties": props}}
912 err := client.Create("logs", lr, nil)
914 log.WithError(err).Error("Failed to create upload log event on API server")
918 } else if r.Method == "GET" {
919 if collection != nil && collection.PortableDataHash != "" {
920 log = log.WithField("portable_data_hash", collection.PortableDataHash)
921 props["portable_data_hash"] = collection.PortableDataHash
923 log.Info("File download")
924 if h.Cluster.Collections.WebDAVLogEvents {
926 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
927 "object_uuid": useruuid,
928 "event_type": "file_download",
929 "properties": props}}
930 err := client.Create("logs", lr, nil)
932 log.WithError(err).Error("Failed to create download log event on API server")
939 func (h *handler) determineCollection(fs arvados.CustomFileSystem, path string) (*arvados.Collection, string) {
940 target := strings.TrimSuffix(path, "/")
941 for cut := len(target); cut >= 0; cut = strings.LastIndexByte(target, '/') {
942 target = target[:cut]
943 fi, err := fs.Stat(target)
944 if os.IsNotExist(err) {
945 // creating a new file/dir, or download
948 } else if err != nil {
951 switch src := fi.Sys().(type) {
952 case *arvados.Collection:
953 return src, strings.TrimPrefix(path[len(target):], "/")
957 if _, ok := src.(error); ok {
965 var lockTidyInterval = time.Minute * 10
967 // Lock the specified collection for reading or writing. Caller must
968 // call Unlock() on the returned Locker when the operation is
970 func (h *handler) collectionLock(collectionID string, writing bool) sync.Locker {
972 defer h.lockMtx.Unlock()
973 if time.Since(h.lockTidied) > lockTidyInterval {
974 // Periodically delete all locks that aren't in use.
975 h.lockTidied = time.Now()
976 for id, locker := range h.lock {
977 if locker.TryLock() {
983 locker := h.lock[collectionID]
985 locker = new(sync.RWMutex)
987 h.lock = map[string]*sync.RWMutex{}
989 h.lock[collectionID] = locker
996 return locker.RLocker()
1000 func ServeCORSPreflight(w http.ResponseWriter, header http.Header) bool {
1001 method := header.Get("Access-Control-Request-Method")
1005 if !browserMethod[method] && !webdavMethod[method] {
1006 w.WriteHeader(http.StatusMethodNotAllowed)
1009 w.Header().Set("Access-Control-Allow-Headers", corsAllowHeadersHeader)
1010 w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, LOCK, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PROPPATCH, PUT, RMCOL, UNLOCK")
1011 w.Header().Set("Access-Control-Allow-Origin", "*")
1012 w.Header().Set("Access-Control-Max-Age", "86400")