1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
24 "git.curoverse.com/arvados.git/sdk/go/auth"
25 "git.curoverse.com/arvados.git/sdk/go/health"
26 "git.curoverse.com/arvados.git/sdk/go/httpserver"
27 "git.curoverse.com/arvados.git/sdk/go/keepclient"
28 log "github.com/Sirupsen/logrus"
29 "golang.org/x/net/webdav"
34 clientPool *arvadosclient.ClientPool
36 healthHandler http.Handler
37 webdavLS webdav.LockSystem
40 // parseCollectionIDFromDNSName returns a UUID or PDH if s begins with
41 // a UUID or URL-encoded PDH; otherwise "".
42 func parseCollectionIDFromDNSName(s string) string {
44 if i := strings.IndexRune(s, '.'); i >= 0 {
47 // Names like {uuid}--collections.example.com serve the same
48 // purpose as {uuid}.collections.example.com but can reduce
49 // cost/effort of using [additional] wildcard certificates.
50 if i := strings.Index(s, "--"); i >= 0 {
53 if arvadosclient.UUIDMatch(s) {
56 if pdh := strings.Replace(s, "-", "+", 1); arvadosclient.PDHMatch(pdh) {
62 var urlPDHDecoder = strings.NewReplacer(" ", "+", "-", "+")
64 // parseCollectionIDFromURL returns a UUID or PDH if s is a UUID or a
65 // PDH (even if it is a PDH with "+" replaced by " " or "-");
67 func parseCollectionIDFromURL(s string) string {
68 if arvadosclient.UUIDMatch(s) {
71 if pdh := urlPDHDecoder.Replace(s); arvadosclient.PDHMatch(pdh) {
77 func (h *handler) setup() {
78 h.clientPool = arvadosclient.MakeClientPool()
80 keepclient.RefreshServiceDiscoveryOnSIGHUP()
82 h.healthHandler = &health.Handler{
83 Token: h.Config.ManagementToken,
87 // Even though we don't accept LOCK requests, every webdav
88 // handler must have a non-nil LockSystem.
89 h.webdavLS = &noLockSystem{}
92 func (h *handler) serveStatus(w http.ResponseWriter, r *http.Request) {
97 cacheStats: h.Config.Cache.Stats(),
100 json.NewEncoder(w).Encode(status)
103 // updateOnSuccess wraps httpserver.ResponseWriter. If the handler
104 // sends an HTTP header indicating success, updateOnSuccess first
105 // calls the provided update func. If the update func fails, a 500
106 // response is sent, and the status code and body sent by the handler
107 // are ignored (all response writes return the update error).
108 type updateOnSuccess struct {
109 httpserver.ResponseWriter
115 func (uos *updateOnSuccess) Write(p []byte) (int, error) {
117 uos.WriteHeader(http.StatusOK)
122 return uos.ResponseWriter.Write(p)
125 func (uos *updateOnSuccess) WriteHeader(code int) {
127 uos.sentHeader = true
128 if code >= 200 && code < 400 {
129 if uos.err = uos.update(); uos.err != nil {
130 code := http.StatusInternalServerError
131 if err, ok := uos.err.(*arvados.TransactionError); ok {
132 code = err.StatusCode
134 log.Printf("update() changes response to HTTP %d: %T %q", code, uos.err, uos.err)
135 http.Error(uos.ResponseWriter, uos.err.Error(), code)
140 uos.ResponseWriter.WriteHeader(code)
144 writeMethod = map[string]bool{
152 webdavMethod = map[string]bool{
162 browserMethod = map[string]bool{
167 // top-level dirs to serve with siteFS
168 siteFSDir = map[string]bool{
169 "": true, // root directory
175 // ServeHTTP implements http.Handler.
176 func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
177 h.setupOnce.Do(h.setup)
180 var statusText string
182 remoteAddr := r.RemoteAddr
183 if xff := r.Header.Get("X-Forwarded-For"); xff != "" {
184 remoteAddr = xff + "," + remoteAddr
186 if xfp := r.Header.Get("X-Forwarded-Proto"); xfp != "" && xfp != "http" {
190 w := httpserver.WrapResponseWriter(wOrig)
193 statusCode = w.WroteStatus()
194 } else if w.WroteStatus() == 0 {
195 w.WriteHeader(statusCode)
196 } else if w.WroteStatus() != statusCode {
197 log.WithField("RequestID", r.Header.Get("X-Request-Id")).Warn(
198 fmt.Sprintf("Our status changed from %d to %d after we sent headers", w.WroteStatus(), statusCode))
200 if statusText == "" {
201 statusText = http.StatusText(statusCode)
205 if strings.HasPrefix(r.URL.Path, "/_health/") && r.Method == "GET" {
206 h.healthHandler.ServeHTTP(w, r)
210 if method := r.Header.Get("Access-Control-Request-Method"); method != "" && r.Method == "OPTIONS" {
211 if !browserMethod[method] && !webdavMethod[method] {
212 statusCode = http.StatusMethodNotAllowed
215 w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Range")
216 w.Header().Set("Access-Control-Allow-Methods", "COPY, DELETE, GET, MKCOL, MOVE, OPTIONS, POST, PROPFIND, PUT, RMCOL")
217 w.Header().Set("Access-Control-Allow-Origin", "*")
218 w.Header().Set("Access-Control-Max-Age", "86400")
219 statusCode = http.StatusOK
223 if !browserMethod[r.Method] && !webdavMethod[r.Method] {
224 statusCode, statusText = http.StatusMethodNotAllowed, r.Method
228 if r.Header.Get("Origin") != "" {
229 // Allow simple cross-origin requests without user
230 // credentials ("user credentials" as defined by CORS,
231 // i.e., cookies, HTTP authentication, and client-side
232 // SSL certificates. See
233 // http://www.w3.org/TR/cors/#user-credentials).
234 w.Header().Set("Access-Control-Allow-Origin", "*")
235 w.Header().Set("Access-Control-Expose-Headers", "Content-Range")
238 pathParts := strings.Split(r.URL.Path[1:], "/")
241 var collectionID string
243 var reqTokens []string
247 credentialsOK := h.Config.TrustAllContent
249 if r.Host != "" && r.Host == h.Config.AttachmentOnlyHost {
252 } else if r.FormValue("disposition") == "attachment" {
256 if collectionID = parseCollectionIDFromDNSName(r.Host); collectionID != "" {
257 // http://ID.collections.example/PATH...
259 } else if r.URL.Path == "/status.json" {
262 } else if siteFSDir[pathParts[0]] {
264 } else if len(pathParts) >= 1 && strings.HasPrefix(pathParts[0], "c=") {
266 collectionID = parseCollectionIDFromURL(pathParts[0][2:])
268 } else if len(pathParts) >= 2 && pathParts[0] == "collections" {
269 if len(pathParts) >= 4 && pathParts[1] == "download" {
270 // /collections/download/ID/TOKEN/PATH...
271 collectionID = parseCollectionIDFromURL(pathParts[2])
272 tokens = []string{pathParts[3]}
276 // /collections/ID/PATH...
277 collectionID = parseCollectionIDFromURL(pathParts[1])
278 tokens = h.Config.AnonymousTokens
283 if collectionID == "" && !useSiteFS {
284 statusCode = http.StatusNotFound
289 if cc := r.Header.Get("Cache-Control"); strings.Contains(cc, "no-cache") || strings.Contains(cc, "must-revalidate") {
293 formToken := r.FormValue("api_token")
294 if formToken != "" && r.Header.Get("Origin") != "" && attachment && r.URL.Query().Get("api_token") == "" {
295 // The client provided an explicit token in the POST
296 // body. The Origin header indicates this *might* be
297 // an AJAX request, in which case redirect-with-cookie
298 // won't work: we should just serve the content in the
299 // POST response. This is safe because:
301 // * We're supplying an attachment, not inline
302 // content, so we don't need to convert the POST to
303 // a GET and avoid the "really resubmit form?"
306 // * The token isn't embedded in the URL, so we don't
307 // need to worry about bookmarks and copy/paste.
308 tokens = append(tokens, formToken)
309 } else if formToken != "" && browserMethod[r.Method] {
310 // The client provided an explicit token in the query
311 // string, or a form in POST body. We must put the
312 // token in an HttpOnly cookie, and redirect to the
313 // same URL with the query param redacted and method =
315 h.seeOtherWithCookie(w, r, "", credentialsOK)
321 tokens = auth.NewCredentialsFromHTTPRequest(r).Tokens
323 h.serveSiteFS(w, r, tokens, credentialsOK, attachment)
327 targetPath := pathParts[stripParts:]
328 if tokens == nil && len(targetPath) > 0 && strings.HasPrefix(targetPath[0], "t=") {
329 // http://ID.example/t=TOKEN/PATH...
330 // /c=ID/t=TOKEN/PATH...
332 // This form must only be used to pass scoped tokens
333 // that give permission for a single collection. See
334 // FormValue case above.
335 tokens = []string{targetPath[0][2:]}
337 targetPath = targetPath[1:]
343 reqTokens = auth.NewCredentialsFromHTTPRequest(r).Tokens
345 tokens = append(reqTokens, h.Config.AnonymousTokens...)
348 if len(targetPath) > 0 && targetPath[0] == "_" {
349 // If a collection has a directory called "t=foo" or
350 // "_", it can be served at
351 // //collections.example/_/t=foo/ or
352 // //collections.example/_/_/ respectively:
353 // //collections.example/t=foo/ won't work because
354 // t=foo will be interpreted as a token "foo".
355 targetPath = targetPath[1:]
359 arv := h.clientPool.Get()
361 statusCode, statusText = http.StatusInternalServerError, "Pool failed: "+h.clientPool.Err().Error()
364 defer h.clientPool.Put(arv)
366 var collection *arvados.Collection
367 tokenResult := make(map[string]int)
368 for _, arv.ApiToken = range tokens {
370 collection, err = h.Config.Cache.Get(arv, collectionID, forceReload)
375 if srvErr, ok := err.(arvadosclient.APIServerError); ok {
376 switch srvErr.HttpStatusCode {
378 // Token broken or insufficient to
379 // retrieve collection
380 tokenResult[arv.ApiToken] = srvErr.HttpStatusCode
384 // Something more serious is wrong
385 statusCode, statusText = http.StatusInternalServerError, err.Error()
388 if collection == nil {
389 if pathToken || !credentialsOK {
390 // Either the URL is a "secret sharing link"
391 // that didn't work out (and asking the client
392 // for additional credentials would just be
393 // confusing), or we don't even accept
394 // credentials at this path.
395 statusCode = http.StatusNotFound
398 for _, t := range reqTokens {
399 if tokenResult[t] == 404 {
400 // The client provided valid token(s), but the
401 // collection was not found.
402 statusCode = http.StatusNotFound
406 // The client's token was invalid (e.g., expired), or
407 // the client didn't even provide one. Propagate the
408 // 401 to encourage the client to use a [different]
411 // TODO(TC): This response would be confusing to
412 // someone trying (anonymously) to download public
413 // data that has been deleted. Allow a referrer to
414 // provide this context somehow?
415 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
416 statusCode = http.StatusUnauthorized
420 kc, err := keepclient.MakeKeepClient(arv)
422 statusCode, statusText = http.StatusInternalServerError, err.Error()
425 kc.RequestID = r.Header.Get("X-Request-Id")
428 if len(targetPath) > 0 {
429 basename = targetPath[len(targetPath)-1]
431 applyContentDispositionHdr(w, r, basename, attachment)
433 client := (&arvados.Client{
434 APIHost: arv.ApiServer,
435 AuthToken: arv.ApiToken,
436 Insecure: arv.ApiInsecure,
437 }).WithRequestID(r.Header.Get("X-Request-Id"))
439 fs, err := collection.FileSystem(client, kc)
441 statusCode, statusText = http.StatusInternalServerError, err.Error()
445 writefs, writeOK := fs.(arvados.CollectionFileSystem)
446 targetIsPDH := arvadosclient.PDHMatch(collectionID)
447 if (targetIsPDH || !writeOK) && writeMethod[r.Method] {
448 statusCode, statusText = http.StatusMethodNotAllowed, errReadOnly.Error()
452 if webdavMethod[r.Method] {
453 if writeMethod[r.Method] {
454 // Save the collection only if/when all
455 // webdav->filesystem operations succeed --
456 // and send a 500 error if the modified
457 // collection can't be saved.
458 w = &updateOnSuccess{
460 update: func() error {
461 return h.Config.Cache.Update(client, *collection, writefs)
465 Prefix: "/" + strings.Join(pathParts[:stripParts], "/"),
466 FileSystem: &webdavFS{
468 writing: writeMethod[r.Method],
469 alwaysReadEOF: r.Method == "PROPFIND",
471 LockSystem: h.webdavLS,
472 Logger: func(_ *http.Request, err error) {
474 log.Printf("error from webdav handler: %q", err)
482 openPath := "/" + strings.Join(targetPath, "/")
483 if f, err := fs.Open(openPath); os.IsNotExist(err) {
484 // Requested non-existent path
485 statusCode = http.StatusNotFound
486 } else if err != nil {
487 // Some other (unexpected) error
488 statusCode, statusText = http.StatusInternalServerError, err.Error()
489 } else if stat, err := f.Stat(); err != nil {
490 // Can't get Size/IsDir (shouldn't happen with a collectionFS!)
491 statusCode, statusText = http.StatusInternalServerError, err.Error()
492 } else if stat.IsDir() && !strings.HasSuffix(r.URL.Path, "/") {
493 // If client requests ".../dirname", redirect to
494 // ".../dirname/". This way, relative links in the
495 // listing for "dirname" can always be "fnm", never
497 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
498 } else if stat.IsDir() {
499 h.serveDirectory(w, r, collection.Name, fs, openPath, true)
501 http.ServeContent(w, r, basename, stat.ModTime(), f)
502 if r.Header.Get("Range") == "" && int64(w.WroteBodyBytes()) != stat.Size() {
503 // If we wrote fewer bytes than expected, it's
504 // too late to change the real response code
505 // or send an error message to the client, but
506 // at least we can try to put some useful
507 // debugging info in the logs.
508 n, err := f.Read(make([]byte, 1024))
509 statusCode, statusText = http.StatusInternalServerError, fmt.Sprintf("f.Size()==%d but only wrote %d bytes; read(1024) returns %d, %s", stat.Size(), w.WroteBodyBytes(), n, err)
515 func (h *handler) serveSiteFS(w http.ResponseWriter, r *http.Request, tokens []string, credentialsOK, attachment bool) {
516 if len(tokens) == 0 {
517 w.Header().Add("WWW-Authenticate", "Basic realm=\"collections\"")
518 http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
521 if writeMethod[r.Method] {
522 http.Error(w, errReadOnly.Error(), http.StatusMethodNotAllowed)
525 arv := h.clientPool.Get()
527 http.Error(w, "Pool failed: "+h.clientPool.Err().Error(), http.StatusInternalServerError)
530 defer h.clientPool.Put(arv)
531 arv.ApiToken = tokens[0]
533 kc, err := keepclient.MakeKeepClient(arv)
535 http.Error(w, err.Error(), http.StatusInternalServerError)
538 kc.RequestID = r.Header.Get("X-Request-Id")
539 client := (&arvados.Client{
540 APIHost: arv.ApiServer,
541 AuthToken: arv.ApiToken,
542 Insecure: arv.ApiInsecure,
543 }).WithRequestID(r.Header.Get("X-Request-Id"))
544 fs := client.SiteFileSystem(kc)
545 f, err := fs.Open(r.URL.Path)
546 if os.IsNotExist(err) {
547 http.Error(w, err.Error(), http.StatusNotFound)
549 } else if err != nil {
550 http.Error(w, err.Error(), http.StatusInternalServerError)
554 if fi, err := f.Stat(); err == nil && fi.IsDir() && r.Method == "GET" {
555 if !strings.HasSuffix(r.URL.Path, "/") {
556 h.seeOtherWithCookie(w, r, r.URL.Path+"/", credentialsOK)
558 h.serveDirectory(w, r, fi.Name(), fs, r.URL.Path, false)
562 if r.Method == "GET" {
563 _, basename := filepath.Split(r.URL.Path)
564 applyContentDispositionHdr(w, r, basename, attachment)
566 wh := webdav.Handler{
568 FileSystem: &webdavFS{
570 writing: writeMethod[r.Method],
571 alwaysReadEOF: r.Method == "PROPFIND",
573 LockSystem: h.webdavLS,
574 Logger: func(_ *http.Request, err error) {
576 log.Printf("error from webdav handler: %q", err)
583 var dirListingTemplate = `<!DOCTYPE HTML>
585 <META name="robots" content="NOINDEX">
586 <TITLE>{{ .CollectionName }}</TITLE>
587 <STYLE type="text/css">
592 background-color: #D9EDF7;
593 border-radius: .25em;
604 font-family: monospace;
611 <H1>{{ .CollectionName }}</H1>
613 <P>This collection of data files is being shared with you through
614 Arvados. You can download individual files listed below. To download
615 the entire directory tree with wget, try:</P>
617 <PRE>$ wget --mirror --no-parent --no-host --cut-dirs={{ .StripParts }} https://{{ .Request.Host }}{{ .Request.URL.Path }}</PRE>
619 <H2>File Listing</H2>
625 <LI>{{" " | printf "%15s " | nbsp}}<A href="{{print "./" .Name}}/">{{.Name}}/</A></LI>
627 <LI>{{.Size | printf "%15d " | nbsp}}<A href="{{print "./" .Name}}">{{.Name}}</A></LI>
632 <P>(No files; this collection is empty.)</P>
639 Arvados is a free and open source software bioinformatics platform.
640 To learn more, visit arvados.org.
641 Arvados is not responsible for the files listed on this page.
648 type fileListEnt struct {
654 func (h *handler) serveDirectory(w http.ResponseWriter, r *http.Request, collectionName string, fs http.FileSystem, base string, recurse bool) {
655 var files []fileListEnt
656 var walk func(string) error
657 if !strings.HasSuffix(base, "/") {
660 walk = func(path string) error {
661 dirname := base + path
663 dirname = strings.TrimSuffix(dirname, "/")
665 d, err := fs.Open(dirname)
669 ents, err := d.Readdir(-1)
673 for _, ent := range ents {
674 if recurse && ent.IsDir() {
675 err = walk(path + ent.Name() + "/")
680 files = append(files, fileListEnt{
681 Name: path + ent.Name(),
689 if err := walk(""); err != nil {
690 http.Error(w, err.Error(), http.StatusInternalServerError)
694 funcs := template.FuncMap{
695 "nbsp": func(s string) template.HTML {
696 return template.HTML(strings.Replace(s, " ", " ", -1))
699 tmpl, err := template.New("dir").Funcs(funcs).Parse(dirListingTemplate)
701 http.Error(w, err.Error(), http.StatusInternalServerError)
704 sort.Slice(files, func(i, j int) bool {
705 return files[i].Name < files[j].Name
707 w.WriteHeader(http.StatusOK)
708 tmpl.Execute(w, map[string]interface{}{
709 "CollectionName": collectionName,
712 "StripParts": strings.Count(strings.TrimRight(r.URL.Path, "/"), "/"),
716 func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) {
717 disposition := "inline"
719 disposition = "attachment"
721 if strings.ContainsRune(r.RequestURI, '?') {
722 // Help the UA realize that the filename is just
723 // "filename.txt", not
724 // "filename.txt?disposition=attachment".
726 // TODO(TC): Follow advice at RFC 6266 appendix D
727 disposition += "; filename=" + strconv.QuoteToASCII(filename)
729 if disposition != "inline" {
730 w.Header().Set("Content-Disposition", disposition)
734 func (h *handler) seeOtherWithCookie(w http.ResponseWriter, r *http.Request, location string, credentialsOK bool) {
735 if formToken := r.FormValue("api_token"); formToken != "" {
737 // It is not safe to copy the provided token
738 // into a cookie unless the current vhost
739 // (origin) serves only a single collection or
740 // we are in TrustAllContent mode.
741 w.WriteHeader(http.StatusBadRequest)
745 // The HttpOnly flag is necessary to prevent
746 // JavaScript code (included in, or loaded by, a page
747 // in the collection being served) from employing the
748 // user's token beyond reading other files in the same
749 // domain, i.e., same collection.
751 // The 303 redirect is necessary in the case of a GET
752 // request to avoid exposing the token in the Location
753 // bar, and in the case of a POST request to avoid
754 // raising warnings when the user refreshes the
756 http.SetCookie(w, &http.Cookie{
757 Name: "arvados_api_token",
758 Value: auth.EncodeTokenCookie([]byte(formToken)),
764 // Propagate query parameters (except api_token) from
765 // the original request.
766 redirQuery := r.URL.Query()
767 redirQuery.Del("api_token")
771 newu, err := u.Parse(location)
773 w.WriteHeader(http.StatusInternalServerError)
779 Scheme: r.URL.Scheme,
782 RawQuery: redirQuery.Encode(),
785 w.Header().Add("Location", redir)
786 w.WriteHeader(http.StatusSeeOther)
787 io.WriteString(w, `<A href="`)
788 io.WriteString(w, html.EscapeString(redir))
789 io.WriteString(w, `">Continue</A>`)