1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/ctxlog"
21 "git.arvados.org/arvados.git/tmp/GOPATH/src/github.com/AdRoll/goamz/s3"
24 // serveS3 handles r and returns true if r is a request from an S3
25 // client, otherwise it returns false.
26 func (h *handler) serveS3(w http.ResponseWriter, r *http.Request) bool {
28 if auth := r.Header.Get("Authorization"); strings.HasPrefix(auth, "AWS ") {
29 split := strings.SplitN(auth[4:], ":", 2)
31 w.WriteHeader(http.StatusUnauthorized)
35 } else if strings.HasPrefix(auth, "AWS4-HMAC-SHA256 ") {
36 w.WriteHeader(http.StatusBadRequest)
37 fmt.Println(w, "V4 signature is not supported")
43 _, kc, client, release, err := h.getClients(r.Header.Get("X-Request-Id"), token)
45 http.Error(w, "Pool failed: "+h.clientPool.Err().Error(), http.StatusInternalServerError)
50 fs := client.SiteFileSystem(kc)
51 fs.ForwardSlashNameSubstitution(h.Config.cluster.Collections.ForwardSlashNameSubstitution)
54 case r.Method == "GET" && strings.Count(strings.TrimSuffix(r.URL.Path, "/"), "/") == 1:
55 // Path is "/{uuid}" or "/{uuid}/", has no object name
58 case r.Method == "GET":
59 fspath := "/by_id" + r.URL.Path
60 fi, err := fs.Stat(fspath)
61 if os.IsNotExist(err) ||
62 (err != nil && err.Error() == "not a directory") ||
63 (fi != nil && fi.IsDir()) {
64 http.Error(w, "not found", http.StatusNotFound)
67 // shallow copy r, and change URL path
70 http.FileServer(fs).ServeHTTP(w, &r)
72 case r.Method == "PUT":
73 if strings.HasSuffix(r.URL.Path, "/") {
74 http.Error(w, "invalid object name (trailing '/' char)", http.StatusBadRequest)
77 fspath := "by_id" + r.URL.Path
78 _, err = fs.Stat(fspath)
79 if err != nil && err.Error() == "not a directory" {
80 // requested foo/bar, but foo is a file
81 http.Error(w, "object name conflicts with existing object", http.StatusBadRequest)
84 f, err := fs.OpenFile(fspath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
85 if os.IsNotExist(err) {
86 // create missing intermediate directories, then try again
87 for i, c := range fspath {
88 if i > 0 && c == '/' {
90 if strings.HasSuffix(dir, "/") {
91 err = errors.New("invalid object name (consecutive '/' chars)")
92 http.Error(w, err.Error(), http.StatusBadRequest)
95 err := fs.Mkdir(dir, 0755)
96 if err != nil && err != os.ErrExist {
97 err = fmt.Errorf("mkdir %q failed: %w", dir, err)
98 http.Error(w, err.Error(), http.StatusInternalServerError)
103 f, err = fs.OpenFile(fspath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
106 err = fmt.Errorf("open %q failed: %w", r.URL.Path, err)
107 http.Error(w, err.Error(), http.StatusBadRequest)
111 _, err = io.Copy(f, r.Body)
113 err = fmt.Errorf("write to %q failed: %w", r.URL.Path, err)
114 http.Error(w, err.Error(), http.StatusBadGateway)
119 err = fmt.Errorf("write to %q failed: %w", r.URL.Path, err)
120 http.Error(w, err.Error(), http.StatusBadGateway)
125 err = fmt.Errorf("sync failed: %w", err)
126 http.Error(w, err.Error(), http.StatusInternalServerError)
129 w.WriteHeader(http.StatusOK)
132 http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
137 func walkFS(fs arvados.CustomFileSystem, path string, fn func(path string, fi os.FileInfo) error) error {
138 f, err := fs.Open(path)
140 return fmt.Errorf("open %q: %w", path, err)
146 fis, err := f.Readdir(-1)
150 sort.Slice(fis, func(i, j int) bool { return fis[i].Name() < fis[j].Name() })
151 for _, fi := range fis {
152 err = fn(path+"/"+fi.Name(), fi)
153 if err == filepath.SkipDir {
155 } else if err != nil {
159 err = walkFS(fs, path+"/"+fi.Name(), fn)
168 var errDone = errors.New("done")
170 func (h *handler) s3list(w http.ResponseWriter, r *http.Request, fs arvados.CustomFileSystem) {
178 params.bucket = strings.SplitN(r.URL.Path[1:], "/", 2)[0]
179 params.delimiter = r.FormValue("delimiter")
180 params.marker = r.FormValue("marker")
181 if mk, _ := strconv.ParseInt(r.FormValue("max-keys"), 10, 64); mk > 0 {
182 params.maxKeys = int(mk)
186 params.prefix = r.FormValue("prefix")
188 bucketdir := "by_id/" + params.bucket
189 // walkpath is the directory (relative to bucketdir) we need
190 // to walk: the innermost directory that is guaranteed to
191 // contain all paths that have the requested prefix. Examples:
192 // prefix "foo/bar" => walkpath "foo"
193 // prefix "foo/bar/" => walkpath "foo/bar"
194 // prefix "foo" => walkpath ""
195 // prefix "" => walkpath ""
196 walkpath := params.prefix
197 if !strings.HasSuffix(walkpath, "/") {
198 walkpath, _ = filepath.Split(walkpath)
200 walkpath = strings.TrimSuffix(walkpath, "/")
202 type commonPrefix struct {
205 type serverListResponse struct {
207 CommonPrefixes []commonPrefix
209 resp := serverListResponse{ListResp: s3.ListResp{
210 Name: strings.SplitN(r.URL.Path[1:], "/", 2)[0],
211 Prefix: params.prefix,
212 Delimiter: params.delimiter,
213 Marker: params.marker,
214 MaxKeys: params.maxKeys,
216 err := walkFS(fs, strings.TrimSuffix(bucketdir+"/"+walkpath, "/"), func(path string, fi os.FileInfo) error {
217 path = path[len(bucketdir)+1:]
218 if !strings.HasPrefix(path, params.prefix) {
219 return filepath.SkipDir
224 if path < params.marker {
227 // TODO: check delimiter, roll up common prefixes
228 if len(resp.Contents)+len(resp.CommonPrefixes) >= params.maxKeys {
229 resp.IsTruncated = true
230 if params.delimiter == "" {
231 resp.NextMarker = path
235 resp.ListResp.Contents = append(resp.ListResp.Contents, s3.Key{
240 if err != nil && err != errDone {
241 http.Error(w, err.Error(), http.StatusInternalServerError)
244 if err := xml.NewEncoder(w).Encode(resp); err != nil {
245 ctxlog.FromContext(r.Context()).WithError(err).Error("error writing xml response")