17464: Log collection uuid for FileSystem requests
authorPeter Amstutz <peter.amstutz@curii.com>
Fri, 11 Jun 2021 19:26:05 +0000 (15:26 -0400)
committerPeter Amstutz <peter.amstutz@curii.com>
Fri, 18 Jun 2021 15:34:44 +0000 (11:34 -0400)
Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <peter.amstutz@curii.com>

sdk/go/arvados/fs_collection.go
services/keep-web/handler.go
services/keep-web/s3.go

index 22e2b31d57e08d6c5dc813017c62b950f61aac01..b743ab368e33f69a5c1710d63dc410af8a380ffc 100644 (file)
@@ -674,6 +674,7 @@ func (dn *dirnode) Child(name string, replace func(inode) (inode, error)) (inode
                        if err != nil {
                                return nil, err
                        }
+                       coll.UUID = dn.fs.uuid
                        data, err := json.Marshal(&coll)
                        if err == nil {
                                data = append(data, '\n')
index ba9114664759b09d701eb68a23b0aa9597264517..43ce57904c5c31dde7d855482afa87341a4fb178 100644 (file)
@@ -491,7 +491,7 @@ func (h *handler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
                http.Error(w, "Not permitted", http.StatusForbidden)
                return
        }
-       h.LogUploadOrDownload(r, sess.arvadosclient, collection, tokenUser)
+       h.LogUploadOrDownload(r, sess.arvadosclient, nil, strings.Join(targetPath, "/"), collection, tokenUser)
 
        if webdavMethod[r.Method] {
                if writeMethod[r.Method] {
@@ -623,7 +623,7 @@ func (h *handler) serveSiteFS(w http.ResponseWriter, r *http.Request, tokens []s
                http.Error(w, "Not permitted", http.StatusForbidden)
                return
        }
-       h.LogUploadOrDownload(r, sess.arvadosclient, nil, tokenUser)
+       h.LogUploadOrDownload(r, sess.arvadosclient, fs, r.URL.Path, nil, tokenUser)
 
        if r.Method == "GET" {
                _, basename := filepath.Split(r.URL.Path)
@@ -882,17 +882,29 @@ func (h *handler) UserPermittedToUploadOrDownload(method string, tokenUser *arva
        return true
 }
 
-func (h *handler) LogUploadOrDownload(r *http.Request, client *arvadosclient.ArvadosClient, collection *arvados.Collection, user *arvados.User) {
+func (h *handler) LogUploadOrDownload(
+       r *http.Request,
+       client *arvadosclient.ArvadosClient,
+       fs arvados.CustomFileSystem,
+       filepath string,
+       collection *arvados.Collection,
+       user *arvados.User) {
+
        log := ctxlog.FromContext(r.Context())
        props := make(map[string]string)
        props["reqPath"] = r.URL.Path
        if user != nil {
                log = log.WithField("user_uuid", user.UUID).
-                       WithField("full_name", user.FullName)
+                       WithField("user_full_name", user.FullName)
+       }
+       if collection == nil && fs != nil {
+               collection, filepath = h.DetermineCollection(fs, filepath)
        }
        if collection != nil {
-               log = log.WithField("collection_uuid", collection.UUID)
+               log = log.WithField("collection_uuid", collection.UUID).
+                       WithField("collection_file_path", filepath)
                props["collection_uuid"] = collection.UUID
+               props["collection_file_path"] = filepath
        }
        if r.Method == "PUT" || r.Method == "POST" {
                log.Info("File upload")
@@ -904,7 +916,7 @@ func (h *handler) LogUploadOrDownload(r *http.Request, client *arvadosclient.Arv
                        client.Create("logs", lr, nil)
                }()
        } else if r.Method == "GET" {
-               if collection != nil {
+               if collection != nil && collection.PortableDataHash != "" {
                        log = log.WithField("portable_data_hash", collection.PortableDataHash)
                        props["portable_data_hash"] = collection.PortableDataHash
                }
@@ -918,3 +930,24 @@ func (h *handler) LogUploadOrDownload(r *http.Request, client *arvadosclient.Arv
                }()
        }
 }
+
+func (h *handler) DetermineCollection(fs arvados.CustomFileSystem, path string) (*arvados.Collection, string) {
+       segments := strings.Split(path, "/")
+       var i int
+       for i = len(segments) - 1; i >= 0; i-- {
+               dir := append([]string{}, segments[0:i]...)
+               dir = append(dir, ".arvados#collection")
+               f, err := fs.OpenFile(strings.Join(dir, "/"), os.O_RDONLY, 0)
+               if err == nil {
+                       decoder := json.NewDecoder(f)
+                       var collection arvados.Collection
+                       err = decoder.Decode(&collection)
+                       if err != nil {
+                               return nil, ""
+                       }
+                       return &collection, strings.Join(segments[i:], "/")
+               }
+               f.Close()
+       }
+       return nil, ""
+}
index 6a0c9433119676fee28045c9c6d34a058ffd0a6d..357c42ae6caee6e84f15c2c20d0f2ac754caa911 100644 (file)
@@ -410,7 +410,7 @@ func (h *handler) serveS3(w http.ResponseWriter, r *http.Request) bool {
                        http.Error(w, "Not permitted", http.StatusForbidden)
                        return true
                }
-               h.LogUploadOrDownload(r, arvclient, nil, tokenUser)
+               h.LogUploadOrDownload(r, arvclient, fs, fspath, nil, tokenUser)
 
                // shallow copy r, and change URL path
                r := *r
@@ -501,7 +501,7 @@ func (h *handler) serveS3(w http.ResponseWriter, r *http.Request) bool {
                                http.Error(w, "Not permitted", http.StatusForbidden)
                                return true
                        }
-                       h.LogUploadOrDownload(r, arvclient, nil, tokenUser)
+                       h.LogUploadOrDownload(r, arvclient, fs, fspath, nil, tokenUser)
 
                        _, err = io.Copy(f, r.Body)
                        if err != nil {